diff --git a/Cargo.lock b/Cargo.lock index 178d09be14..2ae5c462ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -934,6 +934,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "voder" version = "0.0.0" dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cpal 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "hound 3.4.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c6fcc65dd5..b18c0ad658 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ authors = [ "Brandon Thomas ", name = "voder" [dependencies] +byteorder = "1.3.2" # byte-level operations cpal = "0.10.0" # hardware audio out failure = "0.1.5" # required by cpal hound = "3.4.0" # wav encoder/decoder diff --git a/src/ipc.rs b/src/ipc.rs index 4c20a3541f..f911050202 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -4,7 +4,7 @@ use std::collections::VecDeque; use std::sync::{RwLock, PoisonError}; pub struct AudioQueue { - queue: RwLock>, + queue: RwLock>, } impl AudioQueue { @@ -14,7 +14,7 @@ impl AudioQueue { } } - pub fn push_back(&self, val: i16) { + pub fn push_back(&self, val: f32) { match self.queue.write() { Ok(mut queue) => { queue.push_back(val); @@ -25,7 +25,7 @@ impl AudioQueue { } } - pub fn drain(&self) -> Vec { + pub fn drain(&self) -> Vec { match self.queue.write() { Ok(mut queue) => { return queue.drain(..) @@ -37,7 +37,7 @@ impl AudioQueue { } } - pub fn drain_size(&self, size: usize) -> Option> { + pub fn drain_size(&self, size: usize) -> Option> { match self.queue.write() { Ok(mut queue) => { if queue.len() < size { diff --git a/src/main.rs b/src/main.rs index 7c039236d6..dc2ae711b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ //! //! This example records audio and plays it back in real time as it's being recorded. +extern crate byteorder; extern crate cpal; extern crate failure; extern crate tensorflow; @@ -15,6 +16,7 @@ pub mod synthesis; use wavy::*; +use byteorder::{ByteOrder, BigEndian, LittleEndian}; use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; use failure::_core::time::Duration; use ipc::{QueueSender, AudioQueue}; @@ -33,7 +35,7 @@ fn main() { print_version(); //load_model(); // TODO: This works. Temporarily commented out run_cpal_audio().expect("Should work"); - run_audio().expect("should work"); + //run_audio().expect("should work"); //run_audio().expect("should work"); } @@ -66,6 +68,79 @@ fn run_cpal_audio() -> Result<(), failure::Error> { // The channel to share samples. let (tx, rx) = std::sync::mpsc::sync_channel(latency_samples * 2); + + let mut audio_queue = Arc::new(AudioQueue::new()); + let mut audio_queue_2 = audio_queue.clone(); + + thread::spawn(move || { + let mut context = zmq::Context::new(); + let mut socket = context.socket(zmq::REQ).unwrap(); + + socket.connect("tcp://127.0.0.1:5555").unwrap(); + + let mut reconnect = false; + let mut fail_count = 0; + + loop { + let mut drained = match audio_queue_2.drain_size(10000) { + None => { continue; }, + Some(d) => d, + }; + + println!("Len drained: {}", drained.len()); + let mut bytes: Vec = Vec::with_capacity(drained.len()*2); + + for val in drained { + /*let byte_pair = val.to_be_bytes(); + bytes.push(byte_pair[0]); + bytes.push(byte_pair[1]); + bytes.push(byte_pair[3]); + bytes.push(byte_pair[4]);*/ + let mut buf = [0; 4]; + LittleEndian::write_f32(&mut buf, val); + bytes.push(buf[0]); + bytes.push(buf[1]); + bytes.push(buf[2]); + bytes.push(buf[3]); + } + + loop { + if reconnect { + reconnect = false; + + thread::sleep(Duration::from_millis(200)); + + socket = match context.socket(zmq::REQ) { + Ok(s) => s, + Err(e) => { + println!("Error creating socket: {:?}", e); + continue + }, + }; + + match socket.connect("tcp://127.0.0.1:5555") { + Ok(_) => {}, + Err(err) => { + println!("Err B: {:?}", err); + }, + } + } + + match socket.send(&bytes, 0) { + Ok(_) => { break; }, + Err(e) => { + println!("send err: {:?}", e); + fail_count += 1; + if fail_count > 5 { + fail_count = 0; + reconnect = true; + } + }, + } + } + } + }); + event_loop.run(move |id, result| { let data = match result { Ok(data) => data, @@ -80,6 +155,7 @@ fn run_cpal_audio() -> Result<(), failure::Error> { //assert_eq!(id, input_stream_id); let mut output_fell_behind = false; for &sample in buffer.iter() { + audio_queue.push_back(sample); if tx.try_send(sample).is_err() { output_fell_behind = true; } @@ -109,7 +185,7 @@ fn run_cpal_audio() -> Result<(), failure::Error> { }); } -fn run_audio() -> Result<(), AudioError> { +/*fn run_audio() -> Result<(), AudioError> { println!("Opening microphone system"); let mut mic = MicrophoneSystem::new(SampleRate::Normal)?; @@ -198,5 +274,5 @@ fn run_audio() -> Result<(), AudioError> { } });*/ } -} +}*/