use std::fmt;
use std::str::FromStr;
use argh::FromArgs;
use audio_streams::*;
use serde::Serialize;
use super::error::Error;
use super::sys::StreamSource as SysStreamSource;
#[derive(Copy, Clone, Debug, PartialEq, Serialize)]
pub enum StreamSourceEnum {
NoopStream,
Sys(SysStreamSource),
}
impl fmt::Display for StreamSourceEnum {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
StreamSourceEnum::NoopStream => write!(f, "noop"),
StreamSourceEnum::Sys(stream_source) => stream_source.fmt(f),
}
}
}
impl FromStr for StreamSourceEnum {
type Err = Error;
fn from_str(s: &str) -> ::std::result::Result<StreamSourceEnum, Self::Err> {
match s {
"noop" => Ok(StreamSourceEnum::NoopStream),
_ => SysStreamSource::try_from(s).map(StreamSourceEnum::Sys),
}
}
}
fn default_channels() -> usize {
2
}
fn default_sample_format() -> SampleFormat {
SampleFormat::S16LE
}
fn default_rate() -> u32 {
48000
}
fn default_buffer_frames() -> usize {
240
}
fn default_iterations() -> usize {
10
}
fn default_stream_source() -> StreamSourceEnum {
StreamSourceEnum::NoopStream
}
fn parse_stream_source(value: &str) -> Result<StreamSourceEnum, String> {
StreamSourceEnum::from_str(value).map_err(|e| e.to_string())
}
fn parse_format(value: &str) -> Result<SampleFormat, String> {
SampleFormat::from_str(value).map_err(|e| e.to_string())
}
#[derive(Copy, Clone, Debug, FromArgs, Serialize)]
pub struct Args {
#[argh(
option,
short = 'P',
default = "default_stream_source()",
from_str_fn(parse_stream_source)
)]
pub stream_source: StreamSourceEnum,
#[argh(option, short = 'c', default = "default_channels()")]
pub channels: usize,
#[argh(
option,
short = 'f',
default = "default_sample_format()",
from_str_fn(parse_format)
)]
pub format: SampleFormat,
#[argh(option, short = 'r', default = "default_rate()")]
pub rate: u32,
#[argh(option, short = 'b', default = "default_buffer_frames()")]
pub buffer_frames: usize,
#[argh(option, default = "default_iterations()")]
pub iterations: usize,
#[argh(switch)]
pub json: bool,
#[argh(switch)]
pub debug: bool,
}
impl fmt::Display for Args {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
r#"
Playback Source: {:?}
Channels: {}
Format: {:?}
Sample rate: {} frames/s
Buffer size: {} frames
Iterations: {}
"#,
self.stream_source,
self.channels,
self.format,
self.rate,
self.buffer_frames,
self.iterations
)
}
}