Follow changes in pkg.m4, fixing Gnome bug #166537, that happened on 2005-10-17 and...
[gmidimonitor.git] / jack.c
blobbb1ff29612aa5b72664fec1e62e305ff9aec3046
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of gmidimonitor
6 * Copyright (C) 2007 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *****************************************************************************/
23 #include <jack/jack.h>
24 #include <jack/midiport.h>
25 #include <gtk/gtk.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <assert.h>
30 #include "config.h"
32 #include "common.h"
33 #include "jack.h"
34 #include "memory_atomic.h"
35 //#define LOG_LEVEL LOG_LEVEL_DEBUG
36 #include "log.h"
37 #include "list.h"
38 #include "glade.h"
39 #include "gm.h"
40 #include "sysex.h"
42 #include "jack_compat.c"
44 jack_client_t * g_jack_client;
45 jack_port_t * g_jack_input_port;
46 rtsafe_memory_handle g_memory;
48 struct jack_midi_event_buffer
50 struct list_head siblings;
51 size_t buffer_size;
52 unsigned char buffer[0];
55 pthread_mutex_t g_jack_midi_mutex;
56 //pthread_cond_t g_jack_midi_cond;
58 /* transfer from jack process callback to main thread */
59 /* new events are being put at list begining */
60 struct list_head g_jack_midi_events_pending;
62 /* temporary storage used by jack process callback in case g_jack_midi_events_pending is locked by main thread */
63 /* new events are being put at list begining */
64 struct list_head g_jack_midi_events_pending_rt;
66 pthread_t g_jack_midi_tid; /* jack_midi_thread id */
68 gboolean g_jack_midi_thread_exit; /* whther jack_midi_thread shoud exit */
70 int
71 jack_process(jack_nframes_t nframes, void * context)
73 int ret;
74 void * port_buf;
75 jack_midi_event_t in_event;
76 jack_nframes_t event_count;
77 jack_position_t pos;
78 jack_nframes_t i;
79 struct jack_midi_event_buffer * event_buffer;
81 port_buf = jack_port_get_buffer(g_jack_input_port, nframes);
82 event_count = jack_midi_get_event_count(port_buf);
83 jack_transport_query(g_jack_client, &pos);
85 for (i = 0 ; i < event_count; i++)
87 jack_midi_event_get(&in_event, port_buf, i);
89 LOG_DEBUG("midi event with size %u received. (jack_process)", (unsigned int)in_event.size);
91 /* allocate memory for buffer copy */
92 event_buffer = rtsafe_memory_allocate(g_memory, sizeof(struct jack_midi_event_buffer) + in_event.size);
93 if (event_buffer == NULL)
95 LOG_ERROR("Ignored midi event with size %u because memory allocation failed.", (unsigned int)in_event.size);
96 continue;
99 /* copy buffer data */
100 memcpy(event_buffer->buffer, in_event.buffer, in_event.size);
101 event_buffer->buffer_size = in_event.size;
103 /* Add event buffer to g_jack_midi_events_pending_rt list */
104 list_add(&event_buffer->siblings, &g_jack_midi_events_pending_rt);
107 ret = pthread_mutex_trylock(&g_jack_midi_mutex);
108 if (ret == 0)
110 /* We are lucky and we got the lock */
111 LOG_DEBUG("Lucky");
113 /* Move pending events in g_jack_midi_events_pending_rt to begining of g_jack_midi_events_pending */
114 list_splice_init(&g_jack_midi_events_pending_rt, &g_jack_midi_events_pending);
116 /* wakeup jack_midi_thread */
117 //ret = pthread_cond_broadcast(&g_jack_midi_cond);
118 //LOG_DEBUG("pthread_cond_broadcast() result is %d", ret);
119 ret = pthread_mutex_unlock(&g_jack_midi_mutex);
120 //LOG_DEBUG("pthread_mutex_unlock() result is %d", ret);
122 else
124 /* We are not lucky, jack_midi_thread has locked the mutex */
125 LOG_DEBUG("Not lucky (%d)", ret);
128 return 0;
131 gboolean
132 jack_midi_decode(
133 guint8 * buffer,
134 size_t buffer_size,
135 GString * msg_str_ptr,
136 GString * channel_str_ptr)
138 size_t i;
139 unsigned int channel;
140 unsigned int note;
141 unsigned int velocity;
142 const char * note_name;
143 unsigned int octave;
144 unsigned int controller;
145 unsigned int value;
146 const char * controller_name;
147 signed int pitch;
148 const char * drum_name;
150 if (buffer_size == 1 && buffer[0] == 0xFE)
152 g_string_sprintf(msg_str_ptr, "Active sensing");
153 return FALSE; /* disable */
156 if (buffer_size == 3 && (buffer[0] >> 4) == 0x08)
158 channel = (buffer[0] & 0x0F) + 1; /* 1 .. 16 */
159 assert(channel >= 1 && channel <= 16);
161 if (channel == 10)
163 return FALSE; /* ignore note off for drums */
166 note = buffer[1];
167 if (note > 127)
169 goto unknown_event;
172 velocity = buffer[2];
173 if (velocity > 127)
175 goto unknown_event;
178 note_name = g_note_names[note % 12];
179 octave = note / 12 - 1;
181 g_string_sprintf(channel_str_ptr, "%u", channel);
183 g_string_sprintf(
184 msg_str_ptr,
185 "Note off, %s, octave %d, velocity %u",
186 note_name,
187 octave,
188 velocity);
190 return TRUE;
193 if (buffer_size == 3 && (buffer[0] >> 4) == 0x09)
195 channel = (buffer[0] & 0x0F) + 1; /* 1 .. 16 */
196 assert(channel >= 1 && channel <= 16);
198 note = buffer[1];
199 if (note > 127)
201 goto unknown_event;
204 velocity = buffer[2];
205 if (velocity > 127)
207 goto unknown_event;
210 note_name = g_note_names[note % 12];
211 octave = note / 12 - 1;
213 g_string_sprintf(channel_str_ptr, "%u", channel);
215 if (channel == 10)
217 drum_name = gm_get_drum_name(note);
219 else
221 drum_name = NULL;
224 if (drum_name != NULL)
226 if (velocity == 0)
228 return FALSE; /* ignore note off for drums */
231 g_string_sprintf(
232 msg_str_ptr,
233 "Drum: %s (%s, octave %d, velocity %u)",
234 drum_name,
235 note_name,
236 octave,
237 velocity);
239 else
241 g_string_sprintf(
242 msg_str_ptr,
243 "Note on, %s, octave %d, velocity %u",
244 note_name,
245 octave,
246 velocity);
249 return TRUE;
252 if (buffer_size == 3 && (buffer[0] >> 4) == 0x0A)
254 channel = (buffer[0] & 0x0F) + 1; /* 1 .. 16 */
255 assert(channel >= 1 && channel <= 16);
257 note = buffer[1];
258 if (note > 127)
260 goto unknown_event;
263 velocity = buffer[2];
264 if (velocity > 127)
266 goto unknown_event;
269 note_name = g_note_names[note % 12];
270 octave = note / 12 - 1;
272 g_string_sprintf(channel_str_ptr, "%u", channel);
274 g_string_sprintf(
275 msg_str_ptr,
276 "Polyphonic Key Pressure (Aftertouch), %s, octave %d, velocity %u",
277 note_name,
278 octave,
279 velocity);
281 return TRUE;
284 if (buffer_size == 3 && (buffer[0] >> 4) == 0x0B)
286 channel = (buffer[0] & 0x0F) + 1; /* 1 .. 16 */
287 assert(channel >= 1 && channel <= 16);
289 controller = buffer[1];
290 if (controller > 127)
292 goto unknown_event;
295 value = buffer[2];
296 if (value > 127)
298 goto unknown_event;
301 g_string_sprintf(channel_str_ptr, "%u", channel);
303 controller_name = gm_get_controller_name(controller);
305 if (controller_name != NULL)
307 g_string_sprintf(
308 msg_str_ptr,
309 "CC %s (%u), value %u",
310 controller_name,
311 controller,
312 value);
314 else
316 g_string_sprintf(
317 msg_str_ptr,
318 "CC %u, value %u",
319 controller,
320 value);
323 return TRUE;
326 if (buffer_size == 2 && (buffer[0] >> 4) == 0x0C)
328 channel = (buffer[0] & 0x0F) + 1; /* 1 .. 16 */
329 assert(channel >= 1 && channel <= 16);
331 value = buffer[1];
332 if (value > 127)
334 goto unknown_event;
337 g_string_sprintf(channel_str_ptr, "%u", channel);
339 g_string_sprintf(
340 msg_str_ptr,
341 "Program change, %d (%s)",
342 value,
343 gm_get_instrument_name(value));
345 return TRUE;
348 if (buffer_size == 3 && (buffer[0] >> 4) == 0x0E && buffer[1] <= 127 && buffer[1] <= 127)
350 channel = (buffer[0] & 0x0F) + 1; /* 1 .. 16 */
351 assert(channel >= 1 && channel <= 16);
353 pitch = buffer[1];
354 pitch |= (unsigned int)buffer[2] << 7;
355 pitch -= 0x2000;
357 g_string_sprintf(channel_str_ptr, "%u", channel);
359 g_string_sprintf(
360 msg_str_ptr,
361 "Pitchwheel, %d",
362 pitch);
364 return TRUE;
367 if (buffer_size > 0 && buffer[0] == 0xF0)
369 decode_sysex(buffer, buffer_size, msg_str_ptr);
370 return TRUE;
373 if (buffer_size == 1 && buffer[0] == 0xFF)
375 g_string_sprintf(
376 msg_str_ptr,
377 "Reset");
379 return TRUE;
382 unknown_event:
383 g_string_sprintf(
384 msg_str_ptr,
385 "unknown midi event with size %u bytes:",
386 (unsigned int)buffer_size);
388 for (i = 0 ; i < buffer_size ; i++)
390 g_string_append_printf(
391 msg_str_ptr,
392 " %02X",
393 (unsigned int)(buffer[i]));
396 return TRUE;
399 /* The JACK MIDI input handling thread */
400 void *
401 jack_midi_thread(void * context_ptr)
403 struct jack_midi_event_buffer * event_buffer;
404 struct list_head * node_ptr;
405 GtkListStore * list_store_ptr;
406 GtkWidget * child_ptr;
407 GtkTreeIter iter;
408 GString * time_str_ptr;
409 GString * msg_str_ptr;
410 GString * channel_str_ptr;
412 LOG_DEBUG("jack_midi_thread started");
414 child_ptr = get_glade_widget_child(g_main_window_ptr, "list");
416 list_store_ptr = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(child_ptr)));
418 loop:
419 pthread_mutex_lock(&g_jack_midi_mutex);
421 if (g_jack_midi_thread_exit)
423 pthread_mutex_unlock(&g_jack_midi_mutex);
425 LOG_DEBUG("jack_midi_thread exiting");
426 return NULL;
429 rtsafe_memory_sleepy(g_memory);
431 //LOG_DEBUG("checking events...");
433 while (!list_empty(&g_jack_midi_events_pending))
435 node_ptr = g_jack_midi_events_pending.prev;
437 list_del(node_ptr);
439 event_buffer = list_entry(node_ptr, struct jack_midi_event_buffer, siblings);
441 if (g_midi_ignore)
443 goto deallocate_event;
446 LOG_DEBUG("midi event with size %u received.", (unsigned int)event_buffer->buffer_size);
448 time_str_ptr = g_string_new("");
449 channel_str_ptr = g_string_new("");
450 msg_str_ptr = g_string_new("");
452 if (!jack_midi_decode(
453 event_buffer->buffer,
454 event_buffer->buffer_size,
455 msg_str_ptr,
456 channel_str_ptr))
458 /* ignoring specific event */
459 goto deallocate_strings;
462 /* get GTK thread lock */
463 gdk_threads_enter();
465 if (g_row_count >= MAX_LIST_SIZE)
467 gtk_tree_model_get_iter_first(
468 GTK_TREE_MODEL(list_store_ptr),
469 &iter);
471 gtk_list_store_remove(
472 list_store_ptr,
473 &iter);
476 /* Append an empty row to the list store. Iter will point to the new row */
477 gtk_list_store_append(list_store_ptr, &iter);
479 gtk_list_store_set(
480 list_store_ptr,
481 &iter,
482 COL_TIME, time_str_ptr->str,
483 COL_CHANNEL, channel_str_ptr->str,
484 COL_MESSAGE, msg_str_ptr->str,
485 -1);
487 gtk_tree_view_scroll_to_cell(
488 GTK_TREE_VIEW(child_ptr),
489 gtk_tree_model_get_path(
490 gtk_tree_view_get_model(GTK_TREE_VIEW(child_ptr)),
491 &iter),
492 NULL,
493 TRUE,
494 0.0,
495 1.0);
497 /* Force update of scroll position. */
498 /* Is it a bug that it does not update automagically ? */
499 gtk_container_check_resize(GTK_CONTAINER(child_ptr));
501 g_row_count++;
503 /* release GTK thread lock */
504 gdk_threads_leave();
506 deallocate_strings:
507 g_string_free(channel_str_ptr, TRUE);
508 g_string_free(msg_str_ptr, TRUE);
509 g_string_free(time_str_ptr, TRUE);
511 deallocate_event:
512 rtsafe_memory_deallocate(event_buffer);
515 pthread_mutex_unlock(&g_jack_midi_mutex);
517 //LOG_DEBUG("waiting for more events...");
519 //pthread_cond_wait(&g_jack_midi_cond, &g_jack_midi_mutex);
520 usleep(10000);
522 goto loop;
525 void
526 jack_destroy_pending_events()
528 struct jack_midi_event_buffer * event_buffer;
529 struct list_head * node_ptr;
531 while (!list_empty(&g_jack_midi_events_pending))
533 LOG_DEBUG("Destroying pending event");
534 node_ptr = g_jack_midi_events_pending.next;
536 list_del(node_ptr);
538 event_buffer = list_entry(node_ptr, struct jack_midi_event_buffer, siblings);
540 rtsafe_memory_deallocate(event_buffer);
543 while (!list_empty(&g_jack_midi_events_pending_rt))
545 LOG_DEBUG("Destroying realtime pending event");
546 node_ptr = g_jack_midi_events_pending_rt.next;
548 list_del(node_ptr);
550 event_buffer = list_entry(node_ptr, struct jack_midi_event_buffer, siblings);
552 rtsafe_memory_deallocate(event_buffer);
556 gboolean
557 jack_init(const char * name)
559 int ret;
561 LOG_DEBUG("jack_init(\"%s\") called.", name);
563 if (!rtsafe_memory_init(
564 100 * 1024,
565 100,
566 1000,
567 &g_memory))
569 LOG_ERROR("RT-safe memory initialization failed.");
570 goto fail;
573 INIT_LIST_HEAD(&g_jack_midi_events_pending);
574 INIT_LIST_HEAD(&g_jack_midi_events_pending_rt);
576 ret = pthread_mutex_init(&g_jack_midi_mutex, NULL);
577 if (ret != 0)
579 LOG_ERROR("Cannot initialize mutex.");
580 goto fail_uninit_memory;
583 /* ret = pthread_cond_init(&g_jack_midi_cond, NULL); */
584 /* if (ret != 0) */
585 /* { */
586 /* LOG_ERROR("Cannot initialize condition (variable)."); */
587 /* goto fail_uninit_mutex; */
588 /* } */
590 g_jack_midi_thread_exit = FALSE;
592 ret = pthread_create(&g_jack_midi_tid, NULL, jack_midi_thread, NULL);
593 if (ret != 0)
595 LOG_ERROR("Cannot start JACK MIDI thread.");
596 goto fail_uninit_cond;
599 g_jack_client = jack_client_new(name);
600 if (g_jack_client == NULL)
602 LOG_ERROR("Cannot create JACK client.");
603 goto fail_stop_thread;
606 ret = jack_set_process_callback(g_jack_client, jack_process, 0);
607 if (ret != 0)
609 LOG_ERROR("Cannot set JACK process callback.");
610 goto fail_close;
613 g_jack_input_port = jack_port_register(g_jack_client, "midi_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
614 if (g_jack_input_port == NULL)
616 LOG_ERROR("Failed to create input JACK MIDI port");
617 goto fail_close;
620 ret = jack_activate(g_jack_client);
621 if (ret != 0)
623 LOG_ERROR("Cannot activate JACK client.");
624 goto fail_close;
627 return TRUE;
629 fail_close:
630 jack_client_close(g_jack_client);
632 fail_stop_thread:
633 jack_destroy_pending_events();
635 /* Tell jack_midi_thread to exit */
636 pthread_mutex_lock(&g_jack_midi_mutex);
637 g_jack_midi_thread_exit = TRUE;
638 pthread_mutex_unlock(&g_jack_midi_mutex);
640 /* wakeup jack_midi_thread */
641 /* pthread_cond_broadcast(&g_jack_midi_cond); */
643 /* wait thread to finish */
644 pthread_join(g_jack_midi_tid, NULL);
646 fail_uninit_cond:
647 /* pthread_cond_destroy(&g_jack_midi_cond); */
649 /* fail_uninit_mutex: */
650 pthread_mutex_destroy(&g_jack_midi_mutex);
652 fail_uninit_memory:
653 rtsafe_memory_uninit(g_memory);
655 fail:
656 return FALSE;
659 void
660 jack_uninit()
662 LOG_DEBUG("jack_uninit() called.");
664 jack_deactivate(g_jack_client);
666 jack_client_close(g_jack_client);
668 /* Tell jack_midi_thread to exit */
669 pthread_mutex_lock(&g_jack_midi_mutex);
670 g_jack_midi_thread_exit = TRUE;
671 pthread_mutex_unlock(&g_jack_midi_mutex);
673 /* wakeup jack_midi_thread */
674 /* pthread_cond_broadcast(&g_jack_midi_cond); */
676 /* wait thread to finish */
677 pthread_join(g_jack_midi_tid, NULL);
679 jack_destroy_pending_events();
681 /* pthread_cond_destroy(&g_jack_midi_cond); */
683 pthread_mutex_destroy(&g_jack_midi_mutex);
685 rtsafe_memory_uninit(g_memory);