asio_sys/bindings/
errors.rs

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