-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add abstraction layer between alsa and audio api
- Loading branch information
Showing
8 changed files
with
147 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use alsa::{Direction, PCM, pcm::{HwParams, Format, Access}, ValueOr}; | ||
use bark_protocol::time::SampleDuration; | ||
use thiserror::Error; | ||
|
||
use crate::audio::config::DeviceOpt; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum OpenError { | ||
#[error("alsa error: {0}")] | ||
Alsa(#[from] alsa::Error), | ||
#[error("invalid period size (min = {min}, max = {max})")] | ||
InvalidPeriodSize { min: i64, max: i64 }, | ||
#[error("invalid buffer size (min = {min}, max = {max})")] | ||
InvalidBufferSize { min: i64, max: i64 }, | ||
} | ||
|
||
pub fn open_pcm(opt: &DeviceOpt, direction: Direction) | ||
-> Result<PCM, OpenError> | ||
{ | ||
let device_name = opt.device.as_deref().unwrap_or("default"); | ||
let pcm = PCM::new(device_name, direction, false)?; | ||
|
||
{ | ||
let hwp = HwParams::any(&pcm)?; | ||
hwp.set_channels(bark_protocol::CHANNELS.0.into())?; | ||
hwp.set_rate(bark_protocol::SAMPLE_RATE.0, ValueOr::Nearest)?; | ||
hwp.set_format(Format::float())?; | ||
hwp.set_access(Access::RWInterleaved)?; | ||
set_period_size(&hwp, opt.period)?; | ||
set_buffer_size(&hwp, opt.buffer)?; | ||
pcm.hw_params(&hwp)?; | ||
} | ||
|
||
{ | ||
let hwp = pcm.hw_params_current()?; | ||
let swp = pcm.sw_params_current()?; | ||
swp.set_start_threshold(hwp.get_buffer_size()?)?; | ||
} | ||
|
||
let (buffer, period) = pcm.get_params()?; | ||
log::info!("opened ALSA with buffer_size={buffer}, period_size={period}"); | ||
|
||
Ok(pcm) | ||
} | ||
|
||
// period is the size of the discrete chunks of data that are sent to hardware | ||
fn set_period_size(hwp: &HwParams, period: SampleDuration) | ||
-> Result<(), OpenError> | ||
{ | ||
let min = hwp.get_period_size_min()?; | ||
let max = hwp.get_period_size_max()?; | ||
|
||
let period = period.to_frame_count().try_into().ok() | ||
.filter(|size| { *size >= min && *size <= max }) | ||
.ok_or(OpenError::InvalidPeriodSize { min, max })?; | ||
|
||
hwp.set_period_size(period, ValueOr::Nearest)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// period is the size of the discrete chunks of data that are sent to hardware | ||
fn set_buffer_size(hwp: &HwParams, buffer: SampleDuration) | ||
-> Result<(), OpenError> | ||
{ | ||
let min = hwp.get_buffer_size_min()?; | ||
let max = hwp.get_buffer_size_max()?; | ||
|
||
let buffer = buffer.to_frame_count().try_into().ok() | ||
.filter(|size| *size >= min && *size <= max) | ||
.ok_or(OpenError::InvalidBufferSize { min, max })?; | ||
|
||
hwp.set_buffer_size(buffer)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
use bark_core::audio::Frame; | ||
use bark_protocol::time::{SampleDuration, Timestamp}; | ||
use thiserror::Error; | ||
|
||
use self::config::DeviceOpt; | ||
|
||
pub mod alsa { | ||
pub mod config; | ||
pub mod input; | ||
pub mod output; | ||
} | ||
|
||
pub mod config; | ||
pub mod input; | ||
pub mod output; | ||
|
||
#[derive(Debug, Error)] | ||
#[error(transparent)] | ||
pub enum OpenError { | ||
Alsa(#[from] alsa::config::OpenError), | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
#[error(transparent)] | ||
pub enum Error { | ||
Alsa(#[from] ::alsa::Error), | ||
} | ||
|
||
pub struct Input { | ||
alsa: alsa::input::Input, | ||
} | ||
|
||
impl Input { | ||
pub fn new(opt: DeviceOpt) -> Result<Self, OpenError> { | ||
Ok(Input { | ||
alsa: alsa::input::Input::new(opt)?, | ||
}) | ||
} | ||
|
||
pub fn read(&self, audio: &mut [Frame]) -> Result<Timestamp, Error> { | ||
Ok(self.alsa.read(audio)?) | ||
} | ||
} | ||
|
||
pub struct Output { | ||
alsa: alsa::output::Output, | ||
} | ||
|
||
impl Output { | ||
pub fn new(opt: DeviceOpt) -> Result<Self, OpenError> { | ||
Ok(Output { | ||
alsa: alsa::output::Output::new(opt)?, | ||
}) | ||
} | ||
|
||
pub fn write(&self, audio: &[Frame]) -> Result<(), Error> { | ||
Ok(self.alsa.write(audio)?) | ||
} | ||
|
||
pub fn delay(&self) -> Result<SampleDuration, Error> { | ||
Ok(self.alsa.delay()?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters