diff --git a/config/forge.cfg b/config/forge.cfg index d6b501c..70bec76 100644 --- a/config/forge.cfg +++ b/config/forge.cfg @@ -37,4 +37,7 @@ SUB_2_PREFIX=fx_ SUB_3_PREFIX=mix_ SUB_VARIANT_COUNT=16 -MIDI_HW=hw:CARD=nanoKONTROL2 \ No newline at end of file +MIDI_HW=hw:CARD=nanoKONTROL2 + +SELECT_PAGE_COUNT=3 +SELECT_PAGE1= \ No newline at end of file diff --git a/src/forge.c b/src/forge.c index 20d20eb..229a611 100644 --- a/src/forge.c +++ b/src/forge.c @@ -8,6 +8,7 @@ #include "config_file.h" #include "file.h" #include "forge.h" +#include "midi.h" #include "rand.h" #include "shaders.h" #include "shared.h" @@ -25,6 +26,7 @@ static File *fragment_shaders; static File common_shader_code; static Timer timer; static ConfigFile config; +static MidiDevice midi; static void compute_fps() { double fps; @@ -251,6 +253,16 @@ void forge_run(Parameters params) { init_inputs(params.video_in, params.video_in_count, params.video_size); + start_video_captures(params.video_in_count); + + midi = midi_open(config_file_get_str(config, "MIDI_HW", "hw")); + + if (midi.error) { + params.demo = true; + } else { + midi_background_listen(midi, context); + } + window_startup(error_callback); context->internal_height = params.internal_size; @@ -290,8 +302,6 @@ void forge_run(Parameters params) { timer = timer_init(30); - start_video_captures(params.video_in_count); - log_info("Initialized"); while ((window_output == NULL || !window_should_close(window_output)) && @@ -319,6 +329,8 @@ void forge_run(Parameters params) { free_video_captures(params.video_in_count); + midi_close(midi); + free_context(); free_files(frag_count); diff --git a/src/midi.c b/src/midi.c index 8a7760d..00a80d4 100644 --- a/src/midi.c +++ b/src/midi.c @@ -16,10 +16,11 @@ void midi_close(MidiDevice device) { MidiDevice midi_open(char *name) { MidiDevice device; + device.name = name; device.input = NULL; device.output = NULL; - snd_rawmidi_open(&device.input, &device.output, name, 0); + snd_rawmidi_open(&device.input, &device.output, name, SND_RAWMIDI_NONBLOCK); device.error = device.input == NULL || device.output == NULL; @@ -32,5 +33,31 @@ MidiDevice midi_open(char *name) { return device; } +void midi_background_listen(MidiDevice device, SharedContext *context) { + pid_t pid; + int bytes_read; + unsigned char buffer[3]; + + pid = fork(); + if (pid < 0) { + log_error("Could not create subprocess"); + return; + } + if (pid == 0) { + return; + } + log_info("(%s) background acquisition started (pid: %d)", device.name, pid); + + 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); + } + } + + log_info("(%s) background acquisition stopped by main thread (pid: %d)", + device.name, pid); +} + // int bytes_read = snd_rawmidi_read(input, input_buffer, sizeof(input_buffer)); // snd_rawmidi_write(output, buffer, 3); \ No newline at end of file diff --git a/src/midi.h b/src/midi.h index 69a7c14..bdcb4e6 100644 --- a/src/midi.h +++ b/src/midi.h @@ -5,5 +5,6 @@ MidiDevice midi_open(char *name); void midi_close(MidiDevice device); +void midi_background_listen(MidiDevice device, SharedContext *context); #endif /* MIDI_H */ \ No newline at end of file diff --git a/src/types.h b/src/types.h index c2467f4..e629abe 100644 --- a/src/types.h +++ b/src/types.h @@ -153,6 +153,7 @@ typedef struct ConfigFileItem { typedef struct MidiDevice { bool error; + char *name; snd_rawmidi_t *input; snd_rawmidi_t *output; } MidiDevice; diff --git a/src/video.c b/src/video.c index 4973438..d740740 100644 --- a/src/video.c +++ b/src/video.c @@ -341,7 +341,7 @@ void video_background_read(VideoCapture *video_capture, SharedContext *context, if (pid == 0) { return; } - log_info("%s background acquisition started (pid: %d)", video_capture->name, + log_info("(%s) background acquisition started (pid: %d)", video_capture->name, pid); timer = timer_init(30); @@ -355,13 +355,12 @@ void video_background_read(VideoCapture *video_capture, SharedContext *context, } } if (context->stop) { - log_info("%s background acquisition stopped by main thread (pid: %d)", + log_info("(%s) background acquisition stopped by main thread (pid: %d)", video_capture->name, pid); } else { - log_info("%s background acquisition stopped after error (pid: %d)", + log_info("(%s) background acquisition stopped after error (pid: %d)", video_capture->name, pid); } - window_terminate(); exit(context->stop ? EXIT_SUCCESS : EXIT_FAILURE); }