Maintain object property encapsulation
[jack_mixer.git] / jack_mixer.c
blobebff6da6fb6be28955339376fbd29bc73c47edfc
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 *****************************************************************************/
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <math.h>
32 #include <jack/jack.h>
33 #if defined(HAVE_JACK_MIDI)
34 #include <jack/midiport.h>
35 #endif
36 #include <assert.h>
37 #include <pthread.h>
39 #include <glib.h>
41 #include "jack_mixer.h"
42 //#define LOG_LEVEL LOG_LEVEL_DEBUG
43 #include "log.h"
45 #include "jack_compat.h"
47 struct kmeter {
48 float _z1; // filter state
49 float _z2; // filter state
50 float _rms; // max rms value since last read()
51 float _dpk; // current digital peak value
52 int _cnt; // digital peak hold counter
53 bool _flag; // flag set by read(), resets _rms
55 int _hold; // number of JACK periods to hold peak value
56 float _fall; // per period fallback multiplier for peak value
57 float _omega; // ballistics filter constant.
60 struct channel {
61 struct jack_mixer * mixer_ptr;
62 char * name;
63 bool stereo;
64 bool out_mute;
65 float volume_transition_seconds;
66 unsigned int num_volume_transition_steps;
67 float volume;
68 jack_nframes_t volume_idx;
69 float volume_new;
70 float balance;
71 jack_nframes_t balance_idx;
72 float balance_new;
73 float volume_left;
74 float volume_left_new;
75 float volume_right;
76 float volume_right_new;
77 float meter_left;
78 float meter_right;
79 float abspeak;
80 struct kmeter kmeter_left;
81 struct kmeter kmeter_right;
83 jack_port_t * port_left;
84 jack_port_t * port_right;
86 jack_nframes_t peak_frames;
87 float peak_left;
88 float peak_right;
90 jack_default_audio_sample_t * tmp_mixed_frames_left;
91 jack_default_audio_sample_t * tmp_mixed_frames_right;
92 jack_default_audio_sample_t * frames_left;
93 jack_default_audio_sample_t * frames_right;
94 jack_default_audio_sample_t * prefader_frames_left;
95 jack_default_audio_sample_t * prefader_frames_right;
97 jack_default_audio_sample_t * left_buffer_ptr;
98 jack_default_audio_sample_t * right_buffer_ptr;
100 bool NaN_detected;
102 int8_t midi_cc_volume_index;
103 int8_t midi_cc_balance_index;
104 int8_t midi_cc_mute_index;
105 int8_t midi_cc_solo_index;
106 bool midi_cc_volume_picked_up;
107 bool midi_cc_balance_picked_up;
109 bool midi_in_got_events;
110 int midi_out_has_events;
111 void (*midi_change_callback) (void*);
112 void *midi_change_callback_data;
114 jack_mixer_scale_t midi_scale;
117 struct output_channel {
118 struct channel channel;
119 GSList *soloed_channels;
120 GSList *muted_channels;
121 GSList *prefader_channels;
123 bool system; /* system channel, without any associated UI */
124 bool prefader;
127 struct jack_mixer {
128 pthread_mutex_t mutex;
129 jack_client_t * jack_client;
130 GSList *input_channels_list;
131 GSList *output_channels_list;
132 GSList *soloed_channels;
134 jack_port_t * port_midi_in;
135 jack_port_t * port_midi_out;
136 int8_t last_midi_cc;
137 enum midi_behavior_mode midi_behavior;
139 struct channel* midi_cc_map[128];
142 static jack_mixer_output_channel_t
143 create_output_channel(
144 jack_mixer_t mixer,
145 const char * channel_name,
146 bool stereo,
147 bool system);
149 static inline void
150 update_channel_buffers(
151 struct channel * channel_ptr,
152 jack_nframes_t nframes);
154 static void
155 unset_midi_cc_mapping(
156 struct jack_mixer * mixer,
157 int8_t cc);
159 float
160 value_to_db(
161 float value)
163 if (value <= 0)
165 return -INFINITY;
168 return 20.0 * log10f(value);
171 float
172 db_to_value(
173 float db)
175 return powf(10.0, db / 20.0);
178 double
179 interpolate(
180 double start,
181 double end,
182 int step,
183 int steps)
185 double ret;
186 double frac = 0.01;
187 LOG_DEBUG("%f -> %f, %d", start, end, step);
188 if (start <= 0) {
189 if (step <= frac * steps) {
190 ret = frac * end * step / steps;
192 else {
193 ret = db_to_value(value_to_db(frac * end) + (value_to_db(end) - value_to_db(frac * end)) * step / steps);;
196 else if (end <= 0) {
197 if (step >= (1 - frac) * steps) {
198 ret = frac * start - frac * start * step / steps;
200 else {
201 ret = db_to_value(value_to_db(start) + (value_to_db(frac * start) - value_to_db(start)) * step / steps);
204 else {
205 ret = db_to_value(value_to_db(start) + (value_to_db(end) - value_to_db(start)) *step /steps);
207 LOG_DEBUG("interpolate: %f", ret);
208 return ret;
211 #define channel_ptr ((struct channel *)channel)
213 const char*
214 channel_get_name(
215 jack_mixer_channel_t channel)
217 return channel_ptr->name;
220 void
221 channel_rename(
222 jack_mixer_channel_t channel,
223 const char * name)
225 char * new_name;
226 size_t channel_name_size;
227 char * port_name;
228 int ret;
230 new_name = strdup(name);
231 if (new_name == NULL)
233 return;
236 if (channel_ptr->name)
238 free(channel_ptr->name);
241 channel_ptr->name = new_name;
243 if (channel_ptr->stereo)
245 channel_name_size = strlen(name);
246 port_name = malloc(channel_name_size + 3);
247 memcpy(port_name, name, channel_name_size);
249 port_name[channel_name_size] = ' ';
250 port_name[channel_name_size+1] = 'L';
251 port_name[channel_name_size+2] = 0;
253 ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left, port_name);
254 if (ret != 0)
256 /* what could we do here? */
259 port_name[channel_name_size+1] = 'R';
261 ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right, port_name);
262 if (ret != 0)
264 /* what could we do here? */
267 free(port_name);
269 else
271 ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left, name);
272 if (ret != 0)
274 /* what could we do here? */
279 bool
280 channel_is_stereo(
281 jack_mixer_channel_t channel)
283 return channel_ptr->stereo;
286 int8_t
287 channel_get_balance_midi_cc(
288 jack_mixer_channel_t channel)
290 return channel_ptr->midi_cc_balance_index;
294 * Remove assignment for given MIDI CC
296 * This is an internal (static) function
298 static void
299 unset_midi_cc_mapping(
300 struct jack_mixer * mixer,
301 int8_t cc)
303 struct channel *channel = mixer->midi_cc_map[cc];
304 if (!channel) {
305 return;
308 if (channel->midi_cc_volume_index == cc) {
309 channel->midi_cc_volume_index = -1;
311 else if (channel->midi_cc_balance_index == cc) {
312 channel->midi_cc_balance_index = -1;
314 else if (channel->midi_cc_mute_index == cc) {
315 channel->midi_cc_mute_index = -1;
317 else if (channel->midi_cc_solo_index == cc) {
318 channel->midi_cc_solo_index = -1;
321 mixer->midi_cc_map[cc] = NULL;
324 unsigned int
325 channel_set_balance_midi_cc(
326 jack_mixer_channel_t channel,
327 int8_t new_cc)
329 if (new_cc < 0 || new_cc > 127) {
330 return 2; /* error: outside CC value range */
333 /* Remove previous assignment for this CC */
334 unset_midi_cc_mapping(channel_ptr->mixer_ptr, new_cc);
335 /* Remove previous balance CC mapped to this channel (if any) */
336 if (channel_ptr->midi_cc_balance_index != -1) {
337 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
339 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
340 channel_ptr->midi_cc_balance_index = new_cc;
341 return 0;
344 int8_t
345 channel_get_volume_midi_cc(
346 jack_mixer_channel_t channel)
348 return channel_ptr->midi_cc_volume_index;
351 unsigned int
352 channel_set_volume_midi_cc(
353 jack_mixer_channel_t channel,
354 int8_t new_cc)
356 if (new_cc< 0 || new_cc > 127) {
357 return 2; /* error: outside limit CC */
360 /* remove previous assignment for this CC */
361 unset_midi_cc_mapping(channel_ptr->mixer_ptr, new_cc);
362 /* remove previous volume CC mapped to this channel (if any) */
363 if (channel_ptr->midi_cc_volume_index != -1) {
364 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
366 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
367 channel_ptr->midi_cc_volume_index = new_cc;
368 return 0;
371 int8_t
372 channel_get_mute_midi_cc(
373 jack_mixer_channel_t channel)
375 return channel_ptr->midi_cc_mute_index;
378 unsigned int
379 channel_set_mute_midi_cc(
380 jack_mixer_channel_t channel,
381 int8_t new_cc)
383 if (new_cc < 0 || new_cc > 127) {
384 return 2; /* error: outside CC value range */
387 /* Remove previous assignment for this CC */
388 unset_midi_cc_mapping(channel_ptr->mixer_ptr, new_cc);
389 /* Remove previous mute CC mapped to this channel (if any) */
390 if (channel_ptr->midi_cc_mute_index != -1) {
391 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
393 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
394 channel_ptr->midi_cc_mute_index = new_cc;
395 return 0;
398 int8_t
399 channel_get_solo_midi_cc(
400 jack_mixer_channel_t channel)
402 return channel_ptr->midi_cc_solo_index;
405 void
406 channel_set_midi_cc_volume_picked_up(
407 jack_mixer_channel_t channel,
408 bool status)
410 LOG_DEBUG("Setting channel %s volume picked up to %d", channel_ptr->name, status);
411 channel_ptr->midi_cc_volume_picked_up = status;
414 void
415 channel_set_midi_cc_balance_picked_up(
416 jack_mixer_channel_t channel,
417 bool status)
419 LOG_DEBUG("Setting channel %s balance picked up to %d", channel_ptr->name, status);
420 channel_ptr->midi_cc_balance_picked_up = status;
423 unsigned int
424 channel_set_solo_midi_cc(
425 jack_mixer_channel_t channel,
426 int8_t new_cc)
428 if (new_cc < 0 || new_cc > 127) {
429 return 2; /* error: outside limit CC */
432 /* Remove previous assignment for this CC */
433 unset_midi_cc_mapping(channel_ptr->mixer_ptr, new_cc);
434 /* Remove previous solo CC mapped to this channel (if any) */
435 if (channel_ptr->midi_cc_solo_index != -1) {
436 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
438 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
439 channel_ptr->midi_cc_solo_index = new_cc;
440 return 0;
444 channel_autoset_volume_midi_cc(
445 jack_mixer_channel_t channel)
447 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
449 for (int i = 11 ; i < 128 ; i++)
451 if (!mixer_ptr->midi_cc_map[i])
453 mixer_ptr->midi_cc_map[i] = channel_ptr;
454 channel_ptr->midi_cc_volume_index = i;
456 LOG_DEBUG("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
458 return i;
461 return -1;
465 channel_autoset_balance_midi_cc(
466 jack_mixer_channel_t channel)
468 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
470 for (int i = 11; i < 128 ; i++)
472 if (!mixer_ptr->midi_cc_map[i])
474 mixer_ptr->midi_cc_map[i] = channel_ptr;
475 channel_ptr->midi_cc_balance_index = i;
477 LOG_DEBUG("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
479 return i;
482 return -1;
486 channel_autoset_mute_midi_cc(
487 jack_mixer_channel_t channel)
489 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
491 for (int i = 11; i < 128 ; i++)
493 if (!mixer_ptr->midi_cc_map[i])
495 mixer_ptr->midi_cc_map[i] = channel_ptr;
496 channel_ptr->midi_cc_mute_index = i;
498 LOG_DEBUG("New channel \"%s\" mute mapped to CC#%i", channel_ptr->name, i);
500 return i;
503 return -1;
507 channel_autoset_solo_midi_cc(
508 jack_mixer_channel_t channel)
510 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
512 for (int i = 11; i < 128 ; i++)
514 if (!mixer_ptr->midi_cc_map[i])
516 mixer_ptr->midi_cc_map[i] = channel_ptr;
517 channel_ptr->midi_cc_solo_index = i;
519 LOG_DEBUG("New channel \"%s\" solo mapped to CC#%i", channel_ptr->name, i);
521 return i;
524 return -1;
527 void
528 remove_channel(
529 jack_mixer_channel_t channel)
531 GSList *list_ptr;
532 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
533 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
534 free(channel_ptr->name);
536 /* remove references to input channel from all output channels */
537 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
539 struct output_channel *output_channel_ptr = list_ptr->data;
540 output_channel_set_solo(output_channel_ptr, channel, false);
541 output_channel_set_muted(output_channel_ptr, channel, false);
544 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
545 if (channel_ptr->stereo)
547 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
550 if (channel_ptr->midi_cc_volume_index != -1)
552 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
553 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
556 if (channel_ptr->midi_cc_balance_index != -1)
558 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
559 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
562 if (channel_ptr->midi_cc_mute_index != -1)
564 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
565 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
567 if (channel_ptr->midi_cc_solo_index != -1)
569 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
570 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
573 free(channel_ptr->frames_left);
574 free(channel_ptr->frames_right);
575 free(channel_ptr->prefader_frames_left);
576 free(channel_ptr->prefader_frames_right);
578 free(channel_ptr);
581 void
582 channel_stereo_meter_read(
583 jack_mixer_channel_t channel,
584 double * left_ptr,
585 double * right_ptr)
587 assert(channel_ptr);
588 *left_ptr = value_to_db(channel_ptr->meter_left);
589 *right_ptr = value_to_db(channel_ptr->meter_right);
593 void
594 channel_mono_meter_read(
595 jack_mixer_channel_t channel,
596 double * mono_ptr)
598 *mono_ptr = value_to_db(channel_ptr->meter_left);
601 void
602 channel_stereo_kmeter_read(
603 jack_mixer_channel_t channel,
604 double * left_ptr,
605 double * right_ptr,
606 double * left_rms_ptr,
607 double * right_rms_ptr)
609 assert(channel_ptr);
610 *left_ptr = value_to_db(channel_ptr->kmeter_left._dpk);
611 *right_ptr = value_to_db(channel_ptr->kmeter_right._dpk);
612 *left_rms_ptr = value_to_db(channel_ptr->kmeter_left._rms);
613 *right_rms_ptr = value_to_db(channel_ptr->kmeter_right._rms);
614 channel_ptr->kmeter_left._flag = true;
615 channel_ptr->kmeter_right._flag = true;
618 void
619 channel_mono_kmeter_read(
620 jack_mixer_channel_t channel,
621 double * mono_ptr,
622 double * mono_rms_ptr)
624 *mono_ptr = value_to_db(channel_ptr->kmeter_left._dpk);
625 *mono_rms_ptr = value_to_db(channel_ptr->kmeter_left._rms);
626 channel_ptr->kmeter_left._flag = true;
629 void
630 channel_volume_write(
631 jack_mixer_channel_t channel,
632 double volume)
634 assert(channel_ptr);
635 double value = db_to_value(volume);
636 /*If changing volume and find we're in the middle of a previous transition,
637 *then set current volume to place in transition to avoid a jump.*/
638 if (channel_ptr->volume_new != channel_ptr->volume) {
639 channel_ptr->volume = interpolate(channel_ptr->volume,
640 channel_ptr->volume_new,
641 channel_ptr->volume_idx,
642 channel_ptr->num_volume_transition_steps);
644 channel_ptr->volume_idx = 0;
645 if (channel_ptr->volume_new != value) {
646 channel_ptr->midi_out_has_events |= CHANNEL_VOLUME;
648 channel_ptr->volume_new = value;
649 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, value);
652 double
653 channel_volume_read(
654 jack_mixer_channel_t channel)
656 assert(channel_ptr);
657 return value_to_db(channel_ptr->volume_new);
660 void
661 channels_volumes_read(
662 jack_mixer_t mixer_ptr)
664 GSList *node_ptr;
665 struct channel *pChannel;
666 struct jack_mixer * pMixer = (struct jack_mixer *)mixer_ptr;
668 for (node_ptr = pMixer->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
670 pChannel = (struct channel *)node_ptr->data;
671 double vol = channel_volume_read( (jack_mixer_channel_t)pChannel);
672 printf("%s : volume is %f dbFS for mixer channel: %s\n", jack_get_client_name(pMixer->jack_client), vol, pChannel->name);
676 void
677 channel_balance_write(
678 jack_mixer_channel_t channel,
679 double balance)
681 assert(channel_ptr);
682 if (channel_ptr->balance != channel_ptr->balance_new) {
683 channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
684 (channel_ptr->balance_new - channel_ptr->balance) /
685 channel_ptr->num_volume_transition_steps;
687 channel_ptr->balance_idx = 0;
688 if (channel_ptr->balance_new != balance) {
689 channel_ptr->midi_out_has_events |= CHANNEL_BALANCE;
691 channel_ptr->balance_new = balance;
692 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, balance);
695 double
696 channel_balance_read(
697 jack_mixer_channel_t channel)
699 assert(channel_ptr);
700 return channel_ptr->balance_new;
703 double
704 channel_abspeak_read(
705 jack_mixer_channel_t channel)
707 assert(channel_ptr);
708 if (channel_ptr->NaN_detected)
710 return sqrt(-1);
712 else
714 return value_to_db(channel_ptr->abspeak);
718 void
719 channel_abspeak_reset(
720 jack_mixer_channel_t channel)
722 channel_ptr->abspeak = 0;
723 channel_ptr->NaN_detected = false;
726 void
727 channel_out_mute(
728 jack_mixer_channel_t channel)
730 if (!channel_ptr->out_mute) {
731 channel_ptr->out_mute = true;
732 channel_ptr->midi_out_has_events |= CHANNEL_MUTE;
733 LOG_DEBUG("\"%s\" muted", channel_ptr->name);
737 void
738 channel_out_unmute(
739 jack_mixer_channel_t channel)
741 if (channel_ptr->out_mute) {
742 channel_ptr->out_mute = false;
743 channel_ptr->midi_out_has_events |= CHANNEL_MUTE;
744 LOG_DEBUG("\"%s\" un-muted", channel_ptr->name);
748 bool
749 channel_is_out_muted(
750 jack_mixer_channel_t channel)
752 return channel_ptr->out_mute;
755 void
756 channel_solo(
757 jack_mixer_channel_t channel)
759 if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) != NULL)
760 return;
761 channel_ptr->mixer_ptr->soloed_channels = g_slist_prepend(channel_ptr->mixer_ptr->soloed_channels, channel);
762 channel_ptr->midi_out_has_events |= CHANNEL_SOLO;
763 LOG_DEBUG("\"%s\" soloed", channel_ptr->name);
766 void
767 channel_unsolo(
768 jack_mixer_channel_t channel)
770 if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) == NULL)
771 return;
772 channel_ptr->mixer_ptr->soloed_channels = g_slist_remove(channel_ptr->mixer_ptr->soloed_channels, channel);
773 channel_ptr->midi_out_has_events |= CHANNEL_SOLO;
774 LOG_DEBUG("\"%s\" un-soloed", channel_ptr->name);
777 bool
778 channel_is_soloed(
779 jack_mixer_channel_t channel)
781 if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel))
782 return true;
783 return false;
786 void
787 channel_set_midi_scale(
788 jack_mixer_channel_t channel,
789 jack_mixer_scale_t scale)
791 channel_ptr->midi_scale = scale;
794 void
795 channel_set_midi_change_callback(
796 jack_mixer_channel_t channel,
797 void (*midi_change_callback) (void*),
798 void *user_data)
800 channel_ptr->midi_change_callback = midi_change_callback;
801 channel_ptr->midi_change_callback_data = user_data;
804 bool
805 channel_get_midi_in_got_events(
806 jack_mixer_channel_t channel)
808 bool t = channel_ptr->midi_in_got_events;
809 channel_ptr->midi_in_got_events = false;
810 return t;
813 #undef channel_ptr
816 * Process input channels and mix them into one output channel signal
818 static inline void
819 mix_one(
820 struct output_channel *output_mix_channel,
821 GSList *channels_list, /* All input channels */
822 jack_nframes_t start, /* Index of first sample to process */
823 jack_nframes_t end) /* Index of sample to stop processing before */
825 jack_nframes_t i;
827 GSList *node_ptr;
828 struct channel * channel_ptr;
829 jack_default_audio_sample_t frame_left;
830 jack_default_audio_sample_t frame_right;
831 struct channel *mix_channel = (struct channel*)output_mix_channel;
833 /* Zero intermediate mix & output buffers */
834 for (i = start; i < end; i++)
836 mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i] = 0.0;
837 if (mix_channel->stereo)
838 mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i] = 0.0;
841 /* For each input channel: */
842 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
844 channel_ptr = node_ptr->data;
846 /* Skip input channels with activated mute for this output channel */
847 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL || channel_ptr->out_mute) {
848 continue;
851 /* Mix signal of all input channels going to this output channel:
853 * Only add the signal from this input channel:
855 * - if there are no globally soloed channels and no soloed channels for this output-channel;
856 * - or if the input channel is globally soloed and the output channel is not a system
857 * channel (direct out or monitor out);
858 * - or if the input channel is soloed for this output channel.
860 * */
861 if ((!channel_ptr->mixer_ptr->soloed_channels && !output_mix_channel->soloed_channels) ||
862 (channel_ptr->mixer_ptr->soloed_channels &&
863 g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel_ptr) != NULL &&
864 !output_mix_channel->system) ||
865 (output_mix_channel->soloed_channels &&
866 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) != NULL))
869 /* Get either post or pre-fader signal */
870 for (i = start ; i < end ; i++)
872 /* Left/mono signal */
873 if (! output_mix_channel->prefader &&
874 g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL)
876 frame_left = channel_ptr->frames_left[i-start];
878 else {
879 /* Output channel is globally set to pre-fader routing or
880 * input channel has pre-fader routing set for this output channel
882 frame_left = channel_ptr->prefader_frames_left[i-start];
885 if (frame_left == NAN)
886 break;
888 mix_channel->tmp_mixed_frames_left[i] += frame_left;
890 /* Right signal */
891 if (mix_channel->stereo)
893 if (! output_mix_channel->prefader &&
894 g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL)
896 frame_right = channel_ptr->frames_right[i-start];
898 else {
899 /* Pre-fader routing */
900 frame_right = channel_ptr->prefader_frames_right[i-start];
903 if (frame_right == NAN)
904 break;
906 mix_channel->tmp_mixed_frames_right[i] += frame_right;
912 /* Apply output channel volume and compute meter signal and peak values */
913 unsigned int steps = mix_channel->num_volume_transition_steps;
915 for (i = start ; i < end ; i++)
917 /** Apply fader volume if output channel is not set to pre-fader routing */
918 if (! output_mix_channel->prefader) {
919 float volume = mix_channel->volume;
920 float volume_new = mix_channel->volume_new;
921 float vol = volume;
922 float balance = mix_channel->balance;
923 float balance_new = mix_channel->balance_new;
924 float bal = balance;
926 /* Do interpolation during transition to target volume level */
927 if (volume != volume_new) {
928 vol = interpolate(volume, volume_new, mix_channel->volume_idx, steps);
931 /* Do interpolation during transition to target balance */
932 if (balance != balance_new) {
933 bal = mix_channel->balance_idx * (balance_new - balance) / steps + balance;
936 float vol_l;
937 float vol_r;
939 /* Calculate left+right gain from volume and balance levels */
940 if (mix_channel->stereo) {
941 if (bal > 0) {
942 vol_l = vol * (1 - bal);
943 vol_r = vol;
945 else {
946 vol_l = vol;
947 vol_r = vol * (1 + bal);
950 else {
951 vol_l = vol * (1 - bal);
952 vol_r = vol * (1 + bal);
955 /* Apply gain to output mix */
956 mix_channel->tmp_mixed_frames_left[i] *= vol_l;
957 mix_channel->tmp_mixed_frames_right[i] *= vol_r;
960 /* Get peak signal, left/right and combined */
961 frame_left = fabsf(mix_channel->tmp_mixed_frames_left[i]);
962 if (mix_channel->peak_left < frame_left)
964 mix_channel->peak_left = frame_left;
966 if (frame_left > mix_channel->abspeak)
968 mix_channel->abspeak = frame_left;
972 if (mix_channel->stereo)
974 frame_right = fabsf(mix_channel->tmp_mixed_frames_right[i]);
975 if (mix_channel->peak_right < frame_right)
977 mix_channel->peak_right = frame_right;
979 if (frame_right > mix_channel->abspeak)
981 mix_channel->abspeak = frame_right;
986 /* This seems to duplicate what was already done right above? */
987 if (mix_channel->stereo)
989 frame_left = fabsf(mix_channel->tmp_mixed_frames_left[i]);
990 frame_right = fabsf(mix_channel->tmp_mixed_frames_right[i]);
992 if (mix_channel->peak_left < frame_left)
994 mix_channel->peak_left = frame_left;
996 if (frame_left > mix_channel->abspeak)
998 mix_channel->abspeak = frame_left;
1002 if (mix_channel->peak_right < frame_right)
1004 mix_channel->peak_right = frame_right;
1006 if (frame_right > mix_channel->abspeak)
1008 mix_channel->abspeak = frame_right;
1012 else
1014 if (mix_channel->peak_left < frame_left)
1016 mix_channel->peak_left = frame_left;
1018 if (frame_left > mix_channel->abspeak)
1020 mix_channel->abspeak = frame_left;
1025 /* update left/right peak values every so often */
1026 mix_channel->peak_frames++;
1027 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
1029 mix_channel->meter_left = mix_channel->peak_left;
1030 mix_channel->peak_left = 0.0;
1032 if (mix_channel->stereo)
1034 mix_channel->meter_right = mix_channel->peak_right;
1035 mix_channel->peak_right = 0.0;
1038 mix_channel->peak_frames = 0;
1041 /* Finish off volume interpolation */
1042 mix_channel->volume_idx++;
1043 if ((mix_channel->volume != mix_channel->volume_new) && (mix_channel->volume_idx == steps)) {
1044 mix_channel->volume = mix_channel->volume_new;
1045 mix_channel->volume_idx = 0;
1047 /* Finish off volume interpolation */
1048 mix_channel->balance_idx++;
1049 if ((mix_channel->balance != mix_channel->balance_new) && (mix_channel->balance_idx == steps)) {
1050 mix_channel->balance = mix_channel->balance_new;
1051 mix_channel->balance_idx = 0;
1054 /* Finally, if output channel is not muted, put signal into output buffer */
1055 if (!mix_channel->out_mute) {
1056 mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i];
1057 if (mix_channel->stereo)
1058 mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i];
1061 /* Calculate k-metering for output channel*/
1062 kmeter_process(&mix_channel->kmeter_left, mix_channel->tmp_mixed_frames_left, start, end);
1063 kmeter_process(&mix_channel->kmeter_right, mix_channel->tmp_mixed_frames_right, start, end);
1066 static inline void
1067 calc_channel_frames(
1068 struct channel *channel_ptr,
1069 jack_nframes_t start,
1070 jack_nframes_t end)
1072 jack_nframes_t i;
1073 jack_default_audio_sample_t frame_left = 0.0f;
1074 jack_default_audio_sample_t frame_right = 0.0f;
1075 unsigned int steps = channel_ptr->num_volume_transition_steps;
1077 for (i = start ; i < end ; i++)
1079 if (i-start >= MAX_BLOCK_SIZE)
1081 fprintf(stderr, "i-start too high: %d - %d\n", i, start);
1084 /* Save pre-fader signal */
1085 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
1086 if (channel_ptr->stereo)
1087 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
1089 /* Detect de-normals */
1090 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
1092 channel_ptr->NaN_detected = true;
1093 channel_ptr->frames_left[i-start] = NAN;
1094 break;
1097 /* Get current and target channel volume and balance. */
1098 float volume = channel_ptr->volume;
1099 float volume_new = channel_ptr->volume_new;
1100 float vol = volume;
1101 float balance = channel_ptr->balance;
1102 float balance_new = channel_ptr->balance_new;
1103 float bal = balance;
1105 /* During transition do interpolation to target volume level */
1106 if (channel_ptr->volume != channel_ptr->volume_new) {
1107 vol = interpolate(volume, volume_new, channel_ptr->volume_idx, steps);
1110 /* During transition do interpolation to target balance */
1111 if (channel_ptr->balance != channel_ptr->balance_new) {
1112 bal = channel_ptr->balance_idx * (balance_new - balance) / steps + balance;
1115 /* Calculate left+right gain from volume and balance levels */
1116 float vol_l;
1117 float vol_r;
1118 if (channel_ptr->stereo) {
1119 if (bal > 0) {
1120 vol_l = vol * (1 - bal);
1121 vol_r = vol;
1123 else {
1124 vol_l = vol;
1125 vol_r = vol * (1 + bal);
1128 else {
1129 vol_l = vol * (1 - bal);
1130 vol_r = vol * (1 + bal);
1133 /* Calculate left channel post-fader sample */
1134 frame_left = channel_ptr->left_buffer_ptr[i] * vol_l;
1135 /* Calculate right channel post-fader sample */
1136 if (channel_ptr->stereo)
1138 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
1140 channel_ptr->NaN_detected = true;
1141 channel_ptr->frames_right[i-start] = NAN;
1142 break;
1145 frame_right = channel_ptr->right_buffer_ptr[i] * vol_r;
1147 else
1149 frame_right = channel_ptr->left_buffer_ptr[i] * vol_r;
1151 channel_ptr->frames_left[i-start] = frame_left;
1152 channel_ptr->frames_right[i-start] = frame_right;
1154 /* Calculate left+right peak-level and, if need be,
1155 * update abspeak level
1157 if (channel_ptr->stereo)
1159 frame_left = fabsf(frame_left);
1160 frame_right = fabsf(frame_right);
1162 if (channel_ptr->peak_left < frame_left)
1164 channel_ptr->peak_left = frame_left;
1166 if (frame_left > channel_ptr->abspeak)
1168 channel_ptr->abspeak = frame_left;
1172 if (channel_ptr->peak_right < frame_right)
1174 channel_ptr->peak_right = frame_right;
1176 if (frame_right > channel_ptr->abspeak)
1178 channel_ptr->abspeak = frame_right;
1182 else
1185 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
1187 if (channel_ptr->peak_left < frame_left)
1189 channel_ptr->peak_left = frame_left;
1191 if (frame_left > channel_ptr->abspeak)
1193 channel_ptr->abspeak = frame_left;
1198 /* Update input channel volume meter every so often */
1199 channel_ptr->peak_frames++;
1200 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
1202 channel_ptr->meter_left = channel_ptr->peak_left;
1203 channel_ptr->peak_left = 0.0;
1205 if (channel_ptr->stereo)
1207 channel_ptr->meter_right = channel_ptr->peak_right;
1208 channel_ptr->peak_right = 0.0;
1211 channel_ptr->peak_frames = 0;
1214 /* Finish off volume & balance level interpolation */
1215 channel_ptr->volume_idx++;
1216 if ((channel_ptr->volume != channel_ptr->volume_new) &&
1217 (channel_ptr->volume_idx == steps)) {
1218 channel_ptr->volume = channel_ptr->volume_new;
1219 channel_ptr->volume_idx = 0;
1221 channel_ptr->balance_idx++;
1222 if ((channel_ptr->balance != channel_ptr->balance_new) &&
1223 (channel_ptr->balance_idx >= steps)) {
1224 channel_ptr->balance = channel_ptr->balance_new;
1225 channel_ptr->balance_idx = 0;
1229 /* Calculate k-metering for input channel */
1230 kmeter_process(&channel_ptr->kmeter_left, channel_ptr->frames_left, start, end);
1231 if (channel_ptr->stereo)
1232 kmeter_process(&channel_ptr->kmeter_right, channel_ptr->frames_right, start, end);
1235 static inline void
1236 mix(
1237 struct jack_mixer * mixer_ptr,
1238 jack_nframes_t start, /* Index of first sample to process */
1239 jack_nframes_t end) /* Index of sample to stop processing before */
1241 GSList *node_ptr;
1242 struct output_channel * output_channel_ptr;
1243 struct channel *channel_ptr;
1245 /* Calculate pre/post-fader output and peak values for each input channel */
1246 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1248 channel_ptr = (struct channel*)node_ptr->data;
1249 calc_channel_frames(channel_ptr, start, end);
1252 /* For all output channels: */
1253 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1255 output_channel_ptr = node_ptr->data;
1256 channel_ptr = (struct channel*)output_channel_ptr;
1258 if (output_channel_ptr->system)
1260 /* Don't bother mixing the channels if we are not connected */
1261 if (channel_ptr->stereo)
1263 if (jack_port_connected(channel_ptr->port_left) == 0 &&
1264 jack_port_connected(channel_ptr->port_right) == 0)
1265 continue;
1267 else {
1268 if (jack_port_connected(channel_ptr->port_left) == 0)
1269 continue;
1273 /* Mix this output channel */
1274 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
1278 static inline void
1279 update_channel_buffers(
1280 struct channel * channel_ptr,
1281 jack_nframes_t nframes)
1283 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
1285 if (channel_ptr->stereo)
1287 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
1291 #define mixer_ptr ((struct jack_mixer *)context)
1293 static int
1294 process(
1295 jack_nframes_t nframes,
1296 void * context)
1298 GSList *node_ptr;
1299 struct channel * channel_ptr;
1300 #if defined(HAVE_JACK_MIDI)
1301 jack_nframes_t i;
1302 jack_nframes_t event_count;
1303 jack_midi_event_t in_event;
1304 unsigned char* midi_out_buffer;
1305 void * midi_buffer;
1306 double volume, balance;
1307 uint8_t cc_channel_index;
1308 uint8_t cc_num, cc_val, cur_cc_val;
1309 #endif
1311 /* Get input ports buffer pointers */
1312 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1314 channel_ptr = node_ptr->data;
1315 update_channel_buffers(channel_ptr, nframes);
1318 /* Get output ports buffer pointer */
1319 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1321 channel_ptr = node_ptr->data;
1322 update_channel_buffers(channel_ptr, nframes);
1325 #if defined(HAVE_JACK_MIDI)
1326 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
1327 event_count = jack_midi_get_event_count(midi_buffer);
1329 for (i = 0 ; i < event_count; i++)
1331 jack_midi_event_get(&in_event, midi_buffer, i);
1333 if (in_event.size != 3 ||
1334 (in_event.buffer[0] & 0xF0) != 0xB0 ||
1335 in_event.buffer[1] > 127 ||
1336 in_event.buffer[2] > 127)
1338 continue;
1341 assert(in_event.time < nframes);
1343 cc_num = (uint8_t)(in_event.buffer[1] & 0x7F);
1344 cc_val = (uint8_t)(in_event.buffer[2] & 0x7F);
1345 mixer_ptr->last_midi_cc = (int8_t)cc_num;
1347 LOG_DEBUG("%u: CC#%u -> %u", (unsigned int)(in_event.buffer[0]), cc_num, cc_val);
1349 /* Do we have a mapping for particular CC? */
1350 channel_ptr = mixer_ptr->midi_cc_map[cc_num];
1351 if (channel_ptr)
1353 if (channel_ptr->midi_cc_balance_index == cc_num)
1355 if (cc_val < 63) {
1356 balance = MAP(cc_val, 0.0, 63.0, -1.0, -0.015625);
1358 else {
1359 balance = MAP(cc_val, 64.0, 127.0, 0.0, 1.0);
1361 if (mixer_ptr->midi_behavior == Pick_Up &&
1362 !channel_ptr->midi_cc_balance_picked_up &&
1363 channel_balance_read(channel_ptr) - balance < BALANCE_PICKUP_THRESHOLD)
1365 channel_set_midi_cc_balance_picked_up(channel_ptr, true);
1367 if ((mixer_ptr->midi_behavior == Pick_Up &&
1368 channel_ptr->midi_cc_balance_picked_up) ||
1369 mixer_ptr->midi_behavior == Jump_To_Value)
1371 channel_balance_write(channel_ptr, balance);
1375 else if (channel_ptr->midi_cc_volume_index == cc_num)
1377 /* Is a MIDI scale set for corresponding channel? */
1378 if (channel_ptr->midi_scale) {
1379 volume = scale_scale_to_db(channel_ptr->midi_scale,
1380 (double)cc_val / 127);
1381 if (mixer_ptr->midi_behavior == Pick_Up &&
1382 !channel_ptr->midi_cc_volume_picked_up)
1384 /* MIDI control in pick-up mode but not picked up yet */
1385 cur_cc_val = (uint8_t)(127 * scale_db_to_scale(
1386 channel_ptr->midi_scale,
1387 value_to_db(channel_ptr->volume)));
1388 if (cc_val == cur_cc_val)
1390 /* Incoming MIDI CC value matches current volume level
1391 * --> MIDI control is picked up
1393 channel_set_midi_cc_volume_picked_up(channel_ptr, true);
1396 if ((mixer_ptr->midi_behavior == Pick_Up &&
1397 channel_ptr->midi_cc_volume_picked_up) ||
1398 mixer_ptr->midi_behavior == Jump_To_Value)
1400 channel_volume_write(channel_ptr, volume);
1404 else if (channel_ptr->midi_cc_mute_index == cc_num)
1406 if (cc_val >= 64) {
1407 channel_out_mute(channel_ptr);
1409 else {
1410 channel_out_unmute(channel_ptr);
1414 else if (channel_ptr->midi_cc_solo_index == cc_num)
1416 if (cc_val >= 64) {
1417 channel_solo(channel_ptr);
1419 else {
1420 channel_unsolo(channel_ptr);
1423 channel_ptr->midi_in_got_events = true;
1424 if (channel_ptr->midi_change_callback) {
1425 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
1431 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_out, nframes);
1432 jack_midi_clear_buffer(midi_buffer);
1434 for(i=0; i<nframes; i++)
1436 for (cc_channel_index=0; cc_channel_index<128; cc_channel_index++)
1438 channel_ptr = mixer_ptr->midi_cc_map[cc_channel_index];
1439 if (!channel_ptr)
1441 continue;
1443 if (!channel_ptr->midi_out_has_events)
1445 continue;
1447 if (channel_ptr->midi_out_has_events & CHANNEL_VOLUME && channel_ptr->midi_scale)
1449 midi_out_buffer = jack_midi_event_reserve(midi_buffer, 0, 3);
1450 if (!midi_out_buffer)
1451 continue;
1453 midi_out_buffer[0] = 0xB0; /* control change */
1454 midi_out_buffer[1] = channel_ptr->midi_cc_volume_index;
1455 midi_out_buffer[2] = (unsigned char)(127 * scale_db_to_scale(channel_ptr->midi_scale,
1456 value_to_db(channel_ptr->volume_new)));
1458 LOG_DEBUG(
1459 "%u: CC#%u <- %u",
1460 (unsigned int)midi_out_buffer[0],
1461 (unsigned int)midi_out_buffer[1],
1462 (unsigned int)midi_out_buffer[2]);
1464 if (channel_ptr->midi_out_has_events & CHANNEL_BALANCE)
1466 midi_out_buffer = jack_midi_event_reserve(midi_buffer, 0, 3);
1467 if (!midi_out_buffer)
1468 continue;
1470 midi_out_buffer[0] = 0xB0; /* control change */
1471 midi_out_buffer[1] = channel_ptr->midi_cc_balance_index;
1472 balance = channel_balance_read(channel_ptr);
1473 if (balance < 0.0) {
1474 midi_out_buffer[2] = (unsigned char)(MAP(balance, -1.0, -0.015625, 0.0, 63.0) + 0.5);
1476 else {
1477 midi_out_buffer[2] = (unsigned char)(MAP(balance, 0.0, 1.0, 64.0, 127.0) + 0.5);
1480 LOG_DEBUG(
1481 "%u: CC#%u <- %u",
1482 (unsigned int)midi_out_buffer[0],
1483 (unsigned int)midi_out_buffer[1],
1484 (unsigned int)midi_out_buffer[2]);
1486 if (channel_ptr->midi_out_has_events & CHANNEL_MUTE)
1488 midi_out_buffer = jack_midi_event_reserve(midi_buffer, 0, 3);
1489 if (!midi_out_buffer)
1490 continue;
1492 midi_out_buffer[0] = 0xB0; /* control change */
1493 midi_out_buffer[1] = channel_ptr->midi_cc_mute_index;
1494 midi_out_buffer[2] = (unsigned char)(channel_is_out_muted(channel_ptr) ? 127 : 0);
1496 LOG_DEBUG(
1497 "%u: CC#%u <- %u",
1498 (unsigned int)midi_out_buffer[0],
1499 (unsigned int)midi_out_buffer[1],
1500 (unsigned int)midi_out_buffer[2]);
1502 if (channel_ptr->midi_out_has_events & CHANNEL_SOLO)
1504 midi_out_buffer = jack_midi_event_reserve(midi_buffer, 0, 3);
1505 if (!midi_out_buffer)
1506 continue;
1508 midi_out_buffer[0] = 0xB0; /* control change */
1509 midi_out_buffer[1] = channel_ptr->midi_cc_solo_index;
1510 midi_out_buffer[2] = (unsigned char)(channel_is_soloed(channel_ptr) ? 127 : 0);
1512 LOG_DEBUG(
1513 "%u: CC#%u <- %u",
1514 (unsigned int)midi_out_buffer[0],
1515 (unsigned int)midi_out_buffer[1],
1516 (unsigned int)midi_out_buffer[2]);
1518 channel_ptr->midi_out_has_events = 0;
1522 #endif
1524 mix(mixer_ptr, 0, nframes);
1526 return 0;
1529 #undef mixer_ptr
1531 jack_mixer_t
1532 create(
1533 const char * jack_client_name_ptr,
1534 bool stereo)
1536 (void) stereo;
1537 int ret;
1538 struct jack_mixer * mixer_ptr;
1539 int i;
1542 mixer_ptr = malloc(sizeof(struct jack_mixer));
1543 if (mixer_ptr == NULL)
1545 goto exit;
1548 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
1549 if (ret != 0)
1551 goto exit_free;
1554 mixer_ptr->input_channels_list = NULL;
1555 mixer_ptr->output_channels_list = NULL;
1557 mixer_ptr->soloed_channels = NULL;
1559 mixer_ptr->last_midi_cc = -1;
1561 mixer_ptr->midi_behavior = Jump_To_Value;
1563 for (i = 0 ; i < 128 ; i++)
1565 mixer_ptr->midi_cc_map[i] = NULL;
1568 LOG_DEBUG("Initializing JACK");
1569 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
1570 if (mixer_ptr->jack_client == NULL)
1572 LOG_ERROR("Cannot create JACK client.");
1573 LOG_NOTICE("Please make sure JACK daemon is running.");
1574 goto exit_destroy_mutex;
1577 LOG_DEBUG("JACK client created");
1579 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
1582 #if defined(HAVE_JACK_MIDI)
1583 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1584 if (mixer_ptr->port_midi_in == NULL)
1586 LOG_ERROR("Cannot create JACK MIDI in port");
1587 goto close_jack;
1590 mixer_ptr->port_midi_out = jack_port_register(mixer_ptr->jack_client, "midi out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
1591 if (mixer_ptr->port_midi_out == NULL)
1593 LOG_ERROR("Cannot create JACK MIDI out port");
1594 goto close_jack;
1597 #endif
1599 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1600 if (ret != 0)
1602 LOG_ERROR("Cannot set JACK process callback");
1603 goto close_jack;
1606 ret = jack_activate(mixer_ptr->jack_client);
1607 if (ret != 0)
1609 LOG_ERROR("Cannot activate JACK client");
1610 goto close_jack;
1613 return mixer_ptr;
1615 close_jack:
1616 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1618 exit_destroy_mutex:
1619 pthread_mutex_destroy(&mixer_ptr->mutex);
1621 exit_free:
1622 free(mixer_ptr);
1624 exit:
1625 return NULL;
1628 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1630 void
1631 destroy(
1632 jack_mixer_t mixer)
1634 LOG_DEBUG("Uninitializing JACK");
1636 assert(mixer_ctx_ptr->jack_client != NULL);
1638 jack_client_close(mixer_ctx_ptr->jack_client);
1640 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1642 free(mixer_ctx_ptr);
1646 unsigned int
1647 get_channels_count(
1648 jack_mixer_t mixer)
1650 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1653 const char*
1654 get_client_name(
1655 jack_mixer_t mixer)
1657 return jack_get_client_name(mixer_ctx_ptr->jack_client);
1660 int8_t
1661 get_last_midi_cc(
1662 jack_mixer_t mixer)
1664 return mixer_ctx_ptr->last_midi_cc;
1667 unsigned int
1668 set_last_midi_cc(
1669 jack_mixer_t mixer,
1670 int8_t new_cc) {
1671 mixer_ctx_ptr->last_midi_cc = new_cc;
1672 return 0;
1676 get_midi_behavior_mode(
1677 jack_mixer_t mixer)
1679 return mixer_ctx_ptr->midi_behavior;
1682 unsigned int
1683 set_midi_behavior_mode(
1684 jack_mixer_t mixer,
1685 enum midi_behavior_mode mode)
1687 mixer_ctx_ptr->midi_behavior = mode;
1688 return 0;
1691 jack_mixer_channel_t
1692 add_channel(
1693 jack_mixer_t mixer,
1694 const char * channel_name,
1695 bool stereo)
1697 struct channel * channel_ptr;
1698 char * port_name = NULL;
1699 size_t channel_name_size;
1701 channel_ptr = malloc(sizeof(struct channel));
1702 if (channel_ptr == NULL)
1704 goto fail;
1707 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1709 channel_ptr->name = strdup(channel_name);
1710 if (channel_ptr->name == NULL)
1712 goto fail_free_channel;
1715 channel_name_size = strlen(channel_name);
1717 if (stereo)
1719 port_name = malloc(channel_name_size + 3);
1720 if (port_name == NULL)
1722 goto fail_free_channel_name;
1725 memcpy(port_name, channel_name, channel_name_size);
1726 port_name[channel_name_size] = ' ';
1727 port_name[channel_name_size+1] = 'L';
1728 port_name[channel_name_size+2] = 0;
1730 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1731 if (channel_ptr->port_left == NULL)
1733 goto fail_free_port_name;
1736 port_name[channel_name_size+1] = 'R';
1738 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1739 if (channel_ptr->port_right == NULL)
1741 goto fail_unregister_left_channel;
1744 else
1746 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1747 if (channel_ptr->port_left == NULL)
1749 goto fail_free_channel_name;
1753 channel_ptr->stereo = stereo;
1755 int sr = jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client);
1756 int fsize = jack_get_buffer_size(channel_ptr->mixer_ptr->jack_client);
1758 channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1759 channel_ptr->num_volume_transition_steps =
1760 channel_ptr->volume_transition_seconds * sr + 1;
1761 channel_ptr->volume = 0.0;
1762 channel_ptr->volume_new = 0.0;
1763 channel_ptr->balance = 0.0;
1764 channel_ptr->balance_new = 0.0;
1765 channel_ptr->meter_left = -1.0;
1766 channel_ptr->meter_right = -1.0;
1767 channel_ptr->abspeak = 0.0;
1768 channel_ptr->out_mute = false;
1770 kmeter_init(&channel_ptr->kmeter_left, sr, fsize, .5f, 15.0f);
1771 kmeter_init(&channel_ptr->kmeter_right, sr, fsize, .5f, 15.0f);
1773 channel_ptr->peak_left = 0.0;
1774 channel_ptr->peak_right = 0.0;
1775 channel_ptr->peak_frames = 0;
1777 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1778 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1779 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1780 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1782 channel_ptr->NaN_detected = false;
1784 channel_ptr->midi_cc_volume_index = -1;
1785 channel_ptr->midi_cc_balance_index = -1;
1786 channel_ptr->midi_cc_mute_index = -1;
1787 channel_ptr->midi_cc_solo_index = -1;
1788 channel_ptr->midi_cc_volume_picked_up = false;
1789 channel_ptr->midi_cc_balance_picked_up = false;
1791 channel_ptr->midi_change_callback = NULL;
1792 channel_ptr->midi_change_callback_data = NULL;
1793 channel_ptr->midi_out_has_events = 0;
1795 channel_ptr->midi_scale = NULL;
1797 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1798 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1800 free(port_name);
1801 return channel_ptr;
1803 fail_unregister_left_channel:
1804 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1806 fail_free_port_name:
1807 free(port_name);
1809 fail_free_channel_name:
1810 free(channel_ptr->name);
1812 fail_free_channel:
1813 free(channel_ptr);
1814 channel_ptr = NULL;
1816 fail:
1817 return NULL;
1820 static jack_mixer_output_channel_t
1821 create_output_channel(
1822 jack_mixer_t mixer,
1823 const char * channel_name,
1824 bool stereo,
1825 bool system)
1827 struct channel * channel_ptr;
1828 struct output_channel * output_channel_ptr;
1829 char * port_name = NULL;
1830 size_t channel_name_size;
1832 output_channel_ptr = malloc(sizeof(struct output_channel));
1833 channel_ptr = (struct channel*)output_channel_ptr;
1834 if (channel_ptr == NULL)
1836 goto fail;
1839 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1841 channel_ptr->name = strdup(channel_name);
1842 if (channel_ptr->name == NULL)
1844 goto fail_free_channel;
1847 if (stereo)
1849 channel_name_size = strlen(channel_name);
1851 port_name = malloc(channel_name_size + 4);
1852 if (port_name == NULL)
1854 goto fail_free_channel_name;
1857 memcpy(port_name, channel_name, channel_name_size);
1858 port_name[channel_name_size] = ' ';
1859 port_name[channel_name_size+1] = 'L';
1860 port_name[channel_name_size+2] = 0;
1862 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1863 if (channel_ptr->port_left == NULL)
1865 goto fail_free_port_name;
1868 port_name[channel_name_size+1] = 'R';
1870 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1871 if (channel_ptr->port_right == NULL)
1873 goto fail_unregister_left_channel;
1876 else
1878 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1879 if (channel_ptr->port_left == NULL)
1881 goto fail_free_channel_name;
1885 channel_ptr->stereo = stereo;
1886 channel_ptr->out_mute = false;
1888 int sr = jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client);
1889 int fsize = jack_get_buffer_size(channel_ptr->mixer_ptr->jack_client);
1891 channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1892 channel_ptr->num_volume_transition_steps =
1893 channel_ptr->volume_transition_seconds * sr + 1;
1894 channel_ptr->volume = 0.0;
1895 channel_ptr->volume_new = 0.0;
1896 channel_ptr->balance = 0.0;
1897 channel_ptr->balance_new = 0.0;
1898 channel_ptr->meter_left = -1.0;
1899 channel_ptr->meter_right = -1.0;
1900 channel_ptr->abspeak = 0.0;
1901 kmeter_init(&channel_ptr->kmeter_left, sr, fsize, 0.5f, 15.0f);
1902 kmeter_init(&channel_ptr->kmeter_right, sr, fsize, 0.5f, 15.0f);
1904 channel_ptr->peak_left = 0.0;
1905 channel_ptr->peak_right = 0.0;
1906 channel_ptr->peak_frames = 0;
1908 channel_ptr->tmp_mixed_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1909 channel_ptr->tmp_mixed_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1910 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1911 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1912 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1913 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1915 channel_ptr->NaN_detected = false;
1917 channel_ptr->midi_cc_volume_index = -1;
1918 channel_ptr->midi_cc_balance_index = -1;
1919 channel_ptr->midi_cc_mute_index = -1;
1920 channel_ptr->midi_cc_solo_index = -1;
1921 channel_ptr->midi_cc_volume_picked_up = false;
1922 channel_ptr->midi_cc_balance_picked_up = false;
1924 channel_ptr->midi_change_callback = NULL;
1925 channel_ptr->midi_change_callback_data = NULL;
1927 channel_ptr->midi_scale = NULL;
1929 output_channel_ptr->soloed_channels = NULL;
1930 output_channel_ptr->muted_channels = NULL;
1931 output_channel_ptr->prefader_channels = NULL;
1932 output_channel_ptr->system = system;
1933 output_channel_ptr->prefader = false;
1935 free(port_name);
1936 return output_channel_ptr;
1938 fail_unregister_left_channel:
1939 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1941 fail_free_port_name:
1942 free(port_name);
1944 fail_free_channel_name:
1945 free(channel_ptr->name);
1947 fail_free_channel:
1948 free(channel_ptr);
1949 channel_ptr = NULL;
1951 fail:
1952 return NULL;
1955 jack_mixer_output_channel_t
1956 add_output_channel(
1957 jack_mixer_t mixer,
1958 const char * channel_name,
1959 bool stereo,
1960 bool system)
1962 struct output_channel *output_channel_ptr;
1963 struct channel *channel_ptr;
1965 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1966 if (output_channel_ptr == NULL) {
1967 return NULL;
1969 channel_ptr = (struct channel*)output_channel_ptr;
1971 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1972 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1974 return output_channel_ptr;
1977 void
1978 remove_channels(
1979 jack_mixer_t mixer)
1981 GSList *list_ptr;
1982 for (list_ptr = mixer_ctx_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
1984 struct channel *input_channel_ptr = list_ptr->data;
1985 remove_channel((jack_mixer_channel_t)input_channel_ptr);
1989 #define km ((struct kmeter *) kmeter)
1991 void
1992 kmeter_init(
1993 jack_mixer_kmeter_t kmeter,
1994 int sr,
1995 int fsize,
1996 float hold,
1997 float fall)
1999 km->_z1 = 0;
2000 km->_z2 = 0;
2001 km->_rms = 0;
2002 km->_dpk = 0;
2003 km->_cnt = 0;
2004 km->_flag = false;
2006 float t;
2007 km->_omega = 9.72f / sr;
2008 t = (float) fsize / sr;
2009 km->_hold = (int)(hold / t + 0.5f);
2010 km->_fall = powf(10.0f, -0.05f * fall * t);
2013 void
2014 kmeter_process(
2015 jack_mixer_kmeter_t kmeter,
2016 jack_default_audio_sample_t *p,
2017 int start,
2018 int end)
2020 int i;
2021 jack_default_audio_sample_t s, t, z1, z2;
2023 if (km->_flag) {
2024 km->_rms = 0;
2025 km->_flag = 0;
2028 z1 = km->_z1;
2029 z2 = km->_z2;
2031 t = 0;
2033 for (i = start; i < end; i++) {
2034 s = p[i];
2035 s *= s;
2036 if (t < s) t = s;
2037 z1 += km->_omega * (s - z1);
2038 z2 += km->_omega * (z1 - z2);
2040 t = sqrtf(t);
2042 km->_z1 = z1 + 1e-20f;
2043 km->_z2 = z2 + 1e-20f;
2045 s = sqrtf(2 * z2);
2046 if (s > km->_rms) km->_rms = s;
2048 if (t > km->_dpk) {
2049 km->_dpk = t;
2050 km->_cnt = km->_hold;
2052 else if (km->_cnt) {
2053 km->_cnt--;
2055 else {
2056 km->_dpk *= km->_fall;
2057 km->_dpk += 1e-10f;
2061 void
2062 remove_output_channel(
2063 jack_mixer_output_channel_t output_channel)
2065 struct output_channel *output_channel_ptr = output_channel;
2066 struct channel *channel_ptr = output_channel;
2068 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
2069 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
2070 free(channel_ptr->name);
2072 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
2073 if (channel_ptr->stereo)
2075 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
2078 if (channel_ptr->midi_cc_volume_index != -1)
2080 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
2081 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
2084 if (channel_ptr->midi_cc_balance_index != -1)
2086 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
2087 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
2090 if (channel_ptr->midi_cc_mute_index != -1)
2092 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
2093 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
2096 if (channel_ptr->midi_cc_solo_index != -1)
2098 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
2099 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
2102 g_slist_free(output_channel_ptr->soloed_channels);
2103 g_slist_free(output_channel_ptr->muted_channels);
2104 g_slist_free(output_channel_ptr->prefader_channels);
2106 free(channel_ptr->tmp_mixed_frames_left);
2107 free(channel_ptr->tmp_mixed_frames_right);
2108 free(channel_ptr->frames_left);
2109 free(channel_ptr->frames_right);
2110 free(channel_ptr->prefader_frames_left);
2111 free(channel_ptr->prefader_frames_right);
2113 free(channel_ptr);
2116 void
2117 output_channel_set_solo(
2118 jack_mixer_output_channel_t output_channel,
2119 jack_mixer_channel_t channel,
2120 bool solo_value)
2122 struct output_channel *output_channel_ptr = output_channel;
2124 if (solo_value) {
2125 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
2126 return;
2127 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
2129 else {
2130 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
2131 return;
2132 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
2136 void
2137 output_channel_set_muted(
2138 jack_mixer_output_channel_t output_channel,
2139 jack_mixer_channel_t channel,
2140 bool muted_value)
2142 struct output_channel *output_channel_ptr = output_channel;
2144 if (muted_value) {
2145 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
2146 return;
2147 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
2149 else {
2150 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
2151 return;
2152 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
2156 bool
2157 output_channel_is_muted(
2158 jack_mixer_output_channel_t output_channel,
2159 jack_mixer_channel_t channel)
2161 struct output_channel *output_channel_ptr = output_channel;
2163 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
2164 return true;
2165 return false;
2168 bool
2169 output_channel_is_solo(
2170 jack_mixer_output_channel_t output_channel,
2171 jack_mixer_channel_t channel)
2173 struct output_channel *output_channel_ptr = output_channel;
2175 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
2176 return true;
2177 return false;
2180 void
2181 output_channel_set_prefader(
2182 jack_mixer_output_channel_t output_channel,
2183 bool pfl_value)
2185 struct output_channel *output_channel_ptr = output_channel;
2186 output_channel_ptr->prefader = pfl_value;
2189 bool
2190 output_channel_is_prefader(
2191 jack_mixer_output_channel_t output_channel)
2193 struct output_channel *output_channel_ptr = output_channel;
2194 return output_channel_ptr->prefader;
2197 void
2198 output_channel_set_in_prefader(
2199 jack_mixer_output_channel_t output_channel,
2200 jack_mixer_channel_t channel,
2201 bool prefader_value)
2203 struct output_channel *output_channel_ptr = output_channel;
2205 if (prefader_value) {
2206 if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
2207 return;
2208 output_channel_ptr->prefader_channels = g_slist_prepend(output_channel_ptr->prefader_channels, channel);
2210 else {
2211 if (g_slist_find(output_channel_ptr->prefader_channels, channel) == NULL)
2212 return;
2213 output_channel_ptr->prefader_channels = g_slist_remove(output_channel_ptr->prefader_channels, channel);
2217 bool
2218 output_channel_is_in_prefader(
2219 jack_mixer_output_channel_t output_channel,
2220 jack_mixer_channel_t channel)
2222 struct output_channel *output_channel_ptr = output_channel;
2224 if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
2225 return true;
2226 return false;