Skip to content

Commit 6cfec32

Browse files
committed
Fixes frames getting out of order due to constant span_length
The `current_span_length` is in samples not frames. Rodio was using a fixed span_length for all sources regardless of their number of channels. If that span length was not wholly dividable by the number of channels it would create an offset. Credit for finding this goes to: @will3942
1 parent 89646c0 commit 6cfec32

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/queue.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub struct SourcesQueueOutput<S> {
117117
input: Arc<SourcesQueueInput<S>>,
118118
}
119119

120-
const THRESHOLD: usize = 512;
121120
impl<S> Source for SourcesQueueOutput<S>
122121
where
123122
S: Sample + Send + 'static,
@@ -143,7 +142,7 @@ where
143142
&& self.input.next_sounds.lock().unwrap().is_empty()
144143
{
145144
// The next source will be a filler silence which will have the length of `THRESHOLD`
146-
return Some(THRESHOLD);
145+
return Some(self.silent_span_length());
147146
}
148147
}
149148

@@ -156,7 +155,7 @@ where
156155
}
157156

158157
// Otherwise we use the constant value.
159-
Some(THRESHOLD)
158+
Some(self.silent_span_length())
160159
}
161160

162161
#[inline]
@@ -234,7 +233,10 @@ where
234233
let mut next = self.input.next_sounds.lock().unwrap();
235234

236235
if next.len() == 0 {
237-
let silence = Box::new(Zero::<S>::new_samples(1, 44100, THRESHOLD)) as Box<_>;
236+
// queue reports number of channels for the current source. Not the silence source.
237+
// `self.silent_span_length` accounts for this.
238+
let silence =
239+
Box::new(Zero::<S>::new_samples(1, 44100, self.silent_span_length())) as Box<_>;
238240
if self.input.keep_alive_if_empty.load(Ordering::Acquire) {
239241
// Play a short silence in order to avoid spinlocking.
240242
(silence, None)
@@ -250,6 +252,11 @@ where
250252
self.signal_after_end = signal_after_end;
251253
Ok(())
252254
}
255+
256+
/// 200 frames of silence
257+
fn silent_span_length(&self) -> usize {
258+
200 * self.channels() as usize
259+
}
253260
}
254261

255262
#[cfg(test)]

0 commit comments

Comments
 (0)