Make Driver type responsible for managing user callbacks

This commit is contained in:
mitchmindtree 2019-06-29 05:47:58 +10:00
parent 4dafb212fb
commit 191b90909a
2 changed files with 21 additions and 11 deletions

View File

@ -579,6 +579,17 @@ impl Driver {
Ok(()) Ok(())
} }
/// Adds a callback to the list of active callbacks.
///
/// The given function receives the index of the buffer currently ready for processing.
pub fn set_callback<F>(&self, callback: F)
where
F: 'static + FnMut(i32) + Send,
{
let mut bc = BUFFER_CALLBACK.lock().unwrap();
bc.push(Some(BufferCallback(Box::new(callback))));
}
/// Consumes and destroys the `Driver`, stopping the streams if they are running and releasing /// Consumes and destroys the `Driver`, stopping the streams if they are running and releasing
/// any associated resources. /// any associated resources.
pub fn destroy(mut self) -> Result<(), AsioError> { pub fn destroy(mut self) -> Result<(), AsioError> {
@ -597,7 +608,15 @@ impl Driver {
asio_result!(ai::ASIOExit())?; asio_result!(ai::ASIOExit())?;
ai::remove_current_driver(); ai::remove_current_driver();
} }
// Clear any existing stream callbacks.
if let Ok(mut bcs) = BUFFER_CALLBACK.lock() {
bcs.clear();
}
// Indicate to the
self.loaded.store(false, atomic::Ordering::SeqCst); self.loaded.store(false, atomic::Ordering::SeqCst);
Ok(()) Ok(())
} }
} }
@ -614,15 +633,6 @@ impl Drop for Driver {
unsafe impl Send for AsioStream {} unsafe impl Send for AsioStream {}
/// Adds a callback to the list of active callbacks
pub fn set_callback<F: 'static>(callback: F) -> ()
where
F: FnMut(i32) + Send,
{
let mut bc = BUFFER_CALLBACK.lock().unwrap();
bc.push(Some(BufferCallback(Box::new(callback))));
}
/// Idicates the sample rate has changed /// Idicates the sample rate has changed
/// TODO Change the sample rate when this /// TODO Change the sample rate when this
/// is called. /// is called.

View File

@ -225,7 +225,7 @@ impl EventLoop {
// Set the input callback. // Set the input callback.
// This is most performance critical part of the ASIO bindings. // This is most performance critical part of the ASIO bindings.
sys::set_callback(move |buffer_index| unsafe { driver.set_callback(move |buffer_index| unsafe {
// If not playing return early. // If not playing return early.
// TODO: Don't assume `count` is valid - we should search for the matching `StreamId`. // TODO: Don't assume `count` is valid - we should search for the matching `StreamId`.
if let Some(s) = cpal_streams.lock().unwrap().get(count) { if let Some(s) = cpal_streams.lock().unwrap().get(count) {
@ -400,7 +400,7 @@ impl EventLoop {
let mut interleaved = vec![0u8; len_bytes]; let mut interleaved = vec![0u8; len_bytes];
let mut silence_asio_buffer = SilenceAsioBuffer::default(); let mut silence_asio_buffer = SilenceAsioBuffer::default();
sys::set_callback(move |buffer_index| unsafe { driver.set_callback(move |buffer_index| unsafe {
// If not playing, return early. // If not playing, return early.
// TODO: Don't assume `count` is valid - we should search for the matching `StreamId`. // TODO: Don't assume `count` is valid - we should search for the matching `StreamId`.
if let Some(s) = cpal_streams.lock().unwrap().get(count) { if let Some(s) = cpal_streams.lock().unwrap().get(count) {