coreaudio 0.1.0

A safe and simple wrapper around the CoreAudio HAL
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! # Properties

#![allow(unsafe_code)]

// ----- Imports ------------
use crate::{data_types::{BufferFrameSizeRange, SampleRateRange, StreamDescription}, errors::{CoreAudioError, ErrorKind}, object::{Device, Stream, System}};
use std::marker::PhantomData;
use core_foundation::{base::TCFType, string::{CFString, CFStringRef}};
use coreaudio_sys::{
    AudioObjectID,
    AudioObjectPropertyAddress,
    AudioObjectPropertyScope,
    AudioObjectPropertySelector,
    AudioStreamBasicDescription,
    AudioValueRange,
    kAudioDevicePropertyAvailableNominalSampleRates,
    kAudioDevicePropertyBufferFrameSize,
    kAudioDevicePropertyBufferFrameSizeRange,
    kAudioDevicePropertyDeviceIsAlive,
    kAudioDevicePropertyDeviceIsRunning,
    kAudioDevicePropertyDeviceUID,
    kAudioDevicePropertyLatency,
    kAudioDevicePropertyNominalSampleRate,
    kAudioDevicePropertyStreams,
    kAudioDevicePropertyHogMode,
    kAudioHardwarePropertyBoxList,
    kAudioHardwarePropertyClockDeviceList,
    kAudioHardwarePropertyDefaultInputDevice,
    kAudioHardwarePropertyDefaultOutputDevice,
    kAudioHardwarePropertyDevices,
    kAudioHardwarePropertyIsInitingOrExiting,
    kAudioHardwarePropertyPlugInList,
    kAudioHardwarePropertyPowerHint,
    kAudioHardwarePropertySleepingIsAllowed,
    kAudioHardwarePropertyTapList,
    kAudioObjectPropertyElementMain,
    kAudioObjectPropertyName,
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyScopeInput,
    kAudioObjectPropertyScopeOutput,
    kAudioStreamPropertyDirection,
    kAudioStreamPropertyIsActive,
    kAudioStreamPropertyLatency,
    kAudioStreamPropertyPhysicalFormat,
    kAudioStreamPropertyVirtualFormat,
};

// ---- Structs -------------
/// Indicates a property to be read only
pub struct ReadOnly;

/// Indicates a property to be readable and writeable
pub struct ReadWrite;

/// Indicates a property to be listenable
pub struct Listenable;

/// Indicates a property to be unlistenable
pub struct Silent;

pub struct Property<T, Object, Access, L> {
    pub(crate) address: AudioObjectPropertyAddress,
    pub(crate) read: fn(&[u8]) -> Result<T, CoreAudioError>,
    pub(crate) encode: Option<fn(T) -> Vec<u8>>,
    _object: PhantomData<Object>,
    _access: PhantomData<Access>,
    _listenable: PhantomData<L>,
}

impl<T, Object, Access, L> Property<T, Object, Access, L> {
    pub(crate) const fn new(
        address: AudioObjectPropertyAddress,
        read: fn(&[u8]) -> Result<T, CoreAudioError>,
        encode: Option<fn(T) -> Vec<u8>>,
    ) -> Self {
        Self {
            address,
            read,
            encode,
            _object: PhantomData,
            _access: PhantomData,
            _listenable: PhantomData,
        }
    }
}

// ---- Read functions (private) ----
fn read_string(bytes: &[u8]) -> Result<String, CoreAudioError> {
    if bytes.len() != size_of::<CFStringRef>() {
        return Err(CoreAudioError::from_error_kind(ErrorKind::CFStringConversion));
    }

    let ptr = usize::from_ne_bytes(bytes[..size_of::<usize>()].try_into()
        .map_err(|_| CoreAudioError::from_error_kind(ErrorKind::CFStringConversion))?
    ) as CFStringRef;

    if ptr.is_null() {
        return Err(CoreAudioError::from_error_kind(ErrorKind::CFStringConversion));
    }

    let cf_string = unsafe { CFString::wrap_under_get_rule(ptr) };
    Ok(cf_string.to_string())
}

fn read_bool(bytes: &[u8]) -> Result<bool, CoreAudioError> {
    let value = u32::from_ne_bytes(
        match bytes[..4].try_into() {
            Ok(value) => value,
            Err(_) => return Err(CoreAudioError::from_error_kind(ErrorKind::BoolConversion)),
        }
    ) != 0;

    Ok(value)
}

fn read_f64(bytes: &[u8]) -> Result<f64, CoreAudioError> {
    let value = f64::from_ne_bytes(
        match bytes[..8].try_into() {
            Ok(value) => value,
            Err(_) => return Err(CoreAudioError::from_error_kind(ErrorKind::F64Conversion)),
        }
    );

    Ok(value)
}

fn read_u32(bytes: &[u8]) -> Result<u32, CoreAudioError> {
    let value = u32::from_ne_bytes(
        match bytes[..4].try_into() {
            Ok(value) => value,
            Err(_) => return Err(CoreAudioError::from_error_kind(ErrorKind::U32Conversion)),
        }
    );

    Ok(value)
}

fn read_i32(bytes: &[u8]) -> Result<i32, CoreAudioError> {
    let value = i32::from_ne_bytes(
        match bytes[..4].try_into() {
            Ok(value) => value,
            Err(_) => return Err(CoreAudioError::from_error_kind(ErrorKind::I32Conversion)),
        }
    );

    Ok(value)
}

fn read_audio_object_id(bytes: &[u8]) -> Result<AudioObjectID, CoreAudioError> {
    let value = u32::from_ne_bytes(
        match bytes[..4].try_into() {
            Ok(value) => value,
            Err(_) => return Err(CoreAudioError::from_error_kind(ErrorKind::AudioObjectIdConversion)),
        }
    );

    Ok(value)
}

fn read_stream_description(bytes: &[u8]) -> Result<StreamDescription, CoreAudioError> {
    if bytes.len() != size_of::<AudioStreamBasicDescription>() {
        return Err(CoreAudioError::from_error_kind(ErrorKind::StreamDescriptionConversion));
    }

    let asbd = unsafe { 
        std::ptr::read(bytes.as_ptr() as *const AudioStreamBasicDescription) 
    };
    
    StreamDescription::try_from(asbd)
        .map_err(|_| CoreAudioError::from_error_kind(ErrorKind::StreamDescriptionConversion))
}

fn read_buffer_size_range(bytes: &[u8]) -> Result<BufferFrameSizeRange, CoreAudioError> {
    if bytes.len() != size_of::<AudioValueRange>() {
        return Err(CoreAudioError::from_error_kind(ErrorKind::ValueRangeConversion));
    }

    let range = unsafe {
        std::ptr::read(bytes.as_ptr() as *const AudioValueRange)
    };

    Ok(BufferFrameSizeRange::from(range))
}

fn read_vec_audio_object_id(bytes: &[u8]) -> Result<Vec<AudioObjectID>, CoreAudioError> {
    if bytes.len() % size_of::<AudioObjectID>() != 0 {
        return Err(CoreAudioError::from_error_kind(ErrorKind::AudioObjectIdConversion));
    }

    Ok(bytes.chunks(size_of::<AudioObjectID>())
        .map(|chunk| unsafe { std::ptr::read(chunk.as_ptr() as *const AudioObjectID) })
        .collect())
}

fn read_vec_sample_rate_range(bytes: &[u8]) -> Result<Vec<SampleRateRange>, CoreAudioError> {
    if bytes.len() % size_of::<AudioValueRange>() != 0 {
        return Err(CoreAudioError::from_error_kind(ErrorKind::ValueRangeConversion));
    }

    Ok(bytes.chunks(size_of::<AudioValueRange>())
        .map(|chunk| unsafe { 
            SampleRateRange::from(std::ptr::read(chunk.as_ptr() as *const AudioValueRange)) 
        })
        .collect())
}

fn encode_f64(value: f64) -> Vec<u8> {
    value.to_ne_bytes().to_vec()
}

fn encode_u32(value: u32) -> Vec<u8> {
    value.to_ne_bytes().to_vec()
}

fn encode_i32(value: i32) -> Vec<u8> {
    value.to_ne_bytes().to_vec()
}

fn encode_bool(value: bool) -> Vec<u8> {
    encode_u32(value as u32)
}

fn encode_audio_object_id(value: AudioObjectID) -> Vec<u8> {
    encode_u32(value)
}

fn encode_stream_description(value: StreamDescription) -> Vec<u8> {
    let asbd: AudioStreamBasicDescription = value.into();
    let size = size_of::<AudioStreamBasicDescription>();
    let mut bytes = vec![0u8; size];
    unsafe {
        std::ptr::write(bytes.as_mut_ptr() as *mut AudioStreamBasicDescription, asbd);
    }
    bytes
}

// ---- Helper ----
const fn address(
    selector: AudioObjectPropertySelector,
    scope: AudioObjectPropertyScope,
) -> AudioObjectPropertyAddress {
    AudioObjectPropertyAddress {
        mSelector: selector,
        mScope: scope,
        mElement: kAudioObjectPropertyElementMain,
    }
}

// ---- Device constants ----

/// Human readable name of the device
pub const DEVICE_NAME: Property<String, Device, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioObjectPropertyName,
            kAudioObjectPropertyScopeGlobal
        ),
        read_string,
        None,
    );

/// Persistent unique identifier for the device
/// 
/// Not the same as the device id
pub const DEVICE_UID: Property<String, Device, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioDevicePropertyDeviceUID,
            kAudioObjectPropertyScopeGlobal
        ),
        read_string,
        None,
    );

/// Whether the device is still alive and connected
pub const DEVICE_IS_ALIVE: Property<bool, Device, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyDeviceIsAlive,
            kAudioObjectPropertyScopeGlobal
        ),
        read_bool,
        None,
    );

/// Whether the device is currently running I/O
pub const DEVICE_IS_RUNNING: Property<bool, Device, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyDeviceIsRunning,
            kAudioObjectPropertyScopeGlobal
        ),
        read_bool,
        None,
    );

/// The current nominal sample rate of the device
pub const DEVICE_NOMINAL_SAMPLE_RATE: Property<f64, Device, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyNominalSampleRate,
            kAudioObjectPropertyScopeGlobal
        ),
        read_f64,
        Some(encode_f64),
    );

/// All sample rates supported by the device
pub(crate) const DEVICE_AVAILABLE_SAMPLE_RATES: Property<Vec<SampleRateRange>, Device, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioDevicePropertyAvailableNominalSampleRates,
            kAudioObjectPropertyScopeGlobal
        ),
        read_vec_sample_rate_range,
        None,
    );

/// The number of frames in the I/O buffer
pub const DEVICE_BUFFER_FRAME_SIZE: Property<u32, Device, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyBufferFrameSize,
            kAudioObjectPropertyScopeGlobal
        ),
        read_u32,
        Some(encode_u32)
    );

/// The valid range of buffer frame sizes for the device
pub(crate) const DEVICE_BUFFER_FRAME_SIZE_RANGE: Property<BufferFrameSizeRange, Device, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioDevicePropertyBufferFrameSizeRange,
            kAudioObjectPropertyScopeGlobal
        ),
        read_buffer_size_range,
        None,
    );

/// Input latency of the device in frames
pub const DEVICE_INPUT_LATENCY: Property<u32, Device, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioDevicePropertyLatency,
            kAudioObjectPropertyScopeInput
        ),
        read_u32,
        None,
    );

/// Output latency of the device in frames
pub const DEVICE_OUTPUT_LATENCY: Property<u32, Device, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioDevicePropertyLatency,
            kAudioObjectPropertyScopeOutput
        ),
        read_u32,
        None
    );

/// All input streams on the device
pub(crate) const DEVICE_INPUT_STREAMS: Property<Vec<AudioObjectID>, Device, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyStreams,
            kAudioObjectPropertyScopeInput
        ),
        read_vec_audio_object_id,
        None,
    );

/// All output streams on the device
pub(crate) const DEVICE_OUTPUT_STREAMS: Property<Vec<AudioObjectID>, Device, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyStreams,
            kAudioObjectPropertyScopeOutput
        ),
        read_vec_audio_object_id,
        None,
    );

pub const DEVICE_HOG_MODE: Property<i32, Device, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioDevicePropertyHogMode,
            kAudioObjectPropertyScopeGlobal
        ),
        read_i32,
        Some(encode_i32)
    );

// ---- Stream constants ----

/// Human readable name of the stream
pub const STREAM_NAME: Property<String, Stream, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioObjectPropertyName,
            kAudioObjectPropertyScopeGlobal
        ),
        read_string,
        None,
    );

/// Whether the stream is currently active
pub const STREAM_IS_ACTIVE: Property<bool, Stream, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioStreamPropertyIsActive,
            kAudioObjectPropertyScopeGlobal
        ),
        read_bool,
        None,
    );

/// Direction of the stream — 0 for output, 1 for input
pub const STREAM_DIRECTION: Property<u32, Stream, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioStreamPropertyDirection,
            kAudioObjectPropertyScopeGlobal
        ),
        read_u32,
        None,
    );

/// The virtual format of the stream as presented to the client
pub(crate) const STREAM_VIRTUAL_FORMAT: Property<StreamDescription, Stream, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioStreamPropertyVirtualFormat,
            kAudioObjectPropertyScopeGlobal
        ),
        read_stream_description,
        Some(encode_stream_description)
    );

/// The physical format of the stream as presented to the hardware
pub(crate) const STREAM_PHYSICAL_FORMAT: Property<StreamDescription, Stream, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioStreamPropertyPhysicalFormat,
            kAudioObjectPropertyScopeGlobal
        ),
        read_stream_description,
        Some(encode_stream_description)
    );

/// Latency of the stream in frames
pub const STREAM_LATENCY: Property<u32, Stream, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioStreamPropertyLatency,
            kAudioObjectPropertyScopeGlobal
        ),
        read_u32,
        None,
    );

// ---- System constants ----

/// Human readable name of the system object
pub const SYSTEM_NAME: Property<String, System, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioObjectPropertyName,
            kAudioObjectPropertyScopeGlobal
        ),
        read_string,
        None,
    );

/// All devices currently known to the HAL
pub(crate) const SYSTEM_DEVICES: Property<Vec<AudioObjectID>, System, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertyDevices,
            kAudioObjectPropertyScopeGlobal
        ), read_vec_audio_object_id,
        None,
    );

/// The current default input device
pub(crate) const SYSTEM_DEFAULT_INPUT: Property<AudioObjectID, System, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertyDefaultInputDevice,
            kAudioObjectPropertyScopeGlobal
        ),
        read_audio_object_id,
        Some(encode_audio_object_id)
    );

/// The current default output device
pub(crate) const SYSTEM_DEFAULT_OUTPUT: Property<AudioObjectID, System, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertyDefaultOutputDevice,
            kAudioObjectPropertyScopeGlobal
        ),
        read_audio_object_id,
        Some(encode_audio_object_id)
    );

/// All audio boxes known to the HAL
pub(crate) const SYSTEM_BOX_LIST: Property<Vec<AudioObjectID>, System, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertyBoxList,
            kAudioObjectPropertyScopeGlobal
        ),
        read_vec_audio_object_id,
        None,
    );

/// All clock devices known to the HAL
pub(crate) const SYSTEM_CLOCK_DEVICE_LIST: Property<Vec<AudioObjectID>, System, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertyClockDeviceList,
            kAudioObjectPropertyScopeGlobal
        ),
        read_vec_audio_object_id,
        None,
    );

/// Whether the HAL is currently initialising or shutting down
pub const SYSTEM_IS_INITING_OR_EXITING: Property<bool, System, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioHardwarePropertyIsInitingOrExiting,
            kAudioObjectPropertyScopeGlobal
        ),
        read_bool,
        None,
    );

/// Whether the system is permitted to sleep while audio is running
pub const SYSTEM_SLEEPING_IS_ALLOWED: Property<bool, System, ReadWrite, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertySleepingIsAllowed,
            kAudioObjectPropertyScopeGlobal
        ),
        read_bool,
        Some(encode_bool)
    );

/// All HAL plugins currently loaded
pub(crate) const SYSTEM_PLUGIN_LIST: Property<Vec<AudioObjectID>, System, ReadOnly, Silent> =
    Property::new(
        address(
            kAudioHardwarePropertyPlugInList,
            kAudioObjectPropertyScopeGlobal
        ),
        read_vec_audio_object_id,
        None,
    );

/// Hints to the HAL about the current power situation
pub const SYSTEM_POWER_HINT: Property<u32, System, ReadWrite, Silent> =
    Property::new(
        address(
            kAudioHardwarePropertyPowerHint,
            kAudioObjectPropertyScopeGlobal
        ),
        read_u32,
        Some(encode_u32)
    );

/// All audio taps known to the HAL
pub(crate) const SYSTEM_TAP_LIST: Property<Vec<AudioObjectID>, System, ReadOnly, Listenable> =
    Property::new(
        address(
            kAudioHardwarePropertyTapList,
            kAudioObjectPropertyScopeGlobal
        ), read_vec_audio_object_id,
        None,
    );