oxisynth 0.1.0

Rust soundfont synthesizer
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
use super::super::channel_pool::Channel;
use super::super::conv::{concave, convex};
use super::super::voice_pool::Voice;

use super::generator::GeneratorType;

use soundfont::raw::{
    ControllerPalette, GeneralPalette, Modulator as SFModulator, ModulatorSource,
    ModulatorTransform, SourceDirection, SourcePolarity, SourceType,
};

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Mod {
    pub dest: GeneratorType,
    pub amount: f64,

    pub src: ModulatorSource,
    pub src2: ModulatorSource,
}

impl Mod {
    pub const fn const_from(mod_src: &SFModulator) -> Self {
        let mut amount = mod_src.amount as f64;

        let dest = mod_src.dest; // index of controlled generator

        if let SourceType::Unknown(_) = mod_src.src.ty {
            // This shouldn't happen - unknown type!
            // Deactivate the modulator by setting the amount to 0.
            amount = 0.0;
        }

        if let SourceType::Unknown(_) = mod_src.amt_src.ty {
            // This shouldn't happen - unknown type!
            // Deactivate the modulator by setting the amount to 0.
            amount = 0.0;
        }

        // SF2.01 only uses the 'linear' transform (0).
        // Deactivate the modulator by setting the amount to 0 in any other case.
        if mod_src.transform as u8 != ModulatorTransform::Linear as u8 {
            amount = 0.0;
        }

        Self {
            src: mod_src.src,
            amount,
            dest: GeneratorType::const_try_from(dest as u8).unwrap(), /* index of controlled generator */
            src2: mod_src.amt_src,
        }
    }
}

impl From<&SFModulator> for Mod {
    fn from(mod_src: &SFModulator) -> Self {
        Self::const_from(mod_src)
    }
}

impl Default for Mod {
    fn default() -> Self {
        Self {
            dest: GeneratorType::StartAddrOfs,
            src: 0.into(),
            src2: 0.into(),
            amount: 0.0,
        }
    }
}

impl Mod {
    pub fn get_dest(&self) -> GeneratorType {
        self.dest
    }

    pub fn get_value(&self, chan: &Channel, voice: &Voice) -> f32 {
        // 'special treatment' for default controller
        //
        //  Reference: SF2.01 section 8.4.2
        //
        // The GM default controller 'vel-to-filter cut off' is not clearly
        // defined: If implemented according to the specs, the filter
        // frequency jumps between vel=63 and vel=64.  To maintain
        // compatibility with existing sound fonts, the implementation is
        // 'hardcoded', it is impossible to implement using only one
        // modulator otherwise.
        //
        // I assume here, that the 'intention' of the paragraph is one
        // octave (1200 cents) filter frequency shift between vel=127 and
        // vel=64.  'amount' is (-2400), at least as long as the controller
        // is set to default.
        //
        // Further, the 'appearance' of the modulator (source enumerator,
        // destination enumerator, flags etc) is different from that
        // described in section 8.4.2, but it matches the definition used in
        // several SF2.1 sound fonts (where it is used only to turn it off).
        if self.src.controller_palette == ControllerPalette::General(GeneralPalette::NoteOnVelocity)
            && self.src.is_unipolar()
            && self.src.is_negative()
            && self.src.is_linear()
            && self.src2.controller_palette
                == ControllerPalette::General(GeneralPalette::NoteOnVelocity)
            && self.src2.is_unipolar()
            && self.src2.is_positive()
            && self.src2.is_switch()
            && self.dest == GeneratorType::FilterFc
        {
            return 0.0;
        }

        let mut range1: f32 = 127.0f32;
        // get the initial value of the first source
        let mut v1 = if self.src.index > 0 {
            use GeneralPalette::*;
            let v1 = match self.src.controller_palette {
                ControllerPalette::Midi(id) => chan.cc(id as usize) as f32,
                ControllerPalette::General(g) => match g {
                    NoController => range1,
                    NoteOnVelocity => voice.vel() as f32,
                    NoteOnKeyNumber => voice.key() as f32,
                    PolyPressure => chan.key_pressure(voice.key() as usize) as f32,
                    ChannelPressure => chan.channel_pressure() as f32,
                    PitchWheel => {
                        range1 = 0x4000 as f32;
                        chan.pitch_bend() as f32
                    }
                    PitchWheelSensitivity => chan.pitch_wheel_sensitivity() as f32,
                    _ => 0.0,
                },
            };

            use SourceDirection::*;
            use SourcePolarity::*;
            use SourceType::*;

            match (self.src.ty, self.src.polarity, self.src.direction) {
                // 0
                (Linear, Unipolar, Positive) => v1 / range1,
                // 1
                (Linear, Unipolar, Negative) => 1.0 - v1 / range1,
                // 2
                (Linear, Bipolar, Positive) => -1.0 + 2.0 * v1 / range1,
                // 3
                (Linear, Bipolar, Negative) => 1.0 - 2.0 * v1 / range1,

                // 4
                (Concave, Unipolar, Positive) => concave(v1),
                // 5
                (Concave, Unipolar, Negative) => concave(127.0 - v1),
                // 6
                (Concave, Bipolar, Positive) => {
                    if v1 > 64.0 {
                        concave(2.0 * (v1 - 64.0))
                    } else {
                        -concave(2.0 * (64.0 - v1))
                    }
                }
                // 7
                (Concave, Bipolar, Negative) => {
                    if v1 > 64.0 {
                        -concave(2.0 * (v1 - 64.0))
                    } else {
                        concave(2.0 * (64.0 - v1))
                    }
                }

                // 8
                (Convex, Unipolar, Positive) => convex(v1),
                // 9
                (Convex, Unipolar, Negative) => convex(127.0 - v1),
                // 10
                (Convex, Bipolar, Positive) => {
                    if v1 > 64.0 {
                        convex(2.0 * (v1 - 64.0))
                    } else {
                        -convex(2.0 * (64.0 - v1))
                    }
                }
                // 11
                (Convex, Bipolar, Negative) => {
                    if v1 > 64.0 {
                        -convex(2.0 * (v1 - 64.0))
                    } else {
                        convex(2.0 * (64.0 - v1))
                    }
                }

                // 12
                (Switch, Unipolar, Positive) => {
                    if v1 >= 64.0 {
                        1.0
                    } else {
                        0.0
                    }
                }
                // 13
                (Switch, Unipolar, Negative) => {
                    if v1 >= 64.0 {
                        0.0
                    } else {
                        1.0
                    }
                }
                // 14
                (Switch, Bipolar, Positive) => {
                    if v1 >= 64.0 {
                        1.0
                    } else {
                        -1.0
                    }
                }
                // 15
                (Switch, Bipolar, Negative) => {
                    if v1 >= 64.0 {
                        -1.0
                    } else {
                        1.0
                    }
                }

                _ => v1,
            }
        } else {
            return 0.0;
        };

        // no need to go further
        if v1 == 0.0 {
            return 0.0;
        }

        let range2: f32 = 127.0f32;
        // get the second input source
        let v2 = if self.src2.index > 0 {
            use GeneralPalette::*;
            let v2 = match self.src2.controller_palette {
                ControllerPalette::Midi(id) => chan.cc(id as usize) as f32,
                ControllerPalette::General(g) => match g {
                    NoController => range2,
                    NoteOnVelocity => voice.vel() as f32,
                    NoteOnKeyNumber => voice.key() as f32,
                    PolyPressure => chan.key_pressure(voice.key() as usize) as f32,
                    ChannelPressure => chan.channel_pressure() as f32,
                    PitchWheel => chan.pitch_bend() as f32,
                    PitchWheelSensitivity => chan.pitch_wheel_sensitivity() as f32,
                    _ => {
                        // https://github.com/divideconcept/FluidLite/blob/fdd05bad03cdb24d1f78b5fe3453842890c1b0e8/src/fluid_mod.c#L282
                        // why is this setting v1 to 0.0?
                        v1 = 0.0;
                        1.0
                    }
                },
            };

            use SourceDirection::*;
            use SourcePolarity::*;
            use SourceType::*;

            // transform the second input value

            match (self.src2.ty, self.src2.polarity, self.src2.direction) {
                // 0
                (Linear, Unipolar, Positive) => v2 / range2,
                // 1
                (Linear, Unipolar, Negative) => 1.0 - v2 / range2,
                // 2
                (Linear, Bipolar, Positive) => -1.0 + 2.0 * v2 / range2,
                // 3
                (Linear, Bipolar, Negative) => -1.0 + 2.0 * v2 / range2,

                // 4
                (Concave, Unipolar, Positive) => concave(v2),
                // 5
                (Concave, Unipolar, Negative) => concave(127.0 - v2),
                // 6
                (Concave, Bipolar, Positive) => {
                    if v2 > 64.0 {
                        concave(2.0 * (v2 - 64.0))
                    } else {
                        -concave(2.0 * (64.0 - v2))
                    }
                }
                // 7
                (Concave, Bipolar, Negative) => {
                    if v2 > 64.0 {
                        -concave(2.0 * (v2 - 64.0))
                    } else {
                        concave(2.0 * (64.0 - v2))
                    }
                }

                // 8
                (Convex, Unipolar, Positive) => convex(v2),
                // 9
                (Convex, Unipolar, Negative) => 1.0 - convex(v2),
                // 10
                (Convex, Bipolar, Positive) => {
                    if v2 > 64.0 {
                        -convex(2.0 * (v2 - 64.0))
                    } else {
                        convex(2.0 * (64.0 - v2))
                    }
                }
                // 11
                (Convex, Bipolar, Negative) => {
                    if v2 > 64.0 {
                        -convex(2.0 * (v2 - 64.0))
                    } else {
                        convex(2.0 * (64.0 - v2))
                    }
                }

                // 12
                (Switch, Unipolar, Positive) => {
                    if v2 >= 64.0 {
                        1.0
                    } else {
                        0.0
                    }
                }
                // 13
                (Switch, Unipolar, Negative) => {
                    if v2 >= 64.0 {
                        0.0
                    } else {
                        1.0
                    }
                }
                // 14
                (Switch, Bipolar, Positive) => {
                    if v2 >= 64.0 {
                        1.0
                    } else {
                        -1.0
                    }
                }
                // 15
                (Switch, Bipolar, Negative) => {
                    if v2 >= 64.0 {
                        -1.0
                    } else {
                        1.0
                    }
                }

                _ => v2,
            }
        } else {
            1.0
        };

        self.amount as f32 * v1 * v2
    }

    pub fn test_identity(&self, mod2: &Mod) -> bool {
        if self.dest != mod2.dest {
            return false;
        }

        if self.src != mod2.src {
            return false;
        }

        self.src2 == mod2.src2
    }
}

pub mod default {
    use super::Mod;
    use soundfont::raw::{default_modulators, GeneratorType};

    /// 8.4.1  MIDI Note-On Velocity to Initial Attenuation
    pub const DEFAULT_VEL2ATT_MOD: Mod = Mod::const_from(&default_modulators::DEFAULT_VEL2ATT_MOD);

    /// 8.4.2  MIDI Note-On Velocity to Filter Cutoff
    pub const DEFAULT_VEL2FILTER_MOD: Mod =
        Mod::const_from(&default_modulators::DEFAULT_VEL2FILTER_MOD);

    /// 8.4.3  MIDI Channel Pressure to Vibrato LFO Pitch Depth
    pub const DEFAULT_AT2VIBLFO_MOD: Mod =
        Mod::const_from(&default_modulators::DEFAULT_AT2VIBLFO_MOD);

    /// 8.4.4  MIDI Continuous Controller 1 to Vibrato LFO Pitch Depth
    pub const DEFAULT_MOD2VIBLFO_MOD: Mod =
        Mod::const_from(&default_modulators::DEFAULT_MOD2VIBLFO_MOD);

    /// 8.4.5  MIDI Continuous Controller 7 to Initial Attenuation
    pub const DEFAULT_ATT_MOD: Mod = Mod::const_from(&default_modulators::DEFAULT_ATT_MOD);

    /// 8.4.6  MIDI Continuous Controller 10 to Pan Position
    pub const DEFAULT_PAN_MOD: Mod = Mod::const_from(&default_modulators::DEFAULT_PAN_MOD);

    /// 8.4.7  MIDI Continuous Controller 11 to Initial Attenuation
    pub const DEFAULT_EXPR_MOD: Mod = Mod::const_from(&default_modulators::DEFAULT_EXPR_MOD);

    /// 8.4.8  MIDI Continuous Controller 91 to Reverb Effects Send
    pub const DEFAULT_REVERB_MOD: Mod = Mod::const_from(&default_modulators::DEFAULT_REVERB_MOD);

    /// 8.4.9  MIDI Continuous Controller 93 to Chorus Effects Send
    pub const DEFAULT_CHORUS_MOD: Mod = Mod::const_from(&default_modulators::DEFAULT_CHORUS_MOD);

    /// 8.4.10  MIDI Pitch Wheel to Initial Pitch Controlled by MIDI Pitch Wheel Sensitivity
    ///
    /// GeneratorType::Unused5 (59) corresponds to gen::GenParam::Pitch (59)
    pub const DEFAULT_PITCH_BEND_MOD: Mod = Mod::const_from(
        &default_modulators::default_pitch_bend_mod(GeneratorType::Unused5),
    );
}