Skip to content

Commit 1000799

Browse files
author
Gabriel Young
committed
Factor basic note processing into respective processors
1 parent 525be99 commit 1000799

File tree

9 files changed

+184
-83
lines changed

9 files changed

+184
-83
lines changed

build_keyboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ endif
157157
ifeq ($(strip $(AUDIO_ENABLE)), yes)
158158
OPT_DEFS += -DAUDIO_ENABLE
159159
SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
160+
SRC += $(QUANTUM_DIR)/process_keycode/process_audio.c
160161
SRC += $(QUANTUM_DIR)/audio/audio.c
161162
SRC += $(QUANTUM_DIR)/audio/voices.c
162163
SRC += $(QUANTUM_DIR)/audio/luts.c
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "process_audio.h"
2+
#include "audio.h"
3+
4+
static 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+
10+
bool 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+
49+
void process_audio_noteon(uint8_t note) {
50+
play_note(compute_freq_for_midi_note(note), 0xF);
51+
}
52+
53+
void process_audio_noteoff(uint8_t note) {
54+
stop_note(compute_freq_for_midi_note(note));
55+
}
56+
57+
void process_audio_stop_all_notes(void) {
58+
stop_all_notes();
59+
}
60+
61+
__attribute__ ((weak))
62+
void audio_on_user() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef PROCESS_AUDIO_H
2+
#define PROCESS_AUDIO_H
3+
4+
bool process_audio(uint16_t keycode, keyrecord_t *record);
5+
void process_audio_noteon(uint8_t note);
6+
void process_audio_noteoff(uint8_t note);
7+
void process_audio_stop_all_notes(void);
8+
9+
void audio_on_user(void);
10+
11+
#endif

quantum/process_keycode/process_midi.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#include "process_midi.h"
22

3-
#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
3+
#ifdef MIDI_ENABLE
4+
#include "midi.h"
5+
6+
#ifdef MIDI_BASIC
7+
8+
void process_midi_basic_noteon(uint8_t note)
9+
{
10+
midi_send_noteon(&midi_device, 0, note, 128);
11+
}
12+
13+
void process_midi_basic_noteoff(uint8_t note)
14+
{
15+
midi_send_noteoff(&midi_device, 0, note, 0);
16+
}
17+
18+
void 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
426

527
#include "timer.h"
628

@@ -165,7 +187,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
165187
case MI_ALLOFF:
166188
if (record->event.pressed) {
167189
midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
168-
dprintf("midi off\n");
190+
dprintf("midi all notes off\n");
169191
}
170192
return false;
171193
case MI_SUS:
@@ -212,3 +234,5 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
212234
}
213235

214236
#endif // MIDI_ADVANCED
237+
238+
#endif // MIDI_ENABLE

quantum/process_keycode/process_midi.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
#define PROCESS_MIDI_H
33

44
#include "quantum.h"
5-
#include "midi.h"
65

6+
#ifdef MIDI_ENABLE
7+
8+
#ifdef MIDI_BASIC
9+
void process_midi_basic_noteon(uint8_t note);
10+
void process_midi_basic_noteoff(uint8_t note);
11+
void process_midi_basic_stop_all_notes(void);
12+
#endif
13+
14+
#ifdef MIDI_ADVANCED
715
typedef union {
816
uint32_t raw;
917
struct {
@@ -25,5 +33,8 @@ bool process_midi(uint16_t keycode, keyrecord_t *record);
2533
#define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
2634

2735
uint8_t midi_compute_note(uint16_t keycode);
36+
#endif // MIDI_ADVANCED
37+
38+
#endif // MIDI_ENABLE
2839

2940
#endif
Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include "process_music.h"
22

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+
312
bool music_activated = false;
413
uint8_t music_starting_note = 0x0C;
514
int music_offset = 7;
@@ -8,38 +17,41 @@ int music_offset = 7;
817
static bool music_sequence_recording = false;
918
static bool music_sequence_recorded = false;
1019
static bool music_sequence_playing = false;
11-
static float music_sequence[16] = {0};
20+
static uint8_t music_sequence[16] = {0};
1221
static uint8_t music_sequence_count = 0;
1322
static uint8_t music_sequence_position = 0;
1423

1524
static uint16_t music_sequence_timer = 0;
1625
static uint16_t music_sequence_interval = 100;
1726

18-
bool process_music(uint16_t keycode, keyrecord_t *record) {
27+
static 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+
}
1935

20-
#ifdef AUDIO_ENABLE
21-
if (keycode == AU_ON && record->event.pressed) {
22-
audio_on();
23-
return false;
24-
}
36+
static void music_noteoff(uint8_t note) {
37+
#ifdef AUDIO_ENABLE
38+
process_audio_noteoff(note);
39+
#endif
40+
#if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
41+
process_midi_basic_noteoff(note);
42+
#endif
43+
}
2544

26-
if (keycode == AU_OFF && record->event.pressed) {
27-
audio_off();
28-
return false;
29-
}
45+
static void music_all_notes_off(void) {
46+
#ifdef AUDIO_ENABLE
47+
process_audio_stop_all_notes();
48+
#endif
49+
#if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
50+
process_midi_basic_stop_all_notes();
51+
#endif
52+
}
3053

31-
if (keycode == AU_TOG && record->event.pressed) {
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
54+
bool process_music(uint16_t keycode, keyrecord_t *record) {
4355

4456
if (keycode == MU_ON && record->event.pressed) {
4557
music_on();
@@ -63,26 +75,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
6375
return false;
6476
}
6577

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-
8078
if (music_activated) {
8179

8280
if (keycode == KC_LCTL && record->event.pressed) { // Start recording
83-
#ifdef AUDIO_ENABLE
84-
stop_all_notes();
85-
#endif
81+
music_all_notes_off();
8682
music_sequence_recording = true;
8783
music_sequence_recorded = false;
8884
music_sequence_playing = false;
@@ -91,9 +87,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
9187
}
9288

9389
if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
94-
#ifdef AUDIO_ENABLE
95-
stop_all_notes();
96-
#endif
90+
music_all_notes_off();
9791
if (music_sequence_recording) { // was recording
9892
music_sequence_recorded = true;
9993
}
@@ -103,9 +97,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
10397
}
10498

10599
if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
106-
#ifdef AUDIO_ENABLE
107-
stop_all_notes();
108-
#endif
100+
music_all_notes_off();
109101
music_sequence_recording = false;
110102
music_sequence_playing = true;
111103
music_sequence_position = 0;
@@ -124,32 +116,27 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
124116
music_sequence_interval+=10;
125117
return false;
126118
}
119+
127120
#define MUSIC_MODE_GUITAR
128121

129-
#ifdef AUDIO_ENABLE
130122
#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);
132124
#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);
134126
#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);
136128
#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);
138130
#endif
139-
#endif // AUDIO_ENABLE
140131

141132
if (record->event.pressed) {
142-
#ifdef AUDIO_ENABLE
143-
play_note(freq, 0xF);
133+
music_noteon(note);
144134
if (music_sequence_recording) {
145-
music_sequence[music_sequence_count] = freq;
135+
music_sequence[music_sequence_count] = note;
146136
music_sequence_count++;
147137
}
148-
#endif
149138
} else {
150-
#ifdef AUDIO_ENABLE
151-
stop_note(freq);
152-
#endif
139+
music_noteoff(note);
153140
}
154141

155142
if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
@@ -177,32 +164,26 @@ void music_on(void) {
177164

178165
void music_off(void) {
179166
music_activated = 0;
180-
#ifdef AUDIO_ENABLE
181-
stop_all_notes();
182-
#endif
167+
music_all_notes_off();
183168
}
184169

185-
186-
__attribute__ ((weak))
187-
void music_on_user() {}
188-
189-
#ifdef AUDIO_ENABLE
190-
__attribute__ ((weak))
191-
void audio_on_user() {}
192-
#endif
193-
194-
__attribute__ ((weak))
195-
void music_scale_user() {}
196-
197170
void matrix_scan_music(void) {
198171
if (music_sequence_playing) {
199172
if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
200173
music_sequence_timer = timer_read();
201-
#ifdef AUDIO_ENABLE
202-
stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
203-
play_note(music_sequence[music_sequence_position], 0xF);
204-
#endif
174+
uint8_t prev_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];
176+
music_noteoff(prev_note);
177+
music_noteon(next_note);
205178
music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
206179
}
207180
}
208181
}
182+
183+
__attribute__ ((weak))
184+
void music_on_user() {}
185+
186+
__attribute__ ((weak))
187+
void music_scale_user() {}
188+
189+
#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))

0 commit comments

Comments
 (0)