This commit is contained in:
2025-09-28 17:26:03 +02:00
parent 6905d0017d
commit 9c50495c85
4 changed files with 128 additions and 6 deletions
+6 -1
View File
@@ -212,6 +212,11 @@ static void key_callback(Window *window, int key,
}
}
static void midi_callback(unsigned char code, float value) {
log_debug("midi: %d %.2f", code, value);
midi_write(midi, code, value);
}
static void loop(bool hr) {
if (hr) {
hot_reload();
@@ -264,7 +269,7 @@ void forge_run(Parameters params) {
if (midi.error) {
params.demo = true;
} else {
if (!midi_background_listen(midi, context)) {
if (!midi_background_listen(midi, context, midi_callback)) {
return;
}
}
+16 -2
View File
@@ -20,7 +20,19 @@ MidiDevice midi_open(char *name) {
return device;
}
bool midi_background_listen(MidiDevice device, SharedContext *context) {
void midi_write(MidiDevice device, unsigned char code, float value) {
unsigned char buffer[3];
buffer[0] = 0xB0;
buffer[1] = code;
buffer[2] = (unsigned char)(256 * value);
snd_rawmidi_write(device.output, buffer, 3);
}
bool midi_background_listen(MidiDevice device, SharedContext *context,
void (*event_callback)(unsigned char code,
float value)) {
pid_t pid;
int bytes_read;
unsigned char buffer[3];
@@ -38,7 +50,9 @@ bool midi_background_listen(MidiDevice device, SharedContext *context) {
while (!context->stop) {
bytes_read = snd_rawmidi_read(device.input, buffer, 3);
if (bytes_read == 3) {
log_debug("midi: %d %.2f", buffer[1], (float)buffer[2] / 256);
event_callback(buffer[1], (float)buffer[2] / 256.0);
log_debug("midi: %02x %d %.2f", buffer[0], buffer[1],
(float)buffer[2] / 256);
}
}
+4 -1
View File
@@ -4,6 +4,9 @@
#define MIDI_H
MidiDevice midi_open(char *name);
bool midi_background_listen(MidiDevice device, SharedContext *context);
void midi_write(MidiDevice device, unsigned char code, float value);
bool midi_background_listen(MidiDevice device, SharedContext *context,
void (*event_callback)(unsigned char code,
float value));
#endif /* MIDI_H */