1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of jack_mixer
6 * Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
7 * Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 *****************************************************************************/
31 #include <jack/jack.h>
32 #if defined(HAVE_JACK_MIDI)
33 #include <jack/midiport.h>
40 #include "jack_mixer.h"
41 //#define LOG_LEVEL LOG_LEVEL_DEBUG
44 #include "jack_compat.h"
46 #define PEAK_FRAMES_CHUNK 4800
48 // we don't know how much to allocate, but we don't want to wait with
49 // allocating until we're in the process() callback, so we just take a
50 // fairly big chunk: 4 periods per buffer, 4096 samples per period.
51 // (not sure if the '*4' is needed)
52 #define MAX_BLOCK_SIZE (4 * 4096)
54 #define FLOAT_EXISTS(x) (!((x) - (x)))
58 struct jack_mixer * mixer_ptr;
68 jack_port_t * port_left;
69 jack_port_t * port_right;
71 jack_nframes_t peak_frames;
75 jack_default_audio_sample_t * frames_left;
76 jack_default_audio_sample_t * frames_right;
77 jack_default_audio_sample_t * prefader_frames_left;
78 jack_default_audio_sample_t * prefader_frames_right;
82 int midi_cc_volume_index;
83 int midi_cc_balance_index;
85 jack_default_audio_sample_t * left_buffer_ptr;
86 jack_default_audio_sample_t * right_buffer_ptr;
89 void (*midi_change_callback) (void*);
90 void *midi_change_callback_data;
92 jack_mixer_scale_t midi_scale;
95 struct output_channel {
96 struct channel channel;
97 GSList *soloed_channels;
98 GSList *muted_channels;
99 bool system; /* system channel, without any associated UI */
105 pthread_mutex_t mutex;
106 jack_client_t * jack_client;
107 GSList *input_channels_list;
108 GSList *output_channels_list;
109 struct output_channel *main_mix_channel;
111 jack_port_t * port_midi_in;
112 unsigned int last_midi_channel;
114 struct channel* midi_cc_map[128];
117 static jack_mixer_output_channel_t create_output_channel(
119 const char * channel_name,
124 update_channel_buffers(
125 struct channel * channel_ptr,
126 jack_nframes_t nframes);
138 return 20.0 * log10f(value);
145 return powf(10.0, db/20.0);
149 calc_channel_volumes(
150 struct channel * channel_ptr)
152 if (channel_ptr->stereo)
154 if (channel_ptr->balance > 0)
156 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
157 channel_ptr->volume_right = channel_ptr->volume;
161 channel_ptr->volume_left = channel_ptr->volume;
162 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
167 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
168 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
173 calc_all_channel_volumes(
174 struct jack_mixer * mixer_ptr)
176 struct channel * channel_ptr;
179 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
181 channel_ptr = list_ptr->data;
182 calc_channel_volumes(channel_ptr);
186 #define channel_ptr ((struct channel *)channel)
190 jack_mixer_channel_t channel)
192 return channel_ptr->name;
197 jack_mixer_channel_t channel,
201 size_t channel_name_size;
205 new_name = strdup(name);
206 if (new_name == NULL)
211 if (channel_ptr->name)
213 free(channel_ptr->name);
216 channel_ptr->name = new_name;
218 if (channel_ptr->stereo)
220 channel_name_size = strlen(name);
221 port_name = malloc(channel_name_size + 3);
222 memcpy(port_name, name, channel_name_size);
224 port_name[channel_name_size] = ' ';
225 port_name[channel_name_size+1] = 'L';
226 port_name[channel_name_size+2] = 0;
228 ret = jack_port_set_name(channel_ptr->port_left, port_name);
231 /* what could we do here? */
234 port_name[channel_name_size+1] = 'R';
236 ret = jack_port_set_name(channel_ptr->port_right, port_name);
239 /* what could we do here? */
246 ret = jack_port_set_name(channel_ptr->port_left, name);
249 /* what could we do here? */
256 jack_mixer_channel_t channel)
258 return channel_ptr->stereo;
262 channel_get_balance_midi_cc(
263 jack_mixer_channel_t channel)
265 return channel_ptr->midi_cc_balance_index;
269 channel_set_balance_midi_cc(
270 jack_mixer_channel_t channel,
274 return 2; /* error: over limit CC */
276 if (channel_ptr->midi_cc_balance_index == new_cc) {
281 /* 0 is special, it removes the link */
282 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
283 channel_ptr->midi_cc_balance_index = 0;
285 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
286 return 1; /* error: cc in use */
288 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
289 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
290 channel_ptr->midi_cc_balance_index = new_cc;
296 channel_get_volume_midi_cc(
297 jack_mixer_channel_t channel)
299 return channel_ptr->midi_cc_volume_index;
303 channel_set_volume_midi_cc(
304 jack_mixer_channel_t channel, unsigned int new_cc)
307 return 2; /* error: over limit CC */
309 if (channel_ptr->midi_cc_volume_index == new_cc) {
314 /* 0 is special, it removes the link */
315 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
316 channel_ptr->midi_cc_volume_index = 0;
318 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
319 return 1; /* error: cc in use */
321 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
322 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
323 channel_ptr->midi_cc_volume_index = new_cc;
329 channel_autoset_midi_cc(
330 jack_mixer_channel_t channel)
332 struct jack_mixer *mixer_ptr;
335 mixer_ptr = channel_ptr->mixer_ptr;
337 for (i = 11 ; i < 128 ; i++)
339 if (mixer_ptr->midi_cc_map[i] == NULL)
341 mixer_ptr->midi_cc_map[i] = channel_ptr;
342 channel_ptr->midi_cc_volume_index = i;
344 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
350 for (; i < 128 ; i++)
352 if (mixer_ptr->midi_cc_map[i] == NULL)
354 mixer_ptr->midi_cc_map[i] = channel_ptr;
355 channel_ptr->midi_cc_balance_index = i;
357 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
366 jack_mixer_channel_t channel)
370 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
371 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
372 free(channel_ptr->name);
374 /* remove references to input channel from all output channels */
375 channel_unmute(channel_ptr);
376 channel_unsolo(channel_ptr);
377 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
379 struct output_channel *output_channel_ptr = list_ptr->data;
380 output_channel_set_solo(output_channel_ptr, channel, false);
381 output_channel_set_muted(output_channel_ptr, channel, false);
384 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
385 if (channel_ptr->stereo)
387 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
390 if (channel_ptr->midi_cc_volume_index != 0)
392 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
393 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
396 if (channel_ptr->midi_cc_balance_index != 0)
398 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
399 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
406 channel_stereo_meter_read(
407 jack_mixer_channel_t channel,
412 *left_ptr = value_to_db(channel_ptr->meter_left);
413 *right_ptr = value_to_db(channel_ptr->meter_right);
417 channel_mono_meter_read(
418 jack_mixer_channel_t channel,
421 *mono_ptr = value_to_db(channel_ptr->meter_left);
425 channel_volume_write(
426 jack_mixer_channel_t channel,
430 channel_ptr->volume = db_to_value(volume);
431 calc_channel_volumes(channel_ptr);
436 jack_mixer_channel_t channel)
439 return value_to_db(channel_ptr->volume);
443 channel_balance_write(
444 jack_mixer_channel_t channel,
448 channel_ptr->balance = balance;
449 calc_channel_volumes(channel_ptr);
453 channel_balance_read(
454 jack_mixer_channel_t channel)
457 return channel_ptr->balance;
461 channel_abspeak_read(
462 jack_mixer_channel_t channel)
465 if (channel_ptr->NaN_detected)
471 return value_to_db(channel_ptr->abspeak);
476 channel_abspeak_reset(
477 jack_mixer_channel_t channel)
479 channel_ptr->abspeak = 0;
480 channel_ptr->NaN_detected = false;
485 jack_mixer_channel_t channel)
487 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
492 jack_mixer_channel_t channel)
494 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
499 jack_mixer_channel_t channel)
501 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
506 jack_mixer_channel_t channel)
508 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
513 jack_mixer_channel_t channel)
515 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
522 jack_mixer_channel_t channel)
524 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
530 channel_set_midi_scale(
531 jack_mixer_channel_t channel,
532 jack_mixer_scale_t scale)
534 channel_ptr->midi_scale = scale;
538 channel_set_midi_change_callback(
539 jack_mixer_channel_t channel,
540 void (*midi_change_callback) (void*),
543 channel_ptr->midi_change_callback = midi_change_callback;
544 channel_ptr->midi_change_callback_data = user_data;
548 channel_get_midi_got_events(
549 jack_mixer_channel_t channel)
551 bool t = channel_ptr->midi_got_events;
552 channel_ptr->midi_got_events = false;
558 /* process input channels and mix them into main mix */
561 struct output_channel *output_mix_channel,
562 GSList *channels_list,
563 jack_nframes_t start, /* index of first sample to process */
564 jack_nframes_t end) /* index of sample to stop processing before */
568 struct channel * channel_ptr;
569 jack_default_audio_sample_t frame_left;
570 jack_default_audio_sample_t frame_right;
571 struct channel *mix_channel = (struct channel*)output_mix_channel;
573 for (i = start; i < end; i++)
575 mix_channel->left_buffer_ptr[i] = 0.0;
576 if (mix_channel->stereo)
577 mix_channel->right_buffer_ptr[i] = 0.0;
581 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
583 channel_ptr = node_ptr->data;
585 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
586 /* skip muted channels */
590 if (output_mix_channel->soloed_channels &&
591 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
592 /* skip channels that are not soloed, when some are */
596 for (i = start ; i < end ; i++)
598 if (! output_mix_channel->prefader) {
599 frame_left = channel_ptr->frames_left[i-start];
601 frame_left = channel_ptr->prefader_frames_left[i-start];
603 if (frame_left == NAN)
605 mix_channel->left_buffer_ptr[i] += frame_left;
607 if (mix_channel->stereo)
609 if (! output_mix_channel->prefader) {
610 frame_right = channel_ptr->frames_right[i-start];
612 frame_right = channel_ptr->prefader_frames_right[i-start];
614 if (frame_right == NAN)
617 mix_channel->right_buffer_ptr[i] += frame_right;
624 /* process main mix channel */
625 for (i = start ; i < end ; i++)
627 if (! output_mix_channel->prefader) {
628 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
629 if (mix_channel->stereo)
631 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
635 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
636 if (mix_channel->peak_left < frame_left)
638 mix_channel->peak_left = frame_left;
640 if (frame_left > mix_channel->abspeak)
642 mix_channel->abspeak = frame_left;
646 if (mix_channel->stereo)
648 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
649 if (mix_channel->peak_right < frame_right)
651 mix_channel->peak_right = frame_right;
653 if (frame_right > mix_channel->abspeak)
655 mix_channel->abspeak = frame_right;
660 mix_channel->peak_frames++;
661 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
663 mix_channel->meter_left = mix_channel->peak_left;
664 mix_channel->peak_left = 0.0;
666 if (mix_channel->stereo)
668 mix_channel->meter_right = mix_channel->peak_right;
669 mix_channel->peak_right = 0.0;
672 mix_channel->peak_frames = 0;
679 struct channel *channel_ptr,
680 jack_nframes_t start,
684 jack_default_audio_sample_t frame_left;
685 jack_default_audio_sample_t frame_right;
687 for (i = start ; i < end ; i++)
689 if (i-start >= MAX_BLOCK_SIZE)
691 fprintf(stderr, "i-start too high: %d - %d\n", i, start);
693 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
694 if (channel_ptr->stereo)
695 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
697 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
699 channel_ptr->NaN_detected = true;
700 channel_ptr->frames_left[i-start] = NAN;
704 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
706 if (channel_ptr->stereo)
708 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
710 channel_ptr->NaN_detected = true;
711 channel_ptr->frames_right[i-start] = NAN;
715 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
719 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
722 channel_ptr->frames_left[i-start] = frame_left;
723 channel_ptr->frames_right[i-start] = frame_right;
725 if (channel_ptr->stereo)
727 frame_left = fabsf(frame_left);
728 frame_right = fabsf(frame_right);
730 if (channel_ptr->peak_left < frame_left)
732 channel_ptr->peak_left = frame_left;
734 if (frame_left > channel_ptr->abspeak)
736 channel_ptr->abspeak = frame_left;
740 if (channel_ptr->peak_right < frame_right)
742 channel_ptr->peak_right = frame_right;
744 if (frame_right > channel_ptr->abspeak)
746 channel_ptr->abspeak = frame_right;
752 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
754 if (channel_ptr->peak_left < frame_left)
756 channel_ptr->peak_left = frame_left;
758 if (frame_left > channel_ptr->abspeak)
760 channel_ptr->abspeak = frame_left;
765 channel_ptr->peak_frames++;
766 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
768 channel_ptr->meter_left = channel_ptr->peak_left;
769 channel_ptr->peak_left = 0.0;
771 if (channel_ptr->stereo)
773 channel_ptr->meter_right = channel_ptr->peak_right;
774 channel_ptr->peak_right = 0.0;
777 channel_ptr->peak_frames = 0;
785 struct jack_mixer * mixer_ptr,
786 jack_nframes_t start, /* index of first sample to process */
787 jack_nframes_t end) /* index of sample to stop processing before */
790 struct output_channel * output_channel_ptr;
791 struct channel *channel_ptr;
793 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
795 channel_ptr = (struct channel*)node_ptr->data;
796 calc_channel_frames(channel_ptr, start, end);
799 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
801 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
803 output_channel_ptr = node_ptr->data;
804 channel_ptr = (struct channel*)output_channel_ptr;
806 if (output_channel_ptr->system)
808 /* Don't bother mixing the channels if we are not connected */
809 if (channel_ptr->stereo)
811 if (jack_port_connected(channel_ptr->port_left) == 0 &&
812 jack_port_connected(channel_ptr->port_right) == 0)
815 if (jack_port_connected(channel_ptr->port_left) == 0)
820 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
825 update_channel_buffers(
826 struct channel * channel_ptr,
827 jack_nframes_t nframes)
829 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
831 if (channel_ptr->stereo)
833 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
837 #define mixer_ptr ((struct jack_mixer *)context)
841 jack_nframes_t nframes,
846 struct channel * channel_ptr;
847 #if defined(HAVE_JACK_MIDI)
848 jack_nframes_t event_count;
849 jack_midi_event_t in_event;
853 jack_nframes_t offset;
855 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
857 channel_ptr = node_ptr->data;
858 update_channel_buffers(channel_ptr, nframes);
861 // Fill output buffers with the input
862 update_channel_buffers((struct channel*)mixer_ptr->main_mix_channel, nframes);
863 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
865 channel_ptr = node_ptr->data;
866 update_channel_buffers(channel_ptr, nframes);
871 #if defined(HAVE_JACK_MIDI)
872 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
873 event_count = jack_midi_get_event_count(midi_buffer);
875 for (i = 0 ; i < event_count; i++)
877 jack_midi_event_get(&in_event, midi_buffer, i);
879 if (in_event.size != 3 ||
880 (in_event.buffer[0] & 0xF0) != 0xB0 ||
881 in_event.buffer[1] > 127 ||
882 in_event.buffer[2] > 127)
887 assert(in_event.time < nframes);
891 (unsigned int)(in_event.buffer[0] & 0x0F),
892 (unsigned int)in_event.buffer[1],
893 (unsigned int)in_event.buffer[2]);
895 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
896 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
898 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
899 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
901 assert(in_event.time >= offset);
903 if (in_event.time > offset)
905 // Perform the mixing of the part between the previous volume change
906 // (or the start of the block) up until this one.
907 mix(mixer_ptr, offset, in_event.time);
908 offset = in_event.time;
911 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
913 byte = in_event.buffer[2];
920 channel_ptr->balance = (float)byte / 63;
921 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
925 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
926 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
929 calc_channel_volumes(channel_ptr);
931 channel_ptr->midi_got_events = true;
932 if (channel_ptr->midi_change_callback)
933 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
941 mix(mixer_ptr, offset, nframes);
950 const char * jack_client_name_ptr)
953 struct jack_mixer * mixer_ptr;
957 mixer_ptr = malloc(sizeof(struct jack_mixer));
958 if (mixer_ptr == NULL)
963 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
969 mixer_ptr->input_channels_list = NULL;
970 mixer_ptr->output_channels_list = NULL;
972 mixer_ptr->last_midi_channel = 0;
974 for (i = 0 ; i < 128 ; i++)
976 mixer_ptr->midi_cc_map[i] = NULL;
979 LOG_DEBUG("Initializing JACK");
980 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
981 if (mixer_ptr->jack_client == NULL)
983 LOG_ERROR("Cannot create JACK client.");
984 LOG_NOTICE("Please make sure JACK daemon is running.");
985 goto exit_destroy_mutex;
988 LOG_DEBUG("JACK client created");
990 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
992 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
993 if (mixer_ptr->main_mix_channel == NULL) {
994 LOG_ERROR("Cannot create main mix channel");
997 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
998 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
1000 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
1002 #if defined(HAVE_JACK_MIDI)
1003 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1004 if (mixer_ptr->port_midi_in == NULL)
1006 LOG_ERROR("Cannot create JACK port");
1011 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
1013 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1016 LOG_ERROR("Cannot set JACK process callback");
1020 ret = jack_activate(mixer_ptr->jack_client);
1023 LOG_ERROR("Cannot activate JACK client");
1030 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1033 pthread_mutex_destroy(&mixer_ptr->mutex);
1042 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1048 LOG_DEBUG("Uninitializing JACK");
1050 assert(mixer_ctx_ptr->jack_client != NULL);
1052 jack_client_close(mixer_ctx_ptr->jack_client);
1054 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1056 free(mixer_ctx_ptr->main_mix_channel);
1057 free(mixer_ctx_ptr);
1060 jack_mixer_channel_t
1061 get_main_mix_channel(
1064 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1071 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1075 get_last_midi_channel(
1078 return mixer_ctx_ptr->last_midi_channel;
1081 jack_mixer_channel_t
1084 const char * channel_name,
1087 struct channel * channel_ptr;
1089 size_t channel_name_size;
1091 channel_ptr = malloc(sizeof(struct channel));
1092 if (channel_ptr == NULL)
1097 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1099 channel_ptr->name = strdup(channel_name);
1100 if (channel_ptr->name == NULL)
1102 goto fail_free_channel;
1105 channel_name_size = strlen(channel_name);
1109 port_name = malloc(channel_name_size + 3);
1110 if (port_name == NULL)
1112 goto fail_free_channel_name;
1115 memcpy(port_name, channel_name, channel_name_size);
1116 port_name[channel_name_size] = ' ';
1117 port_name[channel_name_size+1] = 'L';
1118 port_name[channel_name_size+2] = 0;
1120 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1121 if (channel_ptr->port_left == NULL)
1123 goto fail_free_port_name;
1126 port_name[channel_name_size+1] = 'R';
1128 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1129 if (channel_ptr->port_right == NULL)
1131 goto fail_unregister_left_channel;
1136 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1137 if (channel_ptr->port_left == NULL)
1139 goto fail_free_channel_name;
1143 channel_ptr->stereo = stereo;
1145 channel_ptr->volume = 0.0;
1146 channel_ptr->balance = 0.0;
1147 channel_ptr->meter_left = -1.0;
1148 channel_ptr->meter_right = -1.0;
1149 channel_ptr->abspeak = 0.0;
1151 channel_ptr->peak_left = 0.0;
1152 channel_ptr->peak_right = 0.0;
1153 channel_ptr->peak_frames = 0;
1155 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1156 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1157 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1158 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1160 channel_ptr->NaN_detected = false;
1162 channel_ptr->midi_cc_volume_index = 0;
1163 channel_ptr->midi_cc_balance_index = 0;
1164 channel_ptr->midi_change_callback = NULL;
1165 channel_ptr->midi_change_callback_data = NULL;
1167 channel_ptr->midi_scale = NULL;
1169 calc_channel_volumes(channel_ptr);
1171 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1172 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1176 fail_unregister_left_channel:
1177 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1179 fail_free_port_name:
1182 fail_free_channel_name:
1183 free(channel_ptr->name);
1193 static jack_mixer_output_channel_t
1194 create_output_channel(
1196 const char * channel_name,
1200 struct channel * channel_ptr;
1201 struct output_channel * output_channel_ptr;
1203 size_t channel_name_size;
1205 output_channel_ptr = malloc(sizeof(struct output_channel));
1206 channel_ptr = (struct channel*)output_channel_ptr;
1207 if (channel_ptr == NULL)
1212 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1214 channel_ptr->name = strdup(channel_name);
1215 if (channel_ptr->name == NULL)
1217 goto fail_free_channel;
1222 channel_name_size = strlen(channel_name);
1224 port_name = malloc(channel_name_size + 4);
1225 if (port_name == NULL)
1227 goto fail_free_channel_name;
1230 memcpy(port_name, channel_name, channel_name_size);
1231 port_name[channel_name_size] = ' ';
1232 port_name[channel_name_size+1] = 'L';
1233 port_name[channel_name_size+2] = 0;
1235 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1236 if (channel_ptr->port_left == NULL)
1238 goto fail_free_port_name;
1241 port_name[channel_name_size+1] = 'R';
1243 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1244 if (channel_ptr->port_right == NULL)
1246 goto fail_unregister_left_channel;
1251 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1252 if (channel_ptr->port_left == NULL)
1254 goto fail_free_channel_name;
1258 channel_ptr->stereo = stereo;
1260 channel_ptr->volume = 0.0;
1261 channel_ptr->balance = 0.0;
1262 channel_ptr->meter_left = -1.0;
1263 channel_ptr->meter_right = -1.0;
1264 channel_ptr->abspeak = 0.0;
1266 channel_ptr->peak_left = 0.0;
1267 channel_ptr->peak_right = 0.0;
1268 channel_ptr->peak_frames = 0;
1270 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1271 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1272 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1273 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1275 channel_ptr->NaN_detected = false;
1277 channel_ptr->midi_cc_volume_index = 0;
1278 channel_ptr->midi_cc_balance_index = 0;
1279 channel_ptr->midi_change_callback = NULL;
1280 channel_ptr->midi_change_callback_data = NULL;
1282 channel_ptr->midi_scale = NULL;
1284 output_channel_ptr->soloed_channels = NULL;
1285 output_channel_ptr->muted_channels = NULL;
1286 output_channel_ptr->system = system;
1287 output_channel_ptr->prefader = false;
1289 return output_channel_ptr;
1291 fail_unregister_left_channel:
1292 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1294 fail_free_port_name:
1297 fail_free_channel_name:
1298 free(channel_ptr->name);
1308 jack_mixer_output_channel_t
1311 const char * channel_name,
1315 struct output_channel *output_channel_ptr;
1316 struct channel *channel_ptr;
1318 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1319 if (output_channel_ptr == NULL) {
1322 channel_ptr = (struct channel*)output_channel_ptr;
1324 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1325 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1327 return output_channel_ptr;
1331 remove_output_channel(
1332 jack_mixer_output_channel_t output_channel)
1334 struct output_channel *output_channel_ptr = output_channel;
1335 struct channel *channel_ptr = output_channel;
1337 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1338 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1339 free(channel_ptr->name);
1341 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1342 if (channel_ptr->stereo)
1344 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1347 if (channel_ptr->midi_cc_volume_index != 0)
1349 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1350 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1353 if (channel_ptr->midi_cc_balance_index != 0)
1355 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1356 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1359 g_slist_free(output_channel_ptr->soloed_channels);
1360 g_slist_free(output_channel_ptr->muted_channels);
1366 output_channel_set_solo(
1367 jack_mixer_output_channel_t output_channel,
1368 jack_mixer_channel_t channel,
1371 struct output_channel *output_channel_ptr = output_channel;
1374 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1376 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1378 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1380 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1385 output_channel_set_muted(
1386 jack_mixer_output_channel_t output_channel,
1387 jack_mixer_channel_t channel,
1390 struct output_channel *output_channel_ptr = output_channel;
1393 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1395 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1397 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1399 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1404 output_channel_is_muted(
1405 jack_mixer_output_channel_t output_channel,
1406 jack_mixer_channel_t channel)
1408 struct output_channel *output_channel_ptr = output_channel;
1410 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1416 output_channel_is_solo(
1417 jack_mixer_output_channel_t output_channel,
1418 jack_mixer_channel_t channel)
1420 struct output_channel *output_channel_ptr = output_channel;
1422 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1428 output_channel_set_prefader(
1429 jack_mixer_output_channel_t output_channel,
1432 struct output_channel *output_channel_ptr = output_channel;
1433 output_channel_ptr->prefader = pfl_value;
1437 output_channel_is_prefader(
1438 jack_mixer_output_channel_t output_channel)
1440 struct output_channel *output_channel_ptr = output_channel;
1441 return output_channel_ptr->prefader;