Skip to content

Commit dc71378

Browse files
committed
Use bitflags via associated consts rather than modules
1 parent d40035f commit dc71378

File tree

3 files changed

+150
-172
lines changed

3 files changed

+150
-172
lines changed

src/audio_unit/audio_format.rs

Lines changed: 143 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@
66

77
use libc;
88

9-
pub use self::standard_flags::StandardFlags;
10-
pub use self::linear_pcm_flags::LinearPCMFlags;
11-
pub use self::apple_lossless_flags::AppleLosslessFlags;
12-
pub use self::audio_time_stamp_flags::AudioTimeStampFlags;
13-
14-
159
/// A type-safe representation of both the `AudioFormatId` and their associated flags.
1610
#[derive(Copy, Clone, Debug)]
1711
#[allow(non_camel_case_types)]
1812
pub enum AudioFormat {
1913
/// Linear PCM; a non-compressed audio data format with one frame per packet.
2014
///
2115
/// **Available** in OS X v10.0 and later.
22-
LinearPCM(LinearPCMFlags), // = 1819304813,
16+
LinearPCM(LinearPcmFlags), // = 1819304813,
2317
/// An AC-3 codec.
2418
///
2519
/// **Available** in OS X v10.2 and later.
@@ -183,7 +177,7 @@ impl AudioFormat {
183177
/// Convert from the FFI C format and flags to a typesafe Rust enum representation.
184178
pub fn from_format_and_flag(format: libc::c_uint, flag: Option<u32>) -> Option<AudioFormat> {
185179
match (format, flag) {
186-
(1819304813, Some(i)) => Some(AudioFormat::LinearPCM(LinearPCMFlags::from_bits_truncate(i))),
180+
(1819304813, Some(i)) => Some(AudioFormat::LinearPCM(LinearPcmFlags::from_bits_truncate(i))),
187181
(1633889587, _) => Some(AudioFormat::AC3),
188182
(1667326771, Some(i)) => Some(AudioFormat::F60958AC3(StandardFlags::from_bits_truncate(i))),
189183
(1768775988, _) => Some(AudioFormat::AppleIMA4),
@@ -268,155 +262,143 @@ impl AudioFormat {
268262
}
269263

270264

271-
/// A wrapper around the const **StandardFlags**.
272-
pub mod standard_flags {
273-
bitflags! {
274-
/// Standard flags for use in the **F60958AC3** **AudioFormat** variant.
265+
bitflags! {
266+
/// Standard flags for use in the **F60958AC3** **AudioFormat** variant.
267+
///
268+
/// Note: In the original Core Audio API these are consolidated with what we have named the
269+
/// **StandardFlags** and **AppleLosslessFlags** types under the `AudioFormatFlag` type. We
270+
/// have chosen to separate these for greater type safety and clearer compatibility with
271+
/// the **AudioFormat** type.
272+
///
273+
/// Original documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/AudioStreamBasicDescription_Flags).
274+
pub struct StandardFlags: u32 {
275+
/// Set for floating point, clear for integer.
275276
///
276-
/// Note: In the original Core Audio API these are consolidated with what we have named the
277-
/// **StandardFlags** and **AppleLosslessFlags** types under the `AudioFormatFlag` type. We
278-
/// have chosen to separate these for greater type safety and clearer compatibility with
279-
/// the **AudioFormat** type.
280-
///
281-
/// Original documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/AudioStreamBasicDescription_Flags).
282-
pub struct StandardFlags: u32 {
283-
/// Set for floating point, clear for integer.
284-
///
285-
/// **Available** in OS X v10.2 and later.
286-
const IS_FLOAT = 1;
287-
/// Set for big endian, clear for little endian.
288-
///
289-
/// **Available** in OS X v10.2 and later.
290-
const IS_BIG_ENDIAN = 2;
291-
/// Set for signed integer, clear for unsigned integer.
292-
///
293-
/// Note: This is only valid if `IS_FLOAT` is clear.
294-
///
295-
/// **Available** in OS X v10.2 and later.
296-
const IS_SIGNED_INTEGER = 4;
297-
/// Set if the sample bits occupy the entire available bits for the channel, clear if they
298-
/// are high- or low-aligned within the channel.
299-
///
300-
/// **Available** in OS X v10.2 and later.
301-
const IS_PACKED = 8;
302-
/// Set if the sample bits are placed into the high bits of the channel, clear for low bit
303-
/// placement.
304-
///
305-
/// Note: This is only valid if `IS_PACKED` is clear.
306-
///
307-
/// **Available** in OS X v10.2 and later.
308-
const IS_ALIGNED_HIGH = 16;
309-
/// Set if the sample for each channel are located contiguously and the channels are laid
310-
/// out end to end.
311-
///
312-
/// Clear if the samples for each frame are laid out contiguously and the frames laid out
313-
/// end to end.
314-
///
315-
/// **Available** in OS X v10.2 and later.
316-
const IS_NON_INTERLEAVED = 32;
317-
/// Set to indicate when a format is nonmixable.
318-
///
319-
/// Note: that this flag is only used when interacting with the HAL's stream format
320-
/// information. It is **not** valid for any other use.
321-
///
322-
/// **Available** in OS X v10.3 and later.
323-
const IS_NON_MIXABLE = 64;
324-
}
325-
}
326-
}
327-
328-
329-
/// A wrapper around the const **LinearPCMFlags**.
330-
pub mod linear_pcm_flags {
331-
bitflags! {
332-
/// Flags for use within the **LinearPCM** **AudioFormat**.
277+
/// **Available** in OS X v10.2 and later.
278+
const IS_FLOAT = 1;
279+
/// Set for big endian, clear for little endian.
333280
///
334-
/// Note: In the original Core Audio API these are consolidated with what we have named the
335-
/// **StandardFlags** and **AppleLosslessFlags** types under the `AudioFormatFlag` type. We
336-
/// have chosen to separate these for greater type safety and clearer compatibility with
337-
/// the **AudioFormat** type.
281+
/// **Available** in OS X v10.2 and later.
282+
const IS_BIG_ENDIAN = 2;
283+
/// Set for signed integer, clear for unsigned integer.
338284
///
339-
/// Original documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/AudioStreamBasicDescription_Flags).
340-
pub struct LinearPCMFlags: u32 {
341-
/// Synonmyn for the **IS_FLOAT** **StandardFlags**.
342-
///
343-
/// **Available** in OS X v10.0 and later.
344-
const IS_FLOAT = 1;
345-
/// Synonmyn for the **IS_BIG_ENDIAN** **StandardFlags**.
346-
///
347-
/// **Available** in OS X v10.0 and later.
348-
const IS_BIG_ENDIAN = 2;
349-
/// Synonmyn for the **IS_SIGNED_INTEGER** **StandardFlags**.
350-
///
351-
/// **Available** in OS X v10.0 and later.
352-
const IS_SIGNED_INTEGER = 4;
353-
/// Synonmyn for the **IS_PACKED** **StandardFlags**.
354-
///
355-
/// **Available** in OS X v10.0 and later.
356-
const IS_PACKED = 8;
357-
/// Synonmyn for the **IS_ALIGNED_HIGH** **StandardFlags**.
358-
///
359-
/// **Available** in OS X v10.0 and later.
360-
const IS_ALIGNED_HIGH = 16;
361-
/// Synonmyn for the **IS_NON_INTERLEAVED** **StandardFlags**.
362-
///
363-
/// **Available** in OS X v10.2 and later.
364-
const IS_NON_INTERLEAVED = 32;
365-
/// Synonmyn for the **IS_NON_MIXABLE** **StandardFlags**.
366-
///
367-
/// **Available** in OS X v10.3 and later.
368-
const IS_NON_MIXABLE = 64;
369-
/// The linear PCM flags contain a 6-bit bitfield indicating that an integer format is to
370-
/// be interpreted as fixed point.
371-
///
372-
/// The value indicates the number of bits are used to represent the fractional portion of
373-
/// each sample value.
374-
///
375-
/// This constant indicates the bit position (counting from the right) of the bitfield in
376-
/// `mFormatFlags` field.
377-
///
378-
/// TODO: Review whether or not this flag indicates that we need to treat LinearPCM format
379-
/// uniquely in some way.
380-
///
381-
/// **Available** in OS X v10.6 and later.
382-
const FLAGS_SAMPLE_FRACTION_SHIFT = 7;
383-
/// The number of fractional bits.
384-
///
385-
/// `== (<other_flags> & FLAGS_SAMPLE_FRACTION_MASK) >> FLAGS_SAMPLE_FRACTION_SHIFT`
386-
///
387-
/// **Available** in OS X v10.6 and later.
388-
const FLAGS_SAMPLE_FRACTION_MASK = 8064;
389-
}
285+
/// Note: This is only valid if `IS_FLOAT` is clear.
286+
///
287+
/// **Available** in OS X v10.2 and later.
288+
const IS_SIGNED_INTEGER = 4;
289+
/// Set if the sample bits occupy the entire available bits for the channel, clear if they
290+
/// are high- or low-aligned within the channel.
291+
///
292+
/// **Available** in OS X v10.2 and later.
293+
const IS_PACKED = 8;
294+
/// Set if the sample bits are placed into the high bits of the channel, clear for low bit
295+
/// placement.
296+
///
297+
/// Note: This is only valid if `IS_PACKED` is clear.
298+
///
299+
/// **Available** in OS X v10.2 and later.
300+
const IS_ALIGNED_HIGH = 16;
301+
/// Set if the sample for each channel are located contiguously and the channels are laid
302+
/// out end to end.
303+
///
304+
/// Clear if the samples for each frame are laid out contiguously and the frames laid out
305+
/// end to end.
306+
///
307+
/// **Available** in OS X v10.2 and later.
308+
const IS_NON_INTERLEAVED = 32;
309+
/// Set to indicate when a format is nonmixable.
310+
///
311+
/// Note: that this flag is only used when interacting with the HAL's stream format
312+
/// information. It is **not** valid for any other use.
313+
///
314+
/// **Available** in OS X v10.3 and later.
315+
const IS_NON_MIXABLE = 64;
390316
}
391317
}
392318

393-
394-
/// A wrapper around the const **AppleLosslessFlags**.
395-
pub mod apple_lossless_flags {
396-
bitflags! {
397-
/// Flags set for Apple Lossless data.
319+
bitflags! {
320+
/// Flags for use within the **LinearPCM** **AudioFormat**.
321+
///
322+
/// Note: In the original Core Audio API these are consolidated with what we have named the
323+
/// **StandardFlags** and **AppleLosslessFlags** types under the `AudioFormatFlag` type. We
324+
/// have chosen to separate these for greater type safety and clearer compatibility with
325+
/// the **AudioFormat** type.
326+
///
327+
/// Original documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/AudioStreamBasicDescription_Flags).
328+
pub struct LinearPcmFlags: u32 {
329+
/// Synonmyn for the **IS_FLOAT** **StandardFlags**.
330+
///
331+
/// **Available** in OS X v10.0 and later.
332+
const IS_FLOAT = 1;
333+
/// Synonmyn for the **IS_BIG_ENDIAN** **StandardFlags**.
334+
///
335+
/// **Available** in OS X v10.0 and later.
336+
const IS_BIG_ENDIAN = 2;
337+
/// Synonmyn for the **IS_SIGNED_INTEGER** **StandardFlags**.
338+
///
339+
/// **Available** in OS X v10.0 and later.
340+
const IS_SIGNED_INTEGER = 4;
341+
/// Synonmyn for the **IS_PACKED** **StandardFlags**.
342+
///
343+
/// **Available** in OS X v10.0 and later.
344+
const IS_PACKED = 8;
345+
/// Synonmyn for the **IS_ALIGNED_HIGH** **StandardFlags**.
346+
///
347+
/// **Available** in OS X v10.0 and later.
348+
const IS_ALIGNED_HIGH = 16;
349+
/// Synonmyn for the **IS_NON_INTERLEAVED** **StandardFlags**.
350+
///
351+
/// **Available** in OS X v10.2 and later.
352+
const IS_NON_INTERLEAVED = 32;
353+
/// Synonmyn for the **IS_NON_MIXABLE** **StandardFlags**.
398354
///
399355
/// **Available** in OS X v10.3 and later.
356+
const IS_NON_MIXABLE = 64;
357+
/// The linear PCM flags contain a 6-bit bitfield indicating that an integer format is to
358+
/// be interpreted as fixed point.
400359
///
401-
/// Note: In the original Core Audio API these are consolidated with what we have named the
402-
/// **StandardFlags** and **AppleLosslessFlags** types under the `AudioFormatFlag` type. We
403-
/// have chosen to separate these for greater type safety and clearer compatibility with
404-
/// the **AudioFormat** type.
360+
/// The value indicates the number of bits are used to represent the fractional portion of
361+
/// each sample value.
405362
///
406-
/// Original documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/AudioStreamBasicDescription_Flags).
407-
pub struct AppleLosslessFlags: u32 {
408-
/// Sourced from 16 bit native endian signed integer data.
409-
const BIT_16_SOURCE_DATA = 1;
410-
/// Sourced from 20 bit native endian signed integer data aligned high in 24 bits.
411-
const BIT_20_SOURCE_DATA = 2;
412-
/// Sourced from 24 bit native endian signed integer data.
413-
const BIT_24_SOURCE_DATA = 3;
414-
/// Sourced from 32 bit native endian signed integer data.
415-
const BIT_32_SOURCE_DATA = 4;
416-
}
363+
/// This constant indicates the bit position (counting from the right) of the bitfield in
364+
/// `mFormatFlags` field.
365+
///
366+
/// TODO: Review whether or not this flag indicates that we need to treat LinearPCM format
367+
/// uniquely in some way.
368+
///
369+
/// **Available** in OS X v10.6 and later.
370+
const FLAGS_SAMPLE_FRACTION_SHIFT = 7;
371+
/// The number of fractional bits.
372+
///
373+
/// `== (<other_flags> & FLAGS_SAMPLE_FRACTION_MASK) >> FLAGS_SAMPLE_FRACTION_SHIFT`
374+
///
375+
/// **Available** in OS X v10.6 and later.
376+
const FLAGS_SAMPLE_FRACTION_MASK = 8064;
417377
}
418378
}
419379

380+
bitflags! {
381+
/// Flags set for Apple Lossless data.
382+
///
383+
/// **Available** in OS X v10.3 and later.
384+
///
385+
/// Note: In the original Core Audio API these are consolidated with what we have named the
386+
/// **StandardFlags** and **AppleLosslessFlags** types under the `AudioFormatFlag` type. We
387+
/// have chosen to separate these for greater type safety and clearer compatibility with
388+
/// the **AudioFormat** type.
389+
///
390+
/// Original documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/AudioStreamBasicDescription_Flags).
391+
pub struct AppleLosslessFlags: u32 {
392+
/// Sourced from 16 bit native endian signed integer data.
393+
const BIT_16_SOURCE_DATA = 1;
394+
/// Sourced from 20 bit native endian signed integer data aligned high in 24 bits.
395+
const BIT_20_SOURCE_DATA = 2;
396+
/// Sourced from 24 bit native endian signed integer data.
397+
const BIT_24_SOURCE_DATA = 3;
398+
/// Sourced from 32 bit native endian signed integer data.
399+
const BIT_32_SOURCE_DATA = 4;
400+
}
401+
}
420402

421403
/// "Used in the `mFormatFlags` field of an `AudioStreamBasicDescription` structure that
422404
/// describes an MPEG-4 audio stream to specify the type of MPEG-4 audio data.
@@ -474,26 +456,22 @@ impl Mpeg4ObjectId {
474456
}
475457
}
476458

477-
478-
/// A wrapper around the const **AudioTimeStampFlags**.
479-
pub mod audio_time_stamp_flags {
480-
bitflags! {
481-
/// "These flags indicate the valuid fields in an AudioTimeStamp structure."
482-
///
483-
/// **Available** in OS X v10.0 and later.
484-
///
485-
/// Original Documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/Audio_Time_Stamp_Flags).
486-
pub struct AudioTimeStampFlags: u32 {
487-
/// The sample frame time is valid.
488-
const SAMPLE_TIME_VALID = 1;
489-
/// The host time is valid.
490-
const HOST_TIME_VALID = 2;
491-
/// The rate scalar is valid.
492-
const RATE_SCALAR_VALID = 4;
493-
/// The world clock time is valid.
494-
const WORLD_CLOCK_TIME_VALID = 8;
495-
/// The SMPTE time is valid.
496-
const SMPTE_TIME_VALID = 16;
497-
}
459+
bitflags! {
460+
/// "These flags indicate the valuid fields in an AudioTimeStamp structure."
461+
///
462+
/// **Available** in OS X v10.0 and later.
463+
///
464+
/// Original Documentation [here](https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/doc/constant_group/Audio_Time_Stamp_Flags).
465+
pub struct AudioTimeStampFlags: u32 {
466+
/// The sample frame time is valid.
467+
const SAMPLE_TIME_VALID = 1;
468+
/// The host time is valid.
469+
const HOST_TIME_VALID = 2;
470+
/// The rate scalar is valid.
471+
const RATE_SCALAR_VALID = 4;
472+
/// The world clock time is valid.
473+
const WORLD_CLOCK_TIME_VALID = 8;
474+
/// The SMPTE time is valid.
475+
const SMPTE_TIME_VALID = 16;
498476
}
499477
}

src/audio_unit/sample_format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::audio_format::{self, linear_pcm_flags};
1+
use super::audio_format::{self, LinearPcmFlags};
22

33

44
/// Dynamic representation of audio data sample format.
@@ -12,9 +12,9 @@ pub enum SampleFormat {
1212

1313
impl SampleFormat {
1414

15-
pub fn does_match_flags(&self, flags: audio_format::LinearPCMFlags) -> bool {
16-
let is_float = flags.contains(linear_pcm_flags::IS_FLOAT);
17-
let is_signed_integer = flags.contains(linear_pcm_flags::IS_SIGNED_INTEGER);
15+
pub fn does_match_flags(&self, flags: audio_format::LinearPcmFlags) -> bool {
16+
let is_float = flags.contains(LinearPcmFlags::IS_FLOAT);
17+
let is_signed_integer = flags.contains(LinearPcmFlags::IS_SIGNED_INTEGER);
1818
match *self {
1919
SampleFormat::F32 => is_float && !is_signed_integer,
2020
SampleFormat::I32 |
@@ -23,10 +23,10 @@ impl SampleFormat {
2323
}
2424
}
2525

26-
pub fn from_flags_and_bytes_per_frame(flags: audio_format::LinearPCMFlags,
26+
pub fn from_flags_and_bytes_per_frame(flags: audio_format::LinearPcmFlags,
2727
bytes_per_frame: u32) -> Option<Self>
2828
{
29-
Some(if flags.contains(linear_pcm_flags::IS_FLOAT) {
29+
Some(if flags.contains(LinearPcmFlags::IS_FLOAT) {
3030
SampleFormat::F32
3131
} else {
3232
// TODO: Check whether or not we need to consider unsigned ints and `IS_PACKED`.

src/audio_unit/stream_format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct StreamFormat {
4646
/// battery drain when processing audio. iOS provides a Converter audio unit and inclues the
4747
/// interfaces from Audio Converter Services (TODO: look into exposing this).
4848
pub sample_format: SampleFormat,
49-
pub flags: super::audio_format::LinearPCMFlags,
49+
pub flags: super::audio_format::LinearPcmFlags,
5050
pub channels_per_frame: u32,
5151
}
5252

0 commit comments

Comments
 (0)