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>
34 #include "memory_atomic.h"
35 //#define LOG_LEVEL LOG_LEVEL_DEBUG
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
;
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 */
71 jack_process(jack_nframes_t nframes
, void * context
)
75 jack_midi_event_t in_event
;
76 jack_nframes_t event_count
;
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
);
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
);
110 /* We are lucky and we got the lock */
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);
124 /* We are not lucky, jack_midi_thread has locked the mutex */
125 LOG_DEBUG("Not lucky (%d)", ret
);
135 GString
* msg_str_ptr
,
136 GString
* channel_str_ptr
)
139 unsigned int channel
;
141 unsigned int velocity
;
142 const char * note_name
;
144 unsigned int controller
;
146 const char * controller_name
;
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);
163 return FALSE
; /* ignore note off for drums */
172 velocity
= buffer
[2];
178 note_name
= g_note_names
[note
% 12];
179 octave
= note
/ 12 - 1;
181 g_string_sprintf(channel_str_ptr
, "%u", channel
);
185 "Note off, %s, octave %d, velocity %u",
193 if (buffer_size
== 3 && (buffer
[0] >> 4) == 0x09)
195 channel
= (buffer
[0] & 0x0F) + 1; /* 1 .. 16 */
196 assert(channel
>= 1 && channel
<= 16);
204 velocity
= buffer
[2];
210 note_name
= g_note_names
[note
% 12];
211 octave
= note
/ 12 - 1;
213 g_string_sprintf(channel_str_ptr
, "%u", channel
);
217 drum_name
= gm_get_drum_name(note
);
224 if (drum_name
!= NULL
)
228 return FALSE
; /* ignore note off for drums */
233 "Drum: %s (%s, octave %d, velocity %u)",
243 "Note on, %s, octave %d, velocity %u",
252 if (buffer_size
== 3 && (buffer
[0] >> 4) == 0x0A)
254 channel
= (buffer
[0] & 0x0F) + 1; /* 1 .. 16 */
255 assert(channel
>= 1 && channel
<= 16);
263 velocity
= buffer
[2];
269 note_name
= g_note_names
[note
% 12];
270 octave
= note
/ 12 - 1;
272 g_string_sprintf(channel_str_ptr
, "%u", channel
);
276 "Polyphonic Key Pressure (Aftertouch), %s, octave %d, velocity %u",
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)
301 g_string_sprintf(channel_str_ptr
, "%u", channel
);
303 controller_name
= gm_get_controller_name(controller
);
305 if (controller_name
!= NULL
)
309 "CC %s (%u), value %u",
326 if (buffer_size
== 2 && (buffer
[0] >> 4) == 0x0C)
328 channel
= (buffer
[0] & 0x0F) + 1; /* 1 .. 16 */
329 assert(channel
>= 1 && channel
<= 16);
337 g_string_sprintf(channel_str_ptr
, "%u", channel
);
341 "Program change, %d (%s)",
343 gm_get_instrument_name(value
));
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);
354 pitch
|= (unsigned int)buffer
[2] << 7;
357 g_string_sprintf(channel_str_ptr
, "%u", channel
);
367 if (buffer_size
> 0 && buffer
[0] == 0xF0)
369 decode_sysex(buffer
, buffer_size
, msg_str_ptr
);
373 if (buffer_size
== 1 && buffer
[0] == 0xFF)
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(
393 (unsigned int)(buffer
[i
]));
399 /* The JACK MIDI input handling thread */
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
;
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
)));
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");
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
;
439 event_buffer
= list_entry(node_ptr
, struct jack_midi_event_buffer
, siblings
);
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
,
458 /* ignoring specific event */
459 goto deallocate_strings
;
462 /* get GTK thread lock */
465 if (g_row_count
>= MAX_LIST_SIZE
)
467 gtk_tree_model_get_iter_first(
468 GTK_TREE_MODEL(list_store_ptr
),
471 gtk_list_store_remove(
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
);
482 COL_TIME
, time_str_ptr
->str
,
483 COL_CHANNEL
, channel_str_ptr
->str
,
484 COL_MESSAGE
, msg_str_ptr
->str
,
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
)),
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
));
503 /* release GTK thread lock */
507 g_string_free(channel_str_ptr
, TRUE
);
508 g_string_free(msg_str_ptr
, TRUE
);
509 g_string_free(time_str_ptr
, TRUE
);
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);
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
;
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
;
550 event_buffer
= list_entry(node_ptr
, struct jack_midi_event_buffer
, siblings
);
552 rtsafe_memory_deallocate(event_buffer
);
557 jack_init(const char * name
)
561 LOG_DEBUG("jack_init(\"%s\") called.", name
);
563 if (!rtsafe_memory_init(
569 LOG_ERROR("RT-safe memory initialization failed.");
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
);
579 LOG_ERROR("Cannot initialize mutex.");
580 goto fail_uninit_memory
;
583 /* ret = pthread_cond_init(&g_jack_midi_cond, NULL); */
586 /* LOG_ERROR("Cannot initialize condition (variable)."); */
587 /* goto fail_uninit_mutex; */
590 g_jack_midi_thread_exit
= FALSE
;
592 ret
= pthread_create(&g_jack_midi_tid
, NULL
, jack_midi_thread
, NULL
);
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);
609 LOG_ERROR("Cannot set JACK process callback.");
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");
620 ret
= jack_activate(g_jack_client
);
623 LOG_ERROR("Cannot activate JACK client.");
630 jack_client_close(g_jack_client
);
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
);
647 /* pthread_cond_destroy(&g_jack_midi_cond); */
649 /* fail_uninit_mutex: */
650 pthread_mutex_destroy(&g_jack_midi_mutex
);
653 rtsafe_memory_uninit(g_memory
);
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
);