From d6e399973e9bc6447a28b80cacffcbc6a768f1ed Mon Sep 17 00:00:00 2001 From: thajohns Date: Thu, 9 Sep 2021 01:15:22 -0400 Subject: got pre-linking steps working, removed glob includes --- src/synth/adsr.rs | 25 +++--- src/synth/logic.rs | 50 +++++++----- src/synth/lut.rs | 34 ++++++-- src/synth/math.rs | 41 +++++++--- src/synth/mod.rs | 213 +++++++++++++++++++++++++++++++++++++------------- src/synth/noise.rs | 11 ++- src/synth/param.rs | 16 +++- src/synth/rel.rs | 39 +++++---- src/synth/saw.rs | 13 ++- src/synth/sine.rs | 13 ++- src/synth/square.rs | 13 ++- src/synth/triangle.rs | 13 ++- src/synth/util.rs | 32 ++++++-- 13 files changed, 368 insertions(+), 145 deletions(-) (limited to 'src/synth') diff --git a/src/synth/adsr.rs b/src/synth/adsr.rs index ca7be9c..e7dba5d 100644 --- a/src/synth/adsr.rs +++ b/src/synth/adsr.rs @@ -1,6 +1,9 @@ -use super::*; +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, Parameters, Rate, + SampleBuffer, +}; -#[derive(Debug,Clone,Copy,PartialEq,Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Phase { Delay, Attack, @@ -44,7 +47,7 @@ impl Generator for DAHDSR { self.attack_cd = delay; self.cur = 0.0; } - } else{ + } else { self.phase = Phase::Release; } @@ -55,7 +58,7 @@ impl Generator for DAHDSR { if self.attack_cd <= 0.0 { self.phase = Phase::Attack; } - }, + } Phase::Attack => { self.cur += attack; if self.cur >= 1.0 { @@ -63,36 +66,38 @@ impl Generator for DAHDSR { self.phase = Phase::Hold; self.decay_cd = hold; } - }, + } Phase::Hold => { self.decay_cd -= 1.0; if self.decay_cd <= 0.0 { self.phase = Phase::Decay; } - }, + } Phase::Decay => { self.cur -= decay; if self.cur <= sustain { self.cur = sustain; self.phase = Phase::Sustain; } - }, + } Phase::Sustain => { self.cur = sustain; - }, + } Phase::Release => { self.cur -= release; if self.cur < 0.0 { self.cur = 0.0; } - }, + } } *samp = self.cur; } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } diff --git a/src/synth/logic.rs b/src/synth/logic.rs index d5f9401..f6cb013 100644 --- a/src/synth/logic.rs +++ b/src/synth/logic.rs @@ -1,5 +1,8 @@ -use std::{mem, cmp}; -use super::*; +use super::{ + FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, Parameters, Rate, + SampleBuffer, +}; +use std::{cmp, mem}; #[derive(Debug)] pub struct IfElse { @@ -14,11 +17,10 @@ impl Generator for IfElse { let cond_buf = self.cond.eval(params); let iftrue_buf = self.iftrue.eval(params); let iffalse_buf = self.iffalse.eval(params); - - if - cond_buf.rate == Rate::Control && - iftrue_buf.rate == Rate::Control && - iffalse_buf.rate == Rate::Control + + if cond_buf.rate == Rate::Control + && iftrue_buf.rate == Rate::Control + && iffalse_buf.rate == Rate::Control { self.buf.set(if cond_buf.first() >= 0.5 { iftrue_buf.first() @@ -31,9 +33,15 @@ impl Generator for IfElse { self.buf.rate = Rate::Sample; let mut bound = self.buf.len(); - if cond_buf.rate == Rate::Sample { bound = cmp::min(bound, cond_buf.len()); } - if iftrue_buf.rate == Rate::Sample { bound = cmp::min(bound, iftrue_buf.len()); } - if iffalse_buf.rate == Rate::Sample { bound = cmp::min(bound, iffalse_buf.len()); } + if cond_buf.rate == Rate::Sample { + bound = cmp::min(bound, cond_buf.len()); + } + if iftrue_buf.rate == Rate::Sample { + bound = cmp::min(bound, iftrue_buf.len()); + } + if iffalse_buf.rate == Rate::Sample { + bound = cmp::min(bound, iffalse_buf.len()); + } for i in 0..bound { let tv = match iftrue_buf.rate { @@ -48,16 +56,14 @@ impl Generator for IfElse { Rate::Sample => cond_buf[i], Rate::Control => cond_buf.first(), }; - self.buf[i] = if cv >= 0.5 { - tv - } else { - fv - }; + self.buf[i] = if cv >= 0.5 { tv } else { fv }; } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -70,8 +76,16 @@ impl GeneratorFactory for IfElseFactory { let cond = params.remove_param("cond", 0)?.into_gen()?; let iftrue = params.remove_param("iftrue", 1)?.into_gen()?; let iffalse = params.remove_param("iffalse", 2)?.into_gen()?; - let buf = SampleBuffer::new(cmp::max(cmp::max(cond.buffer().len(), iftrue.buffer().len()), iffalse.buffer().len())); - Ok(Box::new(IfElse { cond: cond, iftrue: iftrue, iffalse: iffalse, buf: buf })) + let buf = SampleBuffer::new(cmp::max( + cmp::max(cond.buffer().len(), iftrue.buffer().len()), + iffalse.buffer().len(), + )); + Ok(Box::new(IfElse { + cond: cond, + iftrue: iftrue, + iffalse: iffalse, + buf: buf, + })) } } diff --git a/src/synth/lut.rs b/src/synth/lut.rs index 73b5efa..deda6d9 100644 --- a/src/synth/lut.rs +++ b/src/synth/lut.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamValue, + Parameters, Rate, Sample, SampleBuffer, +}; #[derive(Debug)] pub struct Lut { @@ -14,13 +17,16 @@ impl Generator for Lut { let pvel = self.freq.eval(params).first() / params.env.sample_rate; for i in 0..self.buf.len() { - self.buf[i] = self.lut[(((self.phase + pvel * (i as f32)) % 1.0) * (self.lut.len() as f32)) as usize]; + self.buf[i] = self.lut + [(((self.phase + pvel * (i as f32)) % 1.0) * (self.lut.len() as f32)) as usize]; } self.phase = (self.phase + pvel * (self.buf.len() as f32)) % 1.0; &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -32,7 +38,9 @@ impl GeneratorFactory for LutDataFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Lut { freq: params.remove_param("freq", 0)?.into_gen()?, - phase: params.get_param("phase", 1, &mut ParamValue::Float(0.0)).as_f32()?, + phase: params + .get_param("phase", 1, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(params.env.default_buffer_size), lut: { let mut lut: Vec = Vec::new(); @@ -44,7 +52,10 @@ impl GeneratorFactory for LutDataFactory { } if lut.is_empty() { - return Err(GenFactoryError::MissingRequiredParam("samples".to_string(), 2)); + return Err(GenFactoryError::MissingRequiredParam( + "samples".to_string(), + 2, + )); } lut @@ -62,13 +73,20 @@ impl GeneratorFactory for LutGenFactory { eprintln!("LutGenFactory::new({:?})", params); Ok(Box::new(Lut { freq: params.remove_param("freq", 2)?.into_gen()?, - phase: params.get_param("phase", 3, &mut ParamValue::Float(0.0)).as_f32()?, + phase: params + .get_param("phase", 3, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(params.env.default_buffer_size), lut: { let mut gen = params.remove_param("gen", 0)?.into_gen()?; let samps = params.get_req_param("samples", 1)?.as_f32()?; - let var = params.get_param("var", 4, &mut ParamValue::String("lut_freq".to_string())).as_string()?; - let mut genparams = Parameters { env: params.env.clone(), ..Default::default() }; + let var = params + .get_param("var", 4, &mut ParamValue::String("lut_freq".to_string())) + .as_string()?; + let mut genparams = Parameters { + env: params.env.clone(), + ..Default::default() + }; genparams.env.sample_rate = samps; genparams.vars.insert(var, 1.0); diff --git a/src/synth/math.rs b/src/synth/math.rs index 1889139..c73c0c4 100644 --- a/src/synth/math.rs +++ b/src/synth/math.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, Parameters, Rate, + SampleBuffer, +}; use std::mem; #[derive(Debug)] @@ -20,7 +23,9 @@ impl Generator for Add { } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -31,7 +36,11 @@ pub struct AddFactory; impl GeneratorFactory for AddFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Add { - terms: params.get_pos_params().into_iter().map(|x| x.into_gen()).collect::, _>>()?, + terms: params + .get_pos_params() + .into_iter() + .map(|x| x.into_gen()) + .collect::, _>>()?, buf: SampleBuffer::new(params.env.default_buffer_size), })) } @@ -58,7 +67,9 @@ impl Generator for Mul { } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -69,7 +80,11 @@ pub struct MulFactory; impl GeneratorFactory for MulFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Mul { - factors: params.get_pos_params().into_iter().map(|x| x.into_gen()).collect::, _>>()?, + factors: params + .get_pos_params() + .into_iter() + .map(|x| x.into_gen()) + .collect::, _>>()?, buf: SampleBuffer::new(params.env.default_buffer_size), })) } @@ -91,14 +106,16 @@ impl Generator for Negate { for v in self.buf.iter_mut() { *v *= -1.0; } - }, + } Rate::Control => { self.buf[0] *= -1.0; - }, + } } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -133,14 +150,16 @@ impl Generator for Reciprocate { for v in self.buf.iter_mut() { *v = v.powf(-1.0); } - }, + } Rate::Control => { self.buf[0] = self.buf[0].powf(-1.0); - }, + } } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } diff --git a/src/synth/mod.rs b/src/synth/mod.rs index 862ca38..b832863 100644 --- a/src/synth/mod.rs +++ b/src/synth/mod.rs @@ -1,15 +1,15 @@ #![allow(non_upper_case_globals)] -use std::{iter, cmp, slice, mem, fmt}; -use std::fmt::Debug; +use super::Sample; +use std::collections::HashMap; use std::error::Error; +use std::fmt::Debug; use std::ops::{Index, IndexMut}; -use std::collections::HashMap; -use super::*; +use std::{cmp, fmt, iter, mem, slice}; use ::byteorder::ByteOrder; -#[derive(PartialEq,Eq,Clone,Copy,Debug)] +#[derive(PartialEq, Eq, Clone, Copy, Debug)] pub enum Rate { Sample, Control, @@ -21,7 +21,7 @@ pub struct SampleBuffer { pub rate: Rate, } -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct Environment { pub sample_rate: f32, pub default_buffer_size: usize, @@ -87,10 +87,10 @@ impl SampleBuffer { Rate::Sample => { let len = cmp::min(self.len(), other.len()); self.samples[..len].clone_from_slice(&other.samples[..len]); - }, + } Rate::Control => { self.samples[0] = other.samples[0]; - }, + } } } @@ -109,10 +109,10 @@ impl SampleBuffer { } } }; - }, + } Rate::Control => { self.samples[0] += other.samples[0]; - }, + } } } @@ -131,10 +131,10 @@ impl SampleBuffer { } } }; - }, + } Rate::Control => { self.samples[0] *= other.samples[0]; - }, + } } } @@ -156,11 +156,15 @@ impl SampleBuffer { impl Index for SampleBuffer { type Output = Sample; - fn index(&self, idx: usize) -> &Sample { &self.samples[idx] } + fn index(&self, idx: usize) -> &Sample { + &self.samples[idx] + } } impl IndexMut for SampleBuffer { - fn index_mut(&mut self, idx: usize) -> &mut Sample { &mut self.samples[idx] } + fn index_mut(&mut self, idx: usize) -> &mut Sample { + &mut self.samples[idx] + } } impl Clone for SampleBuffer { @@ -172,13 +176,13 @@ impl Clone for SampleBuffer { } } -pub trait Generator : Debug + Send { +pub trait Generator: Debug + Send { fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer; fn buffer(&self) -> &SampleBuffer; fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer; } -pub type GenBox = Box; +pub type GenBox = Box; #[derive(Debug)] pub enum GenFactoryError { @@ -190,7 +194,7 @@ pub enum GenFactoryError { #[derive(Debug)] pub struct GenFactoryErrorType { pub kind: GenFactoryError, - desc: String + desc: String, } impl GenFactoryErrorType { @@ -201,8 +205,12 @@ impl GenFactoryErrorType { }; ret.desc = match ret.kind { - GenFactoryError::MissingRequiredParam(ref name, pos) => format!("Needed a parameter named {} or at pos {}", name, pos), - GenFactoryError::CannotConvert(from, to) => format!("Cannot convert {:?} to {:?}", from, to), + GenFactoryError::MissingRequiredParam(ref name, pos) => { + format!("Needed a parameter named {} or at pos {}", name, pos) + } + GenFactoryError::CannotConvert(from, to) => { + format!("Cannot convert {:?} to {:?}", from, to) + } GenFactoryError::BadType(ty) => format!("Bad parameter type {:?}", ty), }; @@ -231,17 +239,17 @@ impl Error for GenFactoryErrorType { impl fmt::Display for GenFactoryErrorType { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{}", self.description()) + write!(f, "{}", self.to_string()) } } -impl Into> for GenFactoryError { - fn into(self) -> Box { +impl Into> for GenFactoryError { + fn into(self) -> Box { Box::new(GenFactoryErrorType::new(self)) } } -#[derive(Debug,PartialEq,Eq,Clone,Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum ParamKind { Integer, Float, @@ -271,7 +279,9 @@ impl ParamValue { match *self { ParamValue::Integer(v) => Ok(v), ParamValue::Float(v) => Ok(v as isize), - ParamValue::String(ref v) => v.parse().map_err(|_| GenFactoryError::CannotConvert(ParamKind::String, ParamKind::Integer)), + ParamValue::String(ref v) => v + .parse() + .map_err(|_| GenFactoryError::CannotConvert(ParamKind::String, ParamKind::Integer)), ParamValue::Generator(ref mut g) => Ok(g.eval(&Default::default()).first() as isize), } } @@ -280,7 +290,9 @@ impl ParamValue { match *self { ParamValue::Integer(v) => Ok(v as f32), ParamValue::Float(v) => Ok(v), - ParamValue::String(ref v) => v.parse().map_err(|_| GenFactoryError::CannotConvert(ParamKind::String, ParamKind::Float)), + ParamValue::String(ref v) => v + .parse() + .map_err(|_| GenFactoryError::CannotConvert(ParamKind::String, ParamKind::Float)), ParamValue::Generator(ref mut g) => Ok(g.eval(&Default::default()).first()), } } @@ -290,15 +302,29 @@ impl ParamValue { ParamValue::Integer(v) => Ok(v.to_string()), ParamValue::Float(v) => Ok(v.to_string()), ParamValue::String(ref v) => Ok(v.clone()), - ParamValue::Generator(_) => Err(GenFactoryError::CannotConvert(ParamKind::Generator, ParamKind::String)), + ParamValue::Generator(_) => Err(GenFactoryError::CannotConvert( + ParamKind::Generator, + ParamKind::String, + )), } } pub fn into_gen(self) -> Result { match self { - ParamValue::Integer(v) => Ok(Box::new(self::param::Param { name : "_".to_string(), default: v as f32, buf: SampleBuffer::new(1) })), - ParamValue::Float(v) => Ok(Box::new(self::param::Param { name : "_".to_string(), default: v, buf: SampleBuffer::new(1) })), - ParamValue::String(_) => Err(GenFactoryError::CannotConvert(ParamKind::String, ParamKind::Generator)), + ParamValue::Integer(v) => Ok(Box::new(self::param::Param { + name: "_".to_string(), + default: v as f32, + buf: SampleBuffer::new(1), + })), + ParamValue::Float(v) => Ok(Box::new(self::param::Param { + name: "_".to_string(), + default: v, + buf: SampleBuffer::new(1), + })), + ParamValue::String(_) => Err(GenFactoryError::CannotConvert( + ParamKind::String, + ParamKind::Generator, + )), ParamValue::Generator(g) => Ok(g), } } @@ -310,37 +336,63 @@ impl<'a> From<&'a ParamValue> for ParamKind { } } -#[derive(Debug,Default)] +#[derive(Debug, Default)] pub struct FactoryParameters { pub env: Environment, pub vars: HashMap, } impl FactoryParameters { - pub fn get_param<'a, 'b : 'a>(&'a mut self, name: &str, position: usize, default: &'b mut ParamValue) -> &'a mut ParamValue { + pub fn get_param<'a, 'b: 'a>( + &'a mut self, + name: &str, + position: usize, + default: &'b mut ParamValue, + ) -> &'a mut ParamValue { let position = position.to_string(); - match (self.vars.contains_key(name), self.vars.contains_key(&position)) { + match ( + self.vars.contains_key(name), + self.vars.contains_key(&position), + ) { (true, _) => self.vars.get_mut(name).unwrap(), (false, true) => self.vars.get_mut(&position).unwrap(), (false, false) => default, } } - pub fn get_req_param(&mut self, name: &str, position: usize) -> Result<&mut ParamValue, GenFactoryError> { + pub fn get_req_param( + &mut self, + name: &str, + position: usize, + ) -> Result<&mut ParamValue, GenFactoryError> { let pos = position.to_string(); match (self.vars.contains_key(name), self.vars.contains_key(&pos)) { (true, _) => Ok(self.vars.get_mut(name).unwrap()), (false, true) => Ok(self.vars.get_mut(&pos).unwrap()), - (false, false) => Err(GenFactoryError::MissingRequiredParam(name.to_string(), position)), + (false, false) => Err(GenFactoryError::MissingRequiredParam( + name.to_string(), + position, + )), } } - pub fn remove_param(&mut self, name: &str, position: usize) -> Result { - match self.vars.remove(name).or_else(|| self.vars.remove(&position.to_string())) { + pub fn remove_param( + &mut self, + name: &str, + position: usize, + ) -> Result { + match self + .vars + .remove(name) + .or_else(|| self.vars.remove(&position.to_string())) + { Some(v) => Ok(v), - None => Err(GenFactoryError::MissingRequiredParam(name.to_string(), position)), + None => Err(GenFactoryError::MissingRequiredParam( + name.to_string(), + position, + )), } } @@ -390,26 +442,77 @@ pub use self::noise::Noise; pub mod adsr; pub use self::adsr::DAHDSR; -pub fn all_factories() -> HashMap { +pub fn all_factories() -> HashMap { let mut ret = HashMap::new(); - ret.insert("param".to_string(), &self::param::Factory as &GeneratorFactory); - ret.insert("add".to_string(), &self::math::FactoryAdd as &GeneratorFactory); - ret.insert("mul".to_string(), &self::math::FactoryMul as &GeneratorFactory); - ret.insert("negate".to_string(), &self::math::FactoryNegate as &GeneratorFactory); - ret.insert("reciprocate".to_string(), &self::math::FactoryReciprocate as &GeneratorFactory); - ret.insert("rel".to_string(), &self::rel::Factory as &GeneratorFactory); - ret.insert("ifelse".to_string(), &self::logic::Factory as &GeneratorFactory); - ret.insert("controlrate".to_string(), &self::util::FactoryControlRate as &GeneratorFactory); - ret.insert("samplerate".to_string(), &self::util::FactorySampleRate as &GeneratorFactory); - ret.insert("lutdata".to_string(), &self::lut::FactoryLutData as &GeneratorFactory); - ret.insert("lutgen".to_string(), &self::lut::FactoryLutGen as &GeneratorFactory); - ret.insert("sine".to_string(), &self::sine::Factory as &GeneratorFactory); - ret.insert("saw".to_string(), &self::saw::Factory as &GeneratorFactory); - ret.insert("triangle".to_string(), &self::triangle::Factory as &GeneratorFactory); - ret.insert("square".to_string(), &self::square::Factory as &GeneratorFactory); - ret.insert("noise".to_string(), &self::noise::Factory as &GeneratorFactory); - ret.insert("dahdsr".to_string(), &self::adsr::Factory as &GeneratorFactory); + ret.insert( + "param".to_string(), + &self::param::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "add".to_string(), + &self::math::FactoryAdd as &dyn GeneratorFactory, + ); + ret.insert( + "mul".to_string(), + &self::math::FactoryMul as &dyn GeneratorFactory, + ); + ret.insert( + "negate".to_string(), + &self::math::FactoryNegate as &dyn GeneratorFactory, + ); + ret.insert( + "reciprocate".to_string(), + &self::math::FactoryReciprocate as &dyn GeneratorFactory, + ); + ret.insert( + "rel".to_string(), + &self::rel::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "ifelse".to_string(), + &self::logic::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "controlrate".to_string(), + &self::util::FactoryControlRate as &dyn GeneratorFactory, + ); + ret.insert( + "samplerate".to_string(), + &self::util::FactorySampleRate as &dyn GeneratorFactory, + ); + ret.insert( + "lutdata".to_string(), + &self::lut::FactoryLutData as &dyn GeneratorFactory, + ); + ret.insert( + "lutgen".to_string(), + &self::lut::FactoryLutGen as &dyn GeneratorFactory, + ); + ret.insert( + "sine".to_string(), + &self::sine::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "saw".to_string(), + &self::saw::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "triangle".to_string(), + &self::triangle::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "square".to_string(), + &self::square::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "noise".to_string(), + &self::noise::Factory as &dyn GeneratorFactory, + ); + ret.insert( + "dahdsr".to_string(), + &self::adsr::Factory as &dyn GeneratorFactory, + ); ret } diff --git a/src/synth/noise.rs b/src/synth/noise.rs index 850b75b..765d17e 100644 --- a/src/synth/noise.rs +++ b/src/synth/noise.rs @@ -1,7 +1,10 @@ +use super::{ + FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, Parameters, Rate, + SampleBuffer, +}; use std::mem; -use super::*; -use ::rand::{XorShiftRng, Rng, SeedableRng}; +use ::rand::{Rng, SeedableRng, XorShiftRng}; #[derive(Debug)] pub struct Noise { @@ -19,7 +22,9 @@ impl Generator for Noise { &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } diff --git a/src/synth/param.rs b/src/synth/param.rs index 602be1d..dd3ecb8 100644 --- a/src/synth/param.rs +++ b/src/synth/param.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamValue, + Parameters, Sample, SampleBuffer, +}; #[derive(Debug)] pub struct Param { @@ -9,10 +12,13 @@ pub struct Param { impl Generator for Param { fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer { - self.buf.set(*params.vars.get(&self.name).unwrap_or(&self.default)); + self.buf + .set(*params.vars.get(&self.name).unwrap_or(&self.default)); + &self.buf + } + fn buffer(&self) -> &SampleBuffer { &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -24,7 +30,9 @@ impl GeneratorFactory for ParamFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Param { name: params.get_req_param("name", 0)?.as_string()?, - default: params.get_param("default", 1, &mut ParamValue::Float(0.0)).as_f32()?, + default: params + .get_param("default", 1, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(1), })) } diff --git a/src/synth/rel.rs b/src/synth/rel.rs index d2fbb70..93b0cd1 100644 --- a/src/synth/rel.rs +++ b/src/synth/rel.rs @@ -1,5 +1,8 @@ +use super::{ + FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamKind, ParamValue, + Parameters, Rate, SampleBuffer, +}; use std::{cmp, mem}; -use super::*; #[derive(Debug)] pub enum RelOp { @@ -97,28 +100,32 @@ impl Generator for Rel { 0.0 }; } - }, + } Rate::Control => { let val = left_buf.first(); let thres = right_buf.first(); - self.buf.set(if match self.op { - RelOp::Greater => val > thres, - RelOp::GreaterEqual => val >= thres, - RelOp::Equal => val == thres, - RelOp::NotEqual => val != thres, - RelOp::LessEqual => val <= thres, - RelOp::Less => val < thres, - } { - 1.0 - } else { - 0.0 - }); - }, + self.buf.set( + if match self.op { + RelOp::Greater => val > thres, + RelOp::GreaterEqual => val >= thres, + RelOp::Equal => val == thres, + RelOp::NotEqual => val != thres, + RelOp::LessEqual => val <= thres, + RelOp::Less => val < thres, + } { + 1.0 + } else { + 0.0 + }, + ); + } } &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } diff --git a/src/synth/saw.rs b/src/synth/saw.rs index 3e30ca6..5c36cc1 100644 --- a/src/synth/saw.rs +++ b/src/synth/saw.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamValue, + Parameters, Rate, SampleBuffer, +}; #[derive(Debug)] pub struct Saw { @@ -19,7 +22,9 @@ impl Generator for Saw { self.phase = (self.phase + pvel * (self.buf.len() as f32)) % 1.0; &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -31,7 +36,9 @@ impl GeneratorFactory for SawFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Saw { freq: params.remove_param("freq", 0)?.into_gen()?, - phase: params.get_param("phase", 1, &mut ParamValue::Float(0.0)).as_f32()?, + phase: params + .get_param("phase", 1, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(params.env.default_buffer_size), })) } diff --git a/src/synth/sine.rs b/src/synth/sine.rs index 4476d4a..3be79db 100644 --- a/src/synth/sine.rs +++ b/src/synth/sine.rs @@ -1,5 +1,8 @@ +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamValue, + Parameters, Rate, SampleBuffer, +}; use std::f32::consts::PI; -use super::*; const TAU: f32 = 2f32 * PI; @@ -22,7 +25,9 @@ impl Generator for Sine { self.phase = (self.phase + pvel * (self.buf.len() as f32)) % TAU; &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -34,7 +39,9 @@ impl GeneratorFactory for SineFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Sine { freq: params.remove_param("freq", 0)?.into_gen()?, - phase: params.get_param("phase", 1, &mut ParamValue::Float(0.0)).as_f32()?, + phase: params + .get_param("phase", 1, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(params.env.default_buffer_size), })) } diff --git a/src/synth/square.rs b/src/synth/square.rs index 4b01e07..ef49020 100644 --- a/src/synth/square.rs +++ b/src/synth/square.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamValue, + Parameters, Rate, SampleBuffer, +}; #[derive(Debug)] pub struct Square { @@ -23,7 +26,9 @@ impl Generator for Square { self.phase = (self.phase + pvel * (self.buf.len() as f32)) % 1.0; &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -35,7 +40,9 @@ impl GeneratorFactory for SquareFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Square { freq: params.remove_param("freq", 0)?.into_gen()?, - phase: params.get_param("phase", 1, &mut ParamValue::Float(0.0)).as_f32()?, + phase: params + .get_param("phase", 1, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(params.env.default_buffer_size), })) } diff --git a/src/synth/triangle.rs b/src/synth/triangle.rs index 944c539..7d88ec1 100644 --- a/src/synth/triangle.rs +++ b/src/synth/triangle.rs @@ -1,5 +1,8 @@ +use super::{ + FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, ParamValue, + Parameters, Rate, SampleBuffer, +}; use std::mem; -use super::*; #[derive(Debug)] pub struct Triangle { @@ -27,7 +30,9 @@ impl Generator for Triangle { self.phase = (self.phase + pvel * (self.buf.len() as f32)) % 1.0; &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -39,7 +44,9 @@ impl GeneratorFactory for TriangleFactory { fn new(&self, params: &mut FactoryParameters) -> Result { Ok(Box::new(Triangle { freq: params.remove_param("freq", 0)?.into_gen()?, - phase: params.get_param("phase", 1, &mut ParamValue::Float(0.0)).as_f32()?, + phase: params + .get_param("phase", 1, &mut ParamValue::Float(0.0)) + .as_f32()?, buf: SampleBuffer::new(params.env.default_buffer_size), })) } diff --git a/src/synth/util.rs b/src/synth/util.rs index 5bde721..16d6f7f 100644 --- a/src/synth/util.rs +++ b/src/synth/util.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + mem, FactoryParameters, GenBox, GenFactoryError, Generator, GeneratorFactory, Parameters, Rate, + SampleBuffer, +}; #[derive(Debug)] pub struct ControlRate { @@ -9,7 +12,10 @@ pub struct ControlRate { impl ControlRate { pub fn new(mut gen: GenBox) -> ControlRate { gen.set_buffer(SampleBuffer::new(1)); - ControlRate { value: gen, buf: SampleBuffer::new(1) } + ControlRate { + value: gen, + buf: SampleBuffer::new(1), + } } } @@ -19,15 +25,21 @@ impl Generator for ControlRate { self.buf.update_from(self.value.eval(params)); &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } - fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } + fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { + mem::replace(&mut self.buf, buf) + } } pub struct ControlRateFactory; impl GeneratorFactory for ControlRateFactory { fn new(&self, params: &mut FactoryParameters) -> Result { - Ok(Box::new(ControlRate::new(params.remove_param("gen", 0)?.into_gen()?))) + Ok(Box::new(ControlRate::new( + params.remove_param("gen", 0)?.into_gen()?, + ))) } } @@ -43,7 +55,9 @@ impl Generator for SampleRate { self.buf.set(params.env.sample_rate); &self.buf } - fn buffer(&self) -> &SampleBuffer { &self.buf } + fn buffer(&self) -> &SampleBuffer { + &self.buf + } fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) } @@ -52,8 +66,10 @@ impl Generator for SampleRate { pub struct SampleRateFactory; impl GeneratorFactory for SampleRateFactory { - fn new(&self, params: &mut FactoryParameters) -> Result { - Ok(Box::new(SampleRate { buf: SampleBuffer::new(1) })) + fn new(&self, _params: &mut FactoryParameters) -> Result { + Ok(Box::new(SampleRate { + buf: SampleBuffer::new(1), + })) } } -- cgit v1.2.3-70-g09d2