Skip to content

Commit b1bfb95

Browse files
committed
Clean up comments and code
1 parent de9380d commit b1bfb95

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

examples/sine_advanced.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fn main() -> Result<(), coreaudio::Error> {
9494

9595
// set the sample rate. This isn't actually needed since the sample rate
9696
// will anyway be changed when setting the sample format later.
97+
// Keeping it here as an example.
9798
//println!("set device sample rate");
9899
//set_device_sample_rate(audio_unit_id, SAMPLE_RATE)?;
99100

src/audio_unit/macos_helpers.rs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -322,42 +322,51 @@ pub fn set_device_sample_rate(device_id: AudioDeviceID, new_rate: f64) -> Result
322322
}
323323
}
324324

325-
/// Find the closest match of the physical formats to the provided StreamFormat.
326-
/// Note that only the sample format and rate will be matched, the flags will be ignored.
325+
/// Find the closest match of the physical formats to the provided `StreamFormat`.
326+
/// It will pick the first format it finds that supports the provided sample format, rate and number of channels.
327+
/// The provided format flags in the `StreamFormat` are ignored.
327328
pub fn find_matching_physical_format(
328329
device_id: AudioDeviceID,
329330
stream_format: StreamFormat,
330331
) -> Option<AudioStreamBasicDescription> {
331332
if let Ok(all_formats) = get_supported_physical_stream_formats(device_id) {
332-
let wanted_samplerate = stream_format.sample_rate as usize;
333-
let wanted_bits = stream_format.sample_format.size_in_bits();
334-
let wanted_float = stream_format.sample_format == SampleFormat::F32;
335-
let wanted_channels = stream_format.channels;
333+
let requested_samplerate = stream_format.sample_rate as usize;
334+
let requested_bits = stream_format.sample_format.size_in_bits();
335+
let requested_float = stream_format.sample_format == SampleFormat::F32;
336+
let requested_channels = stream_format.channels;
336337
for fmt in all_formats {
337-
let minrate = fmt.mSampleRateRange.mMinimum as usize;
338-
let maxrate = fmt.mSampleRateRange.mMaximum as usize;
338+
let min_rate = fmt.mSampleRateRange.mMinimum as usize;
339+
let max_rate = fmt.mSampleRateRange.mMaximum as usize;
339340
let rate = fmt.mFormat.mSampleRate as usize;
340341
let channels = fmt.mFormat.mChannelsPerFrame;
341342
if let Some(AudioFormat::LinearPCM(flags)) = AudioFormat::from_format_and_flag(
342343
fmt.mFormat.mFormatID,
343344
Some(fmt.mFormat.mFormatFlags),
344345
) {
345-
let floats = flags.contains(LinearPcmFlags::IS_FLOAT);
346-
let ints = flags.contains(LinearPcmFlags::IS_SIGNED_INTEGER);
347-
if wanted_float != floats || wanted_float == ints {
346+
let is_float = flags.contains(LinearPcmFlags::IS_FLOAT);
347+
let is_int = flags.contains(LinearPcmFlags::IS_SIGNED_INTEGER);
348+
if is_int && is_float {
349+
// Probably never occurs, check just in case
350+
continue;
351+
}
352+
if requested_float && !is_float {
353+
// Wrong number type
354+
continue;
355+
}
356+
if !requested_float && !is_int {
348357
// Wrong number type
349358
continue;
350359
}
351-
if wanted_bits != fmt.mFormat.mBitsPerChannel {
360+
if requested_bits != fmt.mFormat.mBitsPerChannel {
352361
// Wrong number of bits
353362
continue;
354363
}
355-
if wanted_channels > channels {
364+
if requested_channels > channels {
356365
// Too few channels
357366
continue;
358367
}
359-
if rate == wanted_samplerate
360-
|| (wanted_samplerate >= minrate && wanted_samplerate <= maxrate)
368+
if rate == requested_samplerate
369+
|| (requested_samplerate >= min_rate && requested_samplerate <= max_rate)
361370
{
362371
return Some(fmt.mFormat);
363372
}
@@ -373,9 +382,8 @@ pub fn set_device_physical_stream_format(
373382
device_id: AudioDeviceID,
374383
new_asbd: AudioStreamBasicDescription,
375384
) -> Result<(), Error> {
376-
// Check whether or not we need to change the device sample format and rate.
377385
unsafe {
378-
// Get the current sample rate.
386+
// Get the current format.
379387
let property_address = AudioObjectPropertyAddress {
380388
mSelector: kAudioStreamPropertyPhysicalFormat,
381389
mScope: kAudioObjectPropertyScopeGlobal,
@@ -393,10 +401,7 @@ pub fn set_device_physical_stream_format(
393401
);
394402
Error::from_os_status(status)?;
395403
let asbd = maybe_asbd.assume_init();
396-
//println!("Current: {:?}", asbd);
397-
//println!("New: {:?}", new_asbd);
398404

399-
// If the requested sample rate and/or format is different to the device sample rate, update the device.
400405
if !asbds_are_equal(&asbd, &new_asbd) {
401406
let property_address = AudioObjectPropertyAddress {
402407
mSelector: kAudioStreamPropertyPhysicalFormat,
@@ -419,8 +424,7 @@ pub fn set_device_physical_stream_format(
419424
Error::from_os_status(status)?;
420425

421426
// Wait for the reported format to change.
422-
//
423-
// This should not take longer than a few ms, but we timeout after 1 sec just in case.
427+
// This can take up to half a second, but we timeout after 2 sec just in case.
424428
let timer = ::std::time::Instant::now();
425429
loop {
426430
let status = AudioObjectGetPropertyData(
@@ -460,20 +464,15 @@ fn asbds_are_equal(
460464
&& left.mBitsPerChannel == right.mBitsPerChannel
461465
}
462466

463-
/// Get a vector with all supported physical formats as AudioBasicStreamDescriptions.
467+
/// Get a vector with all supported physical formats as AudioBasicRangedDescriptions.
464468
/// Only implemented for macOS, not iOS.
465-
/// TODO: figure out why this sometimes crashes with:
466-
/// malloc: Incorrect checksum for freed object 0x7fca6bc3c538: probably modified after being freed.
467469
pub fn get_supported_physical_stream_formats(
468470
device_id: AudioDeviceID,
469471
) -> Result<Vec<AudioStreamRangedDescription>, Error> {
470472
// Get available formats.
471473
let mut property_address = AudioObjectPropertyAddress {
472474
mSelector: kAudioStreamPropertyPhysicalFormat,
473475
mScope: kAudioObjectPropertyScopeGlobal,
474-
//mScope: kAudioDevicePropertyScopeInput,
475-
//mScope: kAudioObjectPropertyScopeOutput,
476-
//mScope: kAudioDevicePropertyScopeOutput,
477476
mElement: kAudioObjectPropertyElementMaster,
478477
};
479478
let allformats = unsafe {
@@ -503,15 +502,6 @@ pub fn get_supported_physical_stream_formats(
503502
Error::from_os_status(status)?;
504503
formats
505504
};
506-
/*
507-
println!("---- All supported formats ----");
508-
for asbd in formats.iter() {
509-
if let Ok(sf) = StreamFormat::from_asbd(*asbd) {
510-
println!("{:#?}", asbd);
511-
println!("{:#?}", sf);
512-
}
513-
}
514-
*/
515505
Ok(allformats)
516506
}
517507

src/audio_unit/sample_format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub trait Sample {
8787
}
8888

8989
/// Simplified implementation of the `Sample` trait for sample types.
90-
/// This is only implemented for the types that map directly to a numeric type.
90+
/// This is only implemented for the sample types that map directly to a numeric type.
9191
macro_rules! impl_sample {
9292
($($T:ident $format:ident),* $(,)*) => {
9393
$(

src/audio_unit/stream_format.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ impl StreamFormat {
103103
}
104104

105105
/// Convert a StreamFormat into an AudioStreamBasicDescription.
106+
/// Note that this function assumes that only packed formats are used.
107+
/// This only affects I24, since all other formats supported by `StreamFormat`
108+
/// are always packed.
106109
pub fn to_asbd(self) -> sys::AudioStreamBasicDescription {
107110
let StreamFormat {
108111
sample_rate,
@@ -111,7 +114,8 @@ impl StreamFormat {
111114
channels,
112115
} = self;
113116

114-
let (format, maybe_flag) = AudioFormat::LinearPCM(flags).as_format_and_flag();
117+
let (format, maybe_flag) =
118+
AudioFormat::LinearPCM(flags | LinearPcmFlags::IS_PACKED).as_format_and_flag();
115119

116120
let flag = maybe_flag.unwrap_or(::std::u32::MAX - 2147483647);
117121

@@ -121,17 +125,9 @@ impl StreamFormat {
121125
} else {
122126
sample_format.size_in_bytes() as u32 * channels
123127
};
124-
//let bytes_per_frame = sample_format.size_in_bytes() as u32;
125128
const FRAMES_PER_PACKET: u32 = 1;
126129
let bytes_per_packet = bytes_per_frame * FRAMES_PER_PACKET;
127-
//let bits_per_channel = bytes_per_frame / channels * 8;
128-
//let bits_per_channel = if non_interleaved {
129-
// bytes_per_frame * 8
130-
//} else {
131-
// bytes_per_frame / channels * 8
132-
//};
133130
let bits_per_channel = sample_format.size_in_bits();
134-
//let bits_per_channel = bytes_per_frame * 8;
135131

136132
sys::AudioStreamBasicDescription {
137133
mSampleRate: sample_rate,

0 commit comments

Comments
 (0)