mirror of
https://github.com/RustAudio/rodio
synced 2025-03-04 23:17:22 +00:00
Duration more or less works again
This commit is contained in:
parent
efc00ff834
commit
e2fc982652
2 changed files with 20 additions and 14 deletions
|
@ -140,9 +140,19 @@ pub struct Handle<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Handle<'a> {
|
impl<'a> Handle<'a> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn append<S>(&self, source: S) where S: Source + Send + 'static, S::Item: Sample + Clone + Send {
|
pub fn append<S>(&self, source: S) where S: Source + Send + 'static, S::Item: Sample + Clone + Send {
|
||||||
|
if let Some(duration) = source.get_total_duration() {
|
||||||
|
let duration = duration.as_secs() as usize * 1000 +
|
||||||
|
duration.subsec_nanos() as usize / 1000000;
|
||||||
|
self.remaining_duration_ms.fetch_add(duration, Ordering::Relaxed);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
let duration = source.size_hint().0 * 1000 / (source.get_samples_rate() as usize *
|
||||||
|
source.get_channels() as usize);
|
||||||
|
self.remaining_duration_ms.fetch_add(duration, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
let source = UniformSourceIterator::new(source, self.channels, self.samples_rate);
|
let source = UniformSourceIterator::new(source, self.channels, self.samples_rate);
|
||||||
let source = Box::new(source);
|
let source = Box::new(source);
|
||||||
self.next_sounds.lock().unwrap().push(source);
|
self.next_sounds.lock().unwrap().push(source);
|
||||||
|
@ -164,11 +174,6 @@ impl<'a> Handle<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn get_total_duration_ms(&self) -> u32 {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_remaining_duration_ms(&self) -> u32 {
|
pub fn get_remaining_duration_ms(&self) -> u32 {
|
||||||
self.remaining_duration_ms.load(Ordering::Relaxed) as u32
|
self.remaining_duration_ms.load(Ordering::Relaxed) as u32
|
||||||
|
@ -276,6 +281,7 @@ fn background(rx: Receiver<Command>) {
|
||||||
// updating the contents of `remaining_duration_ms`
|
// updating the contents of `remaining_duration_ms`
|
||||||
for &(ref decoder, ref remaining_duration_ms, _) in sounds.iter() {
|
for &(ref decoder, ref remaining_duration_ms, _) in sounds.iter() {
|
||||||
let (num_samples, _) = decoder.size_hint();
|
let (num_samples, _) = decoder.size_hint();
|
||||||
|
// TODO: differenciate sounds from this sink from sounds from other sinks
|
||||||
let num_samples = num_samples + voice.get_pending_samples();
|
let num_samples = num_samples + voice.get_pending_samples();
|
||||||
let value = (num_samples as u64 * 1000 / (voice.get_channels() as u64 *
|
let value = (num_samples as u64 * 1000 / (voice.get_channels() as u64 *
|
||||||
voice.get_samples_rate().0 as u64)) as u32;
|
voice.get_samples_rate().0 as u64)) as u32;
|
||||||
|
@ -319,6 +325,9 @@ impl Iterator for QueueIterator {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
(self.current.size_hint().0, None)
|
// TODO: slow? benchmark this
|
||||||
|
let next_hints = self.next.lock().unwrap().iter()
|
||||||
|
.map(|i| i.size_hint().0).fold(0, |a, b| a + b);
|
||||||
|
(self.current.size_hint().0 + next_hints, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/lib.rs
11
src/lib.rs
|
@ -58,13 +58,9 @@ impl Sink {
|
||||||
self.0.stop()
|
self.0.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of milliseconds in total in the sound file.
|
/// Returns the minimum number of milliseconds remaining before the end of the sound.
|
||||||
#[inline]
|
///
|
||||||
pub fn get_total_duration_ms(&self) -> u32 {
|
/// Note that this is a minimum value, and the sound can last longer.
|
||||||
self.0.get_total_duration_ms()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the number of milliseconds remaining before the end of the sound.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_remaining_duration_ms(&self) -> u32 {
|
pub fn get_remaining_duration_ms(&self) -> u32 {
|
||||||
self.0.get_remaining_duration_ms()
|
self.0.get_remaining_duration_ms()
|
||||||
|
@ -73,6 +69,7 @@ impl Sink {
|
||||||
/// Sleeps the current thread until the sound ends.
|
/// Sleeps the current thread until the sound ends.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn sleep_until_end(&self) {
|
pub fn sleep_until_end(&self) {
|
||||||
|
// TODO: sleep repeatidely until the sound is finished (see the docs of `get_remaining_duration`)
|
||||||
thread::sleep_ms(self.get_remaining_duration_ms());
|
thread::sleep_ms(self.get_remaining_duration_ms());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue