diff options
Diffstat (limited to 'quantum/audio/voices.c')
| -rw-r--r-- | quantum/audio/voices.c | 170 |
1 files changed, 116 insertions, 54 deletions
diff --git a/quantum/audio/voices.c b/quantum/audio/voices.c index 1592618be..8988d827e 100644 --- a/quantum/audio/voices.c +++ b/quantum/audio/voices.c | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | /* Copyright 2016 Jack Humbert | 1 | /* Copyright 2016 Jack Humbert |
| 2 | * Copyright 2020 JohSchneider | ||
| 2 | * | 3 | * |
| 3 | * This program is free software: you can redistribute it and/or modify | 4 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by | 5 | * it under the terms of the GNU General Public License as published by |
| @@ -17,13 +18,19 @@ | |||
| 17 | #include "audio.h" | 18 | #include "audio.h" |
| 18 | #include <stdlib.h> | 19 | #include <stdlib.h> |
| 19 | 20 | ||
| 20 | // these are imported from audio.c | 21 | uint8_t note_timbre = TIMBRE_DEFAULT; |
| 21 | extern uint16_t envelope_index; | 22 | bool glissando = false; |
| 22 | extern float note_timbre; | 23 | bool vibrato = false; |
| 23 | extern float polyphony_rate; | 24 | float vibrato_strength = 0.5; |
| 24 | extern bool glissando; | 25 | float vibrato_rate = 0.125; |
| 25 | 26 | ||
| 27 | uint16_t voices_timer = 0; | ||
| 28 | |||
| 29 | #ifdef AUDIO_VOICE_DEFAULT | ||
| 30 | voice_type voice = AUDIO_VOICE_DEFAULT; | ||
| 31 | #else | ||
| 26 | voice_type voice = default_voice; | 32 | voice_type voice = default_voice; |
| 33 | #endif | ||
| 27 | 34 | ||
| 28 | void set_voice(voice_type v) { voice = v; } | 35 | void set_voice(voice_type v) { voice = v; } |
| 29 | 36 | ||
| @@ -31,22 +38,54 @@ void voice_iterate() { voice = (voice + 1) % number_of_voices; } | |||
| 31 | 38 | ||
| 32 | void voice_deiterate() { voice = (voice - 1 + number_of_voices) % number_of_voices; } | 39 | void voice_deiterate() { voice = (voice - 1 + number_of_voices) % number_of_voices; } |
| 33 | 40 | ||
| 41 | #ifdef AUDIO_VOICES | ||
| 42 | float mod(float a, int b) { | ||
| 43 | float r = fmod(a, b); | ||
| 44 | return r < 0 ? r + b : r; | ||
| 45 | } | ||
| 46 | |||
| 47 | // Effect: 'vibrate' a given target frequency slightly above/below its initial value | ||
| 48 | float voice_add_vibrato(float average_freq) { | ||
| 49 | float vibrato_counter = mod(timer_read() / (100 * vibrato_rate), VIBRATO_LUT_LENGTH); | ||
| 50 | |||
| 51 | return average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength); | ||
| 52 | } | ||
| 53 | |||
| 54 | // Effect: 'slides' the 'frequency' from the starting-point, to the target frequency | ||
| 55 | float voice_add_glissando(float from_freq, float to_freq) { | ||
| 56 | if (to_freq != 0 && from_freq < to_freq && from_freq < to_freq * pow(2, -440 / to_freq / 12 / 2)) { | ||
| 57 | return from_freq * pow(2, 440 / from_freq / 12 / 2); | ||
| 58 | } else if (to_freq != 0 && from_freq > to_freq && from_freq > to_freq * pow(2, 440 / to_freq / 12 / 2)) { | ||
| 59 | return from_freq * pow(2, -440 / from_freq / 12 / 2); | ||
| 60 | } else { | ||
| 61 | return to_freq; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | #endif | ||
| 65 | |||
| 34 | float voice_envelope(float frequency) { | 66 | float voice_envelope(float frequency) { |
| 35 | // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz | 67 | // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz |
| 36 | __attribute__((unused)) uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency)); | 68 | // __attribute__((unused)) uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency)); |
| 69 | #ifdef AUDIO_VOICES | ||
| 70 | uint16_t envelope_index = timer_elapsed(voices_timer); // TODO: multiply in some factor? | ||
| 71 | uint16_t compensated_index = envelope_index / 100; // TODO: correct factor would be? | ||
| 72 | #endif | ||
| 37 | 73 | ||
| 38 | switch (voice) { | 74 | switch (voice) { |
| 39 | case default_voice: | 75 | case default_voice: |
| 40 | glissando = false; | 76 | glissando = false; |
| 41 | note_timbre = TIMBRE_50; | 77 | // note_timbre = TIMBRE_50; //Note: leave the user the possibility to adjust the timbre with 'audio_set_timbre' |
| 42 | polyphony_rate = 0; | ||
| 43 | break; | 78 | break; |
| 44 | 79 | ||
| 45 | #ifdef AUDIO_VOICES | 80 | #ifdef AUDIO_VOICES |
| 46 | 81 | ||
| 82 | case vibrating: | ||
| 83 | glissando = false; | ||
| 84 | vibrato = true; | ||
| 85 | break; | ||
| 86 | |||
| 47 | case something: | 87 | case something: |
| 48 | glissando = false; | 88 | glissando = false; |
| 49 | polyphony_rate = 0; | ||
| 50 | switch (compensated_index) { | 89 | switch (compensated_index) { |
| 51 | case 0 ... 9: | 90 | case 0 ... 9: |
| 52 | note_timbre = TIMBRE_12; | 91 | note_timbre = TIMBRE_12; |
| @@ -57,24 +96,23 @@ float voice_envelope(float frequency) { | |||
| 57 | break; | 96 | break; |
| 58 | 97 | ||
| 59 | case 20 ... 200: | 98 | case 20 ... 200: |
| 60 | note_timbre = .125 + .125; | 99 | note_timbre = 12 + 12; |
| 61 | break; | 100 | break; |
| 62 | 101 | ||
| 63 | default: | 102 | default: |
| 64 | note_timbre = .125; | 103 | note_timbre = 12; |
| 65 | break; | 104 | break; |
| 66 | } | 105 | } |
| 67 | break; | 106 | break; |
| 68 | 107 | ||
| 69 | case drums: | 108 | case drums: |
| 70 | glissando = false; | 109 | glissando = false; |
| 71 | polyphony_rate = 0; | ||
| 72 | // switch (compensated_index) { | 110 | // switch (compensated_index) { |
| 73 | // case 0 ... 10: | 111 | // case 0 ... 10: |
| 74 | // note_timbre = 0.5; | 112 | // note_timbre = 50; |
| 75 | // break; | 113 | // break; |
| 76 | // case 11 ... 20: | 114 | // case 11 ... 20: |
| 77 | // note_timbre = 0.5 * (21 - compensated_index) / 10; | 115 | // note_timbre = 50 * (21 - compensated_index) / 10; |
| 78 | // break; | 116 | // break; |
| 79 | // default: | 117 | // default: |
| 80 | // note_timbre = 0; | 118 | // note_timbre = 0; |
| @@ -88,10 +126,10 @@ float voice_envelope(float frequency) { | |||
| 88 | frequency = (rand() % (int)(40)) + 60; | 126 | frequency = (rand() % (int)(40)) + 60; |
| 89 | switch (envelope_index) { | 127 | switch (envelope_index) { |
| 90 | case 0 ... 10: | 128 | case 0 ... 10: |
| 91 | note_timbre = 0.5; | 129 | note_timbre = 50; |
| 92 | break; | 130 | break; |
| 93 | case 11 ... 20: | 131 | case 11 ... 20: |
| 94 | note_timbre = 0.5 * (21 - envelope_index) / 10; | 132 | note_timbre = 50 * (21 - envelope_index) / 10; |
| 95 | break; | 133 | break; |
| 96 | default: | 134 | default: |
| 97 | note_timbre = 0; | 135 | note_timbre = 0; |
| @@ -103,10 +141,10 @@ float voice_envelope(float frequency) { | |||
| 103 | frequency = (rand() % (int)(1000)) + 1000; | 141 | frequency = (rand() % (int)(1000)) + 1000; |
| 104 | switch (envelope_index) { | 142 | switch (envelope_index) { |
| 105 | case 0 ... 5: | 143 | case 0 ... 5: |
| 106 | note_timbre = 0.5; | 144 | note_timbre = 50; |
| 107 | break; | 145 | break; |
| 108 | case 6 ... 20: | 146 | case 6 ... 20: |
| 109 | note_timbre = 0.5 * (21 - envelope_index) / 15; | 147 | note_timbre = 50 * (21 - envelope_index) / 15; |
| 110 | break; | 148 | break; |
| 111 | default: | 149 | default: |
| 112 | note_timbre = 0; | 150 | note_timbre = 0; |
| @@ -118,10 +156,10 @@ float voice_envelope(float frequency) { | |||
| 118 | frequency = (rand() % (int)(2000)) + 3000; | 156 | frequency = (rand() % (int)(2000)) + 3000; |
| 119 | switch (envelope_index) { | 157 | switch (envelope_index) { |
| 120 | case 0 ... 15: | 158 | case 0 ... 15: |
| 121 | note_timbre = 0.5; | 159 | note_timbre = 50; |
| 122 | break; | 160 | break; |
| 123 | case 16 ... 20: | 161 | case 16 ... 20: |
| 124 | note_timbre = 0.5 * (21 - envelope_index) / 5; | 162 | note_timbre = 50 * (21 - envelope_index) / 5; |
| 125 | break; | 163 | break; |
| 126 | default: | 164 | default: |
| 127 | note_timbre = 0; | 165 | note_timbre = 0; |
| @@ -133,10 +171,10 @@ float voice_envelope(float frequency) { | |||
| 133 | frequency = (rand() % (int)(2000)) + 3000; | 171 | frequency = (rand() % (int)(2000)) + 3000; |
| 134 | switch (envelope_index) { | 172 | switch (envelope_index) { |
| 135 | case 0 ... 35: | 173 | case 0 ... 35: |
| 136 | note_timbre = 0.5; | 174 | note_timbre = 50; |
| 137 | break; | 175 | break; |
| 138 | case 36 ... 50: | 176 | case 36 ... 50: |
| 139 | note_timbre = 0.5 * (51 - envelope_index) / 15; | 177 | note_timbre = 50 * (51 - envelope_index) / 15; |
| 140 | break; | 178 | break; |
| 141 | default: | 179 | default: |
| 142 | note_timbre = 0; | 180 | note_timbre = 0; |
| @@ -145,8 +183,7 @@ float voice_envelope(float frequency) { | |||
| 145 | } | 183 | } |
| 146 | break; | 184 | break; |
| 147 | case butts_fader: | 185 | case butts_fader: |
| 148 | glissando = true; | 186 | glissando = true; |
| 149 | polyphony_rate = 0; | ||
| 150 | switch (compensated_index) { | 187 | switch (compensated_index) { |
| 151 | case 0 ... 9: | 188 | case 0 ... 9: |
| 152 | frequency = frequency / 4; | 189 | frequency = frequency / 4; |
| @@ -159,7 +196,7 @@ float voice_envelope(float frequency) { | |||
| 159 | break; | 196 | break; |
| 160 | 197 | ||
| 161 | case 20 ... 200: | 198 | case 20 ... 200: |
| 162 | note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2) * .125; | 199 | note_timbre = 12 - (uint8_t)(pow(((float)compensated_index - 20) / (200 - 20), 2) * 12.5); |
| 163 | break; | 200 | break; |
| 164 | 201 | ||
| 165 | default: | 202 | default: |
| @@ -169,7 +206,6 @@ float voice_envelope(float frequency) { | |||
| 169 | break; | 206 | break; |
| 170 | 207 | ||
| 171 | // case octave_crunch: | 208 | // case octave_crunch: |
| 172 | // polyphony_rate = 0; | ||
| 173 | // switch (compensated_index) { | 209 | // switch (compensated_index) { |
| 174 | // case 0 ... 9: | 210 | // case 0 ... 9: |
| 175 | // case 20 ... 24: | 211 | // case 20 ... 24: |
| @@ -187,14 +223,13 @@ float voice_envelope(float frequency) { | |||
| 187 | 223 | ||
| 188 | // default: | 224 | // default: |
| 189 | // note_timbre = TIMBRE_12; | 225 | // note_timbre = TIMBRE_12; |
| 190 | // break; | 226 | // break; |
| 191 | // } | 227 | // } |
| 192 | // break; | 228 | // break; |
| 193 | 229 | ||
| 194 | case duty_osc: | 230 | case duty_osc: |
| 195 | // This slows the loop down a substantial amount, so higher notes may freeze | 231 | // This slows the loop down a substantial amount, so higher notes may freeze |
| 196 | glissando = true; | 232 | glissando = true; |
| 197 | polyphony_rate = 0; | ||
| 198 | switch (compensated_index) { | 233 | switch (compensated_index) { |
| 199 | default: | 234 | default: |
| 200 | # define OCS_SPEED 10 | 235 | # define OCS_SPEED 10 |
| @@ -202,38 +237,36 @@ float voice_envelope(float frequency) { | |||
| 202 | // sine wave is slow | 237 | // sine wave is slow |
| 203 | // note_timbre = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5; | 238 | // note_timbre = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5; |
| 204 | // triangle wave is a bit faster | 239 | // triangle wave is a bit faster |
| 205 | note_timbre = (float)abs((compensated_index * OCS_SPEED % 3000) - 1500) * (OCS_AMP / 1500) + (1 - OCS_AMP) / 2; | 240 | note_timbre = (uint8_t)abs((compensated_index * OCS_SPEED % 3000) - 1500) * (OCS_AMP / 1500) + (1 - OCS_AMP) / 2; |
| 206 | break; | 241 | break; |
| 207 | } | 242 | } |
| 208 | break; | 243 | break; |
| 209 | 244 | ||
| 210 | case duty_octave_down: | 245 | case duty_octave_down: |
| 211 | glissando = true; | 246 | glissando = true; |
| 212 | polyphony_rate = 0; | 247 | note_timbre = (uint8_t)(100 * (envelope_index % 2) * .125 + .375 * 2); |
| 213 | note_timbre = (envelope_index % 2) * .125 + .375 * 2; | 248 | if ((envelope_index % 4) == 0) note_timbre = 50; |
| 214 | if ((envelope_index % 4) == 0) note_timbre = 0.5; | ||
| 215 | if ((envelope_index % 8) == 0) note_timbre = 0; | 249 | if ((envelope_index % 8) == 0) note_timbre = 0; |
| 216 | break; | 250 | break; |
| 217 | case delayed_vibrato: | 251 | case delayed_vibrato: |
| 218 | glissando = true; | 252 | glissando = true; |
| 219 | polyphony_rate = 0; | 253 | note_timbre = TIMBRE_50; |
| 220 | note_timbre = TIMBRE_50; | ||
| 221 | # define VOICE_VIBRATO_DELAY 150 | 254 | # define VOICE_VIBRATO_DELAY 150 |
| 222 | # define VOICE_VIBRATO_SPEED 50 | 255 | # define VOICE_VIBRATO_SPEED 50 |
| 223 | switch (compensated_index) { | 256 | switch (compensated_index) { |
| 224 | case 0 ... VOICE_VIBRATO_DELAY: | 257 | case 0 ... VOICE_VIBRATO_DELAY: |
| 225 | break; | 258 | break; |
| 226 | default: | 259 | default: |
| 260 | // TODO: merge/replace with voice_add_vibrato above | ||
| 227 | frequency = frequency * vibrato_lut[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1)) / 1000 * VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)]; | 261 | frequency = frequency * vibrato_lut[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1)) / 1000 * VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)]; |
| 228 | break; | 262 | break; |
| 229 | } | 263 | } |
| 230 | break; | 264 | break; |
| 231 | // case delayed_vibrato_octave: | 265 | // case delayed_vibrato_octave: |
| 232 | // polyphony_rate = 0; | ||
| 233 | // if ((envelope_index % 2) == 1) { | 266 | // if ((envelope_index % 2) == 1) { |
| 234 | // note_timbre = 0.55; | 267 | // note_timbre = 55; |
| 235 | // } else { | 268 | // } else { |
| 236 | // note_timbre = 0.45; | 269 | // note_timbre = 45; |
| 237 | // } | 270 | // } |
| 238 | // #define VOICE_VIBRATO_DELAY 150 | 271 | // #define VOICE_VIBRATO_DELAY 150 |
| 239 | // #define VOICE_VIBRATO_SPEED 50 | 272 | // #define VOICE_VIBRATO_SPEED 50 |
| @@ -246,35 +279,64 @@ float voice_envelope(float frequency) { | |||
| 246 | // } | 279 | // } |
| 247 | // break; | 280 | // break; |
| 248 | // case duty_fifth_down: | 281 | // case duty_fifth_down: |
| 249 | // note_timbre = 0.5; | 282 | // note_timbre = TIMBRE_50; |
| 250 | // if ((envelope_index % 3) == 0) | 283 | // if ((envelope_index % 3) == 0) |
| 251 | // note_timbre = 0.75; | 284 | // note_timbre = TIMBRE_75; |
| 252 | // break; | 285 | // break; |
| 253 | // case duty_fourth_down: | 286 | // case duty_fourth_down: |
| 254 | // note_timbre = 0.0; | 287 | // note_timbre = 0; |
| 255 | // if ((envelope_index % 12) == 0) | 288 | // if ((envelope_index % 12) == 0) |
| 256 | // note_timbre = 0.75; | 289 | // note_timbre = TIMBRE_75; |
| 257 | // if (((envelope_index % 12) % 4) != 1) | 290 | // if (((envelope_index % 12) % 4) != 1) |
| 258 | // note_timbre = 0.75; | 291 | // note_timbre = TIMBRE_75; |
| 259 | // break; | 292 | // break; |
| 260 | // case duty_third_down: | 293 | // case duty_third_down: |
| 261 | // note_timbre = 0.5; | 294 | // note_timbre = TIMBRE_50; |
| 262 | // if ((envelope_index % 5) == 0) | 295 | // if ((envelope_index % 5) == 0) |
| 263 | // note_timbre = 0.75; | 296 | // note_timbre = TIMBRE_75; |
| 264 | // break; | 297 | // break; |
| 265 | // case duty_fifth_third_down: | 298 | // case duty_fifth_third_down: |
| 266 | // note_timbre = 0.5; | 299 | // note_timbre = TIMBRE_50; |
| 267 | // if ((envelope_index % 5) == 0) | 300 | // if ((envelope_index % 5) == 0) |
| 268 | // note_timbre = 0.75; | 301 | // note_timbre = TIMBRE_75; |
| 269 | // if ((envelope_index % 3) == 0) | 302 | // if ((envelope_index % 3) == 0) |
| 270 | // note_timbre = 0.25; | 303 | // note_timbre = TIMBRE_25; |
| 271 | // break; | 304 | // break; |
| 272 | 305 | ||
| 273 | #endif | 306 | #endif // AUDIO_VOICES |
| 274 | 307 | ||
| 275 | default: | 308 | default: |
| 276 | break; | 309 | break; |
| 277 | } | 310 | } |
| 278 | 311 | ||
| 312 | #ifdef AUDIO_VOICES | ||
| 313 | if (vibrato && (vibrato_strength > 0)) { | ||
| 314 | frequency = voice_add_vibrato(frequency); | ||
| 315 | } | ||
| 316 | |||
| 317 | if (glissando) { | ||
| 318 | // TODO: where to keep track of the start-frequency? | ||
| 319 | // frequency = voice_add_glissando(??, frequency); | ||
| 320 | } | ||
| 321 | #endif // AUDIO_VOICES | ||
| 322 | |||
| 279 | return frequency; | 323 | return frequency; |
| 280 | } | 324 | } |
| 325 | |||
| 326 | // Vibrato functions | ||
| 327 | |||
| 328 | void voice_set_vibrato_rate(float rate) { vibrato_rate = rate; } | ||
| 329 | void voice_increase_vibrato_rate(float change) { vibrato_rate *= change; } | ||
| 330 | void voice_decrease_vibrato_rate(float change) { vibrato_rate /= change; } | ||
| 331 | void voice_set_vibrato_strength(float strength) { vibrato_strength = strength; } | ||
| 332 | void voice_increase_vibrato_strength(float change) { vibrato_strength *= change; } | ||
| 333 | void voice_decrease_vibrato_strength(float change) { vibrato_strength /= change; } | ||
| 334 | |||
| 335 | // Timbre functions | ||
| 336 | |||
| 337 | void voice_set_timbre(uint8_t timbre) { | ||
| 338 | if ((timbre > 0) && (timbre < 100)) { | ||
| 339 | note_timbre = timbre; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | uint8_t voice_get_timbre(void) { return note_timbre; } | ||
