Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
xasopheno committed Jun 23, 2023
1 parent b2e64d8 commit 98fef87
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 136 deletions.
13 changes: 12 additions & 1 deletion ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct FunDef {
#[derive(Clone, PartialEq, Debug, Hash)]
pub enum Op {
AsIs,
Out,
Id(String),
Tag(String),
//
Expand All @@ -24,6 +25,9 @@ pub enum Op {
scales: Vec<Scale>,
},
//
FMOsc {
defs: Vec<FmOscDef>,
},
Lowpass {
hash: String,
cutoff_frequency: Rational64,
Expand Down Expand Up @@ -125,7 +129,13 @@ pub enum Op {
},
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Ord, PartialOrd, Hash, Eq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Hash, Ord, PartialOrd, Eq)]
pub struct FmOscDef {
pub fm: Rational64,
pub depth: Rational64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Hash, Ord, PartialOrd, Eq)]
/// Oscillator Type
pub enum OscType {
None,
Expand All @@ -134,6 +144,7 @@ pub enum OscType {
Square { width: Option<Rational64> },
Noise,
Saw,
Fm { defs: Vec<FmOscDef> },
}

impl OscType {
Expand Down
1 change: 1 addition & 0 deletions ast/src/datagen/mod_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub fn eeg_datum_to_point_op(
osc_type: OscType::None,
names: nameset,
filters: vec![],
is_out: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod nameset;
pub mod operations;
pub mod term;
pub use crate::{
ast::{FunDef, Op, Op::*, OscType, ASR},
ast::{FmOscDef, FunDef, Op, Op::*, OscType, ASR},
datagen::Scale,
generator::{
coefs::{Coef, Coefs},
Expand Down
2 changes: 2 additions & 0 deletions ast/src/operations/get_length_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ impl GetLengthRatio<Term> for Op {
) -> Result<Rational64, Error> {
match self {
Op::AsIs {}
| Op::Out {}
| Op::Lowpass { .. }
| Op::FMOsc { .. }
| Op::Highpass { .. }
| Op::Bandpass { .. }
| Op::AD { .. }
Expand Down
3 changes: 3 additions & 0 deletions ast/src/operations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn pad_length(
osc_type: OscType::None,
names: NameSet::new(),
filters: vec![],
is_out: false,
});
}
}
Expand Down Expand Up @@ -114,6 +115,7 @@ pub fn join_sequence(mut l: NormalForm, mut r: NormalForm) -> NormalForm {
osc_type: OscType::None,
names: NameSet::new(),
filters: vec![],
is_out: false,
}])
}
}
Expand All @@ -134,6 +136,7 @@ pub fn join_sequence(mut l: NormalForm, mut r: NormalForm) -> NormalForm {
osc_type: OscType::None,
names: NameSet::new(),
filters: vec![],
is_out: false,
}])
}
}
Expand Down
17 changes: 13 additions & 4 deletions ast/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct PointOp {
pub names: NameSet,
/// Filters
pub filters: Vec<BiquadFilterDef>,
/// Should fade out to nothing
pub is_out: bool,
}

impl Default for PointOp {
Expand All @@ -69,6 +71,7 @@ impl Default for PointOp {
osc_type: OscType::None,
names: NameSet::new(),
filters: vec![],
is_out: false,
}
}
}
Expand Down Expand Up @@ -229,6 +232,7 @@ impl Mul<PointOp> for PointOp {
.chain(&other.filters)
.map(|f| f.to_owned())
.collect(),
is_out: other.is_out,
}
}
}
Expand All @@ -252,9 +256,9 @@ impl<'a, 'b> Mul<&'b PointOp> for &'a PointOp {
other.reverb
},
osc_type: if other.osc_type.is_none() {
self.osc_type
self.osc_type.clone()
} else {
other.osc_type
other.osc_type.clone()
},
attack: self.attack * other.attack,
decay: self.decay * other.decay,
Expand All @@ -267,6 +271,7 @@ impl<'a, 'b> Mul<&'b PointOp> for &'a PointOp {
.chain(&other.filters)
.map(|f| f.to_owned())
.collect(),
is_out: other.is_out,
}
}
}
Expand All @@ -288,7 +293,7 @@ impl MulAssign for PointOp {
other.reverb
},
osc_type: if other.osc_type == OscType::None {
self.osc_type
self.osc_type.clone()
} else {
other.osc_type
},
Expand All @@ -303,6 +308,7 @@ impl MulAssign for PointOp {
.chain(&other.filters)
.map(|f| f.to_owned())
.collect(),
is_out: other.is_out,
}
}
}
Expand Down Expand Up @@ -334,7 +340,7 @@ impl PointOp {
other.reverb
},
osc_type: if other.osc_type.is_none() {
self.osc_type
self.osc_type.clone()
} else {
other.osc_type
},
Expand All @@ -349,6 +355,7 @@ impl PointOp {
.chain(&other.filters)
.map(|f| f.to_owned())
.collect(),
is_out: other.is_out,
}
}

Expand All @@ -368,6 +375,7 @@ impl PointOp {
osc_type: OscType::None,
names: NameSet::new(),
filters: vec![],
is_out: false,
}
}
pub fn init_silent() -> PointOp {
Expand All @@ -386,6 +394,7 @@ impl PointOp {
osc_type: OscType::None,
names: NameSet::new(),
filters: vec![],
is_out: false,
}
}

Expand Down
14 changes: 14 additions & 0 deletions ast/src/operations/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ impl Normalize<Term> for Op {
) -> Result<(), Error> {
match self {
Op::AsIs => {}
Op::Out => {
input.fmap_mut(|op| {
op.is_out = true;
op.fm = Ratio::new(0, 1);
op.fa = Ratio::new(0, 1);
op.g = Ratio::new(0, 1);
op.l = Ratio::new(0, 1)
});
}
Op::Lambda {
term,
input_name,
Expand All @@ -37,6 +46,11 @@ impl Normalize<Term> for Op {
handle_id_error(id, defs)?.apply_to_normal_form(input, defs)?;
}

Op::FMOsc { defs } => input.fmap_mut(|op| {
op.osc_type = OscType::Fm {
defs: defs.to_owned(),
}
}),
Op::Lowpass {
hash,
cutoff_frequency,
Expand Down
6 changes: 4 additions & 2 deletions core/src/generation/timed_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ impl TimedOp {
decay: self.decay,
asr: self.asr,
portamento: self.portamento,
osc_type: self.osc_type,
osc_type: self.osc_type.clone(),
names: NameSet::new(),
filters: Vec::new(),
//TODO
is_out: false,
}
}

Expand All @@ -90,7 +92,7 @@ impl TimedOp {
pm: point_op.pm,
pa: point_op.pa,
attack: point_op.attack,
osc_type: point_op.osc_type,
osc_type: point_op.osc_type.clone(),
decay: point_op.decay,
reverb: point_op
.reverb
Expand Down
2 changes: 2 additions & 0 deletions core/src/renderable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ mod tests {
next_r_silent: false,
names: vec![],
filters: vec![],
next_out: false,
},
RenderOp {
f: 330.0,
Expand All @@ -128,6 +129,7 @@ mod tests {
next_r_silent: false,
names: vec![],
filters: vec![],
next_out: false,
},
RenderOp {
f: 0.0,
Expand Down
3 changes: 2 additions & 1 deletion instrument/src/asr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use weresocool_ast::ASR;
impl Voice {
pub fn calculate_op_gain(
&mut self,
next_out: bool,
silence_now: bool,
silence_next: bool,
index: usize,
total_length: usize,
) -> f64 {
if self.asr == ASR::Long {
if next_out || self.asr == ASR::Long {
calculate_long_gain(
self.past.gain,
self.current.gain,
Expand Down
3 changes: 3 additions & 0 deletions instrument/src/gain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ impl Voice {
}
}

#[inline]
pub fn silence_now(&self) -> bool {
self.current.silent()
}

#[inline]
pub fn silence_to_sound(&self) -> bool {
self.past.silent() && !self.current.silent()
}

#[inline]
pub fn sound_to_silence(&self) -> bool {
!self.past.silent() && self.current.silent()
}
Expand Down
10 changes: 9 additions & 1 deletion instrument/src/renderable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct RenderOp {
pub next_r_silent: bool,
pub names: Vec<String>,
pub filters: Vec<BiquadFilterDef>,
pub next_out: bool,
}

impl RenderOp {
Expand All @@ -60,6 +61,7 @@ impl RenderOp {
osc_type: OscType::None,
next_l_silent: false,
next_r_silent: false,
next_out: false,
names: Vec::new(),
filters: Vec::new(),
}
Expand All @@ -84,6 +86,7 @@ impl RenderOp {
osc_type: OscType::None,
next_l_silent: true,
next_r_silent: true,
next_out: false,
names: Vec::new(),
filters: Vec::new(),
}
Expand Down Expand Up @@ -115,6 +118,7 @@ impl RenderOp {
osc_type,
next_l_silent: true,
next_r_silent: true,
next_out: false,
names: vec![],
filters,
}
Expand Down Expand Up @@ -185,6 +189,8 @@ fn pointop_to_renderop(
let settings = Settings::global();
let mut next_l_gain = 0.0;
let mut next_r_gain = 0.0;
let mut next_out = false;
let _next_ = false;
let next_silent;

match next {
Expand All @@ -193,6 +199,7 @@ fn pointop_to_renderop(
next_l_gain = l;
next_r_gain = r;
next_silent = op.is_silent();
next_out = op.is_out;
}

None => next_silent = true,
Expand All @@ -219,7 +226,7 @@ fn pointop_to_renderop(
total_samples: (l * settings.sample_rate).round() as usize,
attack: r_to_f64(point_op.attack * basis.a) * settings.sample_rate,
decay: r_to_f64(point_op.decay * basis.d) * settings.sample_rate,
osc_type: point_op.osc_type,
osc_type: point_op.osc_type.clone(),
asr: point_op.asr,
portamento: (r_to_f64(point_op.portamento) * 1024_f64) as usize,
voice,
Expand All @@ -237,6 +244,7 @@ fn pointop_to_renderop(
q_factor: f.q_factor,
})
.collect(),
next_out,
};

*time += point_op.l * basis.l;
Expand Down
2 changes: 2 additions & 0 deletions instrument/src/renderable/render_voice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl RenderVoice {
index: self.sample_index,
names: current_op.names.clone(),
filters: current_op.filters.clone(),
osc_type: current_op.osc_type.clone(),
..*current_op
});
self.sample_index += samples_left_in_batch;
Expand All @@ -61,6 +62,7 @@ impl RenderVoice {
index: self.sample_index,
names: current_op.names.clone(),
filters: current_op.filters.clone(),
osc_type: current_op.osc_type.clone(),
..*current_op
});

Expand Down

0 comments on commit 98fef87

Please sign in to comment.