diff --git a/crates/molotov-core/src/audio/builder.rs b/crates/molotov-core/src/audio/builder.rs new file mode 100644 index 0000000..64e0675 --- /dev/null +++ b/crates/molotov-core/src/audio/builder.rs @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +use super::types::{AAudioDirection, AAudioFormat}; + +pub type AAudioDataCallback = unsafe extern "C" fn( + stream: *mut (), + user_data: *mut (), + audio_data: *mut (), + num_frames: i32, +) -> i32; + +pub struct AAudioStreamBuilder { + direction: AAudioDirection, + sample_rate: i32, + channel_count: i32, + format: AAudioFormat, + pub buffer_capacity_in_frames: i32, + data_callback: Option, + user_data: *mut (), +} + +impl Default for AAudioStreamBuilder { + fn default() -> Self { + Self::new() + } +} + +impl AAudioStreamBuilder { + pub fn new() -> Self { + Self { + direction: AAudioDirection::Output, + sample_rate: 48000, + channel_count: 2, + format: AAudioFormat::PcmFloat, + buffer_capacity_in_frames: 1024, + data_callback: None, + user_data: std::ptr::null_mut(), + } + } + + pub fn set_direction(&mut self, direction: AAudioDirection) { + self.direction = direction; + } + + pub fn direction(&self) -> AAudioDirection { + self.direction + } + + pub fn set_sample_rate(&mut self, sample_rate: i32) { + self.sample_rate = sample_rate; + } + + pub fn sample_rate(&self) -> i32 { + self.sample_rate + } + + pub fn set_channel_count(&mut self, channel_count: i32) { + self.channel_count = channel_count; + } + + pub fn channel_count(&self) -> i32 { + self.channel_count + } + + pub fn set_format(&mut self, format: AAudioFormat) { + self.format = format; + } + + pub fn format(&self) -> AAudioFormat { + self.format + } + + pub fn set_data_callback(&mut self, callback: Option, user_data: *mut ()) { + self.data_callback = callback; + self.user_data = user_data; + } + + pub fn data_callback(&self) -> Option { + self.data_callback + } + + pub fn user_data(&self) -> *mut () { + self.user_data + } +} diff --git a/crates/molotov-core/src/audio/mod.rs b/crates/molotov-core/src/audio/mod.rs new file mode 100644 index 0000000..1bb4555 --- /dev/null +++ b/crates/molotov-core/src/audio/mod.rs @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pub mod builder; +pub mod types; diff --git a/crates/molotov-core/src/audio/types.rs b/crates/molotov-core/src/audio/types.rs new file mode 100644 index 0000000..b5489f2 --- /dev/null +++ b/crates/molotov-core/src/audio/types.rs @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#[repr(i32)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AAudioDirection { + Output = 0, + Input = 1, +} + +#[repr(i32)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AAudioFormat { + Unspecified = 0, + PcmI16 = 1, + PcmFloat = 2, +} + +#[repr(i32)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AAudioStreamState { + Uninitialized = 0, + Open = 1, + Starting = 2, + Started = 3, + Pausing = 4, + Paused = 5, + Stopping = 6, + Stopped = 7, + Closing = 8, + Closed = 9, + Disconnected = 10, +} + +#[repr(i32)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AAudioResult { + Ok = 0, + ErrorBase = -900, + ErrorDisconnected = -899, + ErrorIllegalArgument = -898, + ErrorInternal = -897, + ErrorInvalidState = -895, + ErrorInvalidHandle = -892, + ErrorUnimplemented = -890, + ErrorUnavailable = -889, + ErrorNoFreeHandles = -888, + ErrorNoMemory = -887, + ErrorNull = -886, + ErrorTimeout = -885, +} diff --git a/crates/molotov-core/src/lib.rs b/crates/molotov-core/src/lib.rs index 4880353..60ffa62 100644 --- a/crates/molotov-core/src/lib.rs +++ b/crates/molotov-core/src/lib.rs @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later +pub mod audio; pub mod dynamic; pub mod elf; pub mod linker; diff --git a/crates/molotov-core/tests/aaudio_builder_test.rs b/crates/molotov-core/tests/aaudio_builder_test.rs new file mode 100644 index 0000000..788b763 --- /dev/null +++ b/crates/molotov-core/tests/aaudio_builder_test.rs @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +use molotov_core::audio::builder::AAudioStreamBuilder; +use molotov_core::audio::types::{AAudioDirection, AAudioFormat}; + +#[test] +fn test_stream_builder_defaults_and_setters() { + let mut builder = AAudioStreamBuilder::new(); + builder.set_direction(AAudioDirection::Input); + builder.set_sample_rate(48000); + builder.set_channel_count(2); + builder.set_format(AAudioFormat::PcmFloat); + + assert_eq!(builder.direction(), AAudioDirection::Input); + assert_eq!(builder.sample_rate(), 48000); + assert_eq!(builder.channel_count(), 2); + assert_eq!(builder.format(), AAudioFormat::PcmFloat); +}