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;
87 jack_mixer_output_channel_t output;
90 struct output_channel {
91 struct channel channel;
92 GSList *soloed_channels;
93 GSList *muted_channels;
94 bool system; /* system channel, without any associated UI */
100 pthread_mutex_t mutex;
101 jack_client_t * jack_client;
102 GSList *input_channels_list;
103 GSList *output_channels_list;
104 struct output_channel *main_mix_channel;
106 jack_port_t * port_midi_in;
107 unsigned int last_midi_channel;
109 struct channel* midi_cc_map[128];
112 static jack_mixer_output_channel_t create_output_channel(
114 const char * channel_name,
127 return 20.0 * log10f(value);
134 return powf(10.0, db/20.0);
138 calc_channel_volumes(
139 struct channel * channel_ptr)
141 if (channel_ptr->stereo)
143 if (channel_ptr->balance > 0)
145 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
146 channel_ptr->volume_right = channel_ptr->volume;
150 channel_ptr->volume_left = channel_ptr->volume;
151 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
156 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
157 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
162 calc_all_channel_volumes(
163 struct jack_mixer * mixer_ptr)
165 struct channel * channel_ptr;
168 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
170 channel_ptr = list_ptr->data;
171 calc_channel_volumes(channel_ptr);
175 #define channel_ptr ((struct channel *)channel)
179 jack_mixer_channel_t channel)
181 return channel_ptr->name;
186 jack_mixer_channel_t channel,
190 size_t channel_name_size;
194 new_name = strdup(name);
195 if (new_name == NULL)
200 if (channel_ptr->name)
202 free(channel_ptr->name);
205 channel_ptr->name = new_name;
207 if (channel_ptr->stereo)
209 channel_name_size = strlen(name);
210 port_name = malloc(channel_name_size + 3);
211 memcpy(port_name, name, channel_name_size);
213 port_name[channel_name_size] = ' ';
214 port_name[channel_name_size+1] = 'L';
215 port_name[channel_name_size+2] = 0;
217 ret = jack_port_set_name(channel_ptr->port_left, port_name);
220 /* what could we do here? */
223 port_name[channel_name_size+1] = 'R';
225 ret = jack_port_set_name(channel_ptr->port_right, port_name);
228 /* what could we do here? */
235 ret = jack_port_set_name(channel_ptr->port_left, name);
238 /* what could we do here? */
245 jack_mixer_channel_t channel)
247 return channel_ptr->stereo;
251 channel_get_balance_midi_cc(
252 jack_mixer_channel_t channel)
254 return channel_ptr->midi_cc_balance_index;
258 channel_set_balance_midi_cc(
259 jack_mixer_channel_t channel,
263 return 2; /* error: over limit CC */
265 if (channel_ptr->midi_cc_balance_index == new_cc) {
270 /* 0 is special, it removes the link */
271 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
272 channel_ptr->midi_cc_balance_index = 0;
274 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
275 return 1; /* error: cc in use */
277 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
278 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
279 channel_ptr->midi_cc_balance_index = new_cc;
285 channel_get_volume_midi_cc(
286 jack_mixer_channel_t channel)
288 return channel_ptr->midi_cc_volume_index;
292 channel_set_volume_midi_cc(
293 jack_mixer_channel_t channel, unsigned int new_cc)
296 return 2; /* error: over limit CC */
298 if (channel_ptr->midi_cc_volume_index == new_cc) {
303 /* 0 is special, it removes the link */
304 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
305 channel_ptr->midi_cc_volume_index = 0;
307 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
308 return 1; /* error: cc in use */
310 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
311 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
312 channel_ptr->midi_cc_volume_index = new_cc;
318 channel_autoset_midi_cc(
319 jack_mixer_channel_t channel)
321 struct jack_mixer *mixer_ptr;
324 mixer_ptr = channel_ptr->mixer_ptr;
326 for (i = 11 ; i < 128 ; i++)
328 if (mixer_ptr->midi_cc_map[i] == NULL)
330 mixer_ptr->midi_cc_map[i] = channel_ptr;
331 channel_ptr->midi_cc_volume_index = i;
333 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
339 for (; i < 128 ; i++)
341 if (mixer_ptr->midi_cc_map[i] == NULL)
343 mixer_ptr->midi_cc_map[i] = channel_ptr;
344 channel_ptr->midi_cc_balance_index = i;
346 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
355 jack_mixer_channel_t channel)
359 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
360 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
361 free(channel_ptr->name);
363 /* remove references to input channel from all output channels */
364 channel_unmute(channel_ptr);
365 channel_unsolo(channel_ptr);
366 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
368 struct output_channel *output_channel_ptr = list_ptr->data;
369 output_channel_set_solo(output_channel_ptr, channel, false);
370 output_channel_set_muted(output_channel_ptr, channel, false);
373 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
374 if (channel_ptr->stereo)
376 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
379 if (channel_ptr->midi_cc_volume_index != 0)
381 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
382 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
385 if (channel_ptr->midi_cc_balance_index != 0)
387 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
388 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
390 remove_output_channel(channel_ptr->output);
396 channel_stereo_meter_read(
397 jack_mixer_channel_t channel,
402 *left_ptr = value_to_db(channel_ptr->meter_left);
403 *right_ptr = value_to_db(channel_ptr->meter_right);
407 channel_mono_meter_read(
408 jack_mixer_channel_t channel,
411 *mono_ptr = value_to_db(channel_ptr->meter_left);
415 channel_volume_write(
416 jack_mixer_channel_t channel,
420 channel_ptr->volume = db_to_value(volume);
421 calc_channel_volumes(channel_ptr);
426 jack_mixer_channel_t channel)
429 return value_to_db(channel_ptr->volume);
433 channel_balance_write(
434 jack_mixer_channel_t channel,
438 channel_ptr->balance = balance;
439 calc_channel_volumes(channel_ptr);
443 channel_balance_read(
444 jack_mixer_channel_t channel)
447 return channel_ptr->balance;
451 channel_abspeak_read(
452 jack_mixer_channel_t channel)
455 if (channel_ptr->NaN_detected)
461 return value_to_db(channel_ptr->abspeak);
466 channel_abspeak_reset(
467 jack_mixer_channel_t channel)
469 channel_ptr->abspeak = 0;
470 channel_ptr->NaN_detected = false;
475 jack_mixer_channel_t channel)
477 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
482 jack_mixer_channel_t channel)
484 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
489 jack_mixer_channel_t channel)
491 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
496 jack_mixer_channel_t channel)
498 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
503 jack_mixer_channel_t channel)
505 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
512 jack_mixer_channel_t channel)
514 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
520 channel_set_midi_scale(
521 jack_mixer_channel_t channel,
522 jack_mixer_scale_t scale)
524 channel_ptr->midi_scale = scale;
528 channel_set_midi_change_callback(
529 jack_mixer_channel_t channel,
530 void (*midi_change_callback) (void*),
533 channel_ptr->midi_change_callback = midi_change_callback;
534 channel_ptr->midi_change_callback_data = user_data;
539 /* process input channels and mix them into main mix */
542 struct output_channel *output_mix_channel,
543 GSList *channels_list,
544 jack_nframes_t start, /* index of first sample to process */
545 jack_nframes_t end) /* index of sample to stop processing before */
549 struct channel * channel_ptr;
550 jack_default_audio_sample_t frame_left;
551 jack_default_audio_sample_t frame_right;
552 struct channel *mix_channel = (struct channel*)output_mix_channel;
554 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
556 channel_ptr = node_ptr->data;
558 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
559 /* skip muted channels */
563 if (output_mix_channel->soloed_channels &&
564 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
565 /* skip channels that are not soloed, when some are */
569 for (i = start ; i < end ; i++)
571 if (! output_mix_channel->prefader) {
572 frame_left = channel_ptr->frames_left[i-start];
574 frame_left = channel_ptr->prefader_frames_left[i-start];
576 if (frame_left == NAN)
578 mix_channel->left_buffer_ptr[i] += frame_left;
580 if (mix_channel->stereo)
582 if (! output_mix_channel->prefader) {
583 frame_right = channel_ptr->frames_right[i-start];
585 frame_right = channel_ptr->prefader_frames_right[i-start];
587 if (frame_right == NAN)
590 mix_channel->right_buffer_ptr[i] += frame_right;
597 /* process main mix channel */
598 for (i = start ; i < end ; i++)
600 if (! output_mix_channel->prefader) {
601 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
602 if (mix_channel->stereo)
604 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
608 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
609 if (mix_channel->peak_left < frame_left)
611 mix_channel->peak_left = frame_left;
613 if (frame_left > mix_channel->abspeak)
615 mix_channel->abspeak = frame_left;
619 if (mix_channel->stereo)
621 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
622 if (mix_channel->peak_right < frame_right)
624 mix_channel->peak_right = frame_right;
626 if (frame_right > mix_channel->abspeak)
628 mix_channel->abspeak = frame_right;
633 mix_channel->peak_frames++;
634 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
636 mix_channel->meter_left = mix_channel->peak_left;
637 mix_channel->peak_left = 0.0;
639 if (mix_channel->stereo)
641 mix_channel->meter_right = mix_channel->peak_right;
642 mix_channel->peak_right = 0.0;
645 mix_channel->peak_frames = 0;
652 struct channel *channel_ptr,
653 jack_nframes_t start,
657 jack_default_audio_sample_t frame_left;
658 jack_default_audio_sample_t frame_right;
660 channel_ptr->frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
661 channel_ptr->frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
663 channel_ptr->prefader_frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
664 channel_ptr->prefader_frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
666 for (i = start ; i < end ; i++)
668 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
669 if (channel_ptr->stereo)
670 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
672 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
674 channel_ptr->NaN_detected = true;
675 channel_ptr->frames_left[i-start] = NAN;
679 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
681 if (channel_ptr->stereo)
683 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
685 channel_ptr->NaN_detected = true;
686 channel_ptr->frames_right[i-start] = NAN;
690 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
694 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
697 channel_ptr->frames_left[i-start] = frame_left;
698 channel_ptr->frames_right[i-start] = frame_right;
700 if (channel_ptr->stereo)
702 frame_left = fabsf(frame_left);
703 frame_right = fabsf(frame_right);
705 if (channel_ptr->peak_left < frame_left)
707 channel_ptr->peak_left = frame_left;
709 if (frame_left > channel_ptr->abspeak)
711 channel_ptr->abspeak = frame_left;
715 if (channel_ptr->peak_right < frame_right)
717 channel_ptr->peak_right = frame_right;
719 if (frame_right > channel_ptr->abspeak)
721 channel_ptr->abspeak = frame_right;
727 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
729 if (channel_ptr->peak_left < frame_left)
731 channel_ptr->peak_left = frame_left;
733 if (frame_left > channel_ptr->abspeak)
735 channel_ptr->abspeak = frame_left;
740 channel_ptr->peak_frames++;
741 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
743 channel_ptr->meter_left = channel_ptr->peak_left;
744 channel_ptr->peak_left = 0.0;
746 if (channel_ptr->stereo)
748 channel_ptr->meter_right = channel_ptr->peak_right;
749 channel_ptr->peak_right = 0.0;
752 channel_ptr->peak_frames = 0;
760 struct jack_mixer * mixer_ptr,
761 jack_nframes_t start, /* index of first sample to process */
762 jack_nframes_t end) /* index of sample to stop processing before */
765 struct output_channel * output_channel_ptr;
766 struct channel *channel_ptr;
768 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
770 channel_ptr = (struct channel*)node_ptr->data;
771 calc_channel_frames(channel_ptr, start, end);
774 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
776 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
778 output_channel_ptr = node_ptr->data;
779 channel_ptr = (struct channel*)output_channel_ptr;
781 if (output_channel_ptr->system)
783 /* Don't bother mixing the channels if we are not connected */
784 if (channel_ptr->stereo)
786 if (jack_port_connected(channel_ptr->port_left) == 0 &&
787 jack_port_connected(channel_ptr->port_right) == 0)
790 if (jack_port_connected(channel_ptr->port_left) == 0)
795 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
798 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
800 channel_ptr = (struct channel*)node_ptr->data;
801 free(channel_ptr->frames_left);
802 free(channel_ptr->frames_right);
803 free(channel_ptr->prefader_frames_left);
804 free(channel_ptr->prefader_frames_right);
809 update_channel_buffers(
810 struct channel * channel_ptr,
811 jack_nframes_t nframes)
813 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
815 if (channel_ptr->stereo)
817 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
821 #define mixer_ptr ((struct jack_mixer *)context)
825 jack_nframes_t nframes,
830 struct channel * channel_ptr;
831 #if defined(HAVE_JACK_MIDI)
832 jack_nframes_t event_count;
833 jack_midi_event_t in_event;
837 jack_nframes_t offset;
839 update_channel_buffers((struct channel*)mixer_ptr->main_mix_channel, nframes);
841 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
843 channel_ptr = node_ptr->data;
845 update_channel_buffers(channel_ptr, nframes);
848 for (i = 0 ; i < nframes ; i++)
850 ((struct channel*)(mixer_ptr->main_mix_channel))->left_buffer_ptr[i] = 0.0;
851 ((struct channel*)(mixer_ptr->main_mix_channel))->right_buffer_ptr[i] = 0.0;
854 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
856 channel_ptr = node_ptr->data;
857 update_channel_buffers(channel_ptr, nframes);
858 for (i = 0 ; i < nframes ; i++)
860 channel_ptr->left_buffer_ptr[i] = 0.0;
861 if (channel_ptr->stereo) {
862 channel_ptr->right_buffer_ptr[i] = 0.0;
869 #if defined(HAVE_JACK_MIDI)
870 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
871 event_count = jack_midi_get_event_count(midi_buffer);
873 for (i = 0 ; i < event_count; i++)
875 jack_midi_event_get(&in_event, midi_buffer, i);
877 if (in_event.size != 3 ||
878 (in_event.buffer[0] & 0xF0) != 0xB0 ||
879 in_event.buffer[1] > 127 ||
880 in_event.buffer[2] > 127)
885 assert(in_event.time < nframes);
889 (unsigned int)(in_event.buffer[0] & 0x0F),
890 (unsigned int)in_event.buffer[1],
891 (unsigned int)in_event.buffer[2]);
893 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
894 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
896 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
897 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
899 assert(in_event.time >= offset);
901 if (in_event.time > offset)
903 mix(mixer_ptr, offset, in_event.time);
904 offset = in_event.time;
907 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
909 byte = in_event.buffer[2];
916 channel_ptr->balance = (float)byte / 63;
917 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
921 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
922 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
925 calc_channel_volumes(channel_ptr);
927 if (channel_ptr->midi_change_callback)
928 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
936 mix(mixer_ptr, offset, nframes);
945 const char * jack_client_name_ptr)
948 struct jack_mixer * mixer_ptr;
952 mixer_ptr = malloc(sizeof(struct jack_mixer));
953 if (mixer_ptr == NULL)
958 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
964 mixer_ptr->input_channels_list = NULL;
965 mixer_ptr->output_channels_list = NULL;
967 mixer_ptr->last_midi_channel = 0;
969 for (i = 0 ; i < 128 ; i++)
971 mixer_ptr->midi_cc_map[i] = NULL;
974 LOG_DEBUG("Initializing JACK");
975 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
976 if (mixer_ptr->jack_client == NULL)
978 LOG_ERROR("Cannot create JACK client.");
979 LOG_NOTICE("Please make sure JACK daemon is running.");
980 goto exit_destroy_mutex;
983 LOG_DEBUG("JACK client created");
985 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
987 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
988 if (mixer_ptr->main_mix_channel == NULL) {
989 LOG_ERROR("Cannot create main mix channel");
992 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
993 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
995 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
997 #if defined(HAVE_JACK_MIDI)
998 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
999 if (mixer_ptr->port_midi_in == NULL)
1001 LOG_ERROR("Cannot create JACK port");
1006 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
1008 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1011 LOG_ERROR("Cannot set JACK process callback");
1015 ret = jack_activate(mixer_ptr->jack_client);
1018 LOG_ERROR("Cannot activate JACK client");
1025 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1028 pthread_mutex_destroy(&mixer_ptr->mutex);
1037 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1043 LOG_DEBUG("Uninitializing JACK");
1045 assert(mixer_ctx_ptr->jack_client != NULL);
1047 jack_client_close(mixer_ctx_ptr->jack_client);
1049 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1051 free(mixer_ctx_ptr->main_mix_channel);
1052 free(mixer_ctx_ptr);
1055 jack_mixer_channel_t
1056 get_main_mix_channel(
1059 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1066 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1070 get_last_midi_channel(
1073 return mixer_ctx_ptr->last_midi_channel;
1076 jack_mixer_channel_t
1079 const char * channel_name,
1082 struct channel * channel_ptr;
1084 size_t channel_name_size;
1085 char * output_channel_name;
1087 channel_ptr = malloc(sizeof(struct channel));
1088 if (channel_ptr == NULL)
1093 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1095 channel_ptr->name = strdup(channel_name);
1096 if (channel_ptr->name == NULL)
1098 goto fail_free_channel;
1101 channel_name_size = strlen(channel_name);
1105 port_name = malloc(channel_name_size + 3);
1106 if (port_name == NULL)
1108 goto fail_free_channel_name;
1111 memcpy(port_name, channel_name, channel_name_size);
1112 port_name[channel_name_size] = ' ';
1113 port_name[channel_name_size+1] = 'L';
1114 port_name[channel_name_size+2] = 0;
1116 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1117 if (channel_ptr->port_left == NULL)
1119 goto fail_free_port_name;
1122 port_name[channel_name_size+1] = 'R';
1124 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1125 if (channel_ptr->port_right == NULL)
1127 goto fail_unregister_left_channel;
1132 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1133 if (channel_ptr->port_left == NULL)
1135 goto fail_free_channel_name;
1139 channel_ptr->stereo = stereo;
1141 channel_ptr->volume = 0.0;
1142 channel_ptr->balance = 0.0;
1143 channel_ptr->meter_left = -1.0;
1144 channel_ptr->meter_right = -1.0;
1145 channel_ptr->abspeak = 0.0;
1147 channel_ptr->peak_left = 0.0;
1148 channel_ptr->peak_right = 0.0;
1149 channel_ptr->peak_frames = 0;
1151 channel_ptr->NaN_detected = false;
1153 channel_ptr->midi_cc_volume_index = 0;
1154 channel_ptr->midi_cc_balance_index = 0;
1155 channel_ptr->midi_change_callback = NULL;
1156 channel_ptr->midi_change_callback_data = NULL;
1158 channel_ptr->midi_scale = NULL;
1160 calc_channel_volumes(channel_ptr);
1162 // for monitoring etc.
1163 output_channel_name = malloc(channel_name_size + 5);
1164 sprintf(output_channel_name, "%s Out", channel_name);
1165 channel_ptr->output = add_output_channel(mixer, output_channel_name, stereo, true);
1166 free(output_channel_name);
1168 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1169 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1173 fail_unregister_left_channel:
1174 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1176 fail_free_port_name:
1179 fail_free_channel_name:
1180 free(channel_ptr->name);
1190 static jack_mixer_output_channel_t
1191 create_output_channel(
1193 const char * channel_name,
1197 struct channel * channel_ptr;
1198 struct output_channel * output_channel_ptr;
1200 size_t channel_name_size;
1202 output_channel_ptr = malloc(sizeof(struct output_channel));
1203 channel_ptr = (struct channel*)output_channel_ptr;
1204 if (channel_ptr == NULL)
1209 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1211 channel_ptr->name = strdup(channel_name);
1212 if (channel_ptr->name == NULL)
1214 goto fail_free_channel;
1219 channel_name_size = strlen(channel_name);
1221 port_name = malloc(channel_name_size + 4);
1222 if (port_name == NULL)
1224 goto fail_free_channel_name;
1227 memcpy(port_name, channel_name, channel_name_size);
1228 port_name[channel_name_size] = ' ';
1229 port_name[channel_name_size+1] = 'L';
1230 port_name[channel_name_size+2] = 0;
1232 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1233 if (channel_ptr->port_left == NULL)
1235 goto fail_free_port_name;
1238 port_name[channel_name_size+1] = 'R';
1240 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1241 if (channel_ptr->port_right == NULL)
1243 goto fail_unregister_left_channel;
1248 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1249 if (channel_ptr->port_left == NULL)
1251 goto fail_free_channel_name;
1255 channel_ptr->stereo = stereo;
1257 channel_ptr->volume = 0.0;
1258 channel_ptr->balance = 0.0;
1259 channel_ptr->meter_left = -1.0;
1260 channel_ptr->meter_right = -1.0;
1261 channel_ptr->abspeak = 0.0;
1263 channel_ptr->peak_left = 0.0;
1264 channel_ptr->peak_right = 0.0;
1265 channel_ptr->peak_frames = 0;
1267 channel_ptr->NaN_detected = false;
1269 channel_ptr->midi_cc_volume_index = 0;
1270 channel_ptr->midi_cc_balance_index = 0;
1271 channel_ptr->midi_change_callback = NULL;
1272 channel_ptr->midi_change_callback_data = NULL;
1274 channel_ptr->midi_scale = NULL;
1276 output_channel_ptr->soloed_channels = NULL;
1277 output_channel_ptr->muted_channels = NULL;
1278 output_channel_ptr->system = system;
1279 output_channel_ptr->prefader = false;
1281 return output_channel_ptr;
1283 fail_unregister_left_channel:
1284 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1286 fail_free_port_name:
1289 fail_free_channel_name:
1290 free(channel_ptr->name);
1300 jack_mixer_output_channel_t
1303 const char * channel_name,
1307 struct output_channel *output_channel_ptr;
1308 struct channel *channel_ptr;
1310 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1311 if (output_channel_ptr == NULL) {
1314 channel_ptr = (struct channel*)output_channel_ptr;
1316 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1317 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1319 return output_channel_ptr;
1323 remove_output_channel(
1324 jack_mixer_output_channel_t output_channel)
1326 struct output_channel *output_channel_ptr = output_channel;
1327 struct channel *channel_ptr = output_channel;
1329 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1330 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1331 free(channel_ptr->name);
1333 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1334 if (channel_ptr->stereo)
1336 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1339 if (channel_ptr->midi_cc_volume_index != 0)
1341 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1342 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1345 if (channel_ptr->midi_cc_balance_index != 0)
1347 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1348 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1351 g_slist_free(output_channel_ptr->soloed_channels);
1352 g_slist_free(output_channel_ptr->muted_channels);
1358 output_channel_set_solo(
1359 jack_mixer_output_channel_t output_channel,
1360 jack_mixer_channel_t channel,
1363 struct output_channel *output_channel_ptr = output_channel;
1366 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1368 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1370 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1372 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1377 output_channel_set_muted(
1378 jack_mixer_output_channel_t output_channel,
1379 jack_mixer_channel_t channel,
1382 struct output_channel *output_channel_ptr = output_channel;
1385 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1387 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1389 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1391 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1396 output_channel_is_muted(
1397 jack_mixer_output_channel_t output_channel,
1398 jack_mixer_channel_t channel)
1400 struct output_channel *output_channel_ptr = output_channel;
1402 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1408 output_channel_is_solo(
1409 jack_mixer_output_channel_t output_channel,
1410 jack_mixer_channel_t channel)
1412 struct output_channel *output_channel_ptr = output_channel;
1414 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1420 output_channel_set_prefader(
1421 jack_mixer_output_channel_t output_channel,
1424 struct output_channel *output_channel_ptr = output_channel;
1425 output_channel_ptr->prefader = pfl_value;
1429 output_channel_is_prefader(
1430 jack_mixer_output_channel_t output_channel)
1432 struct output_channel *output_channel_ptr = output_channel;
1433 return output_channel_ptr->prefader;