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