From 605095f3522bb2111de8dd3b9b2b9474549d9572 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 15 Oct 2016 18:33:30 +0200 Subject: Added destructor to close channels --- src/audio.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index cdeb88a..df4a933 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -2,6 +2,8 @@ extern crate portaudio; use portaudio as pa; use std::sync::mpsc::*; +use std::io; +use std::io::Write; const SAMPLE_RATE: f64 = 44100.0; const FRAMES: usize = 512; @@ -37,10 +39,19 @@ fn get_device_list_returns_devices() { pub struct OpenRecordingChannel<'a> { receiver: Receiver>, - //pa: &'a portaudio::PortAudio, stream: pa::Stream<'a, pa::NonBlocking, pa::Input> } +impl<'a> Drop for OpenRecordingChannel<'a> { + fn drop(&mut self) { + //if stream doesn't close cleanly, don't end the world. It's probably fine. + match self.stream.stop() { + Result::Err(err) => {writeln!(io::stderr(), "Failed to close audio stream. {}", err).ok();}, + _ => {} + } + } +} + pub fn start_listening<'a>(pa: &'a pa::PortAudio, device_index: u32) -> Result, pa::Error> { //let pa = try!(pa::PortAudio::new()); let device_info = try!(pa.device_info(pa::DeviceIndex(device_index))); @@ -68,16 +79,20 @@ pub fn start_listening<'a>(pa: &'a pa::PortAudio, device_index: u32) -> Result