Skip to content
Draft
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
19 changes: 18 additions & 1 deletion src/mixer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Mixer that plays multiple sounds at the same time.

use crate::common::{ChannelCount, SampleRate};
use crate::source::{SeekError, Source, UniformSourceIterator};
use crate::source::{Empty, SeekError, Source, UniformSourceIterator};
use crate::Sample;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -67,6 +67,23 @@ impl Mixer {
// Ignore send errors (channel dropped means MixerSource was dropped)
let _ = self.0.pending_tx.send(Box::new(uniform_source));
}

/// The channel count sources are converted to when added to this mixer.
pub fn channels(&self) -> ChannelCount {
self.0.channels
}

/// The sample rate sources are converted to when added to this mixer.
pub fn sample_rate(&self) -> SampleRate {
self.0.sample_rate
}

/// A source in this mixer's format that never produces a sample. Useful as a
/// [`queue`](crate::queue::queue) keep-alive source, so idling sounds like this one won't
/// need source conversion set up until real content is appended.
pub fn silence(&self) -> Empty {
Empty::new_with_format(self.0.channels, self.0.sample_rate)
}
}

/// The output of the mixer. Implements `Source`.
Expand Down
15 changes: 12 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dasp_sample::FromSample;
use std::sync::mpsc::{Receiver, Sender};

use crate::mixer::Mixer;
use crate::source::SeekError;
use crate::source::{Empty, SeekError};
use crate::Float;
use crate::{queue, source::Done, Source};

Expand Down Expand Up @@ -71,15 +71,24 @@ impl Player {
/// Builds a new `Player`, beginning playback on a stream.
#[inline]
pub fn connect_new(mixer: &Mixer) -> Player {
let (sink, source) = Player::new();
let (sink, source) = Player::new_with_keep_alive(mixer.silence());
mixer.add(source);
sink
}

/// Builds a new `Player`.
#[inline]
pub fn new() -> (Player, queue::SourcesQueueOutput) {
let (queue_tx, queue_rx) = queue::queue(true);
Player::new_with_keep_alive(Empty::new())
}

/// Builds a new `Player` that plays `keep_alive_with` (looping to silence in its format
/// once exhausted) instead of ending when it becomes empty. See [`queue::queue`].
pub fn new_with_keep_alive<S>(keep_alive_with: S) -> (Player, queue::SourcesQueueOutput)
where
S: Source + Send + 'static,
{
let (queue_tx, queue_rx) = queue::queue(Some(Box::new(keep_alive_with)));

let sink = Player {
queue_tx,
Expand Down
Loading
Loading