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 #define FLOAT_EXISTS(x) (!((x) - (x)))
52 struct jack_mixer * mixer_ptr;
62 jack_port_t * port_left;
63 jack_port_t * port_right;
65 jack_nframes_t peak_frames;
69 jack_default_audio_sample_t * frames_left;
70 jack_default_audio_sample_t * frames_right;
71 jack_default_audio_sample_t * prefader_frames_left;
72 jack_default_audio_sample_t * prefader_frames_right;
76 int midi_cc_volume_index;
77 int midi_cc_balance_index;
79 jack_default_audio_sample_t * left_buffer_ptr;
80 jack_default_audio_sample_t * right_buffer_ptr;
82 void (*midi_change_callback) (void*);
83 void *midi_change_callback_data;
85 jack_mixer_scale_t midi_scale;
88 struct output_channel {
89 struct channel channel;
90 GSList *soloed_channels;
91 GSList *muted_channels;
92 bool system; /* system channel, without any associated UI */
98 pthread_mutex_t mutex;
99 jack_client_t * jack_client;
100 GSList *input_channels_list;
101 GSList *output_channels_list;
102 struct output_channel *main_mix_channel;
104 jack_port_t * port_midi_in;
105 unsigned int last_midi_channel;
107 struct channel* midi_cc_map[128];
110 static jack_mixer_output_channel_t create_output_channel(
112 const char * channel_name,
117 update_channel_buffers(
118 struct channel * channel_ptr,
119 jack_nframes_t nframes);
131 return 20.0 * log10f(value);
138 return powf(10.0, db/20.0);
142 calc_channel_volumes(
143 struct channel * channel_ptr)
145 if (channel_ptr->stereo)
147 if (channel_ptr->balance > 0)
149 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
150 channel_ptr->volume_right = channel_ptr->volume;
154 channel_ptr->volume_left = channel_ptr->volume;
155 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
160 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
161 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
166 calc_all_channel_volumes(
167 struct jack_mixer * mixer_ptr)
169 struct channel * channel_ptr;
172 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
174 channel_ptr = list_ptr->data;
175 calc_channel_volumes(channel_ptr);
179 #define channel_ptr ((struct channel *)channel)
183 jack_mixer_channel_t channel)
185 return channel_ptr->name;
190 jack_mixer_channel_t channel,
194 size_t channel_name_size;
198 new_name = strdup(name);
199 if (new_name == NULL)
204 if (channel_ptr->name)
206 free(channel_ptr->name);
209 channel_ptr->name = new_name;
211 if (channel_ptr->stereo)
213 channel_name_size = strlen(name);
214 port_name = malloc(channel_name_size + 3);
215 memcpy(port_name, name, channel_name_size);
217 port_name[channel_name_size] = ' ';
218 port_name[channel_name_size+1] = 'L';
219 port_name[channel_name_size+2] = 0;
221 ret = jack_port_set_name(channel_ptr->port_left, port_name);
224 /* what could we do here? */
227 port_name[channel_name_size+1] = 'R';
229 ret = jack_port_set_name(channel_ptr->port_right, port_name);
232 /* what could we do here? */
239 ret = jack_port_set_name(channel_ptr->port_left, name);
242 /* what could we do here? */
249 jack_mixer_channel_t channel)
251 return channel_ptr->stereo;
255 channel_get_balance_midi_cc(
256 jack_mixer_channel_t channel)
258 return channel_ptr->midi_cc_balance_index;
262 channel_set_balance_midi_cc(
263 jack_mixer_channel_t channel,
267 return 2; /* error: over limit CC */
269 if (channel_ptr->midi_cc_balance_index == new_cc) {
274 /* 0 is special, it removes the link */
275 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
276 channel_ptr->midi_cc_balance_index = 0;
278 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
279 return 1; /* error: cc in use */
281 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
282 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
283 channel_ptr->midi_cc_balance_index = new_cc;
289 channel_get_volume_midi_cc(
290 jack_mixer_channel_t channel)
292 return channel_ptr->midi_cc_volume_index;
296 channel_set_volume_midi_cc(
297 jack_mixer_channel_t channel, unsigned int new_cc)
300 return 2; /* error: over limit CC */
302 if (channel_ptr->midi_cc_volume_index == new_cc) {
307 /* 0 is special, it removes the link */
308 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
309 channel_ptr->midi_cc_volume_index = 0;
311 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
312 return 1; /* error: cc in use */
314 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
315 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
316 channel_ptr->midi_cc_volume_index = new_cc;
322 channel_autoset_midi_cc(
323 jack_mixer_channel_t channel)
325 struct jack_mixer *mixer_ptr;
328 mixer_ptr = channel_ptr->mixer_ptr;
330 for (i = 11 ; i < 128 ; i++)
332 if (mixer_ptr->midi_cc_map[i] == NULL)
334 mixer_ptr->midi_cc_map[i] = channel_ptr;
335 channel_ptr->midi_cc_volume_index = i;
337 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
343 for (; i < 128 ; i++)
345 if (mixer_ptr->midi_cc_map[i] == NULL)
347 mixer_ptr->midi_cc_map[i] = channel_ptr;
348 channel_ptr->midi_cc_balance_index = i;
350 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
359 jack_mixer_channel_t channel)
363 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
364 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
365 free(channel_ptr->name);
367 /* remove references to input channel from all output channels */
368 channel_unmute(channel_ptr);
369 channel_unsolo(channel_ptr);
370 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
372 struct output_channel *output_channel_ptr = list_ptr->data;
373 output_channel_set_solo(output_channel_ptr, channel, false);
374 output_channel_set_muted(output_channel_ptr, channel, false);
377 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
378 if (channel_ptr->stereo)
380 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
383 if (channel_ptr->midi_cc_volume_index != 0)
385 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
386 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
389 if (channel_ptr->midi_cc_balance_index != 0)
391 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
392 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
399 channel_stereo_meter_read(
400 jack_mixer_channel_t channel,
405 *left_ptr = value_to_db(channel_ptr->meter_left);
406 *right_ptr = value_to_db(channel_ptr->meter_right);
410 channel_mono_meter_read(
411 jack_mixer_channel_t channel,
414 *mono_ptr = value_to_db(channel_ptr->meter_left);
418 channel_volume_write(
419 jack_mixer_channel_t channel,
423 channel_ptr->volume = db_to_value(volume);
424 calc_channel_volumes(channel_ptr);
429 jack_mixer_channel_t channel)
432 return value_to_db(channel_ptr->volume);
436 channel_balance_write(
437 jack_mixer_channel_t channel,
441 channel_ptr->balance = balance;
442 calc_channel_volumes(channel_ptr);
446 channel_balance_read(
447 jack_mixer_channel_t channel)
450 return channel_ptr->balance;
454 channel_abspeak_read(
455 jack_mixer_channel_t channel)
458 if (channel_ptr->NaN_detected)
464 return value_to_db(channel_ptr->abspeak);
469 channel_abspeak_reset(
470 jack_mixer_channel_t channel)
472 channel_ptr->abspeak = 0;
473 channel_ptr->NaN_detected = false;
478 jack_mixer_channel_t channel)
480 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
485 jack_mixer_channel_t channel)
487 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
492 jack_mixer_channel_t channel)
494 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
499 jack_mixer_channel_t channel)
501 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
506 jack_mixer_channel_t channel)
508 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
515 jack_mixer_channel_t channel)
517 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
523 channel_set_midi_scale(
524 jack_mixer_channel_t channel,
525 jack_mixer_scale_t scale)
527 channel_ptr->midi_scale = scale;
531 channel_set_midi_change_callback(
532 jack_mixer_channel_t channel,
533 void (*midi_change_callback) (void*),
536 channel_ptr->midi_change_callback = midi_change_callback;
537 channel_ptr->midi_change_callback_data = user_data;
542 /* process input channels and mix them into main mix */
545 struct output_channel *output_mix_channel,
546 GSList *channels_list,
547 jack_nframes_t start, /* index of first sample to process */
548 jack_nframes_t end) /* index of sample to stop processing before */
552 struct channel * channel_ptr;
553 jack_default_audio_sample_t frame_left;
554 jack_default_audio_sample_t frame_right;
555 struct channel *mix_channel = (struct channel*)output_mix_channel;
557 update_channel_buffers(mix_channel, end-start);
558 for (i = 0; i < (end-start); i++)
560 mix_channel->left_buffer_ptr[i] = 0.0;
561 if (mix_channel->stereo)
562 mix_channel->right_buffer_ptr[i] = 0.0;
566 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
568 channel_ptr = node_ptr->data;
570 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
571 /* skip muted channels */
575 if (output_mix_channel->soloed_channels &&
576 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
577 /* skip channels that are not soloed, when some are */
581 for (i = start ; i < end ; i++)
583 if (! output_mix_channel->prefader) {
584 frame_left = channel_ptr->frames_left[i-start];
586 frame_left = channel_ptr->prefader_frames_left[i-start];
588 if (frame_left == NAN)
590 mix_channel->left_buffer_ptr[i] += frame_left;
592 if (mix_channel->stereo)
594 if (! output_mix_channel->prefader) {
595 frame_right = channel_ptr->frames_right[i-start];
597 frame_right = channel_ptr->prefader_frames_right[i-start];
599 if (frame_right == NAN)
602 mix_channel->right_buffer_ptr[i] += frame_right;
609 /* process main mix channel */
610 for (i = start ; i < end ; i++)
612 if (! output_mix_channel->prefader) {
613 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
614 if (mix_channel->stereo)
616 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
620 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
621 if (mix_channel->peak_left < frame_left)
623 mix_channel->peak_left = frame_left;
625 if (frame_left > mix_channel->abspeak)
627 mix_channel->abspeak = frame_left;
631 if (mix_channel->stereo)
633 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
634 if (mix_channel->peak_right < frame_right)
636 mix_channel->peak_right = frame_right;
638 if (frame_right > mix_channel->abspeak)
640 mix_channel->abspeak = frame_right;
645 mix_channel->peak_frames++;
646 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
648 mix_channel->meter_left = mix_channel->peak_left;
649 mix_channel->peak_left = 0.0;
651 if (mix_channel->stereo)
653 mix_channel->meter_right = mix_channel->peak_right;
654 mix_channel->peak_right = 0.0;
657 mix_channel->peak_frames = 0;
664 struct channel *channel_ptr,
665 jack_nframes_t start,
669 jack_default_audio_sample_t frame_left;
670 jack_default_audio_sample_t frame_right;
672 channel_ptr->frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
673 channel_ptr->frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
675 channel_ptr->prefader_frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
676 channel_ptr->prefader_frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
678 for (i = start ; i < end ; i++)
680 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
681 if (channel_ptr->stereo)
682 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
684 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
686 channel_ptr->NaN_detected = true;
687 channel_ptr->frames_left[i-start] = NAN;
691 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
693 if (channel_ptr->stereo)
695 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
697 channel_ptr->NaN_detected = true;
698 channel_ptr->frames_right[i-start] = NAN;
702 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
706 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
709 channel_ptr->frames_left[i-start] = frame_left;
710 channel_ptr->frames_right[i-start] = frame_right;
712 if (channel_ptr->stereo)
714 frame_left = fabsf(frame_left);
715 frame_right = fabsf(frame_right);
717 if (channel_ptr->peak_left < frame_left)
719 channel_ptr->peak_left = frame_left;
721 if (frame_left > channel_ptr->abspeak)
723 channel_ptr->abspeak = frame_left;
727 if (channel_ptr->peak_right < frame_right)
729 channel_ptr->peak_right = frame_right;
731 if (frame_right > channel_ptr->abspeak)
733 channel_ptr->abspeak = frame_right;
739 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
741 if (channel_ptr->peak_left < frame_left)
743 channel_ptr->peak_left = frame_left;
745 if (frame_left > channel_ptr->abspeak)
747 channel_ptr->abspeak = frame_left;
752 channel_ptr->peak_frames++;
753 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
755 channel_ptr->meter_left = channel_ptr->peak_left;
756 channel_ptr->peak_left = 0.0;
758 if (channel_ptr->stereo)
760 channel_ptr->meter_right = channel_ptr->peak_right;
761 channel_ptr->peak_right = 0.0;
764 channel_ptr->peak_frames = 0;
772 struct jack_mixer * mixer_ptr,
773 jack_nframes_t start, /* index of first sample to process */
774 jack_nframes_t end) /* index of sample to stop processing before */
777 struct output_channel * output_channel_ptr;
778 struct channel *channel_ptr;
780 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
782 channel_ptr = (struct channel*)node_ptr->data;
783 calc_channel_frames(channel_ptr, start, end);
786 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
788 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
790 output_channel_ptr = node_ptr->data;
791 channel_ptr = (struct channel*)output_channel_ptr;
793 if (output_channel_ptr->system)
795 /* Don't bother mixing the channels if we are not connected */
796 if (channel_ptr->stereo)
798 if (jack_port_connected(channel_ptr->port_left) == 0 &&
799 jack_port_connected(channel_ptr->port_right) == 0)
802 if (jack_port_connected(channel_ptr->port_left) == 0)
807 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
810 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
812 channel_ptr = (struct channel*)node_ptr->data;
813 free(channel_ptr->frames_left);
814 free(channel_ptr->frames_right);
815 free(channel_ptr->prefader_frames_left);
816 free(channel_ptr->prefader_frames_right);
821 update_channel_buffers(
822 struct channel * channel_ptr,
823 jack_nframes_t nframes)
825 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
827 if (channel_ptr->stereo)
829 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
833 #define mixer_ptr ((struct jack_mixer *)context)
837 jack_nframes_t nframes,
842 struct channel * channel_ptr;
843 #if defined(HAVE_JACK_MIDI)
844 jack_nframes_t event_count;
845 jack_midi_event_t in_event;
849 jack_nframes_t offset;
851 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
853 channel_ptr = node_ptr->data;
854 update_channel_buffers(channel_ptr, nframes);
859 #if defined(HAVE_JACK_MIDI)
860 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
861 event_count = jack_midi_get_event_count(midi_buffer);
863 for (i = 0 ; i < event_count; i++)
865 jack_midi_event_get(&in_event, midi_buffer, i);
867 if (in_event.size != 3 ||
868 (in_event.buffer[0] & 0xF0) != 0xB0 ||
869 in_event.buffer[1] > 127 ||
870 in_event.buffer[2] > 127)
875 assert(in_event.time < nframes);
879 (unsigned int)(in_event.buffer[0] & 0x0F),
880 (unsigned int)in_event.buffer[1],
881 (unsigned int)in_event.buffer[2]);
883 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
884 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
886 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
887 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
889 assert(in_event.time >= offset);
891 if (in_event.time > offset)
893 mix(mixer_ptr, offset, in_event.time);
894 offset = in_event.time;
897 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
899 byte = in_event.buffer[2];
906 channel_ptr->balance = (float)byte / 63;
907 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
911 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
912 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
915 calc_channel_volumes(channel_ptr);
917 if (channel_ptr->midi_change_callback)
918 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
926 mix(mixer_ptr, offset, nframes);
935 const char * jack_client_name_ptr)
938 struct jack_mixer * mixer_ptr;
942 mixer_ptr = malloc(sizeof(struct jack_mixer));
943 if (mixer_ptr == NULL)
948 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
954 mixer_ptr->input_channels_list = NULL;
955 mixer_ptr->output_channels_list = NULL;
957 mixer_ptr->last_midi_channel = 0;
959 for (i = 0 ; i < 128 ; i++)
961 mixer_ptr->midi_cc_map[i] = NULL;
964 LOG_DEBUG("Initializing JACK");
965 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
966 if (mixer_ptr->jack_client == NULL)
968 LOG_ERROR("Cannot create JACK client.");
969 LOG_NOTICE("Please make sure JACK daemon is running.");
970 goto exit_destroy_mutex;
973 LOG_DEBUG("JACK client created");
975 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
977 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
978 if (mixer_ptr->main_mix_channel == NULL) {
979 LOG_ERROR("Cannot create main mix channel");
982 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
983 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
985 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
987 #if defined(HAVE_JACK_MIDI)
988 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
989 if (mixer_ptr->port_midi_in == NULL)
991 LOG_ERROR("Cannot create JACK port");
996 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
998 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1001 LOG_ERROR("Cannot set JACK process callback");
1005 ret = jack_activate(mixer_ptr->jack_client);
1008 LOG_ERROR("Cannot activate JACK client");
1015 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1018 pthread_mutex_destroy(&mixer_ptr->mutex);
1027 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1033 LOG_DEBUG("Uninitializing JACK");
1035 assert(mixer_ctx_ptr->jack_client != NULL);
1037 jack_client_close(mixer_ctx_ptr->jack_client);
1039 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1041 free(mixer_ctx_ptr->main_mix_channel);
1042 free(mixer_ctx_ptr);
1045 jack_mixer_channel_t
1046 get_main_mix_channel(
1049 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1056 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1060 get_last_midi_channel(
1063 return mixer_ctx_ptr->last_midi_channel;
1066 jack_mixer_channel_t
1069 const char * channel_name,
1072 struct channel * channel_ptr;
1074 size_t channel_name_size;
1076 channel_ptr = malloc(sizeof(struct channel));
1077 if (channel_ptr == NULL)
1082 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1084 channel_ptr->name = strdup(channel_name);
1085 if (channel_ptr->name == NULL)
1087 goto fail_free_channel;
1090 channel_name_size = strlen(channel_name);
1094 port_name = malloc(channel_name_size + 3);
1095 if (port_name == NULL)
1097 goto fail_free_channel_name;
1100 memcpy(port_name, channel_name, channel_name_size);
1101 port_name[channel_name_size] = ' ';
1102 port_name[channel_name_size+1] = 'L';
1103 port_name[channel_name_size+2] = 0;
1105 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1106 if (channel_ptr->port_left == NULL)
1108 goto fail_free_port_name;
1111 port_name[channel_name_size+1] = 'R';
1113 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1114 if (channel_ptr->port_right == NULL)
1116 goto fail_unregister_left_channel;
1121 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1122 if (channel_ptr->port_left == NULL)
1124 goto fail_free_channel_name;
1128 channel_ptr->stereo = stereo;
1130 channel_ptr->volume = 0.0;
1131 channel_ptr->balance = 0.0;
1132 channel_ptr->meter_left = -1.0;
1133 channel_ptr->meter_right = -1.0;
1134 channel_ptr->abspeak = 0.0;
1136 channel_ptr->peak_left = 0.0;
1137 channel_ptr->peak_right = 0.0;
1138 channel_ptr->peak_frames = 0;
1140 channel_ptr->NaN_detected = false;
1142 channel_ptr->midi_cc_volume_index = 0;
1143 channel_ptr->midi_cc_balance_index = 0;
1144 channel_ptr->midi_change_callback = NULL;
1145 channel_ptr->midi_change_callback_data = NULL;
1147 channel_ptr->midi_scale = NULL;
1149 calc_channel_volumes(channel_ptr);
1151 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1152 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1156 fail_unregister_left_channel:
1157 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1159 fail_free_port_name:
1162 fail_free_channel_name:
1163 free(channel_ptr->name);
1173 static jack_mixer_output_channel_t
1174 create_output_channel(
1176 const char * channel_name,
1180 struct channel * channel_ptr;
1181 struct output_channel * output_channel_ptr;
1183 size_t channel_name_size;
1185 output_channel_ptr = malloc(sizeof(struct output_channel));
1186 channel_ptr = (struct channel*)output_channel_ptr;
1187 if (channel_ptr == NULL)
1192 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1194 channel_ptr->name = strdup(channel_name);
1195 if (channel_ptr->name == NULL)
1197 goto fail_free_channel;
1202 channel_name_size = strlen(channel_name);
1204 port_name = malloc(channel_name_size + 4);
1205 if (port_name == NULL)
1207 goto fail_free_channel_name;
1210 memcpy(port_name, channel_name, channel_name_size);
1211 port_name[channel_name_size] = ' ';
1212 port_name[channel_name_size+1] = 'L';
1213 port_name[channel_name_size+2] = 0;
1215 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1216 if (channel_ptr->port_left == NULL)
1218 goto fail_free_port_name;
1221 port_name[channel_name_size+1] = 'R';
1223 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1224 if (channel_ptr->port_right == NULL)
1226 goto fail_unregister_left_channel;
1231 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1232 if (channel_ptr->port_left == NULL)
1234 goto fail_free_channel_name;
1238 channel_ptr->stereo = stereo;
1240 channel_ptr->volume = 0.0;
1241 channel_ptr->balance = 0.0;
1242 channel_ptr->meter_left = -1.0;
1243 channel_ptr->meter_right = -1.0;
1244 channel_ptr->abspeak = 0.0;
1246 channel_ptr->peak_left = 0.0;
1247 channel_ptr->peak_right = 0.0;
1248 channel_ptr->peak_frames = 0;
1250 channel_ptr->NaN_detected = false;
1252 channel_ptr->midi_cc_volume_index = 0;
1253 channel_ptr->midi_cc_balance_index = 0;
1254 channel_ptr->midi_change_callback = NULL;
1255 channel_ptr->midi_change_callback_data = NULL;
1257 channel_ptr->midi_scale = NULL;
1259 output_channel_ptr->soloed_channels = NULL;
1260 output_channel_ptr->muted_channels = NULL;
1261 output_channel_ptr->system = system;
1262 output_channel_ptr->prefader = false;
1264 return output_channel_ptr;
1266 fail_unregister_left_channel:
1267 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1269 fail_free_port_name:
1272 fail_free_channel_name:
1273 free(channel_ptr->name);
1283 jack_mixer_output_channel_t
1286 const char * channel_name,
1290 struct output_channel *output_channel_ptr;
1291 struct channel *channel_ptr;
1293 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1294 if (output_channel_ptr == NULL) {
1297 channel_ptr = (struct channel*)output_channel_ptr;
1299 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1300 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1302 return output_channel_ptr;
1306 remove_output_channel(
1307 jack_mixer_output_channel_t output_channel)
1309 struct output_channel *output_channel_ptr = output_channel;
1310 struct channel *channel_ptr = output_channel;
1312 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1313 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1314 free(channel_ptr->name);
1316 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1317 if (channel_ptr->stereo)
1319 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1322 if (channel_ptr->midi_cc_volume_index != 0)
1324 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1325 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1328 if (channel_ptr->midi_cc_balance_index != 0)
1330 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1331 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1334 g_slist_free(output_channel_ptr->soloed_channels);
1335 g_slist_free(output_channel_ptr->muted_channels);
1341 output_channel_set_solo(
1342 jack_mixer_output_channel_t output_channel,
1343 jack_mixer_channel_t channel,
1346 struct output_channel *output_channel_ptr = output_channel;
1349 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1351 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1353 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1355 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1360 output_channel_set_muted(
1361 jack_mixer_output_channel_t output_channel,
1362 jack_mixer_channel_t channel,
1365 struct output_channel *output_channel_ptr = output_channel;
1368 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1370 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1372 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1374 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1379 output_channel_is_muted(
1380 jack_mixer_output_channel_t output_channel,
1381 jack_mixer_channel_t channel)
1383 struct output_channel *output_channel_ptr = output_channel;
1385 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1391 output_channel_is_solo(
1392 jack_mixer_output_channel_t output_channel,
1393 jack_mixer_channel_t channel)
1395 struct output_channel *output_channel_ptr = output_channel;
1397 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1403 output_channel_set_prefader(
1404 jack_mixer_output_channel_t output_channel,
1407 struct output_channel *output_channel_ptr = output_channel;
1408 output_channel_ptr->prefader = pfl_value;
1412 output_channel_is_prefader(
1413 jack_mixer_output_channel_t output_channel)
1415 struct output_channel *output_channel_ptr = output_channel;
1416 return output_channel_ptr->prefader;