1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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;

// maybe use StreamSourceGenerator directly
#[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)]
/// audio_streams_conformance_test
pub struct Args {
    /// the StreamSource to use for playback. (default: noop).
    #[argh(
        option,
        short = 'P',
        default = "default_stream_source()",
        from_str_fn(parse_stream_source)
    )]
    pub stream_source: StreamSourceEnum,
    /// the channel numbers. (default: 2)
    #[argh(option, short = 'c', default = "default_channels()")]
    pub channels: usize,
    /// format. Must be in [U8, S16_LE, S24_LE, S32_LE]. (default:S16_LE)
    #[argh(
        option,
        short = 'f',
        default = "default_sample_format()",
        from_str_fn(parse_format)
    )]
    pub format: SampleFormat,
    /// sample rate. (default: 48000)
    #[argh(option, short = 'r', default = "default_rate()")]
    pub rate: u32,
    /// block buffer size (frames) of each write. (default: 240).
    #[argh(option, short = 'b', default = "default_buffer_frames()")]
    pub buffer_frames: usize,
    /// the iterations to fill in the audio buffer. default: 10)
    #[argh(option, default = "default_iterations()")]
    pub iterations: usize,
    /// whether or not to print in json format
    #[argh(switch)]
    pub json: bool,
    /// whether or not to print the debug messages
    #[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
        )
    }
}