Skip to content

Commit

Permalink
v2.0.3: use input device default buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
GiviMAD committed Apr 20, 2023
1 parent 12634c3 commit ebd80f4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 66 deletions.
84 changes: 42 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustpotter-cli"
version = "2.0.2"
version = "2.0.3"
edition = "2021"
license = "Apache-2.0"
description = "CLI for Rustpotter, an open source wakeword spotter forged in rust."
Expand Down
6 changes: 3 additions & 3 deletions src/cli/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use cpal::{FromSample, Sample};
#[clap()]
pub struct RecordCommand {
#[clap()]
/// Generated record path
/// Generated record path.
output_path: String,
#[clap(short = 'i', long)]
/// Input device index used for record
/// Input device index used for record.
device_index: Option<usize>,
#[clap(short, long)]
/// Input device index used for record
/// Input device configuration index used for record.
config_index: Option<usize>,
#[clap(short, long, default_value_t = 1.)]
/// Adjust the recording volume. value > 1.0 amplifies, value < 1.0 attenuates
Expand Down
58 changes: 38 additions & 20 deletions src/cli/spot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ pub struct SpotCommand {
/// Input device index used for record.
device_index: Option<usize>,
#[clap(short, long)]
/// Input device index used for record.
/// Input device config index used for record.
config_index: Option<usize>,
#[clap(long)]
/// Set the stream buffer size to a near value to the rustpotter buffer size.
custom_buffer_size: bool,
#[clap(long)]
/// Set the stream buffer size.
manual_buffer_size: Option<u32>,
#[clap(short, long, default_value_t = 0.5)]
/// Default detection threshold, only applies to models without threshold.
threshold: f32,
Expand Down Expand Up @@ -109,23 +115,28 @@ pub fn spot(command: SpotCommand) -> Result<(), String> {
println!("Rustpotter config:\n{:?}", config);
}
let mut rustpotter = Rustpotter::new(&config)?;

let required_buffer_size = if host_name == "ALSA" {
// force even buffer size to workaround issue mentioned here https://github.com/RustAudio/cpal/pull/582#pullrequestreview-1095655011
rustpotter.get_samples_per_frame() as u32 * 2

let required_buffer_size: Option<u32> = if command.custom_buffer_size
|| command.manual_buffer_size.is_some()
{
let mut required_buffer_size = command
.manual_buffer_size
.unwrap_or(rustpotter.get_samples_per_frame() as u32);
if host_name == "ALSA" && required_buffer_size % 2 != 0 {
// force even buffer size to workaround issue mentioned here https://github.com/RustAudio/cpal/pull/582#pullrequestreview-1095655011
required_buffer_size = required_buffer_size + 1;
}
if !is_compatible_buffer_size(&device_config.buffer_size(), required_buffer_size) {
clap::Error::raw(
clap::error::ErrorKind::Io,
"Required buffer size does not matches device configuration, try selecting other.\n",
)
.exit();
}
Some(required_buffer_size)
} else {
rustpotter.get_samples_per_frame() as u32
None
};
if !is_compatible_buffer_size(
&device_config.buffer_size(),
required_buffer_size,
) {
clap::Error::raw(
clap::error::ErrorKind::Io,
"Rustpotter required buffer size does not matches device configuration, try selecting other.\n",
)
.exit();
}
for path in command.model_path {
let result = rustpotter.add_wakeword_from_file(&path);
if let Err(error) = result {
Expand All @@ -146,7 +157,8 @@ pub fn spot(command: SpotCommand) -> Result<(), String> {
let stream_config = cpal::StreamConfig {
channels: device_config.channels(),
sample_rate: device_config.sample_rate(),
buffer_size: cpal::BufferSize::Fixed(required_buffer_size),
buffer_size: required_buffer_size
.map_or(cpal::BufferSize::Default, |v| cpal::BufferSize::Fixed(v)),
};
if command.debug {
println!("Audio stream config: {:?}", stream_config);
Expand All @@ -163,7 +175,9 @@ pub fn spot(command: SpotCommand) -> Result<(), String> {
move |data: &[i16], _: &_| {
buffer_i16.extend_from_slice(data);
while buffer_i16.len() >= rustpotter_samples_per_frame {
let detection = rustpotter.process_i16(buffer_i16.drain(0..rustpotter_samples_per_frame).as_slice());
let detection = rustpotter.process_i16(
buffer_i16.drain(0..rustpotter_samples_per_frame).as_slice(),
);
print_detection(
&rustpotter,
detection,
Expand All @@ -183,7 +197,9 @@ pub fn spot(command: SpotCommand) -> Result<(), String> {
move |data: &[i32], _: &_| {
buffer_i32.extend_from_slice(data);
while buffer_i32.len() >= rustpotter_samples_per_frame {
let detection = rustpotter.process_i32(&buffer_i32.drain(0..rustpotter_samples_per_frame).as_slice());
let detection = rustpotter.process_i32(
&buffer_i32.drain(0..rustpotter_samples_per_frame).as_slice(),
);
print_detection(
&rustpotter,
detection,
Expand All @@ -203,7 +219,9 @@ pub fn spot(command: SpotCommand) -> Result<(), String> {
move |data: &[f32], _: &_| {
buffer_f32.extend_from_slice(data);
while buffer_f32.len() >= rustpotter_samples_per_frame {
let detection = rustpotter.process_f32(buffer_f32.drain(0..rustpotter_samples_per_frame).as_slice());
let detection = rustpotter.process_f32(
buffer_f32.drain(0..rustpotter_samples_per_frame).as_slice(),
);
print_detection(
&rustpotter,
detection,
Expand Down

0 comments on commit ebd80f4

Please sign in to comment.