summaryrefslogtreecommitdiff
path: root/quantum/audio/voices.c
blob: 4f511c93badefd6b49f146b8980eee48ecdddf84 (plain)
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
/* Copyright 2016 Jack Humbert
 * Copyright 2020 JohSchneider
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "voices.h"
#include "audio.h"
#include "timer.h"
#include <stdlib.h>
#include <math.h>

uint8_t note_timbre      = TIMBRE_DEFAULT;
bool    glissando        = false;
bool    vibrato          = false;
float   vibrato_strength = 0.5;
float   vibrato_rate     = 0.125;

uint16_t voices_timer = 0;

#ifdef AUDIO_VOICE_DEFAULT
voice_type voice = AUDIO_VOICE_DEFAULT;
#else
voice_type voice = default_voice;
#endif

void set_voice(voice_type v) {
    voice = v;
}

void voice_iterate(void) {
    voice = (voice + 1) % number_of_voices;
}

void voice_deiterate(void) {
    voice = (voice - 1 + number_of_voices) % number_of_voices;
}

#ifdef AUDIO_VOICES
float mod(float a, int b) {
    float r = fmod(a, b);
    return r < 0 ? r + b : r;
}

// Effect: 'vibrate' a given target frequency slightly above/below its initial value
float voice_add_vibrato(float average_freq) {
    float vibrato_counter = mod(timer_read() / (100 * vibrato_rate), VIBRATO_LUT_LENGTH);

    return average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
}

// Effect: 'slides' the 'frequency' from the starting-point, to the target frequency
float voice_add_glissando(float from_freq, float to_freq) {
    if (to_freq != 0 && from_freq < to_freq && from_freq < to_freq * pow(2, -440 / to_freq / 12 / 2)) {
        return from_freq * pow(2, 440 / from_freq / 12 / 2);
    } else if (to_freq != 0 && from_freq > to_freq && from_freq > to_freq * pow(2, 440 / to_freq / 12 / 2)) {
        return from_freq * pow(2, -440 / from_freq / 12 / 2);
    } else {
        return to_freq;
    }
}
#endif

float voice_envelope(float frequency) {
    // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz
//    __attribute__((unused)) uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency));
#ifdef AUDIO_VOICES
    uint16_t envelope_index    = timer_elapsed(voices_timer); // TODO: multiply in some factor?
    uint16_t compensated_index = envelope_index / 100;        // TODO: correct factor would be?
#endif

    switch (voice) {
        case default_voice:
            glissando = false;
            // note_timbre    = TIMBRE_50; //Note: leave the user the possibility to adjust the timbre with 'audio_set_timbre'
            break;

#ifdef AUDIO_VOICES

        case vibrating:
            glissando = false;
            vibrato   = true;
            break;

        case something:
            glissando = false;
            switch (compensated_index) {
                case 0 ... 9:
                    note_timbre = TIMBRE_12;
                    break;

                case 10 ... 19:
                    note_timbre = TIMBRE_25;
                    break;

                case 20 ... 200:
                    note_timbre = 12 + 12;
                    break;

                default:
                    note_timbre = 12;
                    break;
            }
            break;

        case drums:
            glissando = false;
            // switch (compensated_index) {
            //     case 0 ... 10:
            //         note_timbre = 50;
            //         break;
            //     case 11 ... 20:
            //         note_timbre = 50 * (21 - compensated_index) / 10;
            //         break;
            //     default:
            //         note_timbre = 0;
            //         break;
            // }
            // frequency = (rand() % (int)(frequency * 1.2 - frequency)) + (frequency * 0.8);

            if (frequency < 80.0) {
            } else if (frequency < 160.0) {
                // Bass drum: 60 - 100 Hz
                frequency = (rand() % (int)(40)) + 60;
                switch (envelope_index) {
                    case 0 ... 10:
                        note_timbre = 50;
                        break;
                    case 11 ... 20:
                        note_timbre = 50 * (21 - envelope_index) / 10;
                        break;
                    default:
                        note_timbre = 0;
                        break;
                }

            } else if (frequency < 320.0) {
                // Snare drum: 1 - 2 KHz
                frequency = (rand() % (int)(1000)) + 1000;
                switch (envelope_index) {
                    case 0 ... 5:
                        note_timbre = 50;
                        break;
                    case 6 ... 20:
                        note_timbre = 50 * (21 - envelope_index) / 15;
                        break;
                    default:
                        note_timbre = 0;
                        break;
                }

            } else if (frequency < 640.0) {
                // Closed Hi-hat: 3 - 5 KHz
                frequency = (rand() % (int)(2000)) + 3000;
                switch (envelope_index) {
                    case 0 ... 15:
                        note_timbre = 50;
                        break;
                    case 16 ... 20:
                        note_timbre = 50 * (21 - envelope_index) / 5;
                        break;
                    default:
                        note_timbre = 0;
                        break;
                }

            } else if (frequency < 1280.0) {
                // Open Hi-hat: 3 - 5 KHz
                frequency = (rand() % (int)(2000)) + 3000;
                switch (envelope_index) {
                    case 0 ... 35:
                        note_timbre = 50;
                        break;
                    case 36 ... 50:
                        note_timbre = 50 * (51 - envelope_index) / 15;
                        break;
                    default:
                        note_timbre = 0;
                        break;
                }
            }
            break;
        case butts_fader:
            glissando = true;
            switch (compensated_index) {
                case 0 ... 9:
                    frequency   = frequency / 4;
                    note_timbre = TIMBRE_12;
                    break;

                case 10 ... 19:
                    frequency   = frequency / 2;
                    note_timbre = TIMBRE_12;
                    break;

                case 20 ... 200:
                    note_timbre = 12 - (uint8_t)(pow(((float)compensated_index - 20) / (200 - 20), 2) * 12.5);
                    break;

                default:
                    note_timbre = 0;
                    break;
            }
            break;

            // case octave_crunch:
            //     switch (compensated_index) {
            //         case 0 ... 9:
            //         case 20 ... 24:
            //         case 30 ... 32:
            //             frequency = frequency / 2;
            //             note_timbre = TIMBRE_12;
            //         break;

            //         case 10 ... 19:
            //         case 25 ... 29:
            //         case 33 ... 35:
            //             frequency = frequency * 2;
            //             note_timbre = TIMBRE_12;
            //          break;

            //         default:
            //             note_timbre = TIMBRE_12;
            //          break;
            //     }
            //  break;

        case duty_osc:
            // This slows the loop down a substantial amount, so higher notes may freeze
            glissando = true;
            switch (compensated_index) {
                default:
#    define OCS_SPEED 10
#    define OCS_AMP .25
                    // sine wave is slow
                    // note_timbre = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5;
                    // triangle wave is a bit faster
                    note_timbre = (uint8_t)abs((compensated_index * OCS_SPEED % 3000) - 1500) * (OCS_AMP / 1500) + (1 - OCS_AMP) / 2;
                    break;
            }
            break;

        case duty_octave_down:
            glissando   = true;
            note_timbre = (uint8_t)(100 * (envelope_index % 2) * .125 + .375 * 2);
            if ((envelope_index % 4) == 0) note_timbre = 50;
            if ((envelope_index % 8) == 0) note_timbre = 0;
            break;
        case delayed_vibrato:
            glissando   = true;
            note_timbre = TIMBRE_50;
#    define VOICE_VIBRATO_DELAY 150
#    define VOICE_VIBRATO_SPEED 50
            switch (compensated_index) {
                case 0 ... VOICE_VIBRATO_DELAY:
                    break;
                default:
                    // TODO: merge/replace with voice_add_vibrato above
                    frequency = frequency * vibrato_lut[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1)) / 1000 * VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)];
                    break;
            }
            break;
            // case delayed_vibrato_octave:
            //     if ((envelope_index % 2) == 1) {
            //         note_timbre = 55;
            //     } else {
            //         note_timbre = 45;
            //     }
            //     #define VOICE_VIBRATO_DELAY 150
            //     #define VOICE_VIBRATO_SPEED 50
            //     switch (compensated_index) {
            //         case 0 ... VOICE_VIBRATO_DELAY:
            //             break;
            //         default:
            //             frequency = frequency * VIBRATO_LUT[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1))/1000*VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)];
            //             break;
            //     }
            //     break;
            // case duty_fifth_down:
            //     note_timbre = TIMBRE_50;
            //     if ((envelope_index % 3) == 0)
            //         note_timbre = TIMBRE_75;
            //     break;
            // case duty_fourth_down:
            //     note_timbre = 0;
            //     if ((envelope_index % 12) == 0)
            //         note_timbre = TIMBRE_75;
            //     if (((envelope_index % 12) % 4) != 1)
            //         note_timbre = TIMBRE_75;
            //     break;
            // case duty_third_down:
            //     note_timbre = TIMBRE_50;
            //     if ((envelope_index % 5) == 0)
            //         note_timbre = TIMBRE_75;
            //     break;
            // case duty_fifth_third_down:
            //     note_timbre = TIMBRE_50;
            //     if ((envelope_index % 5) == 0)
            //         note_timbre = TIMBRE_75;
            //     if ((envelope_index % 3) == 0)
            //         note_timbre = TIMBRE_25;
            //     break;

#endif // AUDIO_VOICES

        default:
            break;
    }

#ifdef AUDIO_VOICES
    if (vibrato && (vibrato_strength > 0)) {
        frequency = voice_add_vibrato(frequency);
    }

    if (glissando) {
        // TODO: where to keep track of the start-frequency?
        // frequency = voice_add_glissando(??, frequency);
    }
#endif // AUDIO_VOICES

    return frequency;
}

// Vibrato functions

void voice_set_vibrato_rate(float rate) {
    vibrato_rate = rate;
}
void voice_increase_vibrato_rate(float change) {
    vibrato_rate *= change;
}
void voice_decrease_vibrato_rate(float change) {
    vibrato_rate /= change;
}
void voice_set_vibrato_strength(float strength) {
    vibrato_strength = strength;
}
void voice_increase_vibrato_strength(float change) {
    vibrato_strength *= change;
}
void voice_decrease_vibrato_strength(float change) {
    vibrato_strength /= change;
}

// Timbre functions

void voice_set_timbre(uint8_t timbre) {
    if ((timbre > 0) && (timbre < 100)) {
        note_timbre = timbre;
    }
}
uint8_t voice_get_timbre(void) {
    return note_timbre;
}