Skip to content

Commit

Permalink
Empty Source type should report 0 channels
Browse files Browse the repository at this point in the history
move channel from fix to constructor, add test

Empty Source should not have a sample rate. Sample Rate Conversion to handle Empty source properly

add more doc around Empty
  • Loading branch information
DivineGod committed May 1, 2023
1 parent 023ee21 commit 1167092
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
12 changes: 12 additions & 0 deletions examples/channel_volume.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use rodio::source::ChannelVolume;

fn main() {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

let input = rodio::source::SineWave::new(440.0);
let chan_vol = ChannelVolume::new(input, vec![0.01, 0.01, 0.0, 0.0, 0.0, 0.0]);
sink.append(chan_vol);

sink.sleep_until_end();
}
12 changes: 11 additions & 1 deletion src/conversions/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ where
from: cpal::ChannelCount,
to: cpal::ChannelCount,
) -> ChannelCountConverter<I> {
assert!(from >= 1);
assert!(to >= 1);
let from = match from {
0 => to,
n => n,
};

ChannelCountConverter {
input,
Expand Down Expand Up @@ -139,4 +142,11 @@ mod test {
let output = ChannelCountConverter::new(input.into_iter(), 2, 1);
assert_eq!(output.len(), 2);
}

#[test]
fn zero_input() {
let input = vec![1u16, 2, 3, 4, 5, 6];
let output = ChannelCountConverter::new(input.into_iter(), 0, 3);
assert_eq!(output.len(), 6);
}
}
5 changes: 4 additions & 1 deletion src/conversions/sample_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ where
to: cpal::SampleRate,
num_channels: cpal::ChannelCount,
) -> SampleRateConverter<I> {
let from = from.0;
let from = match from.0 {
0 => to.0,
n => n,
};
let to = to.0;

assert!(from >= 1);
Expand Down
9 changes: 6 additions & 3 deletions src/source/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::time::Duration;
use crate::{Sample, Source};

/// An empty source.
///
/// The empty source is special in that it will never return any data.
/// It also reports 0 channels, a sample rate of 0, and a Duration of 0.
#[derive(Debug, Copy, Clone)]
pub struct Empty<S>(PhantomData<S>);

Expand Down Expand Up @@ -36,17 +39,17 @@ where
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
None
Some(0)
}

#[inline]
fn channels(&self) -> u16 {
1
0
}

#[inline]
fn sample_rate(&self) -> u32 {
48000
0
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/source/sine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl SineWave {
#[inline]
pub fn new(freq: f32) -> SineWave {
SineWave {
freq: freq,
freq,
num_sample: 0,
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/source/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ where
// Limit the frame length to something reasonable
let frame_len = input.current_frame_len().map(|x| x.min(32768));

let from_channels = input.channels();
let from_channels = match input.channels() {
n if n == 0 => target_channels,
n => n,
};
let from_sample_rate = input.sample_rate();

let input = Take {
Expand Down

0 comments on commit 1167092

Please sign in to comment.