summaryrefslogtreecommitdiff
path: root/src/synth/mod.rs
diff options
context:
space:
mode:
authorthajohns <thajohns@clarkson.edu>2021-09-09 01:15:22 -0400
committerGraham Northup <grahamnorthup@yahoo.com>2021-09-10 04:05:55 -0400
commitd6e399973e9bc6447a28b80cacffcbc6a768f1ed (patch)
tree772939eb5d49bdb6ee8e5d632f2224867f6e817d /src/synth/mod.rs
parentee11b17c0e2796bd3d843a8a516c12453af75927 (diff)
got pre-linking steps working, removed glob includes
Diffstat (limited to 'src/synth/mod.rs')
-rw-r--r--src/synth/mod.rs213
1 files changed, 158 insertions, 55 deletions
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<usize> 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<usize> 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<Generator>;
+pub type GenBox = Box<dyn Generator>;
#[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<Box<Error>> for GenFactoryError {
- fn into(self) -> Box<Error> {
+impl Into<Box<dyn Error>> for GenFactoryError {
+ fn into(self) -> Box<dyn Error> {
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<GenBox, GenFactoryError> {
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<String, ParamValue>,
}
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<ParamValue, GenFactoryError> {
- match self.vars.remove(name).or_else(|| self.vars.remove(&position.to_string())) {
+ pub fn remove_param(
+ &mut self,
+ name: &str,
+ position: usize,
+ ) -> Result<ParamValue, GenFactoryError> {
+ 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<String, &'static GeneratorFactory> {
+pub fn all_factories() -> HashMap<String, &'static dyn GeneratorFactory> {
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
}