aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Young <gabeplaysdrums@live.com>2017-02-25 19:25:33 -0800
committerGabriel Young <gabeplaysdrums@live.com>2017-02-25 19:25:33 -0800
commit1000799d1ef594bf9f48076986ec300ef9e536db (patch)
treee561400e2a5f2e0c27238b1e2602164140039918
parent525be99ee938aa6e48448d7dd6ea6e6fe50bb36d (diff)
downloadqmk_firmware-1000799d1ef594bf9f48076986ec300ef9e536db.tar.gz
qmk_firmware-1000799d1ef594bf9f48076986ec300ef9e536db.zip
Factor basic note processing into respective processors
-rw-r--r--build_keyboard.mk1
-rw-r--r--quantum/process_keycode/process_audio.c62
-rw-r--r--quantum/process_keycode/process_audio.h11
-rw-r--r--quantum/process_keycode/process_midi.c28
-rw-r--r--quantum/process_keycode/process_midi.h13
-rw-r--r--quantum/process_keycode/process_music.c137
-rw-r--r--quantum/process_keycode/process_music.h5
-rw-r--r--quantum/quantum.c3
-rw-r--r--quantum/quantum.h7
9 files changed, 184 insertions, 83 deletions
diff --git a/build_keyboard.mk b/build_keyboard.mk
index eea8d5919..07dfe85b4 100644
--- a/build_keyboard.mk
+++ b/build_keyboard.mk
@@ -157,6 +157,7 @@ endif
157ifeq ($(strip $(AUDIO_ENABLE)), yes) 157ifeq ($(strip $(AUDIO_ENABLE)), yes)
158 OPT_DEFS += -DAUDIO_ENABLE 158 OPT_DEFS += -DAUDIO_ENABLE
159 SRC += $(QUANTUM_DIR)/process_keycode/process_music.c 159 SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
160 SRC += $(QUANTUM_DIR)/process_keycode/process_audio.c
160 SRC += $(QUANTUM_DIR)/audio/audio.c 161 SRC += $(QUANTUM_DIR)/audio/audio.c
161 SRC += $(QUANTUM_DIR)/audio/voices.c 162 SRC += $(QUANTUM_DIR)/audio/voices.c
162 SRC += $(QUANTUM_DIR)/audio/luts.c 163 SRC += $(QUANTUM_DIR)/audio/luts.c
diff --git a/quantum/process_keycode/process_audio.c b/quantum/process_keycode/process_audio.c
new file mode 100644
index 000000000..5b5da546e
--- /dev/null
+++ b/quantum/process_keycode/process_audio.c
@@ -0,0 +1,62 @@
1#include "process_audio.h"
2#include "audio.h"
3
4static float compute_freq_for_midi_note(uint8_t note)
5{
6 // https://en.wikipedia.org/wiki/MIDI_tuning_standard
7 return pow(2.0, (note - 69) / 12.0) * 440.0f;
8}
9
10bool process_audio(uint16_t keycode, keyrecord_t *record) {
11
12 if (keycode == AU_ON && record->event.pressed) {
13 audio_on();
14 return false;
15 }
16
17 if (keycode == AU_OFF && record->event.pressed) {
18 audio_off();
19 return false;
20 }
21
22 if (keycode == AU_TOG && record->event.pressed) {
23 if (is_audio_on())
24 {
25 audio_off();
26 }
27 else
28 {
29 audio_on();
30 }
31 return false;
32 }
33
34 if (keycode == MUV_IN && record->event.pressed) {
35 voice_iterate();
36 music_scale_user();
37 return false;
38 }
39
40 if (keycode == MUV_DE && record->event.pressed) {
41 voice_deiterate();
42 music_scale_user();
43 return false;
44 }
45
46 return true
47}
48
49void process_audio_noteon(uint8_t note) {
50 play_note(compute_freq_for_midi_note(note), 0xF);
51}
52
53void process_audio_noteoff(uint8_t note) {
54 stop_note(compute_freq_for_midi_note(note));
55}
56
57void process_audio_stop_all_notes(void) {
58 stop_all_notes();
59}
60
61__attribute__ ((weak))
62void audio_on_user() {} \ No newline at end of file
diff --git a/quantum/process_keycode/process_audio.h b/quantum/process_keycode/process_audio.h
new file mode 100644
index 000000000..59a17725a
--- /dev/null
+++ b/quantum/process_keycode/process_audio.h
@@ -0,0 +1,11 @@
1#ifndef PROCESS_AUDIO_H
2#define PROCESS_AUDIO_H
3
4bool process_audio(uint16_t keycode, keyrecord_t *record);
5void process_audio_noteon(uint8_t note);
6void process_audio_noteoff(uint8_t note);
7void process_audio_stop_all_notes(void);
8
9void audio_on_user(void);
10
11#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c
index 161f04a24..214bba902 100644
--- a/quantum/process_keycode/process_midi.c
+++ b/quantum/process_keycode/process_midi.c
@@ -1,6 +1,28 @@
1#include "process_midi.h" 1#include "process_midi.h"
2 2
3#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED) 3#ifdef MIDI_ENABLE
4#include "midi.h"
5
6#ifdef MIDI_BASIC
7
8void process_midi_basic_noteon(uint8_t note)
9{
10 midi_send_noteon(&midi_device, 0, note, 128);
11}
12
13void process_midi_basic_noteoff(uint8_t note)
14{
15 midi_send_noteoff(&midi_device, 0, note, 0);
16}
17
18void process_midi_basic_stop_all_notes(void)
19{
20 midi_send_cc(&midi_device, 0, 0x7B, 0);
21}
22
23#endif // MIDI_BASIC
24
25#ifdef MIDI_ADVANCED
4 26
5#include "timer.h" 27#include "timer.h"
6 28
@@ -165,7 +187,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
165 case MI_ALLOFF: 187 case MI_ALLOFF:
166 if (record->event.pressed) { 188 if (record->event.pressed) {
167 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0); 189 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
168 dprintf("midi off\n"); 190 dprintf("midi all notes off\n");
169 } 191 }
170 return false; 192 return false;
171 case MI_SUS: 193 case MI_SUS:
@@ -212,3 +234,5 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
212} 234}
213 235
214#endif // MIDI_ADVANCED 236#endif // MIDI_ADVANCED
237
238#endif // MIDI_ENABLE
diff --git a/quantum/process_keycode/process_midi.h b/quantum/process_keycode/process_midi.h
index ffd41579f..0f559ec23 100644
--- a/quantum/process_keycode/process_midi.h
+++ b/quantum/process_keycode/process_midi.h
@@ -2,8 +2,16 @@
2#define PROCESS_MIDI_H 2#define PROCESS_MIDI_H
3 3
4#include "quantum.h" 4#include "quantum.h"
5#include "midi.h"
6 5
6#ifdef MIDI_ENABLE
7
8#ifdef MIDI_BASIC
9void process_midi_basic_noteon(uint8_t note);
10void process_midi_basic_noteoff(uint8_t note);
11void process_midi_basic_stop_all_notes(void);
12#endif
13
14#ifdef MIDI_ADVANCED
7typedef union { 15typedef union {
8 uint32_t raw; 16 uint32_t raw;
9 struct { 17 struct {
@@ -25,5 +33,8 @@ bool process_midi(uint16_t keycode, keyrecord_t *record);
25#define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1) 33#define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
26 34
27uint8_t midi_compute_note(uint16_t keycode); 35uint8_t midi_compute_note(uint16_t keycode);
36#endif // MIDI_ADVANCED
37
38#endif // MIDI_ENABLE
28 39
29#endif \ No newline at end of file 40#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c
index ac906b628..a1e270df1 100644
--- a/quantum/process_keycode/process_music.c
+++ b/quantum/process_keycode/process_music.c
@@ -1,5 +1,14 @@
1#include "process_music.h" 1#include "process_music.h"
2 2
3#ifdef AUDIO_ENABLE
4#include "process_audio.h"
5#endif
6#if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
7#include "process_midi.h"
8#endif
9
10#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
11
3bool music_activated = false; 12bool music_activated = false;
4uint8_t music_starting_note = 0x0C; 13uint8_t music_starting_note = 0x0C;
5int music_offset = 7; 14int music_offset = 7;
@@ -8,38 +17,41 @@ int music_offset = 7;
8static bool music_sequence_recording = false; 17static bool music_sequence_recording = false;
9static bool music_sequence_recorded = false; 18static bool music_sequence_recorded = false;
10static bool music_sequence_playing = false; 19static bool music_sequence_playing = false;
11static float music_sequence[16] = {0}; 20static uint8_t music_sequence[16] = {0};
12static uint8_t music_sequence_count = 0; 21static uint8_t music_sequence_count = 0;
13static uint8_t music_sequence_position = 0; 22static uint8_t music_sequence_position = 0;
14 23
15static uint16_t music_sequence_timer = 0; 24static uint16_t music_sequence_timer = 0;
16static uint16_t music_sequence_interval = 100; 25static uint16_t music_sequence_interval = 100;
17 26
18bool process_music(uint16_t keycode, keyrecord_t *record) { 27static void music_noteon(uint8_t note) {
28 #ifdef AUDIO_ENABLE
29 process_audio_noteon(note);
30 #endif
31 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
32 process_midi_basic_noteon(note);
33 #endif
34}
19 35
20 #ifdef AUDIO_ENABLE 36static void music_noteoff(uint8_t note) {
21 if (keycode == AU_ON && record->event.pressed) { 37 #ifdef AUDIO_ENABLE
22 audio_on(); 38 process_audio_noteoff(note);
23 return false; 39 #endif
24 } 40 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
41 process_midi_basic_noteoff(note);
42 #endif
43}
25 44
26 if (keycode == AU_OFF && record->event.pressed) { 45static void music_all_notes_off(void) {
27 audio_off(); 46 #ifdef AUDIO_ENABLE
28 return false; 47 process_audio_stop_all_notes();
29 } 48 #endif
49 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
50 process_midi_basic_stop_all_notes();
51 #endif
52}
30 53
31 if (keycode == AU_TOG && record->event.pressed) { 54bool process_music(uint16_t keycode, keyrecord_t *record) {
32 if (is_audio_on())
33 {
34 audio_off();
35 }
36 else
37 {
38 audio_on();
39 }
40 return false;
41 }
42 #endif // AUDIO_ENABLE
43 55
44 if (keycode == MU_ON && record->event.pressed) { 56 if (keycode == MU_ON && record->event.pressed) {
45 music_on(); 57 music_on();
@@ -63,26 +75,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
63 return false; 75 return false;
64 } 76 }
65 77
66 #ifdef AUDIO_ENABLE
67 if (keycode == MUV_IN && record->event.pressed) {
68 voice_iterate();
69 music_scale_user();
70 return false;
71 }
72
73 if (keycode == MUV_DE && record->event.pressed) {
74 voice_deiterate();
75 music_scale_user();
76 return false;
77 }
78 #endif // AUDIO_ENABLE
79
80 if (music_activated) { 78 if (music_activated) {
81 79
82 if (keycode == KC_LCTL && record->event.pressed) { // Start recording 80 if (keycode == KC_LCTL && record->event.pressed) { // Start recording
83 #ifdef AUDIO_ENABLE 81 music_all_notes_off();
84 stop_all_notes();
85 #endif
86 music_sequence_recording = true; 82 music_sequence_recording = true;
87 music_sequence_recorded = false; 83 music_sequence_recorded = false;
88 music_sequence_playing = false; 84 music_sequence_playing = false;
@@ -91,9 +87,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
91 } 87 }
92 88
93 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing 89 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
94 #ifdef AUDIO_ENABLE 90 music_all_notes_off();
95 stop_all_notes();
96 #endif
97 if (music_sequence_recording) { // was recording 91 if (music_sequence_recording) { // was recording
98 music_sequence_recorded = true; 92 music_sequence_recorded = true;
99 } 93 }
@@ -103,9 +97,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
103 } 97 }
104 98
105 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing 99 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
106 #ifdef AUDIO_ENABLE 100 music_all_notes_off();
107 stop_all_notes();
108 #endif
109 music_sequence_recording = false; 101 music_sequence_recording = false;
110 music_sequence_playing = true; 102 music_sequence_playing = true;
111 music_sequence_position = 0; 103 music_sequence_position = 0;
@@ -124,32 +116,27 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
124 music_sequence_interval+=10; 116 music_sequence_interval+=10;
125 return false; 117 return false;
126 } 118 }
119
127 #define MUSIC_MODE_GUITAR 120 #define MUSIC_MODE_GUITAR
128 121
129 #ifdef AUDIO_ENABLE
130 #ifdef MUSIC_MODE_CHROMATIC 122 #ifdef MUSIC_MODE_CHROMATIC
131 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(MATRIX_ROWS - record->event.key.row)); 123 uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
132 #elif defined(MUSIC_MODE_GUITAR) 124 #elif defined(MUSIC_MODE_GUITAR)
133 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 7)*5.0/12); 125 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
134 #elif defined(MUSIC_MODE_VIOLIN) 126 #elif defined(MUSIC_MODE_VIOLIN)
135 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 5)*7.0/12); 127 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
136 #else 128 #else
137 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + SCALE[record->event.key.col + music_offset])/12.0+(MATRIX_ROWS - record->event.key.row)); 129 uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
138 #endif 130 #endif
139 #endif // AUDIO_ENABLE
140 131
141 if (record->event.pressed) { 132 if (record->event.pressed) {
142 #ifdef AUDIO_ENABLE 133 music_noteon(note);
143 play_note(freq, 0xF);
144 if (music_sequence_recording) { 134 if (music_sequence_recording) {
145 music_sequence[music_sequence_count] = freq; 135 music_sequence[music_sequence_count] = note;
146 music_sequence_count++; 136 music_sequence_count++;
147 } 137 }
148 #endif
149 } else { 138 } else {
150 #ifdef AUDIO_ENABLE 139 music_noteoff(note);
151 stop_note(freq);
152 #endif
153 } 140 }
154 141
155 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through 142 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
@@ -177,32 +164,26 @@ void music_on(void) {
177 164
178void music_off(void) { 165void music_off(void) {
179 music_activated = 0; 166 music_activated = 0;
180 #ifdef AUDIO_ENABLE 167 music_all_notes_off();
181 stop_all_notes();
182 #endif
183} 168}
184 169
185
186__attribute__ ((weak))
187void music_on_user() {}
188
189#ifdef AUDIO_ENABLE
190__attribute__ ((weak))
191void audio_on_user() {}
192#endif
193
194__attribute__ ((weak))
195void music_scale_user() {}
196
197void matrix_scan_music(void) { 170void matrix_scan_music(void) {
198 if (music_sequence_playing) { 171 if (music_sequence_playing) {
199 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { 172 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
200 music_sequence_timer = timer_read(); 173 music_sequence_timer = timer_read();
201 #ifdef AUDIO_ENABLE 174 uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
202 stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]); 175 uint8_t next_note = music_sequence[music_sequence_position];
203 play_note(music_sequence[music_sequence_position], 0xF); 176 music_noteoff(prev_note);
204 #endif 177 music_noteon(next_note);
205 music_sequence_position = (music_sequence_position + 1) % music_sequence_count; 178 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
206 } 179 }
207 } 180 }
208} 181}
182
183__attribute__ ((weak))
184void music_on_user() {}
185
186__attribute__ ((weak))
187void music_scale_user() {}
188
189#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) \ No newline at end of file
diff --git a/quantum/process_keycode/process_music.h b/quantum/process_keycode/process_music.h
index 318b3e387..69913b276 100644
--- a/quantum/process_keycode/process_music.h
+++ b/quantum/process_keycode/process_music.h
@@ -3,6 +3,8 @@
3 3
4#include "quantum.h" 4#include "quantum.h"
5 5
6#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
7
6bool process_music(uint16_t keycode, keyrecord_t *record); 8bool process_music(uint16_t keycode, keyrecord_t *record);
7 9
8bool is_music_on(void); 10bool is_music_on(void);
@@ -10,7 +12,6 @@ void music_toggle(void);
10void music_on(void); 12void music_on(void);
11void music_off(void); 13void music_off(void);
12 14
13void audio_on_user(void);
14void music_on_user(void); 15void music_on_user(void);
15void music_scale_user(void); 16void music_scale_user(void);
16 17
@@ -24,4 +25,6 @@ void matrix_scan_music(void);
24 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), } 25 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
25#endif 26#endif
26 27
28#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
29
27#endif \ No newline at end of file 30#endif \ No newline at end of file
diff --git a/quantum/quantum.c b/quantum/quantum.c
index 83fa87708..7a27a568a 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -153,6 +153,9 @@ bool process_record_quantum(keyrecord_t *record) {
153 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED) 153 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
154 process_midi(keycode, record) && 154 process_midi(keycode, record) &&
155 #endif 155 #endif
156 #ifdef AUDIO_ENABLE
157 process_audio(keycode, record) &&
158 #endif
156 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) 159 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
157 process_music(keycode, record) && 160 process_music(keycode, record) &&
158 #endif 161 #endif
diff --git a/quantum/quantum.h b/quantum/quantum.h
index 580d51202..77732d43f 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -35,11 +35,16 @@ extern uint32_t default_layer_state;
35 35
36#ifdef MIDI_ENABLE 36#ifdef MIDI_ENABLE
37 #include <lufa.h> 37 #include <lufa.h>
38#ifdef MIDI_ADVANCED
38 #include "process_midi.h" 39 #include "process_midi.h"
39#endif 40#endif
41#endif // MIDI_ENABLE
40 42
41#ifdef AUDIO_ENABLE 43#ifdef AUDIO_ENABLE
42 #include "audio.h" 44 #include "process_audio.h"
45#endif
46
47#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
43 #include "process_music.h" 48 #include "process_music.h"
44#endif 49#endif
45 50