Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Elapsed filter #273

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/elapsed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate rodio;

use rodio::Source;
use std::io::BufReader;
use std::time::{Duration};
use std::sync::{Mutex, Arc};
use std::thread;

fn main() {
let device = rodio::default_output_device().unwrap();
let sink = rodio::Sink::new(&device);

let file = std::fs::File::open("examples/music.ogg").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();

let timer = Arc::new(Mutex::new(Duration::from_secs(0)));
let with_elapsed = source.buffered().elapsed(Arc::clone(&timer));
sink.append(with_elapsed);

while !sink.empty() {
let val = *timer.lock().unwrap();

println!("Music has played for {}.{} seconds", val.as_secs(), val.subsec_millis());
thread::sleep(Duration::from_secs(1));
}
}
76 changes: 76 additions & 0 deletions src/source/elapsed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::time::Duration;
use std::sync::{Mutex, Arc};

use Sample;
use Source;

/// Internal function that builds a `Elapsed` object.
pub fn elapsed<I>(input: I, duration: Arc<Mutex<Duration>>) -> Elapsed<I>
where
I: Source,
I::Item: Sample,
{
Elapsed {
input: input,
duration: duration,
}
}

/// Filter that updates a `Duration` with the current elapsed time.
#[derive(Clone, Debug)]
pub struct Elapsed<I> {
input: I,
duration: Arc<Mutex<Duration>>,
}

impl<I> Iterator for Elapsed<I>
where
I: Source,
I::Item: Sample,
{
type Item = I::Item;

#[inline]
fn next(&mut self) -> Option<I::Item> {
let mut duration = self.duration.lock().unwrap();

// Calculate sample_time in nanoseconds
let sample_time = (1_000_000_000 / self.sample_rate()) / self.channels() as u32;

let time_elapsed = Duration::from_nanos(sample_time.into());
*duration += time_elapsed;

self.input.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.input.size_hint()
}
}

impl<I> Source for Elapsed<I>
where
I: Source,
I::Item: Sample,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
self.input.current_frame_len()
}

#[inline]
fn channels(&self) -> u16 {
self.input.channels()
}

#[inline]
fn sample_rate(&self) -> u32 {
self.input.sample_rate()
}

#[inline]
fn total_duration(&self) -> Option<Duration> {
self.input.total_duration()
}
}
21 changes: 21 additions & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Sources of sound and various filters.

use std::time::Duration;
use std::sync::{Arc, Mutex};

use Sample;

Expand All @@ -13,6 +14,7 @@ pub use self::delay::Delay;
pub use self::done::Done;
pub use self::empty::Empty;
pub use self::fadein::FadeIn;
pub use self::elapsed::Elapsed;
pub use self::from_factory::{from_factory, FromFactoryIter};
pub use self::from_iter::{from_iter, FromIter};
pub use self::mix::Mix;
Expand All @@ -37,6 +39,7 @@ mod delay;
mod done;
mod empty;
mod fadein;
mod elapsed;
mod from_factory;
mod from_iter;
mod mix;
Expand Down Expand Up @@ -225,6 +228,24 @@ where
fadein::fadein(self, duration)
}

/// Updates the supplied `Duration` with the total elapsed time for the source
///
/// # Example
///
/// ```ignore
/// use std::time::Duration;
///
/// let duration = Arc::new(Mutex::new(Duration::from_secs(0)));
/// let source = source.buffered().elapsed(Arc::clone(&duration));
/// ```
#[inline]
fn elapsed(self, duration: Arc<Mutex<Duration>>) -> Elapsed<Self>
where
Self: Sized,
{
elapsed::elapsed(self, duration)
}

/// Calls the `access` closure on `Self` the first time the source is iterated and every
/// time `period` elapses.
///
Expand Down