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

Channel difference handling #290

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions symphonia-play/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ mod cpal {
}
else {
// Use the default config for Windows.
// We can't control amount of channels or sample rate in received default config, so process case of difference later.
device
.default_output_config()
.expect("Failed to get the default output config.")
Expand Down Expand Up @@ -297,10 +298,22 @@ mod cpal {
}

let sample_buf = SampleBuffer::<T>::new(duration, spec);

let resampler = if spec.rate != config.sample_rate.0 {
info!("resampling {} Hz to {} Hz", spec.rate, config.sample_rate.0);
Some(Resampler::new(spec, config.sample_rate.0 as usize, duration))
let resampler = if spec.rate != config.sample_rate.0
|| spec.channels.count() != config.channels as usize
{
info!(
"resampling {} Hz ({} channels) to {} Hz ({} channels)",
spec.rate,
spec.channels.count(),
config.sample_rate.0,
config.channels
);
Some(Resampler::new(
spec,
config.sample_rate.0 as usize,
duration,
config.channels as usize,
))
}
else {
None
Expand Down
23 changes: 19 additions & 4 deletions symphonia-play/src/resampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Resampler<T> {
output: Vec<Vec<f32>>,
interleaved: Vec<T>,
duration: usize,
output_channels: usize,
}

impl<T> Resampler<T>
Expand Down Expand Up @@ -45,13 +46,14 @@ where
}

// Interleave the planar samples from Rubato.
let num_channels = self.output.len();
// In some cases amount of channels in audio stream isn't equal to the amount of channels in the output device, so fill excess output channels with default values.
let num_channels = self.output_channels;

self.interleaved.resize(num_channels * self.output[0].len(), T::MID);

for (i, frame) in self.interleaved.chunks_exact_mut(num_channels).enumerate() {
for (ch, s) in frame.iter_mut().enumerate() {
*s = self.output[ch][i].into_sample();
*s = self.output.get(ch).map(|x| x[i].into_sample()).unwrap_or(T::MID);
}
}

Expand All @@ -63,7 +65,13 @@ impl<T> Resampler<T>
where
T: Sample + FromSample<f32> + IntoSample<f32>,
{
pub fn new(spec: SignalSpec, to_sample_rate: usize, duration: u64) -> Self {
pub fn new(
spec: SignalSpec,
to_sample_rate: usize,
duration: u64,
output_channels: usize,
) -> Self {
assert!(output_channels > 0);
let duration = duration as usize;
let num_channels = spec.channels.count();

Expand All @@ -80,7 +88,14 @@ where

let input = vec![Vec::with_capacity(duration); num_channels];

Self { resampler, input, output, duration, interleaved: Default::default() }
Self {
resampler,
input,
output,
duration,
interleaved: Default::default(),
output_channels,
}
}

/// Resamples a planar/non-interleaved input.
Expand Down