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 float value_to_db(float value)
119 return 20.0 * log10f(value);
122 float db_to_value(float db)
124 return powf(10.0, db/20.0);
127 static jack_mixer_output_channel_t
128 create_output_channel(
130 const char * channel_name,
136 calc_channel_volumes(struct channel * channel_ptr)
138 if (channel_ptr->stereo)
140 if (channel_ptr->balance > 0)
142 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
143 channel_ptr->volume_right = channel_ptr->volume;
147 channel_ptr->volume_left = channel_ptr->volume;
148 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
153 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
154 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
159 calc_all_channel_volumes(
160 struct jack_mixer * mixer_ptr)
162 struct channel * channel_ptr;
165 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
167 channel_ptr = list_ptr->data;
168 calc_channel_volumes(channel_ptr);
172 #define channel_ptr ((struct channel *)channel)
174 const char * channel_get_name(jack_mixer_channel_t channel)
176 return channel_ptr->name;
179 void channel_rename(jack_mixer_channel_t channel, const char * name)
182 size_t channel_name_size;
186 new_name = strdup(name);
187 if (new_name == NULL)
192 if (channel_ptr->name)
194 free(channel_ptr->name);
197 channel_ptr->name = new_name;
199 if (channel_ptr->stereo)
201 channel_name_size = strlen(name);
202 port_name = malloc(channel_name_size + 3);
203 memcpy(port_name, name, channel_name_size);
205 port_name[channel_name_size] = ' ';
206 port_name[channel_name_size+1] = 'L';
207 port_name[channel_name_size+2] = 0;
209 ret = jack_port_set_name(channel_ptr->port_left, port_name);
212 /* what could we do here? */
215 port_name[channel_name_size+1] = 'R';
217 ret = jack_port_set_name(channel_ptr->port_right, port_name);
220 /* what could we do here? */
227 ret = jack_port_set_name(channel_ptr->port_left, name);
230 /* what could we do here? */
235 bool channel_is_stereo(jack_mixer_channel_t channel)
237 return channel_ptr->stereo;
240 unsigned int channel_get_balance_midi_cc(jack_mixer_channel_t channel)
242 return channel_ptr->midi_cc_balance_index;
245 unsigned int channel_set_balance_midi_cc(jack_mixer_channel_t channel, unsigned int new_cc)
248 return 2; /* error: over limit CC */
250 if (channel_ptr->midi_cc_balance_index == new_cc) {
255 /* 0 is special, it removes the link */
256 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
257 channel_ptr->midi_cc_balance_index = 0;
259 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
260 return 1; /* error: cc in use */
262 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
263 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
264 channel_ptr->midi_cc_balance_index = new_cc;
269 unsigned int channel_get_volume_midi_cc(jack_mixer_channel_t channel)
271 return channel_ptr->midi_cc_volume_index;
274 unsigned int channel_set_volume_midi_cc(jack_mixer_channel_t channel, unsigned int new_cc)
277 return 2; /* error: over limit CC */
279 if (channel_ptr->midi_cc_volume_index == new_cc) {
284 /* 0 is special, it removes the link */
285 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
286 channel_ptr->midi_cc_volume_index = 0;
288 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
289 return 1; /* error: cc in use */
291 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
292 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
293 channel_ptr->midi_cc_volume_index = new_cc;
299 channel_autoset_midi_cc(jack_mixer_channel_t channel)
301 struct jack_mixer *mixer_ptr;
304 mixer_ptr = channel_ptr->mixer_ptr;
306 for (i = 11 ; i < 128 ; i++)
308 if (mixer_ptr->midi_cc_map[i] == NULL)
310 mixer_ptr->midi_cc_map[i] = channel_ptr;
311 channel_ptr->midi_cc_volume_index = i;
313 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
319 for (; i < 128 ; i++)
321 if (mixer_ptr->midi_cc_map[i] == NULL)
323 mixer_ptr->midi_cc_map[i] = channel_ptr;
324 channel_ptr->midi_cc_balance_index = i;
326 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
334 void remove_channel(jack_mixer_channel_t channel)
336 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
337 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
338 free(channel_ptr->name);
340 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
341 if (channel_ptr->stereo)
343 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
346 if (channel_ptr->midi_cc_volume_index != 0)
348 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
349 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
352 if (channel_ptr->midi_cc_balance_index != 0)
354 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
355 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
357 remove_output_channel(channel_ptr->output);
362 void channel_stereo_meter_read(jack_mixer_channel_t channel, double * left_ptr, double * right_ptr)
365 *left_ptr = value_to_db(channel_ptr->meter_left);
366 *right_ptr = value_to_db(channel_ptr->meter_right);
369 void channel_mono_meter_read(jack_mixer_channel_t channel, double * mono_ptr)
371 *mono_ptr = value_to_db(channel_ptr->meter_left);
374 void channel_volume_write(jack_mixer_channel_t channel, double volume)
377 channel_ptr->volume = db_to_value(volume);
378 calc_channel_volumes(channel_ptr);
383 jack_mixer_channel_t channel)
386 return value_to_db(channel_ptr->volume);
389 void channel_balance_write(jack_mixer_channel_t channel, double balance)
392 channel_ptr->balance = balance;
393 calc_channel_volumes(channel_ptr);
397 channel_balance_read(
398 jack_mixer_channel_t channel)
401 return channel_ptr->balance;
404 double channel_abspeak_read(jack_mixer_channel_t channel)
407 if (channel_ptr->NaN_detected)
413 return value_to_db(channel_ptr->abspeak);
417 void channel_abspeak_reset(jack_mixer_channel_t channel)
419 channel_ptr->abspeak = 0;
420 channel_ptr->NaN_detected = false;
423 void channel_mute(jack_mixer_channel_t channel)
425 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
428 void channel_unmute(jack_mixer_channel_t channel)
430 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
433 void channel_solo(jack_mixer_channel_t channel)
435 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
438 void channel_unsolo(jack_mixer_channel_t channel)
440 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
443 bool channel_is_muted(jack_mixer_channel_t channel)
445 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
450 bool channel_is_soloed(jack_mixer_channel_t channel)
452 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
458 channel_set_midi_scale(
459 jack_mixer_channel_t channel,
460 jack_mixer_scale_t scale)
462 channel_ptr->midi_scale = scale;
466 channel_set_midi_change_callback(
467 jack_mixer_channel_t channel,
468 void (*midi_change_callback) (void*),
471 channel_ptr->midi_change_callback = midi_change_callback;
472 channel_ptr->midi_change_callback_data = user_data;
477 /* process input channels and mix them into main mix */
482 struct output_channel *output_mix_channel,
483 GSList *channels_list,
484 jack_nframes_t start, /* index of first sample to process */
485 jack_nframes_t end) /* index of sample to stop processing before */
489 struct channel * channel_ptr;
490 jack_default_audio_sample_t frame_left;
491 jack_default_audio_sample_t frame_right;
492 struct channel *mix_channel = (struct channel*)output_mix_channel;
494 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
496 channel_ptr = node_ptr->data;
498 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
499 /* skip muted channels */
503 if (output_mix_channel->soloed_channels &&
504 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
505 /* skip channels that are not soloed, when some are */
509 for (i = start ; i < end ; i++)
511 if (! output_mix_channel->prefader) {
512 frame_left = channel_ptr->frames_left[i-start];
514 frame_left = channel_ptr->prefader_frames_left[i-start];
516 if (frame_left == NAN)
518 mix_channel->left_buffer_ptr[i] += frame_left;
520 if (mix_channel->stereo)
522 if (! output_mix_channel->prefader) {
523 frame_right = channel_ptr->frames_right[i-start];
525 frame_right = channel_ptr->prefader_frames_right[i-start];
527 if (frame_right == NAN)
530 mix_channel->right_buffer_ptr[i] += frame_right;
537 /* process main mix channel */
538 for (i = start ; i < end ; i++)
540 if (! output_mix_channel->prefader) {
541 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
542 if (mix_channel->stereo)
544 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
548 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
549 if (mix_channel->peak_left < frame_left)
551 mix_channel->peak_left = frame_left;
553 if (frame_left > mix_channel->abspeak)
555 mix_channel->abspeak = frame_left;
559 if (mix_channel->stereo)
561 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
562 if (mix_channel->peak_right < frame_right)
564 mix_channel->peak_right = frame_right;
566 if (frame_right > mix_channel->abspeak)
568 mix_channel->abspeak = frame_right;
573 mix_channel->peak_frames++;
574 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
576 mix_channel->meter_left = mix_channel->peak_left;
577 mix_channel->peak_left = 0.0;
579 if (mix_channel->stereo)
581 mix_channel->meter_right = mix_channel->peak_right;
582 mix_channel->peak_right = 0.0;
585 mix_channel->peak_frames = 0;
594 struct channel *channel_ptr,
595 jack_nframes_t start,
599 jack_default_audio_sample_t frame_left;
600 jack_default_audio_sample_t frame_right;
602 channel_ptr->frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
603 channel_ptr->frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
605 channel_ptr->prefader_frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
606 channel_ptr->prefader_frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
608 for (i = start ; i < end ; i++)
610 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
611 if (channel_ptr->stereo)
612 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
614 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
616 channel_ptr->NaN_detected = true;
617 channel_ptr->frames_left[i-start] = NAN;
621 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
623 if (channel_ptr->stereo)
625 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
627 channel_ptr->NaN_detected = true;
628 channel_ptr->frames_right[i-start] = NAN;
632 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
636 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
639 channel_ptr->frames_left[i-start] = frame_left;
640 channel_ptr->frames_right[i-start] = frame_right;
642 if (channel_ptr->stereo)
644 frame_left = fabsf(frame_left);
645 frame_right = fabsf(frame_right);
647 if (channel_ptr->peak_left < frame_left)
649 channel_ptr->peak_left = frame_left;
651 if (frame_left > channel_ptr->abspeak)
653 channel_ptr->abspeak = frame_left;
657 if (channel_ptr->peak_right < frame_right)
659 channel_ptr->peak_right = frame_right;
661 if (frame_right > channel_ptr->abspeak)
663 channel_ptr->abspeak = frame_right;
669 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
671 if (channel_ptr->peak_left < frame_left)
673 channel_ptr->peak_left = frame_left;
675 if (frame_left > channel_ptr->abspeak)
677 channel_ptr->abspeak = frame_left;
682 channel_ptr->peak_frames++;
683 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
685 channel_ptr->meter_left = channel_ptr->peak_left;
686 channel_ptr->peak_left = 0.0;
688 if (channel_ptr->stereo)
690 channel_ptr->meter_right = channel_ptr->peak_right;
691 channel_ptr->peak_right = 0.0;
694 channel_ptr->peak_frames = 0;
704 struct jack_mixer * mixer_ptr,
705 jack_nframes_t start, /* index of first sample to process */
706 jack_nframes_t end) /* index of sample to stop processing before */
709 struct output_channel * output_channel_ptr;
710 struct channel *channel_ptr;
712 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
714 channel_ptr = (struct channel*)node_ptr->data;
715 calc_channel_frames(channel_ptr, start, end);
718 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
720 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
722 output_channel_ptr = node_ptr->data;
723 channel_ptr = (struct channel*)output_channel_ptr;
725 if (output_channel_ptr->system)
727 /* Don't bother mixing the channels if we are not connected */
728 if (channel_ptr->stereo)
730 if (jack_port_connected(channel_ptr->port_left) == 0 &&
731 jack_port_connected(channel_ptr->port_right) == 0)
734 if (jack_port_connected(channel_ptr->port_left) == 0)
739 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
742 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
744 channel_ptr = (struct channel*)node_ptr->data;
745 free(channel_ptr->frames_left);
746 free(channel_ptr->frames_right);
747 free(channel_ptr->prefader_frames_left);
748 free(channel_ptr->prefader_frames_right);
755 update_channel_buffers(
756 struct channel * channel_ptr,
757 jack_nframes_t nframes)
759 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
761 if (channel_ptr->stereo)
763 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
767 #define mixer_ptr ((struct jack_mixer *)context)
770 process(jack_nframes_t nframes, void * context)
774 struct channel * channel_ptr;
775 #if defined(HAVE_JACK_MIDI)
776 jack_nframes_t event_count;
777 jack_midi_event_t in_event;
781 jack_nframes_t offset;
783 update_channel_buffers((struct channel*)mixer_ptr->main_mix_channel, nframes);
785 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
787 channel_ptr = node_ptr->data;
789 update_channel_buffers(channel_ptr, nframes);
792 for (i = 0 ; i < nframes ; i++)
794 ((struct channel*)(mixer_ptr->main_mix_channel))->left_buffer_ptr[i] = 0.0;
795 ((struct channel*)(mixer_ptr->main_mix_channel))->right_buffer_ptr[i] = 0.0;
798 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
800 channel_ptr = node_ptr->data;
801 update_channel_buffers(channel_ptr, nframes);
802 for (i = 0 ; i < nframes ; i++)
804 channel_ptr->left_buffer_ptr[i] = 0.0;
805 if (channel_ptr->stereo) {
806 channel_ptr->right_buffer_ptr[i] = 0.0;
814 #if defined(HAVE_JACK_MIDI)
815 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
816 event_count = jack_midi_get_event_count(midi_buffer);
818 for (i = 0 ; i < event_count; i++)
820 jack_midi_event_get(&in_event, midi_buffer, i);
822 if (in_event.size != 3 ||
823 (in_event.buffer[0] & 0xF0) != 0xB0 ||
824 in_event.buffer[1] > 127 ||
825 in_event.buffer[2] > 127)
830 assert(in_event.time < nframes);
834 (unsigned int)(in_event.buffer[0] & 0x0F),
835 (unsigned int)in_event.buffer[1],
836 (unsigned int)in_event.buffer[2]);
838 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
839 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
841 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
842 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
844 assert(in_event.time >= offset);
846 if (in_event.time > offset)
848 mix(mixer_ptr, offset, in_event.time);
849 offset = in_event.time;
852 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
854 byte = in_event.buffer[2];
861 channel_ptr->balance = (float)byte / 63;
862 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
866 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
867 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
870 calc_channel_volumes(channel_ptr);
872 if (channel_ptr->midi_change_callback)
873 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
881 mix(mixer_ptr, offset, nframes);
890 const char * jack_client_name_ptr)
893 struct jack_mixer * mixer_ptr;
897 mixer_ptr = malloc(sizeof(struct jack_mixer));
898 if (mixer_ptr == NULL)
903 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
909 mixer_ptr->input_channels_list = NULL;
910 mixer_ptr->output_channels_list = NULL;
912 mixer_ptr->last_midi_channel = 0;
914 for (i = 0 ; i < 128 ; i++)
916 mixer_ptr->midi_cc_map[i] = NULL;
919 LOG_DEBUG("Initializing JACK");
920 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
921 if (mixer_ptr->jack_client == NULL)
923 LOG_ERROR("Cannot create JACK client.");
924 LOG_NOTICE("Please make sure JACK daemon is running.");
925 goto exit_destroy_mutex;
928 LOG_DEBUG("JACK client created");
930 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
932 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
933 if (mixer_ptr->main_mix_channel == NULL) {
934 LOG_ERROR("Cannot create main mix channel");
937 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
938 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
940 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
942 #if defined(HAVE_JACK_MIDI)
943 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
944 if (mixer_ptr->port_midi_in == NULL)
946 LOG_ERROR("Cannot create JACK port");
951 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
953 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
956 LOG_ERROR("Cannot set JACK process callback");
960 ret = jack_activate(mixer_ptr->jack_client);
963 LOG_ERROR("Cannot activate JACK client");
970 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
973 pthread_mutex_destroy(&mixer_ptr->mutex);
982 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
988 LOG_DEBUG("Uninitializing JACK");
990 assert(mixer_ctx_ptr->jack_client != NULL);
992 jack_client_close(mixer_ctx_ptr->jack_client);
994 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
996 free(mixer_ctx_ptr->main_mix_channel);
1000 jack_mixer_channel_t
1001 get_main_mix_channel(
1004 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1011 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1015 get_last_midi_channel(
1018 return mixer_ctx_ptr->last_midi_channel;
1021 jack_mixer_channel_t
1024 const char * channel_name,
1027 struct channel * channel_ptr;
1029 size_t channel_name_size;
1030 char * output_channel_name;
1032 channel_ptr = malloc(sizeof(struct channel));
1033 if (channel_ptr == NULL)
1038 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1040 channel_ptr->name = strdup(channel_name);
1041 if (channel_ptr->name == NULL)
1043 goto fail_free_channel;
1046 channel_name_size = strlen(channel_name);
1050 port_name = malloc(channel_name_size + 3);
1051 if (port_name == NULL)
1053 goto fail_free_channel_name;
1056 memcpy(port_name, channel_name, channel_name_size);
1057 port_name[channel_name_size] = ' ';
1058 port_name[channel_name_size+1] = 'L';
1059 port_name[channel_name_size+2] = 0;
1061 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1062 if (channel_ptr->port_left == NULL)
1064 goto fail_free_port_name;
1067 port_name[channel_name_size+1] = 'R';
1069 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1070 if (channel_ptr->port_right == NULL)
1072 goto fail_unregister_left_channel;
1077 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1078 if (channel_ptr->port_left == NULL)
1080 goto fail_free_channel_name;
1084 channel_ptr->stereo = stereo;
1086 channel_ptr->volume = 0.0;
1087 channel_ptr->balance = 0.0;
1088 channel_ptr->meter_left = -1.0;
1089 channel_ptr->meter_right = -1.0;
1090 channel_ptr->abspeak = 0.0;
1092 channel_ptr->peak_left = 0.0;
1093 channel_ptr->peak_right = 0.0;
1094 channel_ptr->peak_frames = 0;
1096 channel_ptr->NaN_detected = false;
1098 channel_ptr->midi_cc_volume_index = 0;
1099 channel_ptr->midi_cc_balance_index = 0;
1100 channel_ptr->midi_change_callback = NULL;
1101 channel_ptr->midi_change_callback_data = NULL;
1103 channel_ptr->midi_scale = NULL;
1105 calc_channel_volumes(channel_ptr);
1107 // for monitoring etc.
1108 output_channel_name = malloc(channel_name_size + 5);
1109 sprintf(output_channel_name, "%s Out", channel_name);
1110 channel_ptr->output = add_output_channel(mixer, output_channel_name, stereo, true);
1111 free(output_channel_name);
1113 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1114 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1118 fail_unregister_left_channel:
1119 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1121 fail_free_port_name:
1124 fail_free_channel_name:
1125 free(channel_ptr->name);
1135 static jack_mixer_output_channel_t
1136 create_output_channel(
1138 const char * channel_name,
1142 struct channel * channel_ptr;
1143 struct output_channel * output_channel_ptr;
1145 size_t channel_name_size;
1147 output_channel_ptr = malloc(sizeof(struct output_channel));
1148 channel_ptr = (struct channel*)output_channel_ptr;
1149 if (channel_ptr == NULL)
1154 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1156 channel_ptr->name = strdup(channel_name);
1157 if (channel_ptr->name == NULL)
1159 goto fail_free_channel;
1164 channel_name_size = strlen(channel_name);
1166 port_name = malloc(channel_name_size + 4);
1167 if (port_name == NULL)
1169 goto fail_free_channel_name;
1172 memcpy(port_name, channel_name, channel_name_size);
1173 port_name[channel_name_size] = ' ';
1174 port_name[channel_name_size+1] = 'L';
1175 port_name[channel_name_size+2] = 0;
1177 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1178 if (channel_ptr->port_left == NULL)
1180 goto fail_free_port_name;
1183 port_name[channel_name_size+1] = 'R';
1185 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1186 if (channel_ptr->port_right == NULL)
1188 goto fail_unregister_left_channel;
1193 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1194 if (channel_ptr->port_left == NULL)
1196 goto fail_free_channel_name;
1200 channel_ptr->stereo = stereo;
1202 channel_ptr->volume = 0.0;
1203 channel_ptr->balance = 0.0;
1204 channel_ptr->meter_left = -1.0;
1205 channel_ptr->meter_right = -1.0;
1206 channel_ptr->abspeak = 0.0;
1208 channel_ptr->peak_left = 0.0;
1209 channel_ptr->peak_right = 0.0;
1210 channel_ptr->peak_frames = 0;
1212 channel_ptr->NaN_detected = false;
1214 channel_ptr->midi_cc_volume_index = 0;
1215 channel_ptr->midi_cc_balance_index = 0;
1216 channel_ptr->midi_change_callback = NULL;
1217 channel_ptr->midi_change_callback_data = NULL;
1219 channel_ptr->midi_scale = NULL;
1221 output_channel_ptr->soloed_channels = NULL;
1222 output_channel_ptr->muted_channels = NULL;
1223 output_channel_ptr->system = system;
1224 output_channel_ptr->prefader = false;
1226 return output_channel_ptr;
1228 fail_unregister_left_channel:
1229 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1231 fail_free_port_name:
1234 fail_free_channel_name:
1235 free(channel_ptr->name);
1245 jack_mixer_output_channel_t
1248 const char * channel_name,
1252 struct output_channel *output_channel_ptr;
1253 struct channel *channel_ptr;
1255 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1256 if (output_channel_ptr == NULL) {
1259 channel_ptr = (struct channel*)output_channel_ptr;
1261 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1262 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1264 return output_channel_ptr;
1268 remove_output_channel(jack_mixer_output_channel_t output_channel)
1270 struct output_channel *output_channel_ptr = output_channel;
1271 struct channel *channel_ptr = output_channel;
1273 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1274 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1275 free(channel_ptr->name);
1277 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1278 if (channel_ptr->stereo)
1280 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1283 if (channel_ptr->midi_cc_volume_index != 0)
1285 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1286 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1289 if (channel_ptr->midi_cc_balance_index != 0)
1291 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1292 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1295 g_slist_free(output_channel_ptr->soloed_channels);
1296 g_slist_free(output_channel_ptr->muted_channels);
1303 output_channel_set_solo(
1304 jack_mixer_output_channel_t output_channel,
1305 jack_mixer_channel_t channel,
1308 struct output_channel *output_channel_ptr = output_channel;
1311 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1313 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1315 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1317 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1322 output_channel_set_muted(
1323 jack_mixer_output_channel_t output_channel,
1324 jack_mixer_channel_t channel,
1327 struct output_channel *output_channel_ptr = output_channel;
1330 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1332 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1334 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1336 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1341 output_channel_is_muted(
1342 jack_mixer_output_channel_t output_channel,
1343 jack_mixer_channel_t channel)
1345 struct output_channel *output_channel_ptr = output_channel;
1347 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1353 output_channel_is_solo(
1354 jack_mixer_output_channel_t output_channel,
1355 jack_mixer_channel_t channel)
1357 struct output_channel *output_channel_ptr = output_channel;
1359 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1365 output_channel_set_prefader(
1366 jack_mixer_output_channel_t output_channel,
1369 struct output_channel *output_channel_ptr = output_channel;
1370 output_channel_ptr->prefader = pfl_value;
1374 output_channel_is_prefader(
1375 jack_mixer_output_channel_t output_channel)
1377 struct output_channel *output_channel_ptr = output_channel;
1378 return output_channel_ptr->prefader;