Path: blob/main/audio_streams_conformance_test/src/args.rs
5392 views
// Copyright 2022 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34use std::fmt;5use std::str::FromStr;67use argh::FromArgs;8use audio_streams::*;9use serde::Serialize;1011use super::error::Error;12use super::sys::StreamSource as SysStreamSource;1314// maybe use StreamSourceGenerator directly15#[derive(Copy, Clone, Debug, PartialEq, Serialize)]16pub enum StreamSourceEnum {17NoopStream,18Sys(SysStreamSource),19}2021impl fmt::Display for StreamSourceEnum {22fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {23match self {24StreamSourceEnum::NoopStream => write!(f, "noop"),25StreamSourceEnum::Sys(stream_source) => stream_source.fmt(f),26}27}28}2930impl FromStr for StreamSourceEnum {31type Err = Error;32fn from_str(s: &str) -> ::std::result::Result<StreamSourceEnum, Self::Err> {33match s {34"noop" => Ok(StreamSourceEnum::NoopStream),35_ => SysStreamSource::try_from(s).map(StreamSourceEnum::Sys),36}37}38}3940fn default_channels() -> usize {41242}4344fn default_sample_format() -> SampleFormat {45SampleFormat::S16LE46}4748fn default_rate() -> u32 {494800050}5152fn default_buffer_frames() -> usize {5324054}5556fn default_iterations() -> usize {571058}5960fn default_stream_source() -> StreamSourceEnum {61StreamSourceEnum::NoopStream62}6364fn parse_stream_source(value: &str) -> Result<StreamSourceEnum, String> {65StreamSourceEnum::from_str(value).map_err(|e| e.to_string())66}6768fn parse_format(value: &str) -> Result<SampleFormat, String> {69SampleFormat::from_str(value).map_err(|e| e.to_string())70}7172#[derive(Copy, Clone, Debug, FromArgs, Serialize)]73/// audio_streams_conformance_test74pub struct Args {75/// the StreamSource to use for playback. (default: noop).76#[argh(77option,78short = 'P',79default = "default_stream_source()",80from_str_fn(parse_stream_source)81)]82pub stream_source: StreamSourceEnum,83/// the channel numbers. (default: 2)84#[argh(option, short = 'c', default = "default_channels()")]85pub channels: usize,86/// format. Must be in [U8, S16_LE, S24_LE, S32_LE]. (default:S16_LE)87#[argh(88option,89short = 'f',90default = "default_sample_format()",91from_str_fn(parse_format)92)]93pub format: SampleFormat,94/// sample rate. (default: 48000)95#[argh(option, short = 'r', default = "default_rate()")]96pub rate: u32,97/// block buffer size (frames) of each write. (default: 240).98#[argh(option, short = 'b', default = "default_buffer_frames()")]99pub buffer_frames: usize,100/// the iterations to fill in the audio buffer. default: 10)101#[argh(option, default = "default_iterations()")]102pub iterations: usize,103/// whether or not to print in json format104#[argh(switch)]105pub json: bool,106/// whether or not to print the debug messages107#[argh(switch)]108pub debug: bool,109}110111impl fmt::Display for Args {112fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {113write!(114f,115r#"116Playback Source: {:?}117Channels: {}118Format: {:?}119Sample rate: {} frames/s120Buffer size: {} frames121Iterations: {}122"#,123self.stream_source,124self.channels,125self.format,126self.rate,127self.buffer_frames,128self.iterations129)130}131}132133134