culsynth 0.1.0

Culsynth DSP Library
Documentation
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
use super::osc::PhaseFxP;
use super::*;
use crate::fixedmath::apply_scalar_i;
use core::mem::transmute;
use core::option::Option;
use rand::{rngs::SmallRng, RngCore, SeedableRng};

/// Default random seed to use if not provided a seed
const RANDOM_SEED: u64 = 0xce607a9d25ec3d88u64; //random 64 bit integer

#[repr(transparent)]
#[derive(Clone, Copy)]
/// A struct to package together the various LFO configuration options in one
/// convenient struct that fits in 16 bits.  We could get away with packing
/// it in 8 bits, but we'll use 16 to allow for future expansion
pub struct LfoOptions {
    bits: u16,
}

impl LfoOptions {
    const BIPOLAR: u16 = 1 << 8;
    const RETRIGGER: u16 = 1 << 9;
    /// The LFO Waveform (Sine, Square, Sample+Hold, etc.)
    pub fn wave(&self) -> Option<LfoWave> {
        let value = (self.bits & 0xFF) as u8;
        LfoWave::try_from(value).ok()
    }
    /// Is this LFO unipolar (0:1) or bipolar (-1:1)?
    pub fn bipolar(&self) -> bool {
        self.bits & Self::BIPOLAR != 0
    }
    /// Does this LFO retrigger/reset on each gate?
    pub fn retrigger(&self) -> bool {
        self.bits & Self::RETRIGGER != 0
    }
    /// Pack the LFO parameters into a `LfoOptions` value
    pub fn new(wave: LfoWave, bipolar: bool, retrigger: bool) -> Self {
        LfoOptions {
            bits: (wave as u16)
                | if bipolar { Self::BIPOLAR } else { 0 }
                | if retrigger { Self::RETRIGGER } else { 0 },
        }
    }
}

impl Default for LfoOptions {
    /// The default value is a bipolar, retriggering sine wave
    fn default() -> Self {
        Self::new(LfoWave::Sine, true, true)
    }
}

#[derive(Default, Clone, Copy)]
#[repr(u8)]
/// The LFO waveform in use
pub enum LfoWave {
    /// Sine wave is default
    #[default]
    Sine,
    /// Square wave
    Square,
    /// Triangle wave
    Triangle,
    /// Sawtooth wave
    Saw,
    /// Sample and Hold
    SampleHold,
    /// Sample and Glide
    SampleGlide,
}

impl LfoWave {
    const ELEM: [LfoWave; 6] = [
        Self::Sine,
        Self::Square,
        Self::Triangle,
        Self::Saw,
        Self::SampleHold,
        Self::SampleGlide,
    ];
    /// Returns a slice to all of the possible LfoWaves
    pub const fn waves() -> &'static [LfoWave] {
        &Self::ELEM
    }
    /// Provides the name of the waveform (long-format)
    pub const fn to_str(&self) -> &'static str {
        [
            "Sine",
            "Square",
            "Triangle",
            "Saw",
            "Sample & Hold",
            "Sample & Glide",
        ][*self as usize]
    }
    /// Provides the name of the waveform (long-format)
    ///
    /// This is a single character (for waveforms with unicode representations)
    /// or up to three character abbreviation (e.g. "S+H")
    pub const fn to_str_short(&self) -> &'static str {
        [
            crate::util::SIN_CHARSTR,
            crate::util::SQ_CHARSTR,
            crate::util::TRI_CHARSTR,
            crate::util::SAW_CHARSTR,
            "S+H",
            "S+G",
        ][*self as usize]
    }
}

impl From<LfoWave> for &'static str {
    fn from(value: LfoWave) -> Self {
        value.to_str()
    }
}

impl TryFrom<u8> for LfoWave {
    type Error = &'static str;
    fn try_from(value: u8) -> Result<Self, &'static str> {
        if value >= LfoWave::Sine as u8 && value <= LfoWave::SampleGlide as u8 {
            unsafe { Ok(transmute::<u8, LfoWave>(value)) }
        } else {
            Err("Conversion of u8 to LfoWave Overflowed")
        }
    }
}

/// A struct packaging together several slices to act as parameters for an LFO
pub struct LfoParams<'a, Smp: Float> {
    /// The frequency of the LFO, in Hz
    pub freq: &'a [Smp],
    /// The depth of oscillation, between 0 and 1
    pub depth: &'a [Smp],
    /// The options, including waveform and retriggering (see [LfoOptions])
    pub opts: &'a [LfoOptions],
}

impl<'a, Smp: Float> LfoParams<'a, Smp> {
    /// The length of this parameter pack is defined as the length of the
    /// smallest subslice
    pub fn len(&self) -> usize {
        min_size(&[self.freq.len(), self.depth.len(), self.opts.len()])
    }
    /// True if any slice is zero length
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// A mutable LFO parameter pack (see [LfoParams])
pub struct MutLfoParams<'a, Smp: Float> {
    /// The frequency of the LFO, in Hz
    pub freq: &'a mut [Smp],
    /// The depth of oscillation, between 0 and 1
    pub depth: &'a mut [Smp],
    /// The options, including waveform and retriggering (see [LfoOptions])
    pub opts: &'a mut [LfoOptions],
}

impl<'a, Smp: Float> MutLfoParams<'a, Smp> {
    /// The length of this parameter pack is defined as the length of the
    /// smallest subslice
    pub fn len(&self) -> usize {
        min_size(&[self.freq.len(), self.depth.len(), self.opts.len()])
    }
    /// True if any slice is zero length
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'a, Smp: Float> From<MutLfoParams<'a, Smp>> for LfoParams<'a, Smp> {
    fn from(value: MutLfoParams<'a, Smp>) -> Self {
        Self {
            freq: value.freq,
            depth: value.depth,
            opts: value.opts,
        }
    }
}

/// A floating-point LFO
#[derive(Clone)]
pub struct Lfo<Smp> {
    outbuf: BufferT<Smp>,
    rng: SmallRng,
    phase: Smp,
    rand_smps: [Smp; 2],
    last_gate: bool,
}

impl<Smp: Float> Lfo<Smp> {
    /// Constructor
    pub fn new(seed: u64) -> Self {
        let mut retval = Self {
            outbuf: [Smp::ZERO; STATIC_BUFFER_SIZE],
            rng: SmallRng::seed_from_u64(seed),
            phase: Smp::ZERO,
            rand_smps: [Smp::ZERO; 2],
            last_gate: false,
        };
        retval.update_rands();
        retval.update_rands();
        retval
    }
    fn update_rands(&mut self) {
        self.rand_smps[1] = self.rand_smps[0];
        let rand_num = self.rng.next_u32() & (u16::MAX as u32);
        self.rand_smps[0] = Smp::from_u16(rand_num as u16) / Smp::from_u16(u16::MAX);
    }
    /// Generate the LFO signal
    ///
    /// Note: The output slice from this function may be shorter than the
    /// input slices.  Callers must check the number of returned samples and
    /// copy them into their own output buffers before calling this function
    /// again to process the remainder of the data.
    pub fn process(&mut self, ctx: &Context<Smp>, gate: &[Smp], params: LfoParams<Smp>) -> &[Smp] {
        let freq = params.freq;
        let depth = params.depth;
        let opts = params.opts;
        let numsamples = min_size(&[
            freq.len(),
            gate.len(),
            opts.len(),
            depth.len(),
            STATIC_BUFFER_SIZE,
        ]);
        for i in 0..numsamples {
            let this_gate = gate[i] > Smp::ONE_HALF;
            if opts[i].retrigger() && this_gate && !self.last_gate {
                self.phase = Smp::ZERO;
            }
            self.last_gate = this_gate;
            //generate waveforms (piecewise defined)
            let frac_2phase_pi = (self.phase + self.phase) / Smp::PI();
            let mut value = match opts[i].wave().unwrap_or_default() {
                LfoWave::Saw => frac_2phase_pi / Smp::TWO,
                LfoWave::Square => {
                    if self.phase < Smp::ZERO {
                        Smp::ONE.neg()
                    } else {
                        Smp::ONE
                    }
                }
                LfoWave::Triangle => {
                    if self.phase < Smp::FRAC_PI_2().neg() {
                        frac_2phase_pi.neg() - Smp::TWO
                    } else if self.phase > Smp::FRAC_PI_2() {
                        Smp::TWO - frac_2phase_pi
                    } else {
                        frac_2phase_pi
                    }
                }
                LfoWave::Sine => {
                    if self.phase < Smp::FRAC_PI_2().neg() {
                        // phase in [-pi, pi/2)
                        // Use the identity sin(x) = -cos(x+pi/2) since our taylor series
                        // approximations are centered about zero and this will be more accurate
                        Smp::fcos(self.phase + Smp::FRAC_PI_2()).neg()
                    } else if self.phase < Smp::FRAC_PI_2() {
                        // phase in [pi/2, pi)
                        // sin(x) = cos(x-pi/2)
                        Smp::fcos(self.phase - Smp::FRAC_PI_2())
                    } else {
                        Smp::fsin(self.phase)
                    }
                }
                LfoWave::SampleHold => self.rand_smps[0],
                LfoWave::SampleGlide => {
                    self.rand_smps[0] + (frac_2phase_pi * (self.rand_smps[1] - self.rand_smps[0]))
                }
            };
            if !opts[i].bipolar() {
                value = (value + Smp::ONE) / Smp::TWO;
            }
            self.outbuf[i] = value * depth[i];
            let phase_per_sample = (freq[i] * Smp::TAU()) / ctx.sample_rate;
            self.phase = self.phase + phase_per_sample;
            // Check if we've crossed from positive phase back to negative:
            if self.phase >= Smp::PI() {
                self.phase = self.phase - Smp::TAU();
                self.update_rands();
            }
        }
        &self.outbuf[0..numsamples]
    }
}

impl<Smp: Float> Default for Lfo<Smp> {
    fn default() -> Self {
        Self::new(RANDOM_SEED)
    }
}

/// A fixed-point LFO parameter pack
pub struct LfoParamsFxP<'a> {
    /// The frequency of the LFO, in Hz
    pub freq: &'a [LfoFreqFxP],
    /// The depth of oscillation, as a number between 0 and 1
    pub depth: &'a [ScalarFxP],
    /// The options for this LFO (see [LfoOptions])
    pub opts: &'a [LfoOptions],
}

impl<'a> LfoParamsFxP<'a> {
    /// The length of this parameter pack is defined as the length of the
    /// smallest subslice
    pub fn len(&self) -> usize {
        min_size(&[self.freq.len(), self.depth.len(), self.opts.len()])
    }
    /// Returns true if any subslice is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// A mutable fixed-point LFO parameter pack (see [LfoParamsFxP])
pub struct MutLfoParamsFxP<'a> {
    /// The frequency of the LFO, in Hz
    pub freq: &'a mut [LfoFreqFxP],
    /// The depth of oscillation, as a number between 0 and 1
    pub depth: &'a mut [ScalarFxP],
    /// The options for this LFO (see [LfoOptions])
    pub opts: &'a mut [LfoOptions],
}

impl<'a> MutLfoParamsFxP<'a> {
    /// The length of this parameter pack is defined as the length of the
    /// smallest subslice
    pub fn len(&self) -> usize {
        min_size(&[self.freq.len(), self.depth.len(), self.opts.len()])
    }
    /// Returns true if any subslice is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'a> From<MutLfoParamsFxP<'a>> for LfoParamsFxP<'a> {
    fn from(value: MutLfoParamsFxP<'a>) -> Self {
        Self {
            freq: value.freq,
            depth: value.depth,
            opts: value.opts,
        }
    }
}

/// A fixed-point LFO:
#[derive(Clone)]
pub struct LfoFxP {
    outbuf: BufferT<SampleFxP>,
    rng: SmallRng,
    rand_smps: [SampleFxP; 2],
    phase: PhaseFxP,
    last_gate: bool,
}

impl LfoFxP {
    /// Constructor
    pub fn new(seed: u64) -> LfoFxP {
        let mut retval = LfoFxP {
            outbuf: [SampleFxP::ZERO; STATIC_BUFFER_SIZE],
            rng: SmallRng::seed_from_u64(seed),
            rand_smps: [SampleFxP::ZERO; 2],
            phase: PhaseFxP::ZERO,
            last_gate: false,
        };
        retval.update_rands();
        retval.update_rands();
        retval
    }
    fn update_rands(&mut self) {
        self.rand_smps[1] = self.rand_smps[0];
        let rand_num = self.rng.next_u32() & (u16::MAX as u32);
        let rand_scalar = ScalarFxP::from_bits(rand_num as u16);
        self.rand_smps[0] = SampleFxP::from_num(rand_scalar);
    }
    /// Generate the LFO signal
    ///
    /// Note: The output slice from this function may be shorter than the
    /// input slices.  Callers must check the number of returned samples and
    /// copy them into their own output buffers before calling this function
    /// again to process the remainder of the data.
    pub fn process(
        &mut self,
        ctx: &ContextFxP,
        gate: &[SampleFxP],
        params: LfoParamsFxP,
    ) -> &[SampleFxP] {
        let freq = params.freq;
        let depth = params.depth;
        let opts = params.opts;
        let numsamples = min_size(&[
            freq.len(),
            gate.len(),
            opts.len(),
            depth.len(),
            STATIC_BUFFER_SIZE,
        ]);
        const FRAC_2_PI: ScalarFxP = ScalarFxP::lit("0x0.a2fa");
        const TWO: SampleFxP = SampleFxP::lit("2");
        const ONE_HALF: SampleFxP = SampleFxP::lit("0.5");
        for i in 0..numsamples {
            let this_gate = gate[i] > ONE_HALF;
            if opts[i].retrigger() && this_gate && !self.last_gate {
                self.phase = PhaseFxP::ZERO;
            }
            self.last_gate = this_gate;
            //generate waveforms (piecewise defined)
            let frac_2phase_pi = apply_scalar_i(SampleFxP::from_num(self.phase), FRAC_2_PI);
            let mut value = match opts[i].wave().unwrap_or_default() {
                LfoWave::Saw => frac_2phase_pi.unwrapped_shr(1),
                LfoWave::Square => {
                    if self.phase < 0 {
                        SampleFxP::NEG_ONE
                    } else {
                        SampleFxP::ONE
                    }
                }
                LfoWave::Triangle => {
                    if self.phase < PhaseFxP::FRAC_PI_2.unwrapped_neg() {
                        frac_2phase_pi.unwrapped_neg() - TWO
                    } else if self.phase > PhaseFxP::FRAC_PI_2 {
                        TWO - frac_2phase_pi
                    } else {
                        frac_2phase_pi
                    }
                }
                LfoWave::Sine => {
                    if self.phase < PhaseFxP::FRAC_PI_2.unwrapped_neg() {
                        // phase in [-pi, pi/2)
                        // Use the identity sin(x) = -cos(x+pi/2) since our taylor series
                        // approximations are centered about zero and this will be more accurate
                        fixedmath::cos_fixed(SampleFxP::from_num(self.phase + PhaseFxP::FRAC_PI_2))
                            .unwrapped_neg()
                    } else if self.phase < PhaseFxP::FRAC_PI_2 {
                        // phase in [pi/2, pi)
                        // sin(x) = cos(x-pi/2)
                        fixedmath::cos_fixed(SampleFxP::from_num(self.phase - PhaseFxP::FRAC_PI_2))
                    } else {
                        fixedmath::sin_fixed(SampleFxP::from_num(self.phase))
                    }
                }
                LfoWave::SampleHold => self.rand_smps[0],
                LfoWave::SampleGlide => {
                    self.rand_smps[0]
                        + SampleFxP::from_num(
                            SampleFxP::from_num(frac_2phase_pi)
                                .wide_mul(self.rand_smps[1] - self.rand_smps[0]),
                        )
                }
            };
            if !opts[i].bipolar() {
                value = (value + SampleFxP::ONE).unwrapped_shr(1);
            }
            self.outbuf[i] = SampleFxP::from_num(value.wide_mul_unsigned(depth[i]));
            let phase_per_sample = PhaseFxP::from_num(
                freq[i]
                    .wide_mul(ctx.sample_rate.frac_2pi4096_sr())
                    .unwrapped_shr(12),
            );
            self.phase += phase_per_sample;
            // Check if we've crossed from positive phase back to negative:
            if self.phase >= PhaseFxP::PI {
                self.phase -= PhaseFxP::TAU;
                self.update_rands();
            }
        }
        &self.outbuf[0..numsamples]
    }
}

impl Default for LfoFxP {
    fn default() -> Self {
        Self::new(RANDOM_SEED)
    }
}