From 629d2fa754dcd4bbdbd1e84ea9f7598806abc840 Mon Sep 17 00:00:00 2001 From: Graham Northup Date: Wed, 27 Sep 2017 00:50:06 -0400 Subject: It makes networked noise! (Poorly, for now) --- src/main.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index fc15e53..e06d2b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,29 @@ use std::io; use std::io::*; +use std::net::*; extern crate synfone; use synfone::synth::*; use synfone::lang::*; +use synfone::client::*; -const FRAMES: usize = 44100 * 2; - -const GEN: &'static str = "add(mul(sine(param('freq', 440)), 0.5), mul(sine(param('freq2', 660)), 0.5))"; +const GEN: &'static str = "mul(sine(param('v_freq', 500)), ifelse(rel(param('v_frame'), '<', param('v_deadline')), param('v_amp'), 0.0))"; fn main() { - let mut params = Parameters::default(); - - let mut gen = Parser::new(Tokenizer::new(GEN.chars())).expect("Failed to get first token").parse().expect("Failed to compile generator"); + let env = Environment::default(); + + let gen = Parser::new(Tokenizer::new(GEN.chars())).expect("Failed to get first token").parse().expect("Failed to compile generator"); + let sock = UdpSocket::bind("0.0.0.0:13676").expect("Failed to bind socket"); - let mut counter = 0; + let mut client = Client::new(sock, vec![gen], env).expect("Failed to create client"); + let mut buf: Vec = Vec::new(); let mut out = io::stdout(); - let mut outbuf: Vec = Vec::new(); - - params.vars.insert("freq".to_string(), 440.0); - params.vars.insert("freq2".to_string(), 660.0); - - while counter < FRAMES { - *params.vars.get_mut("freq").unwrap() = 440.0 + 440.0 * ((counter as f32) / (FRAMES as f32)); - *params.vars.get_mut("freq2").unwrap() = 660.0 + 220.0 * ((counter as f32) / (FRAMES as f32)); - let buf = gen.eval(¶ms); - let curlen = outbuf.len(); - outbuf.reserve_exact(buf.size() - curlen); - unsafe { outbuf.set_len(buf.size()); } - buf.bytes(&mut outbuf); - out.write_all(&outbuf).expect("failed to write to stdout"); - counter += buf.len(); + + eprintln!("Starting."); + + while client.pump(&mut buf) { + out.write_all(&buf).expect("Failed to write samples"); } + + eprintln!("Exiting."); } -- cgit v1.2.3-70-g09d2