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;
88 void (*midi_change_callback) (void*);
89 void *midi_change_callback_data;
91 jack_mixer_scale_t midi_scale;
94 struct output_channel {
95 struct channel channel;
96 GSList *soloed_channels;
97 GSList *muted_channels;
98 bool system; /* system channel, without any associated UI */
104 pthread_mutex_t mutex;
105 jack_client_t * jack_client;
106 GSList *input_channels_list;
107 GSList *output_channels_list;
108 struct output_channel *main_mix_channel;
110 jack_port_t * port_midi_in;
111 unsigned int last_midi_channel;
113 struct channel* midi_cc_map[128];
116 static jack_mixer_output_channel_t create_output_channel(
118 const char * channel_name,
123 update_channel_buffers(
124 struct channel * channel_ptr,
125 jack_nframes_t nframes);
137 return 20.0 * log10f(value);
144 return powf(10.0, db/20.0);
148 calc_channel_volumes(
149 struct channel * channel_ptr)
151 if (channel_ptr->stereo)
153 if (channel_ptr->balance > 0)
155 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
156 channel_ptr->volume_right = channel_ptr->volume;
160 channel_ptr->volume_left = channel_ptr->volume;
161 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
166 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
167 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
172 calc_all_channel_volumes(
173 struct jack_mixer * mixer_ptr)
175 struct channel * channel_ptr;
178 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
180 channel_ptr = list_ptr->data;
181 calc_channel_volumes(channel_ptr);
185 #define channel_ptr ((struct channel *)channel)
189 jack_mixer_channel_t channel)
191 return channel_ptr->name;
196 jack_mixer_channel_t channel,
200 size_t channel_name_size;
204 new_name = strdup(name);
205 if (new_name == NULL)
210 if (channel_ptr->name)
212 free(channel_ptr->name);
215 channel_ptr->name = new_name;
217 if (channel_ptr->stereo)
219 channel_name_size = strlen(name);
220 port_name = malloc(channel_name_size + 3);
221 memcpy(port_name, name, channel_name_size);
223 port_name[channel_name_size] = ' ';
224 port_name[channel_name_size+1] = 'L';
225 port_name[channel_name_size+2] = 0;
227 ret = jack_port_set_name(channel_ptr->port_left, port_name);
230 /* what could we do here? */
233 port_name[channel_name_size+1] = 'R';
235 ret = jack_port_set_name(channel_ptr->port_right, port_name);
238 /* what could we do here? */
245 ret = jack_port_set_name(channel_ptr->port_left, name);
248 /* what could we do here? */
255 jack_mixer_channel_t channel)
257 return channel_ptr->stereo;
261 channel_get_balance_midi_cc(
262 jack_mixer_channel_t channel)
264 return channel_ptr->midi_cc_balance_index;
268 channel_set_balance_midi_cc(
269 jack_mixer_channel_t channel,
273 return 2; /* error: over limit CC */
275 if (channel_ptr->midi_cc_balance_index == new_cc) {
280 /* 0 is special, it removes the link */
281 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
282 channel_ptr->midi_cc_balance_index = 0;
284 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
285 return 1; /* error: cc in use */
287 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
288 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
289 channel_ptr->midi_cc_balance_index = new_cc;
295 channel_get_volume_midi_cc(
296 jack_mixer_channel_t channel)
298 return channel_ptr->midi_cc_volume_index;
302 channel_set_volume_midi_cc(
303 jack_mixer_channel_t channel, unsigned int new_cc)
306 return 2; /* error: over limit CC */
308 if (channel_ptr->midi_cc_volume_index == new_cc) {
313 /* 0 is special, it removes the link */
314 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
315 channel_ptr->midi_cc_volume_index = 0;
317 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
318 return 1; /* error: cc in use */
320 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
321 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
322 channel_ptr->midi_cc_volume_index = new_cc;
328 channel_autoset_midi_cc(
329 jack_mixer_channel_t channel)
331 struct jack_mixer *mixer_ptr;
334 mixer_ptr = channel_ptr->mixer_ptr;
336 for (i = 11 ; i < 128 ; i++)
338 if (mixer_ptr->midi_cc_map[i] == NULL)
340 mixer_ptr->midi_cc_map[i] = channel_ptr;
341 channel_ptr->midi_cc_volume_index = i;
343 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
349 for (; i < 128 ; i++)
351 if (mixer_ptr->midi_cc_map[i] == NULL)
353 mixer_ptr->midi_cc_map[i] = channel_ptr;
354 channel_ptr->midi_cc_balance_index = i;
356 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
365 jack_mixer_channel_t channel)
369 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
370 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
371 free(channel_ptr->name);
373 /* remove references to input channel from all output channels */
374 channel_unmute(channel_ptr);
375 channel_unsolo(channel_ptr);
376 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
378 struct output_channel *output_channel_ptr = list_ptr->data;
379 output_channel_set_solo(output_channel_ptr, channel, false);
380 output_channel_set_muted(output_channel_ptr, channel, false);
383 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
384 if (channel_ptr->stereo)
386 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
389 if (channel_ptr->midi_cc_volume_index != 0)
391 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
392 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
395 if (channel_ptr->midi_cc_balance_index != 0)
397 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
398 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
405 channel_stereo_meter_read(
406 jack_mixer_channel_t channel,
411 *left_ptr = value_to_db(channel_ptr->meter_left);
412 *right_ptr = value_to_db(channel_ptr->meter_right);
416 channel_mono_meter_read(
417 jack_mixer_channel_t channel,
420 *mono_ptr = value_to_db(channel_ptr->meter_left);
424 channel_volume_write(
425 jack_mixer_channel_t channel,
429 channel_ptr->volume = db_to_value(volume);
430 calc_channel_volumes(channel_ptr);
435 jack_mixer_channel_t channel)
438 return value_to_db(channel_ptr->volume);
442 channel_balance_write(
443 jack_mixer_channel_t channel,
447 channel_ptr->balance = balance;
448 calc_channel_volumes(channel_ptr);
452 channel_balance_read(
453 jack_mixer_channel_t channel)
456 return channel_ptr->balance;
460 channel_abspeak_read(
461 jack_mixer_channel_t channel)
464 if (channel_ptr->NaN_detected)
470 return value_to_db(channel_ptr->abspeak);
475 channel_abspeak_reset(
476 jack_mixer_channel_t channel)
478 channel_ptr->abspeak = 0;
479 channel_ptr->NaN_detected = false;
484 jack_mixer_channel_t channel)
486 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
491 jack_mixer_channel_t channel)
493 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
498 jack_mixer_channel_t channel)
500 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
505 jack_mixer_channel_t channel)
507 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
512 jack_mixer_channel_t channel)
514 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
521 jack_mixer_channel_t channel)
523 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
529 channel_set_midi_scale(
530 jack_mixer_channel_t channel,
531 jack_mixer_scale_t scale)
533 channel_ptr->midi_scale = scale;
537 channel_set_midi_change_callback(
538 jack_mixer_channel_t channel,
539 void (*midi_change_callback) (void*),
542 channel_ptr->midi_change_callback = midi_change_callback;
543 channel_ptr->midi_change_callback_data = user_data;
548 /* process input channels and mix them into main mix */
551 struct output_channel *output_mix_channel,
552 GSList *channels_list,
553 jack_nframes_t start, /* index of first sample to process */
554 jack_nframes_t end) /* index of sample to stop processing before */
558 struct channel * channel_ptr;
559 jack_default_audio_sample_t frame_left;
560 jack_default_audio_sample_t frame_right;
561 struct channel *mix_channel = (struct channel*)output_mix_channel;
563 update_channel_buffers(mix_channel, end-start);
564 for (i = 0; i < (end-start); i++)
566 mix_channel->left_buffer_ptr[i] = 0.0;
567 if (mix_channel->stereo)
568 mix_channel->right_buffer_ptr[i] = 0.0;
572 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
574 channel_ptr = node_ptr->data;
576 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
577 /* skip muted channels */
581 if (output_mix_channel->soloed_channels &&
582 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
583 /* skip channels that are not soloed, when some are */
587 for (i = start ; i < end ; i++)
589 if (! output_mix_channel->prefader) {
590 frame_left = channel_ptr->frames_left[i-start];
592 frame_left = channel_ptr->prefader_frames_left[i-start];
594 if (frame_left == NAN)
596 mix_channel->left_buffer_ptr[i] += frame_left;
598 if (mix_channel->stereo)
600 if (! output_mix_channel->prefader) {
601 frame_right = channel_ptr->frames_right[i-start];
603 frame_right = channel_ptr->prefader_frames_right[i-start];
605 if (frame_right == NAN)
608 mix_channel->right_buffer_ptr[i] += frame_right;
615 /* process main mix channel */
616 for (i = start ; i < end ; i++)
618 if (! output_mix_channel->prefader) {
619 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
620 if (mix_channel->stereo)
622 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
626 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
627 if (mix_channel->peak_left < frame_left)
629 mix_channel->peak_left = frame_left;
631 if (frame_left > mix_channel->abspeak)
633 mix_channel->abspeak = frame_left;
637 if (mix_channel->stereo)
639 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
640 if (mix_channel->peak_right < frame_right)
642 mix_channel->peak_right = frame_right;
644 if (frame_right > mix_channel->abspeak)
646 mix_channel->abspeak = frame_right;
651 mix_channel->peak_frames++;
652 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
654 mix_channel->meter_left = mix_channel->peak_left;
655 mix_channel->peak_left = 0.0;
657 if (mix_channel->stereo)
659 mix_channel->meter_right = mix_channel->peak_right;
660 mix_channel->peak_right = 0.0;
663 mix_channel->peak_frames = 0;
670 struct channel *channel_ptr,
671 jack_nframes_t start,
675 jack_default_audio_sample_t frame_left;
676 jack_default_audio_sample_t frame_right;
678 for (i = start ; i < end ; i++)
680 if (i-start >= MAX_BLOCK_SIZE)
682 fprintf(STDERR, "i-start too high: %d - %d\n", i, start);
684 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
685 if (channel_ptr->stereo)
686 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
688 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
690 channel_ptr->NaN_detected = true;
691 channel_ptr->frames_left[i-start] = NAN;
695 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
697 if (channel_ptr->stereo)
699 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
701 channel_ptr->NaN_detected = true;
702 channel_ptr->frames_right[i-start] = NAN;
706 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
710 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
713 channel_ptr->frames_left[i-start] = frame_left;
714 channel_ptr->frames_right[i-start] = frame_right;
716 if (channel_ptr->stereo)
718 frame_left = fabsf(frame_left);
719 frame_right = fabsf(frame_right);
721 if (channel_ptr->peak_left < frame_left)
723 channel_ptr->peak_left = frame_left;
725 if (frame_left > channel_ptr->abspeak)
727 channel_ptr->abspeak = frame_left;
731 if (channel_ptr->peak_right < frame_right)
733 channel_ptr->peak_right = frame_right;
735 if (frame_right > channel_ptr->abspeak)
737 channel_ptr->abspeak = frame_right;
743 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
745 if (channel_ptr->peak_left < frame_left)
747 channel_ptr->peak_left = frame_left;
749 if (frame_left > channel_ptr->abspeak)
751 channel_ptr->abspeak = frame_left;
756 channel_ptr->peak_frames++;
757 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
759 channel_ptr->meter_left = channel_ptr->peak_left;
760 channel_ptr->peak_left = 0.0;
762 if (channel_ptr->stereo)
764 channel_ptr->meter_right = channel_ptr->peak_right;
765 channel_ptr->peak_right = 0.0;
768 channel_ptr->peak_frames = 0;
776 struct jack_mixer * mixer_ptr,
777 jack_nframes_t start, /* index of first sample to process */
778 jack_nframes_t end) /* index of sample to stop processing before */
781 struct output_channel * output_channel_ptr;
782 struct channel *channel_ptr;
784 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
786 channel_ptr = (struct channel*)node_ptr->data;
787 calc_channel_frames(channel_ptr, start, end);
790 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
792 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
794 output_channel_ptr = node_ptr->data;
795 channel_ptr = (struct channel*)output_channel_ptr;
797 if (output_channel_ptr->system)
799 /* Don't bother mixing the channels if we are not connected */
800 if (channel_ptr->stereo)
802 if (jack_port_connected(channel_ptr->port_left) == 0 &&
803 jack_port_connected(channel_ptr->port_right) == 0)
806 if (jack_port_connected(channel_ptr->port_left) == 0)
811 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
816 update_channel_buffers(
817 struct channel * channel_ptr,
818 jack_nframes_t nframes)
820 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
822 if (channel_ptr->stereo)
824 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
828 #define mixer_ptr ((struct jack_mixer *)context)
832 jack_nframes_t nframes,
837 struct channel * channel_ptr;
838 #if defined(HAVE_JACK_MIDI)
839 jack_nframes_t event_count;
840 jack_midi_event_t in_event;
844 jack_nframes_t offset;
846 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
848 channel_ptr = node_ptr->data;
849 update_channel_buffers(channel_ptr, nframes);
854 #if defined(HAVE_JACK_MIDI)
855 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
856 event_count = jack_midi_get_event_count(midi_buffer);
858 for (i = 0 ; i < event_count; i++)
860 jack_midi_event_get(&in_event, midi_buffer, i);
862 if (in_event.size != 3 ||
863 (in_event.buffer[0] & 0xF0) != 0xB0 ||
864 in_event.buffer[1] > 127 ||
865 in_event.buffer[2] > 127)
870 assert(in_event.time < nframes);
874 (unsigned int)(in_event.buffer[0] & 0x0F),
875 (unsigned int)in_event.buffer[1],
876 (unsigned int)in_event.buffer[2]);
878 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
879 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
881 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
882 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
884 assert(in_event.time >= offset);
886 if (in_event.time > offset)
888 mix(mixer_ptr, offset, in_event.time);
889 offset = in_event.time;
892 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
894 byte = in_event.buffer[2];
901 channel_ptr->balance = (float)byte / 63;
902 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
906 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
907 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
910 calc_channel_volumes(channel_ptr);
912 if (channel_ptr->midi_change_callback)
913 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
921 mix(mixer_ptr, offset, nframes);
930 const char * jack_client_name_ptr)
933 struct jack_mixer * mixer_ptr;
937 mixer_ptr = malloc(sizeof(struct jack_mixer));
938 if (mixer_ptr == NULL)
943 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
949 mixer_ptr->input_channels_list = NULL;
950 mixer_ptr->output_channels_list = NULL;
952 mixer_ptr->last_midi_channel = 0;
954 for (i = 0 ; i < 128 ; i++)
956 mixer_ptr->midi_cc_map[i] = NULL;
959 LOG_DEBUG("Initializing JACK");
960 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
961 if (mixer_ptr->jack_client == NULL)
963 LOG_ERROR("Cannot create JACK client.");
964 LOG_NOTICE("Please make sure JACK daemon is running.");
965 goto exit_destroy_mutex;
968 LOG_DEBUG("JACK client created");
970 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
972 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
973 if (mixer_ptr->main_mix_channel == NULL) {
974 LOG_ERROR("Cannot create main mix channel");
977 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
978 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
980 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
982 #if defined(HAVE_JACK_MIDI)
983 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
984 if (mixer_ptr->port_midi_in == NULL)
986 LOG_ERROR("Cannot create JACK port");
991 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
993 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
996 LOG_ERROR("Cannot set JACK process callback");
1000 ret = jack_activate(mixer_ptr->jack_client);
1003 LOG_ERROR("Cannot activate JACK client");
1010 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1013 pthread_mutex_destroy(&mixer_ptr->mutex);
1022 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1028 LOG_DEBUG("Uninitializing JACK");
1030 assert(mixer_ctx_ptr->jack_client != NULL);
1032 jack_client_close(mixer_ctx_ptr->jack_client);
1034 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1036 free(mixer_ctx_ptr->main_mix_channel);
1037 free(mixer_ctx_ptr);
1040 jack_mixer_channel_t
1041 get_main_mix_channel(
1044 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1051 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1055 get_last_midi_channel(
1058 return mixer_ctx_ptr->last_midi_channel;
1061 jack_mixer_channel_t
1064 const char * channel_name,
1067 struct channel * channel_ptr;
1069 size_t channel_name_size;
1071 channel_ptr = malloc(sizeof(struct channel));
1072 if (channel_ptr == NULL)
1077 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1079 channel_ptr->name = strdup(channel_name);
1080 if (channel_ptr->name == NULL)
1082 goto fail_free_channel;
1085 channel_name_size = strlen(channel_name);
1089 port_name = malloc(channel_name_size + 3);
1090 if (port_name == NULL)
1092 goto fail_free_channel_name;
1095 memcpy(port_name, channel_name, channel_name_size);
1096 port_name[channel_name_size] = ' ';
1097 port_name[channel_name_size+1] = 'L';
1098 port_name[channel_name_size+2] = 0;
1100 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1101 if (channel_ptr->port_left == NULL)
1103 goto fail_free_port_name;
1106 port_name[channel_name_size+1] = 'R';
1108 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1109 if (channel_ptr->port_right == NULL)
1111 goto fail_unregister_left_channel;
1116 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1117 if (channel_ptr->port_left == NULL)
1119 goto fail_free_channel_name;
1123 channel_ptr->stereo = stereo;
1125 channel_ptr->volume = 0.0;
1126 channel_ptr->balance = 0.0;
1127 channel_ptr->meter_left = -1.0;
1128 channel_ptr->meter_right = -1.0;
1129 channel_ptr->abspeak = 0.0;
1131 channel_ptr->peak_left = 0.0;
1132 channel_ptr->peak_right = 0.0;
1133 channel_ptr->peak_frames = 0;
1135 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1136 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1137 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1138 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
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->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1251 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1252 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1253 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1255 channel_ptr->NaN_detected = false;
1257 channel_ptr->midi_cc_volume_index = 0;
1258 channel_ptr->midi_cc_balance_index = 0;
1259 channel_ptr->midi_change_callback = NULL;
1260 channel_ptr->midi_change_callback_data = NULL;
1262 channel_ptr->midi_scale = NULL;
1264 output_channel_ptr->soloed_channels = NULL;
1265 output_channel_ptr->muted_channels = NULL;
1266 output_channel_ptr->system = system;
1267 output_channel_ptr->prefader = false;
1269 return output_channel_ptr;
1271 fail_unregister_left_channel:
1272 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1274 fail_free_port_name:
1277 fail_free_channel_name:
1278 free(channel_ptr->name);
1288 jack_mixer_output_channel_t
1291 const char * channel_name,
1295 struct output_channel *output_channel_ptr;
1296 struct channel *channel_ptr;
1298 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1299 if (output_channel_ptr == NULL) {
1302 channel_ptr = (struct channel*)output_channel_ptr;
1304 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1305 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1307 return output_channel_ptr;
1311 remove_output_channel(
1312 jack_mixer_output_channel_t output_channel)
1314 struct output_channel *output_channel_ptr = output_channel;
1315 struct channel *channel_ptr = output_channel;
1317 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1318 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1319 free(channel_ptr->name);
1321 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1322 if (channel_ptr->stereo)
1324 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1327 if (channel_ptr->midi_cc_volume_index != 0)
1329 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1330 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1333 if (channel_ptr->midi_cc_balance_index != 0)
1335 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1336 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1339 g_slist_free(output_channel_ptr->soloed_channels);
1340 g_slist_free(output_channel_ptr->muted_channels);
1346 output_channel_set_solo(
1347 jack_mixer_output_channel_t output_channel,
1348 jack_mixer_channel_t channel,
1351 struct output_channel *output_channel_ptr = output_channel;
1354 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1356 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1358 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1360 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1365 output_channel_set_muted(
1366 jack_mixer_output_channel_t output_channel,
1367 jack_mixer_channel_t channel,
1370 struct output_channel *output_channel_ptr = output_channel;
1373 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1375 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1377 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1379 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1384 output_channel_is_muted(
1385 jack_mixer_output_channel_t output_channel,
1386 jack_mixer_channel_t channel)
1388 struct output_channel *output_channel_ptr = output_channel;
1390 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1396 output_channel_is_solo(
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->soloed_channels, channel) != NULL)
1408 output_channel_set_prefader(
1409 jack_mixer_output_channel_t output_channel,
1412 struct output_channel *output_channel_ptr = output_channel;
1413 output_channel_ptr->prefader = pfl_value;
1417 output_channel_is_prefader(
1418 jack_mixer_output_channel_t output_channel)
1420 struct output_channel *output_channel_ptr = output_channel;
1421 return output_channel_ptr->prefader;