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 bool midi_in_got_events;
89 void (*midi_change_callback) (void*);
90 void *midi_change_callback_data;
91 bool midi_out_has_events;
93 jack_mixer_scale_t midi_scale;
96 struct output_channel {
97 struct channel channel;
98 GSList *soloed_channels;
99 GSList *muted_channels;
100 bool system; /* system channel, without any associated UI */
106 pthread_mutex_t mutex;
107 jack_client_t * jack_client;
108 GSList *input_channels_list;
109 GSList *output_channels_list;
110 struct output_channel *main_mix_channel;
112 jack_port_t * port_midi_in;
113 jack_port_t * port_midi_out;
114 unsigned int last_midi_channel;
116 struct channel* midi_cc_map[128];
119 static jack_mixer_output_channel_t create_output_channel(
121 const char * channel_name,
126 update_channel_buffers(
127 struct channel * channel_ptr,
128 jack_nframes_t nframes);
140 return 20.0 * log10f(value);
147 return powf(10.0, db/20.0);
151 calc_channel_volumes(
152 struct channel * channel_ptr)
154 if (channel_ptr->stereo)
156 if (channel_ptr->balance > 0)
158 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
159 channel_ptr->volume_right = channel_ptr->volume;
163 channel_ptr->volume_left = channel_ptr->volume;
164 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
169 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
170 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
175 calc_all_channel_volumes(
176 struct jack_mixer * mixer_ptr)
178 struct channel * channel_ptr;
181 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
183 channel_ptr = list_ptr->data;
184 calc_channel_volumes(channel_ptr);
188 #define channel_ptr ((struct channel *)channel)
192 jack_mixer_channel_t channel)
194 return channel_ptr->name;
199 jack_mixer_channel_t channel,
203 size_t channel_name_size;
207 new_name = strdup(name);
208 if (new_name == NULL)
213 if (channel_ptr->name)
215 free(channel_ptr->name);
218 channel_ptr->name = new_name;
220 if (channel_ptr->stereo)
222 channel_name_size = strlen(name);
223 port_name = malloc(channel_name_size + 3);
224 memcpy(port_name, name, channel_name_size);
226 port_name[channel_name_size] = ' ';
227 port_name[channel_name_size+1] = 'L';
228 port_name[channel_name_size+2] = 0;
230 ret = jack_port_set_name(channel_ptr->port_left, port_name);
233 /* what could we do here? */
236 port_name[channel_name_size+1] = 'R';
238 ret = jack_port_set_name(channel_ptr->port_right, port_name);
241 /* what could we do here? */
248 ret = jack_port_set_name(channel_ptr->port_left, name);
251 /* what could we do here? */
258 jack_mixer_channel_t channel)
260 return channel_ptr->stereo;
264 channel_get_balance_midi_cc(
265 jack_mixer_channel_t channel)
267 return channel_ptr->midi_cc_balance_index;
271 channel_set_balance_midi_cc(
272 jack_mixer_channel_t channel,
276 return 2; /* error: over limit CC */
278 if (channel_ptr->midi_cc_balance_index == new_cc) {
283 /* 0 is special, it removes the link */
284 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
285 channel_ptr->midi_cc_balance_index = 0;
287 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
288 return 1; /* error: cc in use */
290 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
291 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
292 channel_ptr->midi_cc_balance_index = new_cc;
298 channel_get_volume_midi_cc(
299 jack_mixer_channel_t channel)
301 return channel_ptr->midi_cc_volume_index;
305 channel_set_volume_midi_cc(
306 jack_mixer_channel_t channel, unsigned int new_cc)
309 return 2; /* error: over limit CC */
311 if (channel_ptr->midi_cc_volume_index == new_cc) {
316 /* 0 is special, it removes the link */
317 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
318 channel_ptr->midi_cc_volume_index = 0;
320 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
321 return 1; /* error: cc in use */
323 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
324 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
325 channel_ptr->midi_cc_volume_index = new_cc;
331 channel_autoset_midi_cc(
332 jack_mixer_channel_t channel)
334 struct jack_mixer *mixer_ptr;
337 mixer_ptr = channel_ptr->mixer_ptr;
339 for (i = 11 ; 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_volume_index = i;
346 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
352 for (; i < 128 ; i++)
354 if (mixer_ptr->midi_cc_map[i] == NULL)
356 mixer_ptr->midi_cc_map[i] = channel_ptr;
357 channel_ptr->midi_cc_balance_index = i;
359 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
368 jack_mixer_channel_t channel)
372 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
373 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
374 free(channel_ptr->name);
376 /* remove references to input channel from all output channels */
377 channel_unmute(channel_ptr);
378 channel_unsolo(channel_ptr);
379 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
381 struct output_channel *output_channel_ptr = list_ptr->data;
382 output_channel_set_solo(output_channel_ptr, channel, false);
383 output_channel_set_muted(output_channel_ptr, channel, false);
386 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
387 if (channel_ptr->stereo)
389 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
392 if (channel_ptr->midi_cc_volume_index != 0)
394 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
395 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
398 if (channel_ptr->midi_cc_balance_index != 0)
400 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
401 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
408 channel_stereo_meter_read(
409 jack_mixer_channel_t channel,
414 *left_ptr = value_to_db(channel_ptr->meter_left);
415 *right_ptr = value_to_db(channel_ptr->meter_right);
419 channel_mono_meter_read(
420 jack_mixer_channel_t channel,
423 *mono_ptr = value_to_db(channel_ptr->meter_left);
427 channel_volume_write(
428 jack_mixer_channel_t channel,
432 channel_ptr->volume = db_to_value(volume);
433 channel_ptr->midi_out_has_events = true;
434 calc_channel_volumes(channel_ptr);
439 jack_mixer_channel_t channel)
442 return value_to_db(channel_ptr->volume);
446 channel_balance_write(
447 jack_mixer_channel_t channel,
451 channel_ptr->balance = balance;
452 calc_channel_volumes(channel_ptr);
456 channel_balance_read(
457 jack_mixer_channel_t channel)
460 return channel_ptr->balance;
464 channel_abspeak_read(
465 jack_mixer_channel_t channel)
468 if (channel_ptr->NaN_detected)
474 return value_to_db(channel_ptr->abspeak);
479 channel_abspeak_reset(
480 jack_mixer_channel_t channel)
482 channel_ptr->abspeak = 0;
483 channel_ptr->NaN_detected = false;
488 jack_mixer_channel_t channel)
490 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
495 jack_mixer_channel_t channel)
497 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
502 jack_mixer_channel_t channel)
504 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
509 jack_mixer_channel_t channel)
511 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
516 jack_mixer_channel_t channel)
518 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
525 jack_mixer_channel_t channel)
527 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
533 channel_set_midi_scale(
534 jack_mixer_channel_t channel,
535 jack_mixer_scale_t scale)
537 channel_ptr->midi_scale = scale;
541 channel_set_midi_change_callback(
542 jack_mixer_channel_t channel,
543 void (*midi_change_callback) (void*),
546 channel_ptr->midi_change_callback = midi_change_callback;
547 channel_ptr->midi_change_callback_data = user_data;
551 channel_get_midi_in_got_events(
552 jack_mixer_channel_t channel)
554 bool t = channel_ptr->midi_in_got_events;
555 channel_ptr->midi_in_got_events = false;
561 /* process input channels and mix them into main mix */
564 struct output_channel *output_mix_channel,
565 GSList *channels_list,
566 jack_nframes_t start, /* index of first sample to process */
567 jack_nframes_t end) /* index of sample to stop processing before */
571 struct channel * channel_ptr;
572 jack_default_audio_sample_t frame_left;
573 jack_default_audio_sample_t frame_right;
574 struct channel *mix_channel = (struct channel*)output_mix_channel;
576 for (i = start; i < end; i++)
578 mix_channel->left_buffer_ptr[i] = 0.0;
579 if (mix_channel->stereo)
580 mix_channel->right_buffer_ptr[i] = 0.0;
584 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
586 channel_ptr = node_ptr->data;
588 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
589 /* skip muted channels */
593 if (output_mix_channel->soloed_channels &&
594 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
595 /* skip channels that are not soloed, when some are */
599 for (i = start ; i < end ; i++)
601 if (! output_mix_channel->prefader) {
602 frame_left = channel_ptr->frames_left[i-start];
604 frame_left = channel_ptr->prefader_frames_left[i-start];
606 if (frame_left == NAN)
608 mix_channel->left_buffer_ptr[i] += frame_left;
610 if (mix_channel->stereo)
612 if (! output_mix_channel->prefader) {
613 frame_right = channel_ptr->frames_right[i-start];
615 frame_right = channel_ptr->prefader_frames_right[i-start];
617 if (frame_right == NAN)
620 mix_channel->right_buffer_ptr[i] += frame_right;
627 /* process main mix channel */
628 for (i = start ; i < end ; i++)
630 if (! output_mix_channel->prefader) {
631 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
632 if (mix_channel->stereo)
634 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
638 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
639 if (mix_channel->peak_left < frame_left)
641 mix_channel->peak_left = frame_left;
643 if (frame_left > mix_channel->abspeak)
645 mix_channel->abspeak = frame_left;
649 if (mix_channel->stereo)
651 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
652 if (mix_channel->peak_right < frame_right)
654 mix_channel->peak_right = frame_right;
656 if (frame_right > mix_channel->abspeak)
658 mix_channel->abspeak = frame_right;
663 mix_channel->peak_frames++;
664 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
666 mix_channel->meter_left = mix_channel->peak_left;
667 mix_channel->peak_left = 0.0;
669 if (mix_channel->stereo)
671 mix_channel->meter_right = mix_channel->peak_right;
672 mix_channel->peak_right = 0.0;
675 mix_channel->peak_frames = 0;
682 struct channel *channel_ptr,
683 jack_nframes_t start,
687 jack_default_audio_sample_t frame_left;
688 jack_default_audio_sample_t frame_right;
690 for (i = start ; i < end ; i++)
692 if (i-start >= MAX_BLOCK_SIZE)
694 fprintf(stderr, "i-start too high: %d - %d\n", i, start);
696 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
697 if (channel_ptr->stereo)
698 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
700 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
702 channel_ptr->NaN_detected = true;
703 channel_ptr->frames_left[i-start] = NAN;
707 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
709 if (channel_ptr->stereo)
711 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
713 channel_ptr->NaN_detected = true;
714 channel_ptr->frames_right[i-start] = NAN;
718 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
722 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
725 channel_ptr->frames_left[i-start] = frame_left;
726 channel_ptr->frames_right[i-start] = frame_right;
728 if (channel_ptr->stereo)
730 frame_left = fabsf(frame_left);
731 frame_right = fabsf(frame_right);
733 if (channel_ptr->peak_left < frame_left)
735 channel_ptr->peak_left = frame_left;
737 if (frame_left > channel_ptr->abspeak)
739 channel_ptr->abspeak = frame_left;
743 if (channel_ptr->peak_right < frame_right)
745 channel_ptr->peak_right = frame_right;
747 if (frame_right > channel_ptr->abspeak)
749 channel_ptr->abspeak = frame_right;
755 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
757 if (channel_ptr->peak_left < frame_left)
759 channel_ptr->peak_left = frame_left;
761 if (frame_left > channel_ptr->abspeak)
763 channel_ptr->abspeak = frame_left;
768 channel_ptr->peak_frames++;
769 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
771 channel_ptr->meter_left = channel_ptr->peak_left;
772 channel_ptr->peak_left = 0.0;
774 if (channel_ptr->stereo)
776 channel_ptr->meter_right = channel_ptr->peak_right;
777 channel_ptr->peak_right = 0.0;
780 channel_ptr->peak_frames = 0;
788 struct jack_mixer * mixer_ptr,
789 jack_nframes_t start, /* index of first sample to process */
790 jack_nframes_t end) /* index of sample to stop processing before */
793 struct output_channel * output_channel_ptr;
794 struct channel *channel_ptr;
796 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
798 channel_ptr = (struct channel*)node_ptr->data;
799 calc_channel_frames(channel_ptr, start, end);
802 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
804 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
806 output_channel_ptr = node_ptr->data;
807 channel_ptr = (struct channel*)output_channel_ptr;
809 if (output_channel_ptr->system)
811 /* Don't bother mixing the channels if we are not connected */
812 if (channel_ptr->stereo)
814 if (jack_port_connected(channel_ptr->port_left) == 0 &&
815 jack_port_connected(channel_ptr->port_right) == 0)
818 if (jack_port_connected(channel_ptr->port_left) == 0)
823 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
828 update_channel_buffers(
829 struct channel * channel_ptr,
830 jack_nframes_t nframes)
832 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
834 if (channel_ptr->stereo)
836 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
840 #define mixer_ptr ((struct jack_mixer *)context)
844 jack_nframes_t nframes,
849 struct channel * channel_ptr;
850 #if defined(HAVE_JACK_MIDI)
851 jack_nframes_t event_count;
852 jack_midi_event_t in_event;
853 unsigned char* midi_out_buffer;
856 unsigned int cc_channel_index;
858 jack_nframes_t offset;
860 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
862 channel_ptr = node_ptr->data;
863 update_channel_buffers(channel_ptr, nframes);
866 // Fill output buffers with the input
867 update_channel_buffers((struct channel*)mixer_ptr->main_mix_channel, nframes);
868 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
870 channel_ptr = node_ptr->data;
871 update_channel_buffers(channel_ptr, nframes);
876 #if defined(HAVE_JACK_MIDI)
877 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
878 event_count = jack_midi_get_event_count(midi_buffer);
880 for (i = 0 ; i < event_count; i++)
882 jack_midi_event_get(&in_event, midi_buffer, i);
884 if (in_event.size != 3 ||
885 (in_event.buffer[0] & 0xF0) != 0xB0 ||
886 in_event.buffer[1] > 127 ||
887 in_event.buffer[2] > 127)
892 assert(in_event.time < nframes);
896 (unsigned int)(in_event.buffer[0]),
897 (unsigned int)in_event.buffer[1],
898 (unsigned int)in_event.buffer[2]);
900 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
901 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
903 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
904 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
906 assert(in_event.time >= offset);
908 if (in_event.time > offset)
910 // Perform the mixing of the part between the previous volume change
911 // (or the start of the block) up until this one.
912 mix(mixer_ptr, offset, in_event.time);
913 offset = in_event.time;
916 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
918 byte = in_event.buffer[2];
925 channel_ptr->balance = (float)byte / 63;
926 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
930 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
931 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
934 calc_channel_volumes(channel_ptr);
936 channel_ptr->midi_in_got_events = true;
937 if (channel_ptr->midi_change_callback)
938 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
944 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_out, nframes);
945 jack_midi_clear_buffer(midi_buffer);
947 for(i=0; i<nframes; i++)
949 for (cc_channel_index=0; cc_channel_index<128; cc_channel_index++)
951 channel_ptr = mixer_ptr->midi_cc_map[cc_channel_index];
952 if (channel_ptr == NULL || channel_ptr->midi_scale == NULL)
956 if (channel_ptr->midi_out_has_events == false)
960 if (channel_ptr->midi_cc_balance_index == (unsigned int)cc_channel_index)
964 midi_out_buffer = jack_midi_event_reserve(midi_buffer, i, 3);
965 if (midi_out_buffer == NULL)
969 midi_out_buffer[0] = 0xB0; /* control change */
970 midi_out_buffer[1] = cc_channel_index;
971 midi_out_buffer[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr->midi_scale, value_to_db(channel_ptr->volume)));
975 (unsigned int)(midi_out_buffer[0]),
976 (unsigned int)midi_out_buffer[1],
977 (unsigned int)midi_out_buffer[2]);
979 channel_ptr->midi_out_has_events = false;
985 mix(mixer_ptr, offset, nframes);
994 const char * jack_client_name_ptr,
998 struct jack_mixer * mixer_ptr;
1002 mixer_ptr = malloc(sizeof(struct jack_mixer));
1003 if (mixer_ptr == NULL)
1008 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
1014 mixer_ptr->input_channels_list = NULL;
1015 mixer_ptr->output_channels_list = NULL;
1017 mixer_ptr->last_midi_channel = 0;
1019 for (i = 0 ; i < 128 ; i++)
1021 mixer_ptr->midi_cc_map[i] = NULL;
1024 LOG_DEBUG("Initializing JACK");
1025 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
1026 if (mixer_ptr->jack_client == NULL)
1028 LOG_ERROR("Cannot create JACK client.");
1029 LOG_NOTICE("Please make sure JACK daemon is running.");
1030 goto exit_destroy_mutex;
1033 LOG_DEBUG("JACK client created");
1035 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
1037 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", stereo, false);
1038 if (mixer_ptr->main_mix_channel == NULL) {
1039 LOG_ERROR("Cannot create main mix channel");
1042 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
1043 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
1045 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
1047 #if defined(HAVE_JACK_MIDI)
1048 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1049 if (mixer_ptr->port_midi_in == NULL)
1051 LOG_ERROR("Cannot create JACK MIDI in port");
1055 mixer_ptr->port_midi_out = jack_port_register(mixer_ptr->jack_client, "midi out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
1056 if (mixer_ptr->port_midi_out == NULL)
1058 LOG_ERROR("Cannot create JACK MIDI out port");
1064 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
1066 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1069 LOG_ERROR("Cannot set JACK process callback");
1073 ret = jack_activate(mixer_ptr->jack_client);
1076 LOG_ERROR("Cannot activate JACK client");
1083 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1086 pthread_mutex_destroy(&mixer_ptr->mutex);
1095 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1101 LOG_DEBUG("Uninitializing JACK");
1103 assert(mixer_ctx_ptr->jack_client != NULL);
1105 jack_client_close(mixer_ctx_ptr->jack_client);
1107 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1109 free(mixer_ctx_ptr->main_mix_channel);
1110 free(mixer_ctx_ptr);
1113 jack_mixer_channel_t
1114 get_main_mix_channel(
1117 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1124 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1128 get_last_midi_channel(
1131 return mixer_ctx_ptr->last_midi_channel;
1134 jack_mixer_channel_t
1137 const char * channel_name,
1140 struct channel * channel_ptr;
1142 size_t channel_name_size;
1144 channel_ptr = malloc(sizeof(struct channel));
1145 if (channel_ptr == NULL)
1150 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1152 channel_ptr->name = strdup(channel_name);
1153 if (channel_ptr->name == NULL)
1155 goto fail_free_channel;
1158 channel_name_size = strlen(channel_name);
1162 port_name = malloc(channel_name_size + 3);
1163 if (port_name == NULL)
1165 goto fail_free_channel_name;
1168 memcpy(port_name, channel_name, channel_name_size);
1169 port_name[channel_name_size] = ' ';
1170 port_name[channel_name_size+1] = 'L';
1171 port_name[channel_name_size+2] = 0;
1173 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1174 if (channel_ptr->port_left == NULL)
1176 goto fail_free_port_name;
1179 port_name[channel_name_size+1] = 'R';
1181 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1182 if (channel_ptr->port_right == NULL)
1184 goto fail_unregister_left_channel;
1189 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1190 if (channel_ptr->port_left == NULL)
1192 goto fail_free_channel_name;
1196 channel_ptr->stereo = stereo;
1198 channel_ptr->volume = 0.0;
1199 channel_ptr->balance = 0.0;
1200 channel_ptr->meter_left = -1.0;
1201 channel_ptr->meter_right = -1.0;
1202 channel_ptr->abspeak = 0.0;
1204 channel_ptr->peak_left = 0.0;
1205 channel_ptr->peak_right = 0.0;
1206 channel_ptr->peak_frames = 0;
1208 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1209 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1210 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1211 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1213 channel_ptr->NaN_detected = false;
1215 channel_ptr->midi_cc_volume_index = 0;
1216 channel_ptr->midi_cc_balance_index = 0;
1217 channel_ptr->midi_change_callback = NULL;
1218 channel_ptr->midi_change_callback_data = NULL;
1219 channel_ptr->midi_out_has_events = false;
1221 channel_ptr->midi_scale = NULL;
1223 calc_channel_volumes(channel_ptr);
1225 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1226 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1230 fail_unregister_left_channel:
1231 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1233 fail_free_port_name:
1236 fail_free_channel_name:
1237 free(channel_ptr->name);
1247 static jack_mixer_output_channel_t
1248 create_output_channel(
1250 const char * channel_name,
1254 struct channel * channel_ptr;
1255 struct output_channel * output_channel_ptr;
1257 size_t channel_name_size;
1259 output_channel_ptr = malloc(sizeof(struct output_channel));
1260 channel_ptr = (struct channel*)output_channel_ptr;
1261 if (channel_ptr == NULL)
1266 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1268 channel_ptr->name = strdup(channel_name);
1269 if (channel_ptr->name == NULL)
1271 goto fail_free_channel;
1276 channel_name_size = strlen(channel_name);
1278 port_name = malloc(channel_name_size + 4);
1279 if (port_name == NULL)
1281 goto fail_free_channel_name;
1284 memcpy(port_name, channel_name, channel_name_size);
1285 port_name[channel_name_size] = ' ';
1286 port_name[channel_name_size+1] = 'L';
1287 port_name[channel_name_size+2] = 0;
1289 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1290 if (channel_ptr->port_left == NULL)
1292 goto fail_free_port_name;
1295 port_name[channel_name_size+1] = 'R';
1297 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1298 if (channel_ptr->port_right == NULL)
1300 goto fail_unregister_left_channel;
1305 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1306 if (channel_ptr->port_left == NULL)
1308 goto fail_free_channel_name;
1312 channel_ptr->stereo = stereo;
1314 channel_ptr->volume = 0.0;
1315 channel_ptr->balance = 0.0;
1316 channel_ptr->meter_left = -1.0;
1317 channel_ptr->meter_right = -1.0;
1318 channel_ptr->abspeak = 0.0;
1320 channel_ptr->peak_left = 0.0;
1321 channel_ptr->peak_right = 0.0;
1322 channel_ptr->peak_frames = 0;
1324 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1325 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1326 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1327 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1329 channel_ptr->NaN_detected = false;
1331 channel_ptr->midi_cc_volume_index = 0;
1332 channel_ptr->midi_cc_balance_index = 0;
1333 channel_ptr->midi_change_callback = NULL;
1334 channel_ptr->midi_change_callback_data = NULL;
1336 channel_ptr->midi_scale = NULL;
1338 output_channel_ptr->soloed_channels = NULL;
1339 output_channel_ptr->muted_channels = NULL;
1340 output_channel_ptr->system = system;
1341 output_channel_ptr->prefader = false;
1343 return output_channel_ptr;
1345 fail_unregister_left_channel:
1346 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1348 fail_free_port_name:
1351 fail_free_channel_name:
1352 free(channel_ptr->name);
1362 jack_mixer_output_channel_t
1365 const char * channel_name,
1369 struct output_channel *output_channel_ptr;
1370 struct channel *channel_ptr;
1372 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1373 if (output_channel_ptr == NULL) {
1376 channel_ptr = (struct channel*)output_channel_ptr;
1378 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1379 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1381 return output_channel_ptr;
1385 remove_output_channel(
1386 jack_mixer_output_channel_t output_channel)
1388 struct output_channel *output_channel_ptr = output_channel;
1389 struct channel *channel_ptr = output_channel;
1391 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1392 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1393 free(channel_ptr->name);
1395 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1396 if (channel_ptr->stereo)
1398 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1401 if (channel_ptr->midi_cc_volume_index != 0)
1403 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1404 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1407 if (channel_ptr->midi_cc_balance_index != 0)
1409 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1410 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1413 g_slist_free(output_channel_ptr->soloed_channels);
1414 g_slist_free(output_channel_ptr->muted_channels);
1420 output_channel_set_solo(
1421 jack_mixer_output_channel_t output_channel,
1422 jack_mixer_channel_t channel,
1425 struct output_channel *output_channel_ptr = output_channel;
1428 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1430 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1432 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1434 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1439 output_channel_set_muted(
1440 jack_mixer_output_channel_t output_channel,
1441 jack_mixer_channel_t channel,
1444 struct output_channel *output_channel_ptr = output_channel;
1447 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1449 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1451 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1453 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1458 output_channel_is_muted(
1459 jack_mixer_output_channel_t output_channel,
1460 jack_mixer_channel_t channel)
1462 struct output_channel *output_channel_ptr = output_channel;
1464 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1470 output_channel_is_solo(
1471 jack_mixer_output_channel_t output_channel,
1472 jack_mixer_channel_t channel)
1474 struct output_channel *output_channel_ptr = output_channel;
1476 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1482 output_channel_set_prefader(
1483 jack_mixer_output_channel_t output_channel,
1486 struct output_channel *output_channel_ptr = output_channel;
1487 output_channel_ptr->prefader = pfl_value;
1491 output_channel_is_prefader(
1492 jack_mixer_output_channel_t output_channel)
1494 struct output_channel *output_channel_ptr = output_channel;
1495 return output_channel_ptr->prefader;