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
use super::signal::*;
use super::utils::RingBuffer;
use crate::{as_any_mut, std_signal};
use std::any::Any;
use std::ops::{Index, IndexMut};

/// The `Union` module holds a vector of oscillators and plays one based on the
/// active tag. The `level` field is used to set the volume of whichever signal
/// is playing.
#[derive(Clone)]
pub struct Union {
    tag: Tag,
    waves: Vec<Tag>,
    active: Tag,
    level: In,
}

impl Union {
    pub fn new(waves: Vec<Tag>) -> Self {
        let active = waves[0];
        Union {
            tag: mk_tag(),
            waves,
            active,
            level: 1.into(),
        }
    }

    pub fn waves(&mut self, arg: Vec<Tag>) -> &mut Self {
        self.waves = arg;
        self
    }

    pub fn active(&mut self, arg: Tag) -> &mut Self {
        self.active = arg;
        self
    }

    pub fn level<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.level = arg.into();
        self
    }
}

impl Builder for Union {}

impl Signal for Union {
    std_signal!();
    fn signal(&mut self, rack: &Rack, _sample_rate: Real) -> Real {
        In::val(rack, self.level) * rack.output(self.active)
    }
}

impl Index<usize> for Union {
    type Output = Tag;

    fn index(&self, index: usize) -> &Self::Output {
        &self.waves[index]
    }
}

impl IndexMut<usize> for Union {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.waves[index]
    }
}
/// `Product` multiplies the signals of a vector of synth modules.
#[derive(Clone)]
pub struct Product {
    tag: Tag,
    waves: Vec<Tag>,
}

impl Product {
    pub fn new(waves: Vec<Tag>) -> Self {
        Product {
            tag: mk_tag(),
            waves,
        }
    }

    pub fn waves(&mut self, arg: Vec<Tag>) -> &mut Self {
        self.waves = arg;
        self
    }
}

impl Builder for Product {}

impl Signal for Product {
    std_signal!();
    fn signal(&mut self, rack: &Rack, _sample_rate: Real) -> Real {
        self.waves.iter().fold(1.0, |acc, n| acc * rack.output(*n))
    }
}

impl Index<usize> for Product {
    type Output = Tag;

    fn index(&self, index: usize) -> &Self::Output {
        &self.waves[index]
    }
}

impl IndexMut<usize> for Product {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.waves[index]
    }
}

/// "Voltage controlled amplifier" multiplies the volume of the `wave` by the
/// value of `level`.
#[derive(Copy, Clone)]
pub struct Vca {
    tag: Tag,
    wave: Tag,
    level: In,
}

impl Vca {
    pub fn new(wave: Tag) -> Self {
        Self {
            tag: mk_tag(),
            wave,
            level: 1.into(),
        }
    }

    pub fn wave(&mut self, arg: Tag) -> &mut Self {
        self.wave = arg;
        self
    }

    pub fn level<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.level = arg.into();
        self
    }
}

impl Builder for Vca {}

impl Signal for Vca {
    std_signal!();
    fn signal(&mut self, rack: &Rack, _sample_rate: Real) -> Real {
        rack.output(self.wave) * In::val(rack, self.level)
    }
}

impl Index<&str> for Vca {
    type Output = In;

    fn index(&self, index: &str) -> &Self::Output {
        match index {
            "level" => &self.level,
            _ => panic!("Vca does not have a field named: {}", index),
        }
    }
}

impl IndexMut<&str> for Vca {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        match index {
            "level" => &mut self.level,
            _ => panic!("Vca does not have a field named: {}", index),
        }
    }
}

/// Mixer with individual attenuverters for each wave plus an overall attenuverter.
#[derive(Clone)]
pub struct Mixer {
    tag: Tag,
    waves: Vec<Tag>,
    levels: Vec<In>,
    level: In,
}

impl Mixer {
    pub fn new(waves: Vec<Tag>) -> Self {
        let levels = waves.iter().map(|_| 1.into()).collect();
        Mixer {
            tag: mk_tag(),
            waves,
            levels,
            level: 1.into(),
        }
    }

    pub fn waves(&mut self, arg: Vec<Tag>) -> &mut Self {
        self.waves = arg;
        self.levels.resize_with(self.waves.len(), || 0.5.into());
        self
    }

    pub fn levels<T: Into<In>>(&mut self, arg: Vec<T>) -> &mut Self {
        assert_eq!(
            arg.len(),
            self.waves.len(),
            "Levels must have same length as waves"
        );
        let v = arg.into_iter().map(|x| x.into());
        self.levels = v.collect();
        self
    }

    pub fn level<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.level = arg.into();
        self
    }

    pub fn level_nth<T: Into<In>>(&mut self, n: usize, arg: T) -> &mut Self {
        self.levels[n] = arg.into();
        self
    }
}

impl Builder for Mixer {}

impl Signal for Mixer {
    std_signal!();
    fn signal(&mut self, rack: &Rack, _sample_rate: Real) -> Real {
        self.waves.iter().enumerate().fold(0.0, |acc, (i, n)| {
            acc + rack.output(*n) * In::val(rack, self.levels[i])
        }) * In::val(rack, self.level)
    }
}

impl Index<usize> for Mixer {
    type Output = Tag;

    fn index(&self, index: usize) -> &Self::Output {
        &self.waves[index]
    }
}

impl IndexMut<usize> for Mixer {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.waves[index]
    }
}

/// A cross fade synth module, alpha = 0 means 100% wave 1.
#[derive(Copy, Clone)]
pub struct CrossFade {
    tag: Tag,
    wave1: In,
    wave2: In,
    alpha: In,
}

impl CrossFade {
    pub fn new(wave1: Tag, wave2: Tag) -> Self {
        CrossFade {
            tag: mk_tag(),
            wave1: wave1.into(),
            wave2: wave2.into(),
            alpha: (0.5).into(),
        }
    }

    pub fn wave1<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.wave1 = arg.into();
        self
    }

    pub fn wave2<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.wave2 = arg.into();
        self
    }

    pub fn alpha<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.alpha = arg.into();
        self
    }
}

impl Builder for CrossFade {}

impl Signal for CrossFade {
    std_signal!();
    fn signal(&mut self, rack: &Rack, _sample_rate: Real) -> Real {
        let alpha = In::val(rack, self.alpha);
        alpha * In::val(rack, self.wave2) + (1.0 - alpha) * In::val(rack, self.wave1)
    }
}

impl Index<&str> for CrossFade {
    type Output = In;

    fn index(&self, index: &str) -> &Self::Output {
        match index {
            "wave1" => &self.wave1,
            "wave2" => &self.wave2,
            "alpha" => &self.alpha,
            _ => panic!("CrossFade does not have a field named: {}", index),
        }
    }
}

impl IndexMut<&str> for CrossFade {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        match index {
            "wave1" => &mut self.wave1,
            "wave2" => &mut self.wave2,
            "alpha" => &mut self.alpha,
            _ => panic!("CrossFade does not have a field named: {}", index),
        }
    }
}

/// A `Modulator` is designed to be the input to the `hz` field of a carrier
/// wave. It takes control of the carriers frequency and modulates it's base
/// hz by adding mod_idx * mod_hz * output of modulator wave.
#[derive(Copy, Clone)]
pub struct Modulator {
    tag: Tag,
    wave: In,
    base_hz: In,
    mod_hz: In,
    mod_idx: In,
}

impl Modulator {
    pub fn new(wave: Tag) -> Self {
        Modulator {
            tag: mk_tag(),
            wave: wave.into(),
            base_hz: 0.into(),
            mod_hz: 0.into(),
            mod_idx: 0.into(),
        }
    }

    pub fn wave<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.wave = arg.into();
        self
    }

    pub fn base_hz<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.base_hz = arg.into();
        self
    }

    pub fn mod_hz<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.mod_hz = arg.into();
        self
    }

    pub fn mod_idx<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.mod_idx = arg.into();
        self
    }
}

impl Builder for Modulator {}

impl Signal for Modulator {
    std_signal!();
    fn signal(&mut self, rack: &Rack, _sample_rate: Real) -> Real {
        let mod_hz = In::val(rack, self.mod_hz);
        let mod_idx = In::val(rack, self.mod_idx);
        let base_hz = In::val(rack, self.base_hz);
        base_hz + mod_idx * mod_hz * In::val(rack, self.wave)
    }
}

impl Index<&str> for Modulator {
    type Output = In;

    fn index(&self, index: &str) -> &Self::Output {
        match index {
            "wave" => &self.wave,
            "base_hz" => &self.base_hz,
            "mod_hz" => &self.mod_hz,
            "mod_idx" => &self.mod_idx,
            _ => panic!("Modulator only does not have a field named:  {}", index),
        }
    }
}

impl IndexMut<&str> for Modulator {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        match index {
            "wave" => &mut self.wave,
            "base_hz" => &mut self.base_hz,
            "mod_hz" => &mut self.mod_hz,
            "mod_idx" => &mut self.mod_idx,
            _ => panic!("Modulator only does not have a field named:  {}", index),
        }
    }
}

/// A variable length delay line.
#[derive(Clone)]
pub struct Delay {
    tag: Tag,
    wave: Tag,
    delay_time: In,
    ring_buffer: RingBuffer<Real>,
}

impl Delay {
    pub fn new(wave: Tag, delay_time: In) -> Self {
        let ring = RingBuffer::<Real>::new(0.0, 0);
        Self {
            tag: mk_tag(),
            wave,
            delay_time,
            ring_buffer: ring,
        }
    }

    pub fn wave(&mut self, arg: Tag) -> &mut Self {
        self.wave = arg;
        self
    }

    pub fn delay_time<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.delay_time = arg.into();
        self
    }
}

impl Builder for Delay {}

impl Signal for Delay {
    std_signal!();
    fn signal(&mut self, rack: &Rack, sample_rate: Real) -> Real {
        let delay = In::val(rack, self.delay_time) * sample_rate;
        let rp = self.ring_buffer.read_pos;
        let wp = (delay + rp).ceil();
        self.ring_buffer.set_write_pos(wp as usize);
        self.ring_buffer.set_read_pos(wp - delay);
        if delay > self.ring_buffer.len() as Real - 3.0 {
            self.ring_buffer.resize(delay as usize + 3);
        }
        let val = rack.output(self.wave);
        self.ring_buffer.push(val);
        self.ring_buffer.get_cubic()
    }
}