feat(audio): implement AAudio C-ABI types and stream builder
This commit is contained in:
parent
620fe80608
commit
f3c5a1eef6
5 changed files with 155 additions and 0 deletions
84
crates/molotov-core/src/audio/builder.rs
Normal file
84
crates/molotov-core/src/audio/builder.rs
Normal file
|
|
@ -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<AAudioDataCallback>,
|
||||
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<AAudioDataCallback>, user_data: *mut ()) {
|
||||
self.data_callback = callback;
|
||||
self.user_data = user_data;
|
||||
}
|
||||
|
||||
pub fn data_callback(&self) -> Option<AAudioDataCallback> {
|
||||
self.data_callback
|
||||
}
|
||||
|
||||
pub fn user_data(&self) -> *mut () {
|
||||
self.user_data
|
||||
}
|
||||
}
|
||||
3
crates/molotov-core/src/audio/mod.rs
Normal file
3
crates/molotov-core/src/audio/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
pub mod builder;
|
||||
pub mod types;
|
||||
50
crates/molotov-core/src/audio/types.rs
Normal file
50
crates/molotov-core/src/audio/types.rs
Normal file
|
|
@ -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,
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
pub mod audio;
|
||||
pub mod dynamic;
|
||||
pub mod elf;
|
||||
pub mod linker;
|
||||
|
|
|
|||
17
crates/molotov-core/tests/aaudio_builder_test.rs
Normal file
17
crates/molotov-core/tests/aaudio_builder_test.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
Reference in a new issue