1
0
Fork 0
qmk_firmware/quantum/audio/voices.c

104 lines
2.9 KiB
C
Raw Normal View History

2016-04-21 04:37:45 +00:00
#include "voices.h"
2016-04-22 05:01:38 +00:00
#include "stdlib.h"
2016-04-21 04:37:45 +00:00
// these are imported from audio.c
2016-04-21 04:37:45 +00:00
extern uint16_t envelope_index;
extern float note_timbre;
extern float polyphony_rate;
2016-04-21 04:37:45 +00:00
voice_type voice = duty_osc;
2016-04-21 04:37:45 +00:00
void set_voice(voice_type v) {
2016-04-21 04:40:00 +00:00
voice = v;
2016-04-21 04:37:45 +00:00
}
void voice_iterate() {
voice = (voice + 1) % number_of_voices;
}
void voice_deiterate() {
voice = (voice - 1) % number_of_voices;
}
2016-04-21 04:37:45 +00:00
float voice_envelope(float frequency) {
2016-04-21 04:40:00 +00:00
// envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz
2016-04-21 04:37:45 +00:00
uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency));
switch (voice) {
2016-04-21 04:40:00 +00:00
case default_voice:
note_timbre = TIMBRE_50;
polyphony_rate = 0;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
case butts_fader:
polyphony_rate = 0;
2016-04-21 04:40:00 +00:00
switch (compensated_index) {
case 0 ... 9:
frequency = frequency / 4;
note_timbre = TIMBRE_12;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
case 10 ... 19:
frequency = frequency / 2;
note_timbre = TIMBRE_12;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
case 20 ... 200:
note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
default:
note_timbre = 0;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
}
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
case octave_crunch:
polyphony_rate = 0;
2016-04-21 04:40:00 +00:00
switch (compensated_index) {
case 0 ... 9:
case 20 ... 24:
case 30 ... 32:
frequency = frequency / 2;
note_timbre = TIMBRE_12;
break;
2016-04-22 05:01:38 +00:00
2016-04-21 04:40:00 +00:00
case 10 ... 19:
case 25 ... 29:
case 33 ... 35:
frequency = frequency * 2;
note_timbre = TIMBRE_12;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
default:
note_timbre = TIMBRE_12;
2016-04-22 05:01:38 +00:00
break;
2016-04-21 04:40:00 +00:00
}
2016-04-22 05:01:38 +00:00
break;
case duty_osc:
// This slows the loop down a substantial amount, so higher notes may freeze
polyphony_rate = 0;
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 = (float)abs((compensated_index*OCS_SPEED % 3000) - 1500) * ( OCS_AMP / 1500 ) + (1 - OCS_AMP) / 2;
2016-04-22 05:01:38 +00:00
break;
}
2016-04-22 05:01:38 +00:00
break;
case duty_octave_down:
polyphony_rate = 0;
note_timbre = (envelope_index % 2) * .125 + .375 * 2;
2016-04-22 05:01:38 +00:00
break;
default:
break;
2016-04-21 04:40:00 +00:00
}
2016-04-21 04:37:45 +00:00
2016-04-21 04:40:00 +00:00
return frequency;
2016-04-21 04:37:45 +00:00
}