From c90708142512a7e981b3f8f8f23a5927b2d0f5b6 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Thu, 31 Jul 2025 11:52:40 +0800 Subject: [PATCH 1/8] android input preset --- Cargo.toml | 3 ++- src/lib.rs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) 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/lib.rs b/src/lib.rs index efea3a379..3aa69be12 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(feature = "android-input-preset")] + pub input_preset: ndk::audio::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(feature = "android-input-preset")] + input_preset: ndk::audio::AudioInputPreset::VoiceRecognition, } } } From 34ca87b7f29b774a7f9bfe4e3389e5b724f43d75 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Thu, 31 Jul 2025 14:31:33 +0800 Subject: [PATCH 2/8] fix clippy --- src/host/aaudio/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/host/aaudio/mod.rs b/src/host/aaudio/mod.rs index f5f024461..70f5abfce 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(), @@ -404,6 +403,7 @@ impl DeviceTrait for Device { }; let builder = ndk::audio::AudioStreamBuilder::new()? + .input_preset(config.input_preset) .direction(ndk::audio::AudioDirection::Input) .channel_count(channel_count) .format(format); @@ -437,13 +437,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(), From f792b87aab05fbb1bb1d0ee33a059cc03733533f Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Thu, 31 Jul 2025 15:00:13 +0800 Subject: [PATCH 3/8] pub use AudioInputPreset --- src/platform/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 718e34e0b..838aa2e34 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -708,6 +708,7 @@ mod platform_impl { Stream as AAudioStream, SupportedInputConfigs as AAudioSupportedInputConfigs, SupportedOutputConfigs as AAudioSupportedOutputConfigs, }; + pub use ndk::audio::AudioInputPreset; impl_platform_host!(AAudio aaudio "AAudio"); From 2a4b3756c5eb6b62b044a66600332fce774dd61b Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Thu, 31 Jul 2025 17:43:30 +0800 Subject: [PATCH 4/8] fix lost feature --- src/platform/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 838aa2e34..1dd192a39 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -708,6 +708,7 @@ 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"); From 7f98979bbbb28dbcf1e7f0172c6e41555e3e59b9 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Thu, 31 Jul 2025 17:50:24 +0800 Subject: [PATCH 5/8] fix field --- src/host/aaudio/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/host/aaudio/mod.rs b/src/host/aaudio/mod.rs index 70f5abfce..8ee75d97e 100644 --- a/src/host/aaudio/mod.rs +++ b/src/host/aaudio/mod.rs @@ -403,11 +403,13 @@ impl DeviceTrait for Device { }; let builder = ndk::audio::AudioStreamBuilder::new()? - .input_preset(config.input_preset) .direction(ndk::audio::AudioDirection::Input) .channel_count(channel_count) .format(format); + #[cfg(feature = "android-input-preset")] + let builder = builder.input_preset(config.input_preset); + build_input_stream( self, config, From d673bff49352b544ae47561ed9422b89405ead12 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Fri, 1 Aug 2025 10:19:45 +0800 Subject: [PATCH 6/8] cfg all --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3aa69be12..e5a599c0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,7 +263,7 @@ pub struct StreamConfig { pub channels: ChannelCount, pub sample_rate: SampleRate, pub buffer_size: BufferSize, - #[cfg(feature = "android-input-preset")] + #[cfg(all(target_os = "android", feature = "android-input-preset"))] pub input_preset: ndk::audio::AudioInputPreset, } From 51d0a00f720e13b0733ef081121af06da96cecde Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Fri, 1 Aug 2025 10:23:59 +0800 Subject: [PATCH 7/8] fix cfg --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e5a599c0d..ff1e4a88f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -412,7 +412,7 @@ impl SupportedStreamConfig { channels: self.channels, sample_rate: self.sample_rate, buffer_size: BufferSize::Default, - #[cfg(feature = "android-input-preset")] + #[cfg(all(target_os = "android", feature = "android-input-preset"))] input_preset: ndk::audio::AudioInputPreset::VoiceRecognition, } } From 42a897ceca97bd1aab3c0162fb0985f1121b3d41 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Fri, 1 Aug 2025 10:29:54 +0800 Subject: [PATCH 8/8] use platform --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ff1e4a88f..c005d0a2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,7 +264,7 @@ pub struct StreamConfig { pub sample_rate: SampleRate, pub buffer_size: BufferSize, #[cfg(all(target_os = "android", feature = "android-input-preset"))] - pub input_preset: ndk::audio::AudioInputPreset, + pub input_preset: platform::AudioInputPreset, } /// Describes the minimum and maximum supported buffer size for the device @@ -413,7 +413,7 @@ impl SupportedStreamConfig { sample_rate: self.sample_rate, buffer_size: BufferSize::Default, #[cfg(all(target_os = "android", feature = "android-input-preset"))] - input_preset: ndk::audio::AudioInputPreset::VoiceRecognition, + input_preset: platform::AudioInputPreset::VoiceRecognition, } } }