From 9846bb2d6426e8f79415a943b38e42c833e6b02e Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 15 Feb 2018 16:45:21 +0100 Subject: clock, initial notes --- sv.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 4 deletions(-) (limited to 'sv.c') diff --git a/sv.c b/sv.c index 6e37600..de792f0 100644 --- a/sv.c +++ b/sv.c @@ -7,12 +7,15 @@ #include #include #include +#include #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" +#define MIDICHK(stmt, msg) if((stmt) < 0) {puts("ERROR: "#msg); exit(1);} + int width = 1920; int height = 1080; GLFWwindow* window; @@ -47,6 +50,15 @@ typedef struct Param { Parameter parameters[32]; +static snd_seq_t *seq_handle; +static int in_port; + +void midi_open(void) { + MIDICHK(snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0), "Could not open sequencer"); + MIDICHK(snd_seq_set_client_name(seq_handle, "Midi Listener"), "Could not set client name"); + MIDICHK(in_port = snd_seq_create_simple_port(seq_handle, "listen:in", SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION), "Could not open port"); +} + void screenshot() { unsigned char pixels[width*height*4]; time_t current_time = time(NULL); @@ -196,6 +208,48 @@ void updateParams(int force) { } } +int ticks =0; +int running = 0; +int note = 0; +int vel = 0; + +void *readMidi() { + midi_open(); + while(1) { + snd_seq_event_t *ev = NULL; + snd_seq_event_input(seq_handle, &ev); + if (ev->type == SND_SEQ_EVENT_NOTEON) { + note = ev->data.note.note; + vel = ev->data.note.velocity; + } + else if (ev->type == SND_SEQ_EVENT_NOTEOFF) { + vel = 0; + } + else if(ev->type == SND_SEQ_EVENT_CONTROLLER) + printf("[%d] Control: %2x val(%2x)\n", ev->time.tick, + ev->data.control.param, + ev->data.control.value); + else if(ev->type == SND_SEQ_EVENT_SONGPOS) { + ticks = ev->data.control.value*24; + printf("[%d] SPP: %i val(%i)\n", ev->time.tick, + ev->data.control.param, + ev->data.control.value); + } + else if(ev->type == SND_SEQ_EVENT_START) + running = 1; + else if(ev->type == SND_SEQ_EVENT_STOP) + running = 0; + else if(ev->type == SND_SEQ_EVENT_CONTINUE) + running = 1; + else if(ev->type == SND_SEQ_EVENT_CLOCK) { + if (running == 1) ticks++; + } + else + printf("[%d] Unknown: Unhandled Event Received\n", ev->time.tick); + } +} + + void *readStdin() { while (1) { char * str; @@ -252,13 +306,19 @@ int main(int argc, char **argv) { glTextureStorage2D(backbuffer,1,GL_RGBA32F,width,height); glBindImageTexture(0,backbuffer, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); - pthread_t stdin_t; - pthread_create(&stdin_t, NULL, readStdin, NULL); + //pthread_t stdin_t; + //pthread_create(&stdin_t, NULL, readStdin, NULL); + pthread_t midiin_t; + pthread_create(&midiin_t, NULL, readMidi, NULL); pthread_t frag_t; pthread_create(&frag_t, NULL, watchShader, NULL); while (!glfwWindowShouldClose(window)) { - glUniform1f(glGetUniformLocation(shader.id, "time"),glfwGetTime()); + //glUniform1f(glGetUniformLocation(shader.id, "time"),glfwGetTime()); + //glUniform1f(glGetUniformLocation(shader.id, "time"),glfwGetTime()); + glUniform1f(glGetUniformLocation(shader.id, "ticks"),(float)ticks); + //printf("%i\n",note); + glUniform1i(glGetUniformLocation(shader.id, "program"),note-36); if (shader.new) { createShader(); glUniform2f(glGetUniformLocation(shader.id, "resolution"),width,height); // important!! @@ -273,7 +333,8 @@ int main(int argc, char **argv) { glfwPollEvents(); } - pthread_cancel(stdin_t); + //pthread_cancel(stdin_t); + pthread_cancel(midiin_t); pthread_cancel(frag_t); glfwDestroyWindow(window); glfwTerminate(); -- cgit v1.2.3