33#![ cfg( target_os = "android" ) ]
44
55use crate :: { AudioOutputDevice , BaseAudioOutputDevice , OutputDeviceParameters } ;
6- use aaudio:: {
7- AAudioStream , AAudioStreamBuilder , CallbackResult , Direction , Format , PerformanceMode ,
6+ use ndk:: audio:: {
7+ AudioStream , AudioStreamBuilder , AudioCallbackResult , AudioDirection , AudioFormat , AudioPerformanceMode ,
8+ AudioError ,
89} ;
910use std:: error:: Error ;
1011
1112pub struct AAudioOutputDevice {
12- stream : AAudioStream ,
13+ _stream : AudioStream ,
1314}
1415
1516impl BaseAudioOutputDevice for AAudioOutputDevice { }
1617
1718unsafe impl Send for AAudioOutputDevice { }
1819
19- fn convert_err ( err : aaudio :: Error ) -> Box < dyn Error > {
20+ fn convert_err ( err : AudioError ) -> Box < dyn Error > {
2021 format ! ( "{:?}" , err) . into ( )
2122}
2223
@@ -27,45 +28,37 @@ impl AudioOutputDevice for AAudioOutputDevice {
2728 Self : Sized ,
2829 {
2930 let frame_count = params. channel_sample_count as i32 ;
30- let mut stream = AAudioStreamBuilder :: new ( )
31+ let stream = AudioStreamBuilder :: new ( )
3132 . map_err ( convert_err) ?
3233 // Ensure double buffering is possible.
33- . set_buffer_capacity_in_frames ( 2 * frame_count)
34- . set_channel_count ( params. channels_count as i32 )
35- . set_format ( Format :: F32 )
36- . set_sample_rate ( params. sample_rate as i32 )
37- . set_direction ( Direction :: Output )
38- . set_performance_mode ( PerformanceMode :: LowLatency )
34+ . buffer_capacity_in_frames ( 2 * frame_count)
35+ . channel_count ( params. channels_count as i32 )
36+ . format ( AudioFormat :: PCM_Float )
37+ . sample_rate ( params. sample_rate as i32 )
38+ . direction ( AudioDirection :: Output )
39+ . performance_mode ( AudioPerformanceMode :: LowLatency )
3940 // Force the AAudio to give the buffer of fixed size.
40- . set_frames_per_data_callback ( frame_count)
41- . set_callbacks (
42- move |_, data, num_frames| {
41+ . frames_per_data_callback ( frame_count)
42+ . data_callback (
43+ Box :: new ( move |_, data, num_frames| {
4344 let output_data = unsafe {
4445 std:: slice:: from_raw_parts_mut :: < f32 > (
45- data. as_mut_ptr ( ) as * mut f32 ,
46+ data as * mut f32 ,
4647 num_frames as usize * params. channels_count ,
4748 )
4849 } ;
4950
5051 data_callback ( output_data) ;
5152
52- CallbackResult :: Continue
53- } ,
54- |_, error| eprintln ! ( "AAudio: an error has occurred - {:?}" , error) ,
53+ AudioCallbackResult :: Continue
54+ } )
5555 )
56+ . error_callback ( Box :: new ( |_, error| eprintln ! ( "AAudio: an error has occurred - {:?}" , error) ) )
5657 . open_stream ( )
5758 . map_err ( convert_err) ?;
5859
5960 stream. request_start ( ) . map_err ( convert_err) ?;
6061
61- Ok ( Self { stream } )
62- }
63- }
64-
65- impl Drop for AAudioOutputDevice {
66- fn drop ( & mut self ) {
67- self . stream
68- . release ( )
69- . expect ( "Failed to release the stream!" )
62+ Ok ( Self { _stream : stream } )
7063 }
7164}
0 commit comments