midi write state
This commit is contained in:
@@ -21,6 +21,10 @@
|
||||
#define UNSET_MIDI_CODE 300
|
||||
#endif
|
||||
|
||||
#ifndef MIDI_MAX
|
||||
#define MIDI_MAX 127
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE 1024
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -207,7 +207,7 @@ static void key_callback(Window *window, int key,
|
||||
}
|
||||
}
|
||||
|
||||
static void midi_callback(unsigned char code, float value) {
|
||||
static void midi_callback(unsigned char code, unsigned char value) {
|
||||
state_apply_event(context, state_config, midi, code, value);
|
||||
}
|
||||
|
||||
|
||||
+4
-5
@@ -1,7 +1,6 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "types.h"
|
||||
|
||||
@@ -21,19 +20,19 @@ MidiDevice midi_open(char *name) {
|
||||
return device;
|
||||
}
|
||||
|
||||
void midi_write(MidiDevice device, unsigned char code, float value) {
|
||||
void midi_write(MidiDevice device, unsigned char code, unsigned char value) {
|
||||
unsigned char buffer[3];
|
||||
|
||||
buffer[0] = 0xB0;
|
||||
buffer[1] = code;
|
||||
buffer[2] = (unsigned char)(128 * value);
|
||||
buffer[2] = value;
|
||||
|
||||
snd_rawmidi_write(device.output, buffer, 3);
|
||||
}
|
||||
|
||||
bool midi_background_listen(MidiDevice device, SharedContext *context,
|
||||
void (*event_callback)(unsigned char code,
|
||||
float value)) {
|
||||
unsigned char value)) {
|
||||
pid_t pid;
|
||||
int bytes_read;
|
||||
unsigned char buffer[3];
|
||||
@@ -51,7 +50,7 @@ bool midi_background_listen(MidiDevice device, SharedContext *context,
|
||||
while (!context->stop) {
|
||||
bytes_read = snd_rawmidi_read(device.input, buffer, 3);
|
||||
if (bytes_read == 3) {
|
||||
event_callback(buffer[1], (float)buffer[2] / 256.0);
|
||||
event_callback(buffer[1], buffer[2]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -4,9 +4,9 @@
|
||||
#define MIDI_H
|
||||
|
||||
MidiDevice midi_open(char *name);
|
||||
void midi_write(MidiDevice device, unsigned char code, float value);
|
||||
void midi_write(MidiDevice device, unsigned char code, unsigned char value);
|
||||
bool midi_background_listen(MidiDevice device, SharedContext *context,
|
||||
void (*event_callback)(unsigned char code,
|
||||
float value));
|
||||
unsigned char value));
|
||||
|
||||
#endif /* MIDI_H */
|
||||
+13
-11
@@ -109,7 +109,8 @@ StateConfig state_parse_config(ConfigFile config) {
|
||||
return state_config;
|
||||
}
|
||||
|
||||
static void safe_midi_write(MidiDevice midi, unsigned int code, float value) {
|
||||
static void safe_midi_write(MidiDevice midi, unsigned int code,
|
||||
unsigned char value) {
|
||||
if (code != UNSET_MIDI_CODE) {
|
||||
midi_write(midi, code, value);
|
||||
}
|
||||
@@ -121,7 +122,7 @@ static void update_page(SharedContext *context, StateConfig state_config,
|
||||
// SHOW PAGE
|
||||
for (i = 0; i < state_config.select_page_codes.length; i++) {
|
||||
safe_midi_write(midi, state_config.select_page_codes.values[i],
|
||||
i == context->page ? 1.0 : 0);
|
||||
i == context->page ? MIDI_MAX : 0);
|
||||
}
|
||||
|
||||
// SHOW PAGE ITEM
|
||||
@@ -131,9 +132,10 @@ static void update_page(SharedContext *context, StateConfig state_config,
|
||||
if (context->state[context->selected] >= page_item_min &&
|
||||
context->state[context->selected] < page_item_max) {
|
||||
for (i = 0; i < state_config.select_item_codes.length; i++) {
|
||||
safe_midi_write(
|
||||
midi, state_config.select_item_codes.values[i],
|
||||
i == context->state[context->selected] - page_item_min ? 1 : 0);
|
||||
safe_midi_write(midi, state_config.select_item_codes.values[i],
|
||||
i == context->state[context->selected] - page_item_min
|
||||
? MIDI_MAX
|
||||
: 0);
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < state_config.select_item_codes.length; i++) {
|
||||
@@ -148,12 +150,13 @@ static void update_selected(SharedContext *context, StateConfig state_config,
|
||||
|
||||
for (i = 0; i < state_config.select_frag_codes.length; i++) {
|
||||
safe_midi_write(midi, state_config.select_frag_codes.values[i],
|
||||
i == context->selected ? 1.0 : 0);
|
||||
i == context->selected ? MIDI_MAX : 0);
|
||||
}
|
||||
}
|
||||
|
||||
void state_apply_event(SharedContext *context, StateConfig state_config,
|
||||
MidiDevice midi, unsigned char code, float value) {
|
||||
MidiDevice midi, unsigned char code,
|
||||
unsigned char value) {
|
||||
unsigned int index, part;
|
||||
bool found;
|
||||
|
||||
@@ -185,7 +188,7 @@ void state_apply_event(SharedContext *context, StateConfig state_config,
|
||||
if (index != ARRAY_NOT_FOUND) {
|
||||
found = true;
|
||||
if (value > 0) {
|
||||
context->state[context->selected - 1] =
|
||||
context->state[context->selected] =
|
||||
context->page * state_config.select_item_codes.length + index;
|
||||
update_page(context, state_config, midi);
|
||||
}
|
||||
@@ -204,17 +207,16 @@ void state_apply_event(SharedContext *context, StateConfig state_config,
|
||||
|
||||
// TODO values
|
||||
if (!found) {
|
||||
log_trace("unknown midi: %d %.2f", code, value);
|
||||
log_trace("unknown midi: %d %d", code, value);
|
||||
midi_write(midi, code, value);
|
||||
} else {
|
||||
log_trace("midi: %d %.2f", code, value);
|
||||
log_trace("midi: %d %d", code, value);
|
||||
}
|
||||
}
|
||||
|
||||
bool state_background_midi_write(SharedContext *context,
|
||||
StateConfig state_config, MidiDevice midi) {
|
||||
pid_t pid;
|
||||
unsigned int i, page_item_min, page_item_max;
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
|
||||
+2
-1
@@ -6,7 +6,8 @@
|
||||
StateConfig state_parse_config(ConfigFile config);
|
||||
|
||||
void state_apply_event(SharedContext *context, StateConfig state_config,
|
||||
MidiDevice midi, unsigned char code, float value);
|
||||
MidiDevice midi, unsigned char code,
|
||||
unsigned char value);
|
||||
|
||||
bool state_background_midi_write(SharedContext *context,
|
||||
StateConfig state_config, MidiDevice midi);
|
||||
|
||||
Reference in New Issue
Block a user