aboutsummaryrefslogtreecommitdiff
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
parenta8fd65d86f1bb43a845555ee2ac4b588798684ad (diff)
downloadqmk_firmware-9828aba2a12f03fccbc1095bc8e4918ae58fa31b.tar.gz
qmk_firmware-9828aba2a12f03fccbc1095bc8e4918ae58fa31b.zip
adds multiple voices and the ability to iterate/deiterate between them
-rw-r--r--keyboard/preonic/keymaps/default/keymap.c21
-rw-r--r--quantum/audio/voices.c31
-rw-r--r--quantum/audio/voices.h6
3 files changed, 53 insertions, 5 deletions
diff --git a/keyboard/preonic/keymaps/default/keymap.c b/keyboard/preonic/keymaps/default/keymap.c
index 267bfab3d..f0d5ed603 100644
--- a/keyboard/preonic/keymaps/default/keymap.c
+++ b/keyboard/preonic/keymaps/default/keymap.c
@@ -3,7 +3,6 @@
3#include "eeconfig.h" 3#include "eeconfig.h"
4#ifdef AUDIO_ENABLE 4#ifdef AUDIO_ENABLE
5 #include "audio.h" 5 #include "audio.h"
6 #include "song_list.h"
7#endif 6#endif
8 7
9// Each layer gets a name for readability, which is then used in the keymap matrix below. 8// Each layer gets a name for readability, which is then used in the keymap matrix below.
@@ -31,6 +30,8 @@
31#endif 30#endif
32#define MUS_OFF M(8) 31#define MUS_OFF M(8)
33#define MUS_ON M(9) 32#define MUS_ON M(9)
33#define VC_IN M(10)
34#define VC_DE M(11)
34 35
35// Fillers to make layering more clear 36// Fillers to make layering more clear
36#define _______ KC_TRNS 37#define _______ KC_TRNS
@@ -171,7 +172,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
171 {KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12}, 172 {KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12},
172 {_______, RESET, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_DEL}, 173 {_______, RESET, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_DEL},
173 {_______, _______, _______, AUD_ON, AUD_OFF, AG_NORM, AG_SWAP, QWERTY, COLEMAK, DVORAK, _______, _______}, 174 {_______, _______, _______, AUD_ON, AUD_OFF, AG_NORM, AG_SWAP, QWERTY, COLEMAK, DVORAK, _______, _______},
174 {_______, _______, _______, MUS_ON, MUS_OFF, _______, _______, _______, _______, _______, _______, _______}, 175 {_______, VC_DE, VC_IN, MUS_ON, MUS_OFF, _______, _______, _______, _______, _______, _______, _______},
175 {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______} 176 {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}
176} 177}
177 178
@@ -289,6 +290,22 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
289 #endif 290 #endif
290 } 291 }
291 break; 292 break;
293 case 10:
294 if (record->event.pressed) {
295 #ifdef AUDIO_ENABLE
296 voice_iterate();
297 PLAY_NOTE_ARRAY(music_scale, false, 0);
298 #endif
299 }
300 break;
301 case 11:
302 if (record->event.pressed) {
303 #ifdef AUDIO_ENABLE
304 voice_deiterate();
305 PLAY_NOTE_ARRAY(music_scale, false, 0);
306 #endif
307 }
308 break;
292 } 309 }
293 return MACRO_NONE; 310 return MACRO_NONE;
294}; 311};
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