use crate::device::AudioFormat;
use crate::error::{Error, Result};
pub struct PlaybackFrame {
pub data: Vec<u8>,
pub frames: i32,
pub channels: i32,
pub sample_rate: i32,
pub format: AudioFormat,
}
impl PlaybackFrame {
pub fn from_s16(data: &[i16], channels: i32, sample_rate: i32) -> Result<Self> {
if channels <= 0 {
return Err(Error::InvalidChannels);
}
let frames = data.len() as i32 / channels;
let bytes: Vec<u8> = data
.iter()
.flat_map(|&sample| sample.to_le_bytes())
.collect();
Ok(Self {
data: bytes,
frames,
channels,
sample_rate,
format: AudioFormat::S16,
})
}
pub fn from_f32(data: &[f32], channels: i32, sample_rate: i32) -> Result<Self> {
if channels <= 0 {
return Err(Error::InvalidChannels);
}
let frames = data.len() as i32 / channels;
let bytes: Vec<u8> = data
.iter()
.flat_map(|&sample| sample.to_le_bytes())
.collect();
Ok(Self {
data: bytes,
frames,
channels,
sample_rate,
format: AudioFormat::F32,
})
}
}
pub struct AudioPlaybackConfig {
pub device_id: Option<String>,
pub sample_rate: i32,
pub channels: i32,
}
impl Default for AudioPlaybackConfig {
fn default() -> Self {
Self {
device_id: None,
sample_rate: 48000,
channels: 2,
}
}
}
pub struct AudioPlayback {
config: AudioPlaybackConfig,
}
impl AudioPlayback {
pub fn new<F>(_config: AudioPlaybackConfig, _callback: F) -> Result<Self>
where
F: Fn() -> Option<PlaybackFrame> + Send + Sync + 'static,
{
Err(Error::SessionCreateFailed)
}
pub fn start(&mut self) -> Result<()> {
Err(Error::SessionStartFailed)
}
pub fn stop(&mut self) {
}
pub fn config(&self) -> &AudioPlaybackConfig {
&self.config
}
pub fn sample_rate(&self) -> i32 {
self.config.sample_rate
}
pub fn channels(&self) -> i32 {
self.config.channels
}
}