Skip to content

Commit

Permalink
check for empty buffers when dequeuing into host
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Chatzi committed Sep 23, 2023
1 parent 4637a2c commit 3163919
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ run CMD:

# run all tests - requires `cargo-nextest`
test:
cargo nextest run --all-targets
cargo nextest run --all-targets --all-features

# run a command every time source files change - requires `cargo-watch`
dev CMD='just b':
Expand Down
4 changes: 4 additions & 0 deletions aud/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ cbindgen = "0.26.0"
[[bench]]
name = "audio_packet_sequence"
harness = false

[[bench]]
name = "host_audio_io"
harness = false
96 changes: 50 additions & 46 deletions aud/lib/benches/host_audio_io.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
#[cfg(feature = "bench")]
mod bench {
use aud_lib::audio::*;
use criterion::{criterion_group, criterion_main, Criterion};
use crossbeam::channel::unbounded;
use rand::random;
use std::iter::repeat_with;
use aud_lib::audio::*;
use criterion::{criterion_group, criterion_main, Criterion};
use rand::random;
use std::iter::repeat_with;

fn bench_enqueue(c: &mut Criterion) {
for buffer_size in [128, 256, 512, 1024] {
let mut group = c.benchmark_group(format!("Enqueue Mono Buffer Size {}", buffer_size));
group.bench_function("Mono", |b| {
let (sender, _receiver) = unbounded();
let input_channels = 1;
let selected_channels = vec![0];
let enqueue_fn = test_make_audio_buffer_enqueueing_function(
sender,
input_channels,
selected_channels,
);
let audio_buffer: Vec<f32> = repeat_with(|| random::<f32>())
.take(buffer_size * input_channels)
.collect();
b.iter(|| {
enqueue_fn(&audio_buffer);
});
fn bench_enqueue(c: &mut Criterion) {
for buffer_size in [128, 256, 512, 1024] {
let mut group = c.benchmark_group(format!("Enqueue Mono Buffer Size {}", buffer_size));
group.bench_function("Mono", |b| {
let (sender, _receiver) = crossbeam::channel::unbounded();
let input_channels = 1;
let selected_channels = vec![0];
let enqueue_fn = test_make_audio_buffer_enqueueing_function(
sender,
input_channels,
selected_channels,
);
let audio_buffer: Vec<f32> = repeat_with(|| random::<f32>())
.take(buffer_size * input_channels)
.collect();
b.iter(|| {
enqueue_fn(&audio_buffer);
});
group.finish();
}
});
group.finish();
}
}

fn bench_dequeue(c: &mut Criterion) {
for buffer_size in [128, 256, 512, 1024] {
let mut group = c.benchmark_group(format!("Dequeue Stereo Buffer Size {}", buffer_size));
group.bench_function("Stereo", |b| {
let (sender, receiver) = crossbeam::channel::unbounded();
let total_channels = 2;
let selected_channels = vec![0, 1];
let dequeue_fn = test_make_audio_dequeing_function(
receiver.clone(),
total_channels,
selected_channels,
);
let mut audio_buffer: Vec<f32> = vec![0.0; buffer_size * total_channels];

fn bench_dequeue(c: &mut Criterion) {
for buffer_size in [128, 256, 512, 1024] {
let mut group =
c.benchmark_group(format!("Dequeue Stereo Buffer Size {}", buffer_size));
group.bench_function("Stereo", |b| {
let (_sender, receiver) = unbounded();
let total_channels = 2;
let selected_channels = vec![0, 1];
let dequeue_fn =
test_make_audio_dequeing_function(receiver, total_channels, selected_channels);
let mut audio_buffer: Vec<f32> = vec![0.0; buffer_size * total_channels];
b.iter(|| {
dequeue_fn(&mut audio_buffer);
});
b.iter(|| {
let mock_data = AudioBuffer::with_length(
(buffer_size * total_channels) as u32,
total_channels as u32,
);
sender.send(mock_data.clone()).unwrap(); // Clone the mock data
dequeue_fn(&mut audio_buffer);
});
group.finish();
}
});
group.finish();
}

criterion_group!(host_audio_io, bench_enqueue, bench_dequeue);
criterion_main!(host_audio_io);
}

criterion_group!(host_audio_io, bench_enqueue, bench_dequeue);
criterion_main!(host_audio_io);
27 changes: 13 additions & 14 deletions aud/lib/examples/udp_audio_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@ use std::time::Duration;
#[derive(Default, Debug)]
pub struct AudioInfo {
num_samples: u32,
num_channels: u32,
}

impl From<AudioBuffer> for AudioInfo {
fn from(buf: AudioBuffer) -> Self {
Self {
num_samples: buf.num_frames() as u32,
num_channels: buf.num_channels,
}
}
num_buffers: u32,
num_silence: usize,
}

struct LoggingAudioConsumer {
Expand All @@ -38,14 +30,22 @@ impl Default for LoggingAudioConsumer {
AudioInfo::default(),
|stats: AudioInfo, buffer: &AudioBuffer| AudioInfo {
num_samples: stats.num_samples + buffer.num_frames() as u32,
num_channels: stats.num_channels + buffer.num_channels as u32,
num_buffers: stats.num_buffers + 1,
num_silence: stats.num_silence
+ buffer
.data
.iter()
.all(|s| *s == 0.)
.then_some(1)
.unwrap_or_default(),
},
);
buffers.try_lock().unwrap().clear();
log::info!(
"last second : received {} samples, with {} buffers",
"last second : received {} samples, with {} buffers, with {} silence buffers",
stats.num_samples,
stats.num_channels
stats.num_buffers,
stats.num_silence,
);
sleep(Duration::from_millis(1_000));
}
Expand All @@ -60,7 +60,6 @@ impl Default for LoggingAudioConsumer {

impl AudioConsuming for LoggingAudioConsumer {
fn consume_audio_buffer(&mut self, buffer: AudioBuffer) -> anyhow::Result<()> {
log::info!("{buffer:?}");
let mut buffers = self.buffers.lock().unwrap();
buffers.push(buffer);
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions aud/lib/src/audio/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ where

let buffer = AudioBuffer::from_buffers(received_buffers);
let buffer_num_channels = buffer.num_channels as usize;
if buffer_num_channels == 0 {
return;
}

let buffer_num_frames = buffer.data.len() / buffer_num_channels;

for (frame_idx, frame) in audio_buffer.chunks_mut(total_num_channels).enumerate() {
Expand Down
3 changes: 1 addition & 2 deletions aud/lib/src/audio/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ where
fn try_send_audio(&mut self) -> anyhow::Result<()> {
let buffer = self.audio_provider.retrieve_audio_buffer();
for packet in self.sequence.from_buffer(&buffer).into_packets() {
log::info!("{packet:?}");
if let Err(e) = self.responses.try_send(AudioResponse::Audio(packet)) {
log::error!("Failed to pass audio response to socket tasks : {e}");
}
Expand Down Expand Up @@ -220,7 +219,7 @@ mod test {
}
}

#[test_log::test]
#[test]
fn receiver_can_request_devices() {
let expected_device_list = vec![
AudioDevice {
Expand Down

0 comments on commit 3163919

Please sign in to comment.