Skip to content

Commit

Permalink
cras_tests: Support --effects
Browse files Browse the repository at this point in the history
BUG=b:312599801
TEST=cras_tests capture --effects=0x11 /dev/null

Change-Id: I521310c148a61fde678d5b38d4f0f2cf638259da
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5056791
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
Commit-Queue: Li-Yu Yu <aaronyu@google.com>
  • Loading branch information
afq984 authored and Chromeos LUCI committed Nov 24, 2023
1 parent 1b301f2 commit 571854a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
1 change: 1 addition & 0 deletions cras/client/cras_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"

[dependencies]
audio_streams = "*"
cras-sys = "*"
clap = { version = "3.1.10", features = ["derive"] }
dbus = "0.9"
hound = "3.4.0"
Expand Down
38 changes: 38 additions & 0 deletions cras/client/cras_tests/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use thiserror::Error as ThisError;

use crate::getter::GetCommand;
use crate::setter::SetCommand;
use std::str::FromStr;

#[derive(ThisError, Debug)]
pub enum Error {
Expand Down Expand Up @@ -106,6 +107,22 @@ pub struct AudioOptions {
/// Duration of playing/recording action in seconds.
#[clap(short = 'd', long = "duration")]
pub duration_sec: Option<usize>,

/// Capture effects to enable.
#[clap(long = "effects", value_parser = parse_hex_or_decimal)]
pub effects: Option<u32>,
}

/// Parse string starting with 0x as hex and others as decimal.
fn parse_hex_or_decimal(maybe_hex_string: &str) -> std::result::Result<u32, String> {
if let Some(hex_string) = maybe_hex_string.strip_prefix("0x") {
u32::from_str_radix(hex_string, 16)
} else if let Some(hex_string) = maybe_hex_string.strip_prefix("0X") {
u32::from_str_radix(hex_string, 16)
} else {
u32::from_str(maybe_hex_string)
}
.map_err(|e| format!("invalid numeric value {}: {}", maybe_hex_string, e))
}

impl AudioOptions {
Expand Down Expand Up @@ -220,6 +237,7 @@ mod tests {
format: None,
buffer_size: None,
duration_sec: None,
effects: None,
})
);

Expand All @@ -235,6 +253,7 @@ mod tests {
format: None,
buffer_size: None,
duration_sec: None,
effects: None,
})
);

Expand All @@ -258,6 +277,7 @@ mod tests {
format: None,
buffer_size: None,
duration_sec: None,
effects: None,
})
);

Expand All @@ -283,6 +303,7 @@ mod tests {
format: None,
buffer_size: None,
duration_sec: Some(5),
effects: None,
})
);

Expand All @@ -306,6 +327,23 @@ mod tests {
format: None,
buffer_size: None,
duration_sec: Some(10),
effects: None,
})
);

let command = Command::parse_from(["cras_tests", "capture", "out.wav", "--effects=0x11"]);
assert_eq!(
command,
Command::Capture(AudioOptions {
file_name: PathBuf::from("out.wav"),
loopback_type: None,
file_type_option: None,
frame_rate: None,
num_channels: None,
format: None,
buffer_size: None,
duration_sec: None,
effects: Some(0x11),
})
);

Expand Down
33 changes: 17 additions & 16 deletions cras/client/cras_tests/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};

use audio_streams::{SampleFormat, StreamSource};
use cras_sys::gen::CRAS_SPECIAL_DEVICE;
use hound::{WavReader, WavSpec, WavWriter};

use libchromeos::signal::register_signal_handler;
Expand Down Expand Up @@ -191,6 +192,7 @@ pub fn playback(opts: AudioOptions) -> Result<()> {
let frame_rate;
let format;
let file_type = opts.file_type().map_err(Error::ParseArgs)?;
assert!(opts.effects.is_none(), "playback effects not supported");

let mut sample_source: Box<dyn Read> = match file_type {
FileType::Wav => {
Expand Down Expand Up @@ -416,33 +418,32 @@ pub fn capture(opts: AudioOptions) -> Result<()> {

let mut cras_client = CrasClient::new().map_err(Error::Libcras)?;
cras_client.enable_cras_capture();
let (_control, mut stream) = match opts.loopback_type {

let pin_iodev_index = match opts.loopback_type {
Some(loopback_type) => {
let node_type = match loopback_type {
LoopbackType::PreDsp => CrasNodeType::CRAS_NODE_TYPE_POST_MIX_PRE_DSP,
LoopbackType::PostDsp => CrasNodeType::CRAS_NODE_TYPE_POST_DSP,
};

let loopback_node = cras_client
.input_nodes()
.find(|node| node.node_type == node_type)
.ok_or(Error::NoLoopbackNode(node_type))?;

cras_client
.new_pinned_capture_stream(
loopback_node.iodev_index,
num_channels,
format,
frame_rate,
buffer_size,
CrasStreamEffect(0),
)
.map_err(Error::CreateStream)?
loopback_node.iodev_index
}
None => cras_client
.new_capture_stream(num_channels, format, frame_rate, buffer_size, &[])
.map_err(Error::CreateStream)?,
None => CRAS_SPECIAL_DEVICE::NO_DEVICE as u32,
};
let (_control, mut stream) = cras_client
.new_pinned_capture_stream(
pin_iodev_index,
num_channels,
format,
frame_rate,
buffer_size,
CrasStreamEffect(opts.effects.unwrap_or(0)),
)
.map_err(Error::CreateStream)?;

set_priority_to_realtime();
add_sigint_handler()?;
while !INTERRUPTED.load(Ordering::Acquire) {
Expand Down

0 comments on commit 571854a

Please sign in to comment.