Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stereo -> mono downmixing. #241

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cubeb-backend = "0.13"
float-cmp = "0.6"
libc = "0.2"
mach = "0.3"
num = "0.4.3"
audio-mixer = "0.2"
ringbuf = "0.2.6"
triple_buffer = "5.0.5"
Expand Down
31 changes: 28 additions & 3 deletions src/backend/buffer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::os::raw::c_void;
use std::slice;

use cubeb_backend::SampleFormat;
use num::cast::AsPrimitive;

use super::ringbuf::RingBuffer;

Expand Down Expand Up @@ -36,12 +37,25 @@ fn drop_first_n_channels_in_place<T: Copy>(
}
}

trait DataType: AsPrimitive<f32> {
fn from_f32(v: f32) -> Self;
}

impl<T: AsPrimitive<f32>> DataType for T
where
f32: AsPrimitive<T>,
{
fn from_f32(v: f32) -> T {
v.as_()
}
}

// It can be that the a stereo microphone is in use, but the user asked for mono input. In this
// particular case, downmix the stereo pair into a mono channel. In all other cases, simply drop
// the remaining channels before appending to the ringbuffer, becauses there is no right or wrong
// way to do this, unlike with the output side, where proper channel matrixing can be done.
// Return the number of valid samples in the buffer.
fn remix_or_drop_channels<T: Copy + std::ops::Add<Output = T>>(
fn remix_or_drop_channels<T: DataType>(
input_channels: usize,
output_channels: usize,
data: &mut [T],
Expand All @@ -56,7 +70,8 @@ fn remix_or_drop_channels<T: Copy + std::ops::Add<Output = T>>(
if input_channels == 2 && output_channels == 1 {
let mut read_idx = 0;
for (write_idx, _) in (0..frame_count).enumerate() {
data[write_idx] = data[read_idx] + data[read_idx + 1];
let avg = (data[read_idx].as_() + data[read_idx + 1].as_()) / 2.0;
data[write_idx] = DataType::from_f32(avg);
read_idx += 2;
}
return output_channels * frame_count;
Expand All @@ -76,7 +91,7 @@ fn remix_or_drop_channels<T: Copy + std::ops::Add<Output = T>>(
output_channels * frame_count
}

fn process_data<T: Copy + std::ops::Add<Output = T>>(
fn process_data<T: DataType>(
data: *mut c_void,
frame_count: usize,
input_channel_count: usize,
Expand Down Expand Up @@ -353,3 +368,13 @@ impl fmt::Debug for BufferManager {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn remix_stereo_ints() {
let mut data = [i16::MAX / 2 + 1, i16::MAX / 2 + 1];
assert_eq!(remix_or_drop_channels(2, 1, &mut data, 1), 1);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern crate cubeb_backend;
extern crate float_cmp;
extern crate mach;

extern crate num;

mod backend;
mod capi;

Expand Down
Loading