Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `DEFAULT_SAMPLE_RATE` set to match `cpal::SAMPLE_RATE_48K`.
- Added `Source::resample` and `ResampleConfig` for high-quality sample rate conversion.
- Presets for `AGC`.
- Added `SourceQueueInput::append_with_signal_custom` for custom signal handler.

### Changed

Expand Down
32 changes: 28 additions & 4 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn queue(keep_alive_if_empty: bool) -> (Arc<SourcesQueueInput>, SourcesQueue
// TODO: consider reimplementing this with `from_factory`

type Sound = Box<dyn Source + Send>;
type SignalDone = Option<Sender<()>>;
type SignalDone = Option<(Sender<()>, Option<Box<dyn FnOnce() + Send>>)>;

/// The input of the queue.
pub struct SourcesQueueInput {
Expand Down Expand Up @@ -83,7 +83,27 @@ impl SourcesQueueInput {
self.next_sounds
.lock()
.unwrap()
.push_back((Box::new(source) as Box<_>, Some(tx)));
.push_back((Box::new(source) as Box<_>, Some((tx, None))));
rx
}

/// Adds a new source to the end of the queue.
///
/// The `FnOnce` will be called when the sound has finished playing.
#[inline]
pub fn append_with_signal_custom<T>(
&self,
source: T,
signal: Box<dyn FnOnce() + Send>,
) -> Receiver<()>
where
T: Source + Send + 'static,
{
let (tx, rx) = channel();
self.next_sounds
.lock()
.unwrap()
.push_back((Box::new(source) as Box<_>, Some((tx, Some(signal)))));
rx
}

Expand Down Expand Up @@ -115,7 +135,7 @@ pub struct SourcesQueueOutput {
current: Box<dyn Source + Send>,

// Signal this sender before picking from `next`.
signal_after_end: Option<Sender<()>>,
signal_after_end: SignalDone,

// The next sounds.
input: Arc<SourcesQueueInput>,
Expand Down Expand Up @@ -258,8 +278,12 @@ impl SourcesQueueOutput {
//
// This method is separate so that it is not inlined.
fn go_next(&mut self) -> Result<(), ()> {
if let Some(signal_after_end) = self.signal_after_end.take() {
if let Some((signal_after_end, custom_signal)) = self.signal_after_end.take() {
let _ = signal_after_end.send(());

if let Some(custom_signal) = custom_signal {
custom_signal()
}
}

let (next, signal_after_end) = {
Expand Down
Loading