Skip to content

Commit 1167092

Browse files
committed
Empty Source type should report 0 channels
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
1 parent 023ee21 commit 1167092

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

examples/channel_volume.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use rodio::source::ChannelVolume;
2+
3+
fn main() {
4+
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
5+
let sink = rodio::Sink::try_new(&handle).unwrap();
6+
7+
let input = rodio::source::SineWave::new(440.0);
8+
let chan_vol = ChannelVolume::new(input, vec![0.01, 0.01, 0.0, 0.0, 0.0, 0.0]);
9+
sink.append(chan_vol);
10+
11+
sink.sleep_until_end();
12+
}

src/conversions/channels.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ where
2727
from: cpal::ChannelCount,
2828
to: cpal::ChannelCount,
2929
) -> ChannelCountConverter<I> {
30-
assert!(from >= 1);
3130
assert!(to >= 1);
31+
let from = match from {
32+
0 => to,
33+
n => n,
34+
};
3235

3336
ChannelCountConverter {
3437
input,
@@ -139,4 +142,11 @@ mod test {
139142
let output = ChannelCountConverter::new(input.into_iter(), 2, 1);
140143
assert_eq!(output.len(), 2);
141144
}
145+
146+
#[test]
147+
fn zero_input() {
148+
let input = vec![1u16, 2, 3, 4, 5, 6];
149+
let output = ChannelCountConverter::new(input.into_iter(), 0, 3);
150+
assert_eq!(output.len(), 6);
151+
}
142152
}

src/conversions/sample_rate.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ where
4747
to: cpal::SampleRate,
4848
num_channels: cpal::ChannelCount,
4949
) -> SampleRateConverter<I> {
50-
let from = from.0;
50+
let from = match from.0 {
51+
0 => to.0,
52+
n => n,
53+
};
5154
let to = to.0;
5255

5356
assert!(from >= 1);

src/source/empty.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::time::Duration;
44
use crate::{Sample, Source};
55

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

@@ -36,17 +39,17 @@ where
3639
{
3740
#[inline]
3841
fn current_frame_len(&self) -> Option<usize> {
39-
None
42+
Some(0)
4043
}
4144

4245
#[inline]
4346
fn channels(&self) -> u16 {
44-
1
47+
0
4548
}
4649

4750
#[inline]
4851
fn sample_rate(&self) -> u32 {
49-
48000
52+
0
5053
}
5154

5255
#[inline]

src/source/sine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl SineWave {
1717
#[inline]
1818
pub fn new(freq: f32) -> SineWave {
1919
SineWave {
20-
freq: freq,
20+
freq,
2121
num_sample: 0,
2222
}
2323
}

src/source/uniform.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ where
5656
// Limit the frame length to something reasonable
5757
let frame_len = input.current_frame_len().map(|x| x.min(32768));
5858

59-
let from_channels = input.channels();
59+
let from_channels = match input.channels() {
60+
n if n == 0 => target_channels,
61+
n => n,
62+
};
6063
let from_sample_rate = input.sample_rate();
6164

6265
let input = Take {

0 commit comments

Comments
 (0)