Skip to content

Commit a571f59

Browse files
committed
add test and fix take_span
1 parent b443817 commit a571f59

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

src/source/mix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ where
8181
{
8282
#[inline]
8383
fn parameters_changed(&self) -> bool {
84-
self.input1.parameters_changed() || self.input2.parameters_changed()
84+
false
8585
}
8686

8787
#[inline]

src/source/take_span.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::Source;
77
/// A source that truncates the given source to a certain duration.
88
#[derive(Clone, Debug)]
99
pub struct TakeSpan<I> {
10+
first: bool,
1011
input: I,
1112
}
1213

@@ -15,7 +16,7 @@ where
1516
I: Source,
1617
{
1718
pub fn new(input: I) -> Self {
18-
Self { input }
19+
Self { first: true, input }
1920
}
2021

2122
/// Returns a mutable reference to the inner source.
@@ -31,16 +32,14 @@ where
3132
}
3233
}
3334

34-
impl<I> Iterator for TakeSpan<I>
35-
where
36-
I: Source,
37-
{
35+
impl<I: Source> Iterator for TakeSpan<I> {
3836
type Item = <I as Iterator>::Item;
3937

4038
fn next(&mut self) -> Option<<I as Iterator>::Item> {
41-
if self.input.parameters_changed() {
39+
if self.input.parameters_changed() && !self.first {
4240
None
4341
} else {
42+
self.first = false;
4443
let sample = self.input.next()?;
4544
Some(sample)
4645
}

tests/take_span.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use rodio::Source;
2+
use test_support::{TestSource, TestSpan};
3+
4+
mod test_support;
5+
6+
#[test]
7+
fn param_changes_during_skip() {
8+
let source = TestSource::new()
9+
.with_span(
10+
TestSpan::sample_indexes()
11+
.with_sample_rate(10)
12+
.with_channel_count(1)
13+
.with_sample_count(10),
14+
)
15+
.with_span(
16+
TestSpan::sample_indexes()
17+
.with_sample_rate(20)
18+
.with_channel_count(2)
19+
.with_sample_count(10),
20+
);
21+
22+
let mut span_1 = source.take_span();
23+
assert_eq!(span_1.by_ref().take(9).count(), 9);
24+
assert_eq!(span_1.channels(), 1);
25+
assert_eq!(span_1.sample_rate(), 10);
26+
assert!(span_1.by_ref().next().is_some());
27+
assert_eq!(span_1.by_ref().next(), None);
28+
29+
let mut span_2 = span_1.into_inner().take_span();
30+
assert_eq!(span_2.by_ref().take(9).count(), 9);
31+
assert_eq!(span_2.channels(), 2);
32+
assert_eq!(span_2.sample_rate(), 20);
33+
assert!(span_2.by_ref().next().is_some());
34+
assert_eq!(span_2.by_ref().next(), None);
35+
}

tests/test_support/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ pub enum SampleSource {
1717
},
1818
Silence,
1919
List(Vec<f32>),
20+
SampleIndex,
2021
}
2122

2223
impl SampleSource {
2324
fn get(
2425
&mut self,
2526
pos: usize,
27+
pos_in_source: usize,
2628
sample_rate: SampleRate,
2729
channels: ChannelCount,
2830
numb_samples: usize,
@@ -39,6 +41,8 @@ impl SampleSource {
3941
.collect();
4042
samples.get(pos).copied()
4143
}
44+
SampleSource::SampleIndex if pos < numb_samples => Some(pos_in_source as f32),
45+
SampleSource::SampleIndex => None,
4246
SampleSource::SignalGen { samples, .. } => samples.get(pos).copied(),
4347
SampleSource::Silence { .. } if pos < numb_samples => Some(0.0),
4448
SampleSource::Silence { .. } => None,
@@ -70,6 +74,13 @@ impl TestSpan {
7074
channels: 1,
7175
}
7276
}
77+
pub fn sample_indexes() -> TestSpanBuilder {
78+
TestSpanBuilder {
79+
sample_source: SampleSource::SampleIndex,
80+
sample_rate: 1,
81+
channels: 1,
82+
}
83+
}
7384
pub fn sine(frequency: f32) -> TestSpanBuilder {
7485
TestSpanBuilder {
7586
sample_source: SampleSource::SignalGen {
@@ -101,9 +112,14 @@ impl TestSpan {
101112
}
102113
}
103114

104-
fn get(&mut self, pos: usize) -> Option<Sample> {
105-
self.sample_source
106-
.get(pos, self.sample_rate, self.channels, self.numb_samples)
115+
fn get(&mut self, pos: usize, pos_in_source: usize) -> Option<Sample> {
116+
self.sample_source.get(
117+
pos,
118+
pos_in_source,
119+
self.sample_rate,
120+
self.channels,
121+
self.numb_samples,
122+
)
107123
}
108124

109125
pub fn len(&self) -> usize {
@@ -211,6 +227,7 @@ pub struct TestSource {
211227
pub spans: Vec<TestSpan>,
212228
current_span: usize,
213229
pos_in_span: usize,
230+
pos_in_source: usize,
214231
parameters_changed: bool,
215232
}
216233

@@ -221,6 +238,7 @@ impl TestSource {
221238
current_span: 0,
222239
pos_in_span: 0,
223240
parameters_changed: false,
241+
pos_in_source: 0,
224242
}
225243
}
226244
pub fn with_span(mut self, span: TestSpan) -> Self {
@@ -234,7 +252,8 @@ impl Iterator for TestSource {
234252

235253
fn next(&mut self) -> Option<Self::Item> {
236254
let current_span = self.spans.get_mut(self.current_span)?;
237-
let sample = current_span.get(self.pos_in_span)?;
255+
let sample = current_span.get(self.pos_in_span, self.pos_in_source)?;
256+
self.pos_in_source += 1;
238257
self.pos_in_span += 1;
239258

240259
// if span is out of samples

0 commit comments

Comments
 (0)