summaryrefslogtreecommitdiff
path: root/src/audio.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2016-10-15 18:33:30 +0200
committerJustin Worthe <justin.worthe@gmail.com>2016-10-15 18:33:30 +0200
commit605095f3522bb2111de8dd3b9b2b9474549d9572 (patch)
tree5fb7578db3558a9de2629175a9d983870d6ac52a /src/audio.rs
parente6cbba66fed669c9c6808584a4f1551cfdb5de30 (diff)
Added destructor to close channels
Diffstat (limited to 'src/audio.rs')
-rw-r--r--src/audio.rs25
1 files 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<Vec<f64>>,
- //pa: &'a portaudio::PortAudio,
stream: pa::Stream<'a, pa::NonBlocking, pa::Input<f32>>
}
+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<OpenRecordingChannel<'a>, 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<O
let mut stream = try!(pa.open_non_blocking_stream(stream_settings, callback));
try!(stream.start());
-
- //How do I call this? try!(stream.stop());
-
Ok(OpenRecordingChannel {
receiver: receiver,
-// pa: &pa,
stream: stream
})
}
+#[test]
+fn start_listening_returns_successfully() {
+ let pa = init().expect("Could not init portaudio");
+ let devices = get_device_list(&pa).expect("Getting devices had an error");
+ let device = devices.first().expect("Should have at least one device");
+ let recording_channel = start_listening(&pa, device.0).expect("Error starting listening to first channel");
+}
+
/*
let mut samples_index = 0;