1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! This module is an attempt at rustifying the OSStatus result.

use bindings::audio_unit::OSStatus;
pub use self::audio::Error as AudioError;
pub use self::audio_codec::Error as AudioCodecError;
pub use self::audio_format::Error as AudioFormatError;
pub use self::audio_unit::Error as AudioUnitError;

pub mod audio {
    use bindings::audio_unit::OSStatus;

    #[derive(Copy, Clone, Debug)]
    pub enum Error {
        Unimplemented    = -4,
        FileNotFound     = -43,
        FilePermission   = -54,
        TooManyFilesOpen = -42,
        BadFilePath      = 561017960,
        Param            = -50,
        MemFull          = -108,
        Unknown,
    }

    impl Error {

        pub fn from_os_status(os_status: OSStatus) -> Result<(), Error> {
            match os_status {
                0         => Ok(()),
                -4        => Err(Error::Unimplemented),
                -43       => Err(Error::FileNotFound),
                -54       => Err(Error::FilePermission),
                -42       => Err(Error::TooManyFilesOpen),
                561017960 => Err(Error::BadFilePath),
                -50       => Err(Error::Param),
                -108      => Err(Error::MemFull),
                _         => Err(Error::Unknown),
            }
        }

        pub fn to_os_status(&self) -> OSStatus {
            *self as OSStatus
        }

    }

    impl ::std::fmt::Display for Error {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
            write!(f, "{:?}", self)
        }
    }

    impl ::std::error::Error for Error {
        fn description(&self) -> &str {
            match *self {
                Error::Unimplemented    => "Unimplemented",
                Error::FileNotFound     => "File not found",
                Error::FilePermission   => "File permission",
                Error::TooManyFilesOpen => "Too many files open",
                Error::BadFilePath      => "Bad file path",
                Error::Param            => "Param",
                Error::MemFull          => "Memory full",
                Error::Unknown          => "An unknown error occurred",
            }
        }
    }

}


pub mod audio_codec {
    use bindings::audio_unit::OSStatus;

    #[derive(Copy, Clone, Debug)]
    pub enum Error {
        Unspecified          = 2003329396,
        UnknownProperty      = 2003332927,
        BadPropertySize      = 561211770,
        IllegalOperation     = 1852797029,
        UnsupportedFormat    = 560226676,
        State                = 561214580,
        NotEnoughBufferSpace = 560100710,
        Unknown,
    }

    impl Error {

        pub fn from_os_status(os_status: OSStatus) -> Result<(), Error> {
            match os_status {
                0          => Ok(()),
                2003329396 => Err(Error::Unspecified),
                2003332927 => Err(Error::UnknownProperty),
                561211770  => Err(Error::BadPropertySize),
                1852797029 => Err(Error::IllegalOperation),
                560226676  => Err(Error::UnsupportedFormat),
                561214580  => Err(Error::State),
                560100710  => Err(Error::NotEnoughBufferSpace),
                _          => Err(Error::Unknown),
            }
        }

        pub fn to_os_status(&self) -> OSStatus {
            *self as OSStatus
        }

    }

    impl ::std::fmt::Display for Error {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
            write!(f, "{:?}", self)
        }
    }

    impl ::std::error::Error for Error {
        fn description(&self) -> &str {
            match *self {
                Error::Unspecified          => "Unspecified",
                Error::UnknownProperty      => "Unknown property",
                Error::BadPropertySize      => "Bad property size",
                Error::IllegalOperation     => "Illegal operation",
                Error::UnsupportedFormat    => "Unsupported format",
                Error::State                => "State",
                Error::NotEnoughBufferSpace => "Not enough buffer space",
                Error::Unknown              => "Unknown error occurred",
            }
        }
    }

}


pub mod audio_format {
    use bindings::audio_unit::OSStatus;

    // TODO: Finish implementing these values.
    #[derive(Copy, Clone, Debug)]
    pub enum Error {
        Unspecified, // 'what'
        UnsupportedProperty, // 'prop'
        BadPropertySize, // '!siz'
        BadSpecifierSize, // '!spc'
        UnsupportedDataFormat = 1718449215, // 'fmt?'
        UnknownFormat, // '!fmt'
        Unknown, //
    }

    impl Error {

        pub fn from_os_status(os_status: OSStatus) -> Result<(), Error> {
            match os_status {
                0          => Ok(()),
                1718449215 => Err(Error::UnsupportedDataFormat),
                _ => Err(Error::Unknown),
            }
        }

        pub fn to_os_status(&self) -> OSStatus {
            *self as OSStatus
        }

    }

    impl ::std::fmt::Display for Error {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
            write!(f, "{:?}", self)
        }
    }

    impl ::std::error::Error for Error {
        fn description(&self) -> &str {
            match *self {
                Error::Unspecified           => "An unspecified error",
                Error::UnsupportedProperty   => "The specified property is not supported",
                Error::BadPropertySize       => "Bad property size",
                Error::BadSpecifierSize      => "Bad specifier size",
                Error::UnsupportedDataFormat => "The specified data format is not supported",
                Error::UnknownFormat         => "The specified data format is not a known format",
                Error::Unknown               => "Unknown error occurred",
            }
        }
    }
}


pub mod audio_unit {
    use bindings::audio_unit::OSStatus;

    #[derive(Copy, Clone, Debug)]
    pub enum Error {
        InvalidProperty          = -10879,
        InvalidParameter         = -10878,
        InvalidElement           = -10877,
        NoConnection             = -10876,
        FailedInitialization     = -10875,
        TooManyFramesToProcess   = -10874,
        InvalidFile              = -10871,
        FormatNotSupported       = -10868,
        Uninitialized            = -10867,
        InvalidScope             = -10866,
        PropertyNotWritable      = -10865,
        CannotDoInCurrentContext = -10863,
        InvalidPropertyValue     = -10851,
        PropertyNotInUse         = -10850,
        Initialized              = -10849,
        InvalidOfflineRender     = -10848,
        Unauthorized             = -10847,
        Unknown,
    }

    impl Error {

        pub fn from_os_status(os_status: OSStatus) -> Result<(), Error> {
            match os_status {
                -10879 => Err(Error::InvalidProperty),
                -10878 => Err(Error::InvalidParameter),
                -10877 => Err(Error::InvalidElement),
                -10876 => Err(Error::NoConnection),
                -10875 => Err(Error::FailedInitialization),
                -10874 => Err(Error::TooManyFramesToProcess),
                -10871 => Err(Error::InvalidFile),
                -10868 => Err(Error::FormatNotSupported),
                -10867 => Err(Error::Uninitialized),
                -10866 => Err(Error::InvalidScope),
                -10865 => Err(Error::PropertyNotWritable),
                -10863 => Err(Error::CannotDoInCurrentContext),
                -10851 => Err(Error::InvalidPropertyValue),
                -10850 => Err(Error::PropertyNotInUse),
                -10849 => Err(Error::Initialized),
                -10848 => Err(Error::InvalidOfflineRender),
                -10847 => Err(Error::Unauthorized),
                _      => Err(Error::Unknown),
            }
        }

        pub fn to_os_status(&self) -> OSStatus {
            *self as OSStatus
        }

    }

    impl ::std::fmt::Display for Error {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
            write!(f, "{:?}", self)
        }
    }

    impl ::std::error::Error for Error {
        fn description(&self) -> &str {
            match *self {
                Error::InvalidProperty          => "Invalid property",
                Error::InvalidParameter         => "Invalid parameter",
                Error::InvalidElement           => "Invalid element",
                Error::NoConnection             => "No connection",
                Error::FailedInitialization     => "Failed initialization",
                Error::TooManyFramesToProcess   => "Too many frames to process",
                Error::InvalidFile              => "Invalid file",
                Error::FormatNotSupported       => "Format not supported",
                Error::Uninitialized            => "Uninitialized",
                Error::InvalidScope             => "Invalid scope",
                Error::PropertyNotWritable      => "Property not writable",
                Error::CannotDoInCurrentContext => "Cannot do in current context",
                Error::InvalidPropertyValue     => "Invalid property value",
                Error::PropertyNotInUse         => "Property not in use",
                Error::Initialized              => "Initialized",
                Error::InvalidOfflineRender     => "Invalid offline render",
                Error::Unauthorized             => "Unauthorized",
                Error::Unknown                  => "Unknown error occurred",
            }
        }
    }

}


/// A wrapper around all possible Core Audio errors.
#[derive(Copy, Clone, Debug)]
pub enum Error {
    Unspecified,
    SystemSoundClientMessageTimedOut,
    NoMatchingDefaultAudioUnitFound,
    RenderCallbackBufferFormatDoesNotMatchAudioUnitStreamFormat,
    NoKnownSubtype,
    Audio(AudioError),
    AudioCodec(AudioCodecError),
    AudioFormat(AudioFormatError),
    AudioUnit(AudioUnitError),
    Unknown(OSStatus),
}

impl Error {

    /// Convert an OSStatus to a std Rust Result.
    pub fn from_os_status(os_status: OSStatus) -> Result<(), Error> {
        match os_status {
            0     => Ok(()),
            -1500 => Err(Error::Unspecified),
            -1501 => Err(Error::SystemSoundClientMessageTimedOut),
            _     => {
                match AudioError::from_os_status(os_status) {
                    Ok(())                   => return Ok(()),
                    Err(AudioError::Unknown) => (),
                    Err(err)                 => return Err(Error::Audio(err)),
                }
                match AudioCodecError::from_os_status(os_status) {
                    Ok(())                        => return Ok(()),
                    Err(AudioCodecError::Unknown) => (),
                    Err(err)                      => return Err(Error::AudioCodec(err)),
                }
                match AudioFormatError::from_os_status(os_status) {
                    Ok(()) => return Ok(()),
                    Err(AudioFormatError::Unknown) => (),
                    Err(err) => return Err(Error::AudioFormat(err)),
                }
                match AudioUnitError::from_os_status(os_status) {
                    Ok(())                       => return Ok(()),
                    Err(AudioUnitError::Unknown) => (),
                    Err(err)                     => return Err(Error::AudioUnit(err)),
                }
                Err(Error::Unknown(os_status))
            },
        }
    }

    /// Convert an Error to an OSStatus.
    pub fn to_os_status(&self) -> OSStatus {
        match *self {
            Error::Unspecified                                                 => -1500,
            Error::NoMatchingDefaultAudioUnitFound                             => -1500,
            Error::RenderCallbackBufferFormatDoesNotMatchAudioUnitStreamFormat => -1500,
            Error::SystemSoundClientMessageTimedOut                            => -1501,
            Error::Audio(err)                                                  => err as OSStatus,
            Error::AudioCodec(err)                                             => err as OSStatus,
            Error::AudioUnit(err)                                              => err as OSStatus,
            _                                                                  => -1500,
        }
    }

}

impl ::std::fmt::Display for Error {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        write!(f, "{:?}", self)
    }
}

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Unspecified                      => "An unspecified error has occurred",
            Error::NoMatchingDefaultAudioUnitFound  => "No matching default audio unit found",
            Error::RenderCallbackBufferFormatDoesNotMatchAudioUnitStreamFormat =>
                "The given render callback buffer format does not match the `AudioUnit` `StreamFormat`",
            Error::SystemSoundClientMessageTimedOut => "The system sound client message timed out",
            Error::NoKnownSubtype                   => "The type has no known subtypes",
            Error::Audio(ref err)                   => err.description(),
            Error::AudioCodec(ref err)              => err.description(),
            Error::AudioFormat(ref err)             => err.description(),
            Error::AudioUnit(ref err)               => err.description(),
            Error::Unknown(_)                       => "An unknown error unknown to the coreaudio-rs API occurred",
        }
    }
}