Skip to content

Commit 3d10392

Browse files
authored
Merge pull request #13 from ColorBuffer/main
migrate from "aaudio" crate to "ndk"
2 parents 4f6db07 + 5f44fd1 commit 3d10392

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ members = ["android-examples", "wasm-examples", "ios-example/Rust-TinyAudioExamp
2929
opt-level = 3
3030

3131
[target.'cfg(target_os = "android")'.dependencies]
32-
aaudio = "0.1.1"
32+
ndk = {version = "0.9.0", default-features = false, features = ["audio", "api-level-27"]}
3333

3434
[target.'cfg(target_os = "windows")'.dependencies]
3535
winapi = { version = "0.3.9", features = ["minwindef", "winnt", "windef", "winuser", "dsound", "synchapi", "winbase"] }

src/aaudio.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
#![cfg(target_os = "android")]
44

55
use 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
};
910
use std::error::Error;
1011

1112
pub struct AAudioOutputDevice {
12-
stream: AAudioStream,
13+
_stream: AudioStream,
1314
}
1415

1516
impl BaseAudioOutputDevice for AAudioOutputDevice {}
1617

1718
unsafe 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

Comments
 (0)