/* Copyright (C) 2005 Ian Esten This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include pthread_t output_pthread; jack_ringbuffer_t* midievbuf; jack_ringbuffer_t* mididatabuf; jack_nframes_t bufsize; jack_port_t* inbuf; unsigned char* readbuf; /* for the printf thread to read into from the ringbuffer */ jack_nframes_t sample_rate = 0; volatile int running = 0; /* for stopping the threads */ void* print_midi_thread(void* ommitted_parameter_name) { jack_midi_event_t ev; int read_space = 0; int ev_bytes_read = 0; int data_bytes_read = 0; int i = 0; while(running) { read_space = jack_ringbuffer_read_space(midievbuf); while(read_space > 0) { ev_bytes_read = jack_ringbuffer_peek(midievbuf, (char*)&ev, sizeof(jack_midi_event_t)); if(ev_bytes_read != sizeof(jack_midi_event_t)) continue; data_bytes_read = jack_ringbuffer_peek(mididatabuf, readbuf, ev.size); read_space -= ev_bytes_read; jack_ringbuffer_read_advance(midievbuf, ev_bytes_read); jack_ringbuffer_read_advance(mididatabuf, data_bytes_read); printf("got an event at timestamp %d. data is ", ev.time); for(i=0; ievent_count; i++) { jack_midi_event_get(&ev, buf, i, nframes); jack_ringbuffer_write(midievbuf, (char*)&ev, sizeof(jack_midi_event_t)); jack_ringbuffer_write(mididatabuf, ev.buffer, ev.size); } } void jack_shutdown(void *arg) { printf("jack_shutdown called, goodbye!\n"); running = 0; } int main() { jack_client_t* jack_midi_client = jack_client_new("printjackmidi"); if(jack_midi_client == NULL) { printf("printjackmidi could not create client. is jackd running?\n"); return 1; } inbuf = jack_port_register(jack_midi_client, "in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0); bufsize = jack_get_buffer_size(jack_midi_client); /* somewhat arbitrary buffer sizes, here. */ midievbuf = jack_ringbuffer_create(bufsize * sizeof(jack_midi_event_t)); mididatabuf = jack_ringbuffer_create(bufsize * 3 * sizeof(jack_midi_event_t)); jack_ringbuffer_reset(midievbuf); jack_ringbuffer_reset(mididatabuf); readbuf = malloc(bufsize * 3 * sizeof(jack_midi_event_t)); sample_rate = jack_get_sample_rate(jack_midi_client); running = 1; pthread_create(&output_pthread, NULL, print_midi_thread, NULL); /* register callbacks and start */ jack_set_process_callback(jack_midi_client, process, NULL); jack_activate(jack_midi_client); while(running) sleep(1); jack_deactivate(jack_midi_client); jack_ringbuffer_free(midievbuf); jack_ringbuffer_free(mididatabuf); free(readbuf); }