8
8
pub use self :: action_flags:: ActionFlags ;
9
9
pub use self :: data:: Data ;
10
10
11
+ #[ cfg( target_os = "ios" ) ]
12
+ use audio_unit:: audio_session_get_property;
13
+
11
14
12
15
/// When `set_render_callback` is called, a closure of this type will be used to wrap the given
13
16
/// render callback function.
@@ -398,7 +401,7 @@ impl AudioUnit {
398
401
// First, we'll retrieve the stream format so that we can ensure that the given callback
399
402
// format matches the audio unit's format.
400
403
let id = sys:: kAudioUnitProperty_StreamFormat;
401
- let asbd = try!( self . get_property ( id, Scope :: Output , Element :: Output ) ) ;
404
+ let asbd = try!( self . get_property ( id, Scope :: Input , Element :: Output ) ) ;
402
405
let stream_format = super :: StreamFormat :: from_asbd ( asbd) ?;
403
406
404
407
// If the stream format does not match, return an error indicating this.
@@ -471,7 +474,7 @@ impl AudioUnit {
471
474
// First, we'll retrieve the stream format so that we can ensure that the given callback
472
475
// format matches the audio unit's format.
473
476
let id = sys:: kAudioUnitProperty_StreamFormat;
474
- let asbd = self . get_property ( id, Scope :: Input , Element :: Input ) ?;
477
+ let asbd = self . get_property ( id, Scope :: Output , Element :: Input ) ?;
475
478
let stream_format = super :: StreamFormat :: from_asbd ( asbd) ?;
476
479
477
480
// If the stream format does not match, return an error indicating this.
@@ -482,8 +485,20 @@ impl AudioUnit {
482
485
// Pre-allocate a buffer list for input stream.
483
486
//
484
487
// First, get the current buffer size for pre-allocating the `AudioBuffer`s.
485
- let id = sys:: kAudioDevicePropertyBufferFrameSize;
486
- let mut buffer_frame_size: u32 = self . get_property ( id, Scope :: Global , Element :: Output ) ?;
488
+ #[ cfg( target_os = "macos" ) ]
489
+ let mut buffer_frame_size: u32 = {
490
+ let id = sys:: kAudioDevicePropertyBufferFrameSize;
491
+ let buffer_frame_size: u32 = self . get_property ( id, Scope :: Global , Element :: Output ) ?;
492
+ buffer_frame_size
493
+ } ;
494
+ #[ cfg( target_os = "ios" ) ]
495
+ let mut buffer_frame_size: u32 = {
496
+ let id = sys:: kAudioSessionProperty_CurrentHardwareIOBufferDuration;
497
+ let seconds: f32 = super :: audio_session_get_property ( id) ?;
498
+ let id = sys:: kAudioSessionProperty_CurrentHardwareSampleRate;
499
+ let sample_rate: f64 = super :: audio_session_get_property ( id) ?;
500
+ ( sample_rate * seconds as f64 ) . round ( ) as u32
501
+ } ;
487
502
let mut data: Vec < u8 > = vec ! [ ] ;
488
503
let sample_bytes = stream_format. sample_format . size_in_bytes ( ) ;
489
504
let n_channels = stream_format. channels_per_frame ;
@@ -525,7 +540,7 @@ impl AudioUnit {
525
540
unsafe {
526
541
// Retrieve the up-to-date stream format.
527
542
let id = sys:: kAudioUnitProperty_StreamFormat;
528
- let asbd = match super :: get_property ( audio_unit, id, Scope :: Input , Element :: Output ) {
543
+ let asbd = match super :: get_property ( audio_unit, id, Scope :: Output , Element :: Input ) {
529
544
Err ( err) => return err. to_os_status ( ) ,
530
545
Ok ( asbd) => asbd,
531
546
} ;
0 commit comments