#![cfg_attr(test, deny(missing_docs))]
#![cfg_attr(test, deny(warnings))]
extern crate cpal;
extern crate hound;
#[macro_use]
extern crate lazy_static;
extern crate time;
extern crate vorbis;
pub use cpal::{Endpoint, get_endpoints_list, get_default_endpoint};
pub use conversions::Sample;
pub use decoder::Decoder;
pub use source::Source;
use std::io::{Read, Seek};
use std::time::Duration;
use std::thread;
mod conversions;
mod engine;
pub mod decoder;
pub mod source;
lazy_static! {
static ref ENGINE: engine::Engine = engine::Engine::new();
}
pub struct Sink {
handle: engine::Handle<'static>,
stop: bool,
}
impl Sink {
#[inline]
pub fn new(endpoint: &Endpoint) -> Sink {
Sink {
handle: ENGINE.start(&endpoint),
stop: true,
}
}
#[inline]
pub fn append<S>(&self, source: S) where S: Source + Send + 'static,
S::Item: Sample, S::Item: Send
{
self.handle.append(source);
}
#[inline]
pub fn set_volume(&mut self, value: f32) {
self.handle.set_volume(value);
}
#[inline]
pub fn detach(mut self) {
self.stop = false;
}
#[inline]
pub fn get_min_remaining_duration(&self) -> Duration {
self.handle.get_min_remaining_duration()
}
#[inline]
pub fn sleep_until_end(&self) {
thread::sleep(self.get_min_remaining_duration());
}
}
impl Drop for Sink {
#[inline]
fn drop(&mut self) {
if self.stop {
self.handle.stop();
}
}
}
#[inline]
pub fn play_once<R>(endpoint: &Endpoint, input: R) -> Sink
where R: Read + Seek + Send + 'static
{
let input = decoder::Decoder::new(input);
let sink = Sink::new(endpoint);
sink.append(input);
sink
}