spectrusty_audio/host.rs
1/*
2 Copyright (C) 2020-2022 Rafal Michalski
3
4 This file is part of SPECTRUSTY, a Rust library for building emulators.
5
6 For the full copyright notice, see the lib.rs file.
7*/
8//! Platform dependent audio device streaming implementations.
9//!
10//! To make use of this module enable one of the available features to the `spectrusty_audio` entry in `[dependencies]`
11//! section of the Cargo configuration file.
12use core::fmt;
13use std::error::Error;
14
15#[cfg(feature = "cpal")]
16pub mod cpal;
17
18#[cfg(feature = "sdl2")]
19pub mod sdl2;
20
21/// A list specifying categories of [AudioHandleError] error.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum AudioHandleErrorKind {
24 /// This error occurs when the audio subsystem host or device is not available.
25 AudioSubsystem,
26 /// This error occurs while trying to create or modify an audio stream.
27 AudioStream,
28 /// This error occurs due to the invalid specification of the desired audio parameters or other arguments.
29 InvalidArguments,
30}
31
32/// A common error type returned by all audio handle implementation methods in this module.
33#[derive(Debug, Clone)]
34pub struct AudioHandleError {
35 description: String,
36 kind: AudioHandleErrorKind
37}
38
39impl fmt::Display for AudioHandleError {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 self.description.fmt(f)
42 }
43}
44
45impl Error for AudioHandleError {}
46
47impl AudioHandleError {
48 /// Returns the corresponding category for this error.
49 pub fn kind(&self) -> AudioHandleErrorKind {
50 self.kind
51 }
52}
53
54impl From<(String, AudioHandleErrorKind)> for AudioHandleError {
55 fn from((description, kind): (String, AudioHandleErrorKind)) -> Self {
56 AudioHandleError { description, kind }
57 }
58}