diff --git a/Cargo.toml b/Cargo.toml index 3014e1562..90ffb3260 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.70" [features] asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions. +android-input-preset = ["ndk/api-level-28"] # Deprecated, the `oboe` backend has been removed oboe-shared-stdcxx = [] @@ -83,7 +84,7 @@ js-sys = { version = "0.3.35" } web-sys = { version = "0.3.35", features = [ "AudioContext", "AudioContextOptions", "AudioBuffer", "AudioBufferSourceNode", "AudioNode", "AudioDestinationNode", "Window", "AudioContextState"] } [target.'cfg(target_os = "android")'.dependencies] -ndk = { version = "0.9", default-features = false, features = ["audio", "api-level-26"]} +ndk = { version = "0.9", default-features = false, features = ["audio", "api-level-26"] } ndk-context = "0.1" jni = "0.21" num-derive = "0.4" diff --git a/src/host/aaudio/mod.rs b/src/host/aaudio/mod.rs index f5f024461..8ee75d97e 100644 --- a/src/host/aaudio/mod.rs +++ b/src/host/aaudio/mod.rs @@ -1,4 +1,3 @@ -use std::cell::RefCell; use std::cmp; use std::convert::TryInto; use std::time::{Duration, Instant}; @@ -14,7 +13,7 @@ use crate::{ BackendSpecificError, BufferSize, BuildStreamError, Data, DefaultStreamConfigError, DeviceNameError, DevicesError, InputCallbackInfo, InputStreamTimestamp, OutputCallbackInfo, OutputStreamTimestamp, PauseStreamError, PlayStreamError, SampleFormat, SampleRate, - SizedSample, StreamConfig, StreamError, SupportedBufferSize, SupportedStreamConfig, + StreamConfig, StreamError, SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange, SupportedStreamConfigsError, }; @@ -256,7 +255,7 @@ where ); ndk::audio::AudioCallbackResult::Continue })) - .error_callback(Box::new(move |stream, error| { + .error_callback(Box::new(move |_, error| { (error_callback)(StreamError::from(error)) })) .open_stream()?; @@ -298,7 +297,7 @@ where ); ndk::audio::AudioCallbackResult::Continue })) - .error_callback(Box::new(move |stream, error| { + .error_callback(Box::new(move |_, error| { (error_callback)(StreamError::from(error)) })) .open_stream()?; @@ -388,13 +387,13 @@ impl DeviceTrait for Device { return Err(BackendSpecificError { description: format!("{} format is not supported on Android.", sample_format), } - .into()) + .into()); } }; let channel_count = match config.channels { 1 => 1, 2 => 2, - channels => { + _ => { // TODO: more channels available in native AAudio return Err(BackendSpecificError { description: "More than 2 channels are not supported yet.".to_owned(), @@ -408,6 +407,9 @@ impl DeviceTrait for Device { .channel_count(channel_count) .format(format); + #[cfg(feature = "android-input-preset")] + let builder = builder.input_preset(config.input_preset); + build_input_stream( self, config, @@ -437,13 +439,13 @@ impl DeviceTrait for Device { return Err(BackendSpecificError { description: format!("{} format is not supported on Android.", sample_format), } - .into()) + .into()); } }; let channel_count = match config.channels { 1 => 1, 2 => 2, - channels => { + _ => { // TODO: more channels available in native AAudio return Err(BackendSpecificError { description: "More than 2 channels are not supported yet.".to_owned(), diff --git a/src/lib.rs b/src/lib.rs index efea3a379..c005d0a2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,6 +263,8 @@ pub struct StreamConfig { pub channels: ChannelCount, pub sample_rate: SampleRate, pub buffer_size: BufferSize, + #[cfg(all(target_os = "android", feature = "android-input-preset"))] + pub input_preset: platform::AudioInputPreset, } /// Describes the minimum and maximum supported buffer size for the device @@ -410,6 +412,8 @@ impl SupportedStreamConfig { channels: self.channels, sample_rate: self.sample_rate, buffer_size: BufferSize::Default, + #[cfg(all(target_os = "android", feature = "android-input-preset"))] + input_preset: platform::AudioInputPreset::VoiceRecognition, } } } diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 718e34e0b..1dd192a39 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -708,6 +708,8 @@ mod platform_impl { Stream as AAudioStream, SupportedInputConfigs as AAudioSupportedInputConfigs, SupportedOutputConfigs as AAudioSupportedOutputConfigs, }; + #[cfg(feature = "android-input-preset")] + pub use ndk::audio::AudioInputPreset; impl_platform_host!(AAudio aaudio "AAudio");