diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f149de..cac92da6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/queue.rs b/src/queue.rs index 840e4f50..d0969a24 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -45,7 +45,7 @@ pub fn queue(keep_alive_if_empty: bool) -> (Arc, SourcesQueue // TODO: consider reimplementing this with `from_factory` type Sound = Box; -type SignalDone = Option>; +type SignalDone = Option<(Sender<()>, Option>)>; /// The input of the queue. pub struct SourcesQueueInput { @@ -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( + &self, + source: T, + signal: Box, + ) -> 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 } @@ -115,7 +135,7 @@ pub struct SourcesQueueOutput { current: Box, // Signal this sender before picking from `next`. - signal_after_end: Option>, + signal_after_end: SignalDone, // The next sounds. input: Arc, @@ -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) = {