From 8cfe176e5221a79ffeb1350340df84ca0f70c199 Mon Sep 17 00:00:00 2001 From: shockham Date: Fri, 7 Apr 2017 11:06:00 +0100 Subject: [PATCH 1/2] Fix for loop in EventLoop::run being optimised out in a release build on macOS --- src/coreaudio/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/coreaudio/mod.rs b/src/coreaudio/mod.rs index d60d3e0..2e0e823 100644 --- a/src/coreaudio/mod.rs +++ b/src/coreaudio/mod.rs @@ -16,6 +16,8 @@ use futures::task::Task; use futures::task; use futures::stream::Stream; use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Duration; use self::coreaudio::audio_unit::AudioUnit; use self::coreaudio::audio_unit::render_callback::{self, data}; @@ -50,7 +52,12 @@ impl EventLoop { #[inline] pub fn new() -> EventLoop { EventLoop } #[inline] - pub fn run(&self) { loop {} } + pub fn run(&self) { + loop { + // So the loop does not get optimised out in --release + thread::sleep(Duration::new(1u64, 0u32)); + } + } } pub struct Buffer { @@ -84,9 +91,6 @@ impl Buffer where T: Sample { } } -type NumChannels = usize; -type NumFrames = usize; - pub struct Voice { playing: bool, audio_unit: AudioUnit From 97657fae4a99c3ca025d96ca98881ddeb19ef562 Mon Sep 17 00:00:00 2001 From: shockham Date: Wed, 19 Apr 2017 11:44:42 +0100 Subject: [PATCH 2/2] SampleStream also holds on to the AudioUnit so it is not dropped --- src/coreaudio/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/coreaudio/mod.rs b/src/coreaudio/mod.rs index 2e0e823..6f4cba9 100644 --- a/src/coreaudio/mod.rs +++ b/src/coreaudio/mod.rs @@ -93,12 +93,13 @@ impl Buffer where T: Sample { pub struct Voice { playing: bool, - audio_unit: AudioUnit + audio_unit: Arc>, } #[allow(dead_code)] // the audio_unit will be dropped if we don't hold it. pub struct SamplesStream { inner: Arc>, + audio_unit: Arc>, } @@ -196,20 +197,24 @@ impl Voice { try!(audio_unit.start().map_err(convert_error)); + let au_arc = Arc::new(Mutex::new(audio_unit)); + let samples_stream = SamplesStream { inner: inner, + audio_unit: au_arc.clone(), }; Ok((Voice { playing: true, - audio_unit: audio_unit + audio_unit: au_arc.clone(), }, samples_stream)) } #[inline] pub fn play(&mut self) { if !self.playing { - self.audio_unit.start().unwrap(); + let mut unit = self.audio_unit.lock().unwrap(); + unit.start().unwrap(); self.playing = true; } } @@ -217,7 +222,8 @@ impl Voice { #[inline] pub fn pause(&mut self) { if self.playing { - self.audio_unit.stop().unwrap(); + let mut unit = self.audio_unit.lock().unwrap(); + unit.stop().unwrap(); self.playing = false; } }