aboutsummaryrefslogtreecommitdiff
path: root/quantum/audio
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-04-21 18:14:25 -0400
committerJack Humbert <jack.humb@gmail.com>2016-04-21 18:14:25 -0400
commit9828aba2a12f03fccbc1095bc8e4918ae58fa31b (patch)
tree9990820fa4f42650599b40527c94fe3e6c3f6a2d /quantum/audio
parenta8fd65d86f1bb43a845555ee2ac4b588798684ad (diff)
downloadqmk_firmware-9828aba2a12f03fccbc1095bc8e4918ae58fa31b.tar.gz
qmk_firmware-9828aba2a12f03fccbc1095bc8e4918ae58fa31b.zip
adds multiple voices and the ability to iterate/deiterate between them
Diffstat (limited to 'quantum/audio')
-rw-r--r--quantum/audio/voices.c31
-rw-r--r--quantum/audio/voices.h6
2 files changed, 34 insertions, 3 deletions
diff --git a/quantum/audio/voices.c b/quantum/audio/voices.c
index 51652927b..92ada47f7 100644
--- a/quantum/audio/voices.c
+++ b/quantum/audio/voices.c
@@ -1,23 +1,35 @@
1#include "voices.h" 1#include "voices.h"
2 2
3// these are imported from audio.c
3extern uint16_t envelope_index; 4extern uint16_t envelope_index;
4extern float note_timbre; 5extern float note_timbre;
6extern float polyphony_rate;
5 7
6voice_type voice = default_voice; 8voice_type voice = duty_osc;
7 9
8void set_voice(voice_type v) { 10void set_voice(voice_type v) {
9 voice = v; 11 voice = v;
10} 12}
11 13
14void voice_iterate() {
15 voice = (voice + 1) % number_of_voices;
16}
17
18void voice_deiterate() {
19 voice = (voice - 1) % number_of_voices;
20}
21
12float voice_envelope(float frequency) { 22float voice_envelope(float frequency) {
13 // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz 23 // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz
14 uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency)); 24 uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency));
15 25
16 switch (voice) { 26 switch (voice) {
17 case default_voice: 27 case default_voice:
18 // nothing here on purpose 28 note_timbre = TIMBRE_50;
29 polyphony_rate = 0;
19 break; 30 break;
20 case butts_fader: 31 case butts_fader:
32 polyphony_rate = 0;
21 switch (compensated_index) { 33 switch (compensated_index) {
22 case 0 ... 9: 34 case 0 ... 9:
23 frequency = frequency / 4; 35 frequency = frequency / 4;
@@ -36,6 +48,7 @@ float voice_envelope(float frequency) {
36 } 48 }
37 break; 49 break;
38 case octave_crunch: 50 case octave_crunch:
51 polyphony_rate = 0;
39 switch (compensated_index) { 52 switch (compensated_index) {
40 case 0 ... 9: 53 case 0 ... 9:
41 case 20 ... 24: 54 case 20 ... 24:
@@ -54,6 +67,20 @@ float voice_envelope(float frequency) {
54 break; 67 break;
55 } 68 }
56 break; 69 break;
70 case duty_osc:
71 // This slows the loop down a substantial amount, so higher notes may freeze
72 polyphony_rate = 0;
73 switch (compensated_index) {
74 default:
75 #define SPEED 10
76 #define AMP .75
77 // sine wave is slow
78 // note_timbre = (sin((float)compensated_index/10000*SPEED) * AMP / 2) + .5;
79 // triangle wave is a bit faster
80 note_timbre = (float)abs((compensated_index*SPEED % 3000) - 1500) * ( AMP / 1500 ) + (1 - AMP) / 2;
81 break;
82 }
83 break;
57 } 84 }
58 85
59 return frequency; 86 return frequency;
diff --git a/quantum/audio/voices.h b/quantum/audio/voices.h
index 317f5d98c..44c5066b5 100644
--- a/quantum/audio/voices.h
+++ b/quantum/audio/voices.h
@@ -13,9 +13,13 @@ float voice_envelope(float frequency);
13typedef enum { 13typedef enum {
14 default_voice, 14 default_voice,
15 butts_fader, 15 butts_fader,
16 octave_crunch 16 octave_crunch,
17 duty_osc,
18 number_of_voices // important that this is last
17} voice_type; 19} voice_type;
18 20
19void set_voice(voice_type v); 21void set_voice(voice_type v);
22void voice_iterate();
23void voice_deiterate();
20 24
21#endif \ No newline at end of file 25#endif \ No newline at end of file