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,
119 update_channel_buffers(
120 struct channel * channel_ptr,
121 jack_nframes_t nframes);
133 return 20.0 * log10f(value);
140 return powf(10.0, db/20.0);
144 calc_channel_volumes(
145 struct channel * channel_ptr)
147 if (channel_ptr->stereo)
149 if (channel_ptr->balance > 0)
151 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
152 channel_ptr->volume_right = channel_ptr->volume;
156 channel_ptr->volume_left = channel_ptr->volume;
157 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
162 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
163 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
168 calc_all_channel_volumes(
169 struct jack_mixer * mixer_ptr)
171 struct channel * channel_ptr;
174 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
176 channel_ptr = list_ptr->data;
177 calc_channel_volumes(channel_ptr);
181 #define channel_ptr ((struct channel *)channel)
185 jack_mixer_channel_t channel)
187 return channel_ptr->name;
192 jack_mixer_channel_t channel,
196 size_t channel_name_size;
200 new_name = strdup(name);
201 if (new_name == NULL)
206 if (channel_ptr->name)
208 free(channel_ptr->name);
211 channel_ptr->name = new_name;
213 if (channel_ptr->stereo)
215 channel_name_size = strlen(name);
216 port_name = malloc(channel_name_size + 3);
217 memcpy(port_name, name, channel_name_size);
219 port_name[channel_name_size] = ' ';
220 port_name[channel_name_size+1] = 'L';
221 port_name[channel_name_size+2] = 0;
223 ret = jack_port_set_name(channel_ptr->port_left, port_name);
226 /* what could we do here? */
229 port_name[channel_name_size+1] = 'R';
231 ret = jack_port_set_name(channel_ptr->port_right, port_name);
234 /* what could we do here? */
241 ret = jack_port_set_name(channel_ptr->port_left, name);
244 /* what could we do here? */
251 jack_mixer_channel_t channel)
253 return channel_ptr->stereo;
257 channel_get_balance_midi_cc(
258 jack_mixer_channel_t channel)
260 return channel_ptr->midi_cc_balance_index;
264 channel_set_balance_midi_cc(
265 jack_mixer_channel_t channel,
269 return 2; /* error: over limit CC */
271 if (channel_ptr->midi_cc_balance_index == new_cc) {
276 /* 0 is special, it removes the link */
277 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
278 channel_ptr->midi_cc_balance_index = 0;
280 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
281 return 1; /* error: cc in use */
283 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
284 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
285 channel_ptr->midi_cc_balance_index = new_cc;
291 channel_get_volume_midi_cc(
292 jack_mixer_channel_t channel)
294 return channel_ptr->midi_cc_volume_index;
298 channel_set_volume_midi_cc(
299 jack_mixer_channel_t channel, unsigned int new_cc)
302 return 2; /* error: over limit CC */
304 if (channel_ptr->midi_cc_volume_index == new_cc) {
309 /* 0 is special, it removes the link */
310 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
311 channel_ptr->midi_cc_volume_index = 0;
313 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
314 return 1; /* error: cc in use */
316 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
317 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
318 channel_ptr->midi_cc_volume_index = new_cc;
324 channel_autoset_midi_cc(
325 jack_mixer_channel_t channel)
327 struct jack_mixer *mixer_ptr;
330 mixer_ptr = channel_ptr->mixer_ptr;
332 for (i = 11 ; i < 128 ; i++)
334 if (mixer_ptr->midi_cc_map[i] == NULL)
336 mixer_ptr->midi_cc_map[i] = channel_ptr;
337 channel_ptr->midi_cc_volume_index = i;
339 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
345 for (; i < 128 ; i++)
347 if (mixer_ptr->midi_cc_map[i] == NULL)
349 mixer_ptr->midi_cc_map[i] = channel_ptr;
350 channel_ptr->midi_cc_balance_index = i;
352 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
361 jack_mixer_channel_t channel)
365 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
366 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
367 free(channel_ptr->name);
369 /* remove references to input channel from all output channels */
370 channel_unmute(channel_ptr);
371 channel_unsolo(channel_ptr);
372 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
374 struct output_channel *output_channel_ptr = list_ptr->data;
375 output_channel_set_solo(output_channel_ptr, channel, false);
376 output_channel_set_muted(output_channel_ptr, channel, false);
379 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
380 if (channel_ptr->stereo)
382 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
385 if (channel_ptr->midi_cc_volume_index != 0)
387 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
388 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
391 if (channel_ptr->midi_cc_balance_index != 0)
393 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
394 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
396 remove_output_channel(channel_ptr->output);
402 channel_stereo_meter_read(
403 jack_mixer_channel_t channel,
408 *left_ptr = value_to_db(channel_ptr->meter_left);
409 *right_ptr = value_to_db(channel_ptr->meter_right);
413 channel_mono_meter_read(
414 jack_mixer_channel_t channel,
417 *mono_ptr = value_to_db(channel_ptr->meter_left);
421 channel_volume_write(
422 jack_mixer_channel_t channel,
426 channel_ptr->volume = db_to_value(volume);
427 calc_channel_volumes(channel_ptr);
432 jack_mixer_channel_t channel)
435 return value_to_db(channel_ptr->volume);
439 channel_balance_write(
440 jack_mixer_channel_t channel,
444 channel_ptr->balance = balance;
445 calc_channel_volumes(channel_ptr);
449 channel_balance_read(
450 jack_mixer_channel_t channel)
453 return channel_ptr->balance;
457 channel_abspeak_read(
458 jack_mixer_channel_t channel)
461 if (channel_ptr->NaN_detected)
467 return value_to_db(channel_ptr->abspeak);
472 channel_abspeak_reset(
473 jack_mixer_channel_t channel)
475 channel_ptr->abspeak = 0;
476 channel_ptr->NaN_detected = false;
481 jack_mixer_channel_t channel)
483 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
488 jack_mixer_channel_t channel)
490 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
495 jack_mixer_channel_t channel)
497 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
502 jack_mixer_channel_t channel)
504 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
509 jack_mixer_channel_t channel)
511 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
518 jack_mixer_channel_t channel)
520 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
526 channel_set_midi_scale(
527 jack_mixer_channel_t channel,
528 jack_mixer_scale_t scale)
530 channel_ptr->midi_scale = scale;
534 channel_set_midi_change_callback(
535 jack_mixer_channel_t channel,
536 void (*midi_change_callback) (void*),
539 channel_ptr->midi_change_callback = midi_change_callback;
540 channel_ptr->midi_change_callback_data = user_data;
545 /* process input channels and mix them into main mix */
548 struct output_channel *output_mix_channel,
549 GSList *channels_list,
550 jack_nframes_t start, /* index of first sample to process */
551 jack_nframes_t end) /* index of sample to stop processing before */
555 struct channel * channel_ptr;
556 jack_default_audio_sample_t frame_left;
557 jack_default_audio_sample_t frame_right;
558 struct channel *mix_channel = (struct channel*)output_mix_channel;
560 update_channel_buffers(mix_channel, end-start);
561 for (i = 0; i < (end-start); i++)
563 mix_channel->left_buffer_ptr[i] = 0.0;
564 if (mix_channel->stereo)
565 mix_channel->right_buffer_ptr[i] = 0.0;
569 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
571 channel_ptr = node_ptr->data;
573 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
574 /* skip muted channels */
578 if (output_mix_channel->soloed_channels &&
579 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
580 /* skip channels that are not soloed, when some are */
584 for (i = start ; i < end ; i++)
586 if (! output_mix_channel->prefader) {
587 frame_left = channel_ptr->frames_left[i-start];
589 frame_left = channel_ptr->prefader_frames_left[i-start];
591 if (frame_left == NAN)
593 mix_channel->left_buffer_ptr[i] += frame_left;
595 if (mix_channel->stereo)
597 if (! output_mix_channel->prefader) {
598 frame_right = channel_ptr->frames_right[i-start];
600 frame_right = channel_ptr->prefader_frames_right[i-start];
602 if (frame_right == NAN)
605 mix_channel->right_buffer_ptr[i] += frame_right;
612 /* process main mix channel */
613 for (i = start ; i < end ; i++)
615 if (! output_mix_channel->prefader) {
616 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
617 if (mix_channel->stereo)
619 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
623 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
624 if (mix_channel->peak_left < frame_left)
626 mix_channel->peak_left = frame_left;
628 if (frame_left > mix_channel->abspeak)
630 mix_channel->abspeak = frame_left;
634 if (mix_channel->stereo)
636 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
637 if (mix_channel->peak_right < frame_right)
639 mix_channel->peak_right = frame_right;
641 if (frame_right > mix_channel->abspeak)
643 mix_channel->abspeak = frame_right;
648 mix_channel->peak_frames++;
649 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
651 mix_channel->meter_left = mix_channel->peak_left;
652 mix_channel->peak_left = 0.0;
654 if (mix_channel->stereo)
656 mix_channel->meter_right = mix_channel->peak_right;
657 mix_channel->peak_right = 0.0;
660 mix_channel->peak_frames = 0;
667 struct channel *channel_ptr,
668 jack_nframes_t start,
672 jack_default_audio_sample_t frame_left;
673 jack_default_audio_sample_t frame_right;
675 channel_ptr->frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
676 channel_ptr->frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
678 channel_ptr->prefader_frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
679 channel_ptr->prefader_frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
681 for (i = start ; i < end ; i++)
683 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
684 if (channel_ptr->stereo)
685 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
687 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
689 channel_ptr->NaN_detected = true;
690 channel_ptr->frames_left[i-start] = NAN;
694 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
696 if (channel_ptr->stereo)
698 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
700 channel_ptr->NaN_detected = true;
701 channel_ptr->frames_right[i-start] = NAN;
705 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
709 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
712 channel_ptr->frames_left[i-start] = frame_left;
713 channel_ptr->frames_right[i-start] = frame_right;
715 if (channel_ptr->stereo)
717 frame_left = fabsf(frame_left);
718 frame_right = fabsf(frame_right);
720 if (channel_ptr->peak_left < frame_left)
722 channel_ptr->peak_left = frame_left;
724 if (frame_left > channel_ptr->abspeak)
726 channel_ptr->abspeak = frame_left;
730 if (channel_ptr->peak_right < frame_right)
732 channel_ptr->peak_right = frame_right;
734 if (frame_right > channel_ptr->abspeak)
736 channel_ptr->abspeak = frame_right;
742 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
744 if (channel_ptr->peak_left < frame_left)
746 channel_ptr->peak_left = frame_left;
748 if (frame_left > channel_ptr->abspeak)
750 channel_ptr->abspeak = frame_left;
755 channel_ptr->peak_frames++;
756 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
758 channel_ptr->meter_left = channel_ptr->peak_left;
759 channel_ptr->peak_left = 0.0;
761 if (channel_ptr->stereo)
763 channel_ptr->meter_right = channel_ptr->peak_right;
764 channel_ptr->peak_right = 0.0;
767 channel_ptr->peak_frames = 0;
775 struct jack_mixer * mixer_ptr,
776 jack_nframes_t start, /* index of first sample to process */
777 jack_nframes_t end) /* index of sample to stop processing before */
780 struct output_channel * output_channel_ptr;
781 struct channel *channel_ptr;
783 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
785 channel_ptr = (struct channel*)node_ptr->data;
786 calc_channel_frames(channel_ptr, start, end);
789 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
791 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
793 output_channel_ptr = node_ptr->data;
794 channel_ptr = (struct channel*)output_channel_ptr;
796 if (output_channel_ptr->system)
798 /* Don't bother mixing the channels if we are not connected */
799 if (channel_ptr->stereo)
801 if (jack_port_connected(channel_ptr->port_left) == 0 &&
802 jack_port_connected(channel_ptr->port_right) == 0)
805 if (jack_port_connected(channel_ptr->port_left) == 0)
810 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
813 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
815 channel_ptr = (struct channel*)node_ptr->data;
816 free(channel_ptr->frames_left);
817 free(channel_ptr->frames_right);
818 free(channel_ptr->prefader_frames_left);
819 free(channel_ptr->prefader_frames_right);
824 update_channel_buffers(
825 struct channel * channel_ptr,
826 jack_nframes_t nframes)
828 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
830 if (channel_ptr->stereo)
832 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
836 #define mixer_ptr ((struct jack_mixer *)context)
840 jack_nframes_t nframes,
845 struct channel * channel_ptr;
846 #if defined(HAVE_JACK_MIDI)
847 jack_nframes_t event_count;
848 jack_midi_event_t in_event;
852 jack_nframes_t offset;
854 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
856 channel_ptr = node_ptr->data;
857 update_channel_buffers(channel_ptr, nframes);
862 #if defined(HAVE_JACK_MIDI)
863 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
864 event_count = jack_midi_get_event_count(midi_buffer);
866 for (i = 0 ; i < event_count; i++)
868 jack_midi_event_get(&in_event, midi_buffer, i);
870 if (in_event.size != 3 ||
871 (in_event.buffer[0] & 0xF0) != 0xB0 ||
872 in_event.buffer[1] > 127 ||
873 in_event.buffer[2] > 127)
878 assert(in_event.time < nframes);
882 (unsigned int)(in_event.buffer[0] & 0x0F),
883 (unsigned int)in_event.buffer[1],
884 (unsigned int)in_event.buffer[2]);
886 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
887 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
889 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
890 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
892 assert(in_event.time >= offset);
894 if (in_event.time > offset)
896 mix(mixer_ptr, offset, in_event.time);
897 offset = in_event.time;
900 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
902 byte = in_event.buffer[2];
909 channel_ptr->balance = (float)byte / 63;
910 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
914 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
915 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
918 calc_channel_volumes(channel_ptr);
920 if (channel_ptr->midi_change_callback)
921 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
929 mix(mixer_ptr, offset, nframes);
938 const char * jack_client_name_ptr)
941 struct jack_mixer * mixer_ptr;
945 mixer_ptr = malloc(sizeof(struct jack_mixer));
946 if (mixer_ptr == NULL)
951 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
957 mixer_ptr->input_channels_list = NULL;
958 mixer_ptr->output_channels_list = NULL;
960 mixer_ptr->last_midi_channel = 0;
962 for (i = 0 ; i < 128 ; i++)
964 mixer_ptr->midi_cc_map[i] = NULL;
967 LOG_DEBUG("Initializing JACK");
968 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
969 if (mixer_ptr->jack_client == NULL)
971 LOG_ERROR("Cannot create JACK client.");
972 LOG_NOTICE("Please make sure JACK daemon is running.");
973 goto exit_destroy_mutex;
976 LOG_DEBUG("JACK client created");
978 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
980 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
981 if (mixer_ptr->main_mix_channel == NULL) {
982 LOG_ERROR("Cannot create main mix channel");
985 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
986 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
988 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
990 #if defined(HAVE_JACK_MIDI)
991 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
992 if (mixer_ptr->port_midi_in == NULL)
994 LOG_ERROR("Cannot create JACK port");
999 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
1001 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1004 LOG_ERROR("Cannot set JACK process callback");
1008 ret = jack_activate(mixer_ptr->jack_client);
1011 LOG_ERROR("Cannot activate JACK client");
1018 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1021 pthread_mutex_destroy(&mixer_ptr->mutex);
1030 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1036 LOG_DEBUG("Uninitializing JACK");
1038 assert(mixer_ctx_ptr->jack_client != NULL);
1040 jack_client_close(mixer_ctx_ptr->jack_client);
1042 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1044 free(mixer_ctx_ptr->main_mix_channel);
1045 free(mixer_ctx_ptr);
1048 jack_mixer_channel_t
1049 get_main_mix_channel(
1052 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1059 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1063 get_last_midi_channel(
1066 return mixer_ctx_ptr->last_midi_channel;
1069 jack_mixer_channel_t
1072 const char * channel_name,
1075 struct channel * channel_ptr;
1077 size_t channel_name_size;
1078 char * output_channel_name;
1080 channel_ptr = malloc(sizeof(struct channel));
1081 if (channel_ptr == NULL)
1086 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1088 channel_ptr->name = strdup(channel_name);
1089 if (channel_ptr->name == NULL)
1091 goto fail_free_channel;
1094 channel_name_size = strlen(channel_name);
1098 port_name = malloc(channel_name_size + 3);
1099 if (port_name == NULL)
1101 goto fail_free_channel_name;
1104 memcpy(port_name, channel_name, channel_name_size);
1105 port_name[channel_name_size] = ' ';
1106 port_name[channel_name_size+1] = 'L';
1107 port_name[channel_name_size+2] = 0;
1109 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1110 if (channel_ptr->port_left == NULL)
1112 goto fail_free_port_name;
1115 port_name[channel_name_size+1] = 'R';
1117 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1118 if (channel_ptr->port_right == NULL)
1120 goto fail_unregister_left_channel;
1125 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1126 if (channel_ptr->port_left == NULL)
1128 goto fail_free_channel_name;
1132 channel_ptr->stereo = stereo;
1134 channel_ptr->volume = 0.0;
1135 channel_ptr->balance = 0.0;
1136 channel_ptr->meter_left = -1.0;
1137 channel_ptr->meter_right = -1.0;
1138 channel_ptr->abspeak = 0.0;
1140 channel_ptr->peak_left = 0.0;
1141 channel_ptr->peak_right = 0.0;
1142 channel_ptr->peak_frames = 0;
1144 channel_ptr->NaN_detected = false;
1146 channel_ptr->midi_cc_volume_index = 0;
1147 channel_ptr->midi_cc_balance_index = 0;
1148 channel_ptr->midi_change_callback = NULL;
1149 channel_ptr->midi_change_callback_data = NULL;
1151 channel_ptr->midi_scale = NULL;
1153 calc_channel_volumes(channel_ptr);
1155 // for monitoring etc.
1156 output_channel_name = malloc(channel_name_size + 5);
1157 sprintf(output_channel_name, "%s Out", channel_name);
1158 channel_ptr->output = add_output_channel(mixer, output_channel_name, stereo, true);
1159 free(output_channel_name);
1161 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1162 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1166 fail_unregister_left_channel:
1167 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1169 fail_free_port_name:
1172 fail_free_channel_name:
1173 free(channel_ptr->name);
1183 static jack_mixer_output_channel_t
1184 create_output_channel(
1186 const char * channel_name,
1190 struct channel * channel_ptr;
1191 struct output_channel * output_channel_ptr;
1193 size_t channel_name_size;
1195 output_channel_ptr = malloc(sizeof(struct output_channel));
1196 channel_ptr = (struct channel*)output_channel_ptr;
1197 if (channel_ptr == NULL)
1202 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1204 channel_ptr->name = strdup(channel_name);
1205 if (channel_ptr->name == NULL)
1207 goto fail_free_channel;
1212 channel_name_size = strlen(channel_name);
1214 port_name = malloc(channel_name_size + 4);
1215 if (port_name == NULL)
1217 goto fail_free_channel_name;
1220 memcpy(port_name, channel_name, channel_name_size);
1221 port_name[channel_name_size] = ' ';
1222 port_name[channel_name_size+1] = 'L';
1223 port_name[channel_name_size+2] = 0;
1225 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1226 if (channel_ptr->port_left == NULL)
1228 goto fail_free_port_name;
1231 port_name[channel_name_size+1] = 'R';
1233 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1234 if (channel_ptr->port_right == NULL)
1236 goto fail_unregister_left_channel;
1241 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1242 if (channel_ptr->port_left == NULL)
1244 goto fail_free_channel_name;
1248 channel_ptr->stereo = stereo;
1250 channel_ptr->volume = 0.0;
1251 channel_ptr->balance = 0.0;
1252 channel_ptr->meter_left = -1.0;
1253 channel_ptr->meter_right = -1.0;
1254 channel_ptr->abspeak = 0.0;
1256 channel_ptr->peak_left = 0.0;
1257 channel_ptr->peak_right = 0.0;
1258 channel_ptr->peak_frames = 0;
1260 channel_ptr->NaN_detected = false;
1262 channel_ptr->midi_cc_volume_index = 0;
1263 channel_ptr->midi_cc_balance_index = 0;
1264 channel_ptr->midi_change_callback = NULL;
1265 channel_ptr->midi_change_callback_data = NULL;
1267 channel_ptr->midi_scale = NULL;
1269 output_channel_ptr->soloed_channels = NULL;
1270 output_channel_ptr->muted_channels = NULL;
1271 output_channel_ptr->system = system;
1272 output_channel_ptr->prefader = false;
1274 return output_channel_ptr;
1276 fail_unregister_left_channel:
1277 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1279 fail_free_port_name:
1282 fail_free_channel_name:
1283 free(channel_ptr->name);
1293 jack_mixer_output_channel_t
1296 const char * channel_name,
1300 struct output_channel *output_channel_ptr;
1301 struct channel *channel_ptr;
1303 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1304 if (output_channel_ptr == NULL) {
1307 channel_ptr = (struct channel*)output_channel_ptr;
1309 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1310 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1312 return output_channel_ptr;
1316 remove_output_channel(
1317 jack_mixer_output_channel_t output_channel)
1319 struct output_channel *output_channel_ptr = output_channel;
1320 struct channel *channel_ptr = output_channel;
1322 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1323 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1324 free(channel_ptr->name);
1326 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1327 if (channel_ptr->stereo)
1329 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1332 if (channel_ptr->midi_cc_volume_index != 0)
1334 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1335 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1338 if (channel_ptr->midi_cc_balance_index != 0)
1340 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1341 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1344 g_slist_free(output_channel_ptr->soloed_channels);
1345 g_slist_free(output_channel_ptr->muted_channels);
1351 output_channel_set_solo(
1352 jack_mixer_output_channel_t output_channel,
1353 jack_mixer_channel_t channel,
1356 struct output_channel *output_channel_ptr = output_channel;
1359 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1361 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1363 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1365 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1370 output_channel_set_muted(
1371 jack_mixer_output_channel_t output_channel,
1372 jack_mixer_channel_t channel,
1375 struct output_channel *output_channel_ptr = output_channel;
1378 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1380 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1382 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1384 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1389 output_channel_is_muted(
1390 jack_mixer_output_channel_t output_channel,
1391 jack_mixer_channel_t channel)
1393 struct output_channel *output_channel_ptr = output_channel;
1395 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1401 output_channel_is_solo(
1402 jack_mixer_output_channel_t output_channel,
1403 jack_mixer_channel_t channel)
1405 struct output_channel *output_channel_ptr = output_channel;
1407 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1413 output_channel_set_prefader(
1414 jack_mixer_output_channel_t output_channel,
1417 struct output_channel *output_channel_ptr = output_channel;
1418 output_channel_ptr->prefader = pfl_value;
1422 output_channel_is_prefader(
1423 jack_mixer_output_channel_t output_channel)
1425 struct output_channel *output_channel_ptr = output_channel;
1426 return output_channel_ptr->prefader;