Skip to main content

asio_sys/bindings/
errors.rs

1use std::{error::Error, fmt};
2
3/// Errors that might occur during `Asio::load_driver`.
4#[derive(Clone, Debug)]
5pub enum LoadDriverError {
6    LoadDriverFailed,
7    DriverAlreadyExists,
8    InitializationFailed(AsioError),
9}
10
11/// General errors returned by ASIO.
12#[derive(Clone, Debug)]
13pub enum AsioError {
14    NoDrivers,
15    HardwareMalfunction,
16    InvalidInput,
17    BadMode,
18    HardwareStuck,
19    NoRate,
20    NoMemory,
21    InvalidBufferSize,
22    UnknownError,
23}
24
25#[derive(Debug)]
26pub enum AsioErrorWrapper {
27    ASE_OK = 0,               // This value will be returned whenever the call succeeded
28    ASE_SUCCESS = 0x3f4847a0, // unique success return value for ASIOFuture calls
29    ASE_NotPresent = -1000,   // hardware input or output is not present or available
30    ASE_HWMalfunction,        // hardware is malfunctioning (can be returned by any ASIO function)
31    ASE_InvalidParameter,     // input parameter invalid
32    ASE_InvalidMode,          // hardware is in a bad mode or used in a bad mode
33    ASE_SPNotAdvancing,       // hardware is not running when sample position is inquired
34    ASE_NoClock,              // sample clock or rate cannot be determined or is not present
35    ASE_NoMemory,             // not enough memory for completing the request
36    Invalid,
37}
38
39impl fmt::Display for LoadDriverError {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        match *self {
42            LoadDriverError::LoadDriverFailed => {
43                write!(
44                    f,
45                    "ASIO `loadDriver` function returned `false` indicating failure"
46                )
47            }
48            LoadDriverError::InitializationFailed(ref err) => {
49                write!(f, "{err}")
50            }
51            LoadDriverError::DriverAlreadyExists => {
52                write!(f, "ASIO only supports loading one driver at a time")
53            }
54        }
55    }
56}
57
58impl fmt::Display for AsioError {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        match *self {
61            AsioError::NoDrivers => {
62                write!(f, "hardware input or output is not present or available")
63            }
64            AsioError::HardwareMalfunction => write!(
65                f,
66                "hardware is malfunctioning (can be returned by any ASIO function)"
67            ),
68            AsioError::InvalidInput => write!(f, "input parameter invalid"),
69            AsioError::BadMode => write!(f, "hardware is in a bad mode or used in a bad mode"),
70            AsioError::HardwareStuck => write!(
71                f,
72                "hardware is not running when sample position is inquired"
73            ),
74            AsioError::NoRate => write!(
75                f,
76                "sample clock or rate cannot be determined or is not present"
77            ),
78            AsioError::NoMemory => write!(f, "not enough memory for completing the request"),
79            AsioError::InvalidBufferSize => write!(f, "buffersize out of range for device"),
80            AsioError::UnknownError => write!(f, "Error not in SDK"),
81        }
82    }
83}
84
85impl Error for LoadDriverError {}
86impl Error for AsioError {}
87
88impl From<AsioError> for LoadDriverError {
89    fn from(err: AsioError) -> Self {
90        LoadDriverError::InitializationFailed(err)
91    }
92}
93
94macro_rules! asio_result {
95    ($e:expr) => {{
96        let res = { $e };
97        match res {
98            r if r == AsioErrorWrapper::ASE_OK as i32 => Ok(()),
99            r if r == AsioErrorWrapper::ASE_SUCCESS as i32 => Ok(()),
100            r if r == AsioErrorWrapper::ASE_NotPresent as i32 => Err(AsioError::NoDrivers),
101            r if r == AsioErrorWrapper::ASE_HWMalfunction as i32 => {
102                Err(AsioError::HardwareMalfunction)
103            }
104            r if r == AsioErrorWrapper::ASE_InvalidParameter as i32 => Err(AsioError::InvalidInput),
105            r if r == AsioErrorWrapper::ASE_InvalidMode as i32 => Err(AsioError::BadMode),
106            r if r == AsioErrorWrapper::ASE_SPNotAdvancing as i32 => Err(AsioError::HardwareStuck),
107            r if r == AsioErrorWrapper::ASE_NoClock as i32 => Err(AsioError::NoRate),
108            r if r == AsioErrorWrapper::ASE_NoMemory as i32 => Err(AsioError::NoMemory),
109            _ => Err(AsioError::UnknownError),
110        }
111    }};
112}