1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use crate::{NameSet, OscType, Term, ASR};
use num_rational::{Ratio, Rational64};
use scop::Defs;
use std::{
    collections::HashSet,
    ops::{Mul, MulAssign},
};
use weresocool_error::Error;
use weresocool_filter::BiquadFilterDef;
mod get_length_ratio;
pub mod helpers;
mod normalize;
pub mod substitute;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
/// All operations in the language take a NormalForm as an import and
/// return a NormalForm.
pub struct NormalForm {
    pub operations: Vec<Vec<PointOp>>,
    pub length_ratio: Rational64,
}

#[derive(Debug, Clone, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct PointOp {
    /// Frequency Multiply
    pub fm: Rational64,
    /// Frequency Add
    pub fa: Rational64,
    /// Pan Multiply
    pub pm: Rational64,
    /// Pan Add
    pub pa: Rational64,
    /// Gain Multiply
    pub g: Rational64,
    /// Length Multiply
    pub l: Rational64,
    /// Attack Length
    pub attack: Rational64,
    /// Decay Length
    pub decay: Rational64,
    /// Attack/Sustain/Release Type
    pub asr: ASR,
    /// Portamento Length
    pub portamento: Rational64,
    /// Reverb Multiplier
    pub reverb: Option<Rational64>,
    /// Oscillator Type
    pub osc_type: OscType,
    /// Set of Names
    pub names: NameSet,
    /// Filters
    pub filters: Vec<BiquadFilterDef>,
}

impl Default for PointOp {
    fn default() -> Self {
        PointOp {
            fm: Ratio::new(1, 1),
            fa: Ratio::new(0, 1),
            pm: Ratio::new(1, 1),
            pa: Ratio::new(0, 1),
            g: Ratio::new(1, 1),
            l: Ratio::new(1, 1),
            reverb: None,
            attack: Ratio::new(1, 1),
            decay: Ratio::new(1, 1),
            asr: ASR::Long,
            portamento: Ratio::new(1, 1),
            osc_type: OscType::None,
            names: NameSet::new(),
            filters: vec![],
        }
    }
}

pub trait Normalize<T> {
    fn apply_to_normal_form(
        &self,
        normal_form: &mut NormalForm,
        defs: &mut Defs<T>,
    ) -> Result<(), Error>;
}

pub trait GetLengthRatio<T> {
    fn get_length_ratio(
        &self,
        normal_form: &NormalForm,
        defs: &mut Defs<T>,
    ) -> Result<Rational64, Error>;
}

pub trait Substitute<T> {
    fn substitute(&self, normal_form: &mut NormalForm, defs: &mut Defs<T>) -> Result<Term, Error>;
}

impl GetLengthRatio<Term> for NormalForm {
    fn get_length_ratio(
        &self,
        _normal_form: &NormalForm,
        _defs: &mut Defs<Term>,
    ) -> Result<Rational64, Error> {
        Ok(self.length_ratio)
    }
}

impl Substitute<Term> for NormalForm {
    fn substitute(
        &self,
        _normal_form: &mut NormalForm,
        _defs: &mut Defs<Term>,
    ) -> Result<Term, Error> {
        Ok(Term::Nf(self.clone()))
    }
}

pub fn union_names(b_tree_set: NameSet, left: &NameSet) -> NameSet {
    let mut result = b_tree_set;
    for val in left.to_vec() {
        result.insert(val.clone());
    }

    result
}

impl Mul<NormalForm> for NormalForm {
    type Output = NormalForm;

    fn mul(self, other: NormalForm) -> NormalForm {
        let mut nf_result = vec![];
        let mut max_lr = Rational64::new(0, 1);
        for other_seq in self.operations.iter() {
            for self_seq in other.operations.iter() {
                for other_point_op in other_seq.iter() {
                    let mut seq_result: Vec<PointOp> = vec![];
                    let mut seq_lr = Rational64::new(0, 1);
                    for self_point_op in self_seq.iter() {
                        seq_lr += self_point_op.l * other_point_op.l;
                        seq_result.push(other_point_op * self_point_op);
                    }

                    if seq_lr > max_lr {
                        max_lr = seq_lr
                    }

                    nf_result.push(seq_result);
                }
            }
        }

        NormalForm {
            operations: nf_result,
            length_ratio: max_lr,
        }
    }
}

impl MulAssign<&NormalForm> for NormalForm {
    fn mul_assign(&mut self, other: &NormalForm) {
        let mut nf_result = vec![];
        let mut max_lr = Rational64::new(0, 1);
        for other_seq in self.operations.iter() {
            for self_seq in other.operations.iter() {
                for other_point_op in other_seq.iter() {
                    let mut seq_result: Vec<PointOp> = vec![];
                    let mut seq_lr = Rational64::new(0, 1);
                    for self_point_op in self_seq.iter() {
                        seq_lr += self_point_op.l * other_point_op.l;
                        seq_result.push(other_point_op * self_point_op);
                    }

                    if seq_lr > max_lr {
                        max_lr = seq_lr
                    }

                    nf_result.push(seq_result);
                }
            }
        }

        *self = NormalForm {
            operations: nf_result,
            length_ratio: max_lr,
        }
    }
}

impl Normalize<Term> for NormalForm {
    fn apply_to_normal_form(
        &self,
        input: &mut NormalForm,
        _defs: &mut Defs<Term>,
    ) -> Result<(), Error> {
        *input *= self;
        Ok(())
    }
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl Mul<PointOp> for PointOp {
    type Output = PointOp;

    fn mul(self, other: PointOp) -> PointOp {
        let names = union_names(self.names.clone(), &other.names);
        PointOp {
            fm: self.fm * other.fm,
            fa: self.fa + other.fa,
            pm: self.pm * other.pm,
            pa: self.pa + other.pa,
            g: self.g * other.g,
            l: self.l * other.l,
            reverb: if other.reverb.is_none() {
                self.reverb
            } else {
                other.reverb
            },
            osc_type: if other.osc_type.is_none() {
                self.osc_type
            } else {
                other.osc_type
            },
            attack: self.attack * other.attack,
            decay: self.decay * other.decay,
            asr: other.asr,
            portamento: self.portamento * other.portamento,
            names,
            filters: self
                .filters
                .iter()
                .chain(&other.filters)
                .map(|f| f.to_owned())
                .collect(),
        }
    }
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl<'a, 'b> Mul<&'b PointOp> for &'a PointOp {
    type Output = PointOp;

    fn mul(self, other: &'b PointOp) -> PointOp {
        let names = union_names(self.names.clone(), &other.names);
        PointOp {
            fm: self.fm * other.fm,
            fa: self.fa + other.fa,
            pm: self.pm * other.pm,
            pa: self.pa + other.pa,
            g: self.g * other.g,
            l: self.l * other.l,
            reverb: if other.reverb.is_none() {
                self.reverb
            } else {
                other.reverb
            },
            osc_type: if other.osc_type.is_none() {
                self.osc_type
            } else {
                other.osc_type
            },
            attack: self.attack * other.attack,
            decay: self.decay * other.decay,
            asr: other.asr,
            portamento: self.portamento * other.portamento,
            names,
            filters: self
                .filters
                .iter()
                .chain(&other.filters)
                .map(|f| f.to_owned())
                .collect(),
        }
    }
}

#[allow(clippy::suspicious_op_assign_impl)]
impl MulAssign for PointOp {
    fn mul_assign(&mut self, other: PointOp) {
        let names = union_names(self.names.clone(), &other.names);
        *self = PointOp {
            fm: self.fm * other.fm,
            fa: self.fa + other.fa,
            pm: self.pm * other.pm,
            pa: self.pa + other.pa,
            g: self.g * other.g,
            l: self.l * other.l,
            reverb: if other.reverb.is_none() {
                self.reverb
            } else {
                other.reverb
            },
            osc_type: if other.osc_type == OscType::None {
                self.osc_type
            } else {
                other.osc_type
            },
            attack: self.attack * other.attack,
            decay: self.decay * other.decay,
            asr: other.asr,
            portamento: self.portamento * other.portamento,
            names,
            filters: self
                .filters
                .iter()
                .chain(&other.filters)
                .map(|f| f.to_owned())
                .collect(),
        }
    }
}

impl PointOp {
    pub fn is_silent(&self) -> bool {
        let zero = Rational64::new(0, 1);
        self.fm == zero && self.fa < Rational64::new(20, 1) || self.g == zero
    }

    pub fn silence(&mut self) {
        self.fm = Rational64::from_integer(0);
        self.fa = Rational64::from_integer(0);
        self.g = Rational64::from_integer(0);
    }

    pub fn mod_by(&mut self, other: PointOp, l: Rational64) {
        let names = union_names(self.names.clone(), &other.names);
        *self = PointOp {
            fm: self.fm * other.fm,
            fa: self.fa + other.fa,
            pm: self.pm * other.pm,
            pa: self.pa + other.pa,
            g: self.g * other.g,
            l,
            reverb: if other.reverb.is_none() {
                self.reverb
            } else {
                other.reverb
            },
            osc_type: if other.osc_type.is_none() {
                self.osc_type
            } else {
                other.osc_type
            },
            attack: self.attack * other.attack,
            decay: self.decay * other.decay,
            asr: other.asr,
            portamento: self.portamento * other.portamento,
            names,
            filters: self
                .filters
                .iter()
                .chain(&other.filters)
                .map(|f| f.to_owned())
                .collect(),
        }
    }

    pub fn init() -> PointOp {
        PointOp {
            fm: Ratio::new(1, 1),
            fa: Ratio::new(0, 1),
            pm: Ratio::new(1, 1),
            pa: Ratio::new(0, 1),
            g: Ratio::new(1, 1),
            l: Ratio::new(1, 1),
            reverb: None,
            attack: Ratio::new(1, 1),
            decay: Ratio::new(1, 1),
            asr: ASR::Long,
            portamento: Ratio::new(1, 1),
            osc_type: OscType::None,
            names: NameSet::new(),
            filters: vec![],
        }
    }
    pub fn init_silent() -> PointOp {
        PointOp {
            fm: Ratio::new(0, 1),
            fa: Ratio::new(0, 1),
            pm: Ratio::new(1, 1),
            pa: Ratio::new(0, 1),
            g: Ratio::new(0, 1),
            l: Ratio::new(1, 1),
            reverb: None,
            attack: Ratio::new(1, 1),
            decay: Ratio::new(1, 1),
            portamento: Ratio::new(1, 1),
            asr: ASR::Long,
            osc_type: OscType::None,
            names: NameSet::new(),
            filters: vec![],
        }
    }

    //        pub fn to_op(&self) -> Op {
    //            let osc_op = match self.osc_type {
    //                OscType::Sine => Op::Sine,
    //                OscType::Square => Op::Square,
    //                OscType::Noise => Op::Noise,
    //            };
    //            Op::Compose {
    //                operations: vec![
    //                    osc_op,
    //                    Op::TransposeM { m: self.fm },
    //                    Op::TransposeA { a: self.fa },
    //                    Op::PanM { m: self.pm },
    //                    Op::PanA { a: self.pa },
    //                    Op::Gain { m: self.g },
    //                    Op::Length { m: self.l },
    //                ],
    //            }
    //        }
}

impl NormalForm {
    /// Creates a NormalForm with a single PointOp in the operations
    /// and sets the appropriate length_ratio.
    pub fn init() -> NormalForm {
        NormalForm {
            operations: vec![vec![PointOp::init()]],
            length_ratio: Ratio::new(1, 1),
        }
    }

    /// Creates a NormalForm with empty operations
    /// and set the length_ratio to zero.
    pub fn init_empty() -> NormalForm {
        NormalForm {
            operations: vec![],
            length_ratio: Ratio::new(0, 1),
        }
    }

    /// Applys function 'f' to every PointOp in the NormalForm
    pub fn fmap_mut(&mut self, f: impl Fn(&mut PointOp)) {
        for voice in self.operations.iter_mut() {
            for point_op in voice {
                f(point_op)
            }
        }
    }

    /// Applys function 'f' to every PointOp in the NormalForm
    /// with an impl FnMut allows a mutable function to be passed in.
    pub fn fmap_with_state(&self, mut f: impl FnMut(&PointOp)) {
        for voice in self.operations.iter() {
            for point_op in voice {
                f(point_op)
            }
        }
    }

    /// Given a name, solos that name by calling op.silence() on every op
    /// that doesn't have that name in their NameSet.
    pub fn solo_ops_by_name(&mut self, name: &str) {
        self.fmap_mut(|op: &mut PointOp| {
            if !op.names.contains(name) {
                op.silence();
            };
        })
    }

    /// Returns all the names that exist in the NormalForm
    pub fn names(&self) -> HashSet<String> {
        let mut result = HashSet::new();
        self.fmap_with_state(|op| {
            for name in op.names.to_vec() {
                result.insert(name.clone());
            }
        });

        result
    }

    pub fn partition(&self, name: String) -> (NormalForm, NormalForm) {
        let silence = PointOp::init_silent();
        let mut named = NormalForm::init_empty();
        let mut rest = NormalForm::init_empty();

        for seq in self.operations.iter() {
            let elem_with_name = seq.iter().find(|&p_op| p_op.names.contains(&name));

            let mut named_seq = vec![];
            let mut rest_seq = vec![];
            match elem_with_name {
                Some(_) => {
                    for p_op in seq {
                        let mut name_op: PointOp;
                        let mut rest_op: PointOp;
                        if p_op.names.contains(&name) {
                            name_op = p_op.clone();
                            rest_op = p_op.clone() * silence.clone();
                            rest_op.fa = Rational64::new(0, 1);
                            rest_op.pa = Rational64::new(0, 1);
                        } else {
                            name_op = p_op.clone() * silence.clone();
                            name_op.fa = Rational64::new(0, 1);
                            name_op.pa = Rational64::new(0, 1);
                            rest_op = p_op.clone();
                        }

                        named_seq.push(name_op);
                        rest_seq.push(rest_op);
                    }

                    named.operations.push(named_seq);
                    rest.operations.push(rest_seq);
                }
                None => {
                    rest_seq = seq.clone();
                    rest.operations.push(rest_seq);
                }
            }
        }

        named.length_ratio = self.length_ratio;
        rest.length_ratio = self.length_ratio;

        (named, rest)
    }
}

#[cfg(test)]
mod normalize_tests;