release: 13
[jack_mixer.git] / jack_mixer.c
blob13145080bb3edf952100189dfa885917d46f39ac
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 <math.h>
31 #include <jack/jack.h>
32 #if defined(HAVE_JACK_MIDI)
33 #include <jack/midiport.h>
34 #endif
35 #include <assert.h>
36 #include <pthread.h>
38 #include <glib.h>
40 #include "jack_mixer.h"
41 //#define LOG_LEVEL LOG_LEVEL_DEBUG
42 #include "log.h"
44 #include "jack_compat.h"
46 #define VOLUME_TRANSITION_SECONDS 0.01
48 #define PEAK_FRAMES_CHUNK 4800
50 // we don't know how much to allocate, but we don't want to wait with
51 // allocating until we're in the process() callback, so we just take a
52 // fairly big chunk: 4 periods per buffer, 4096 samples per period.
53 // (not sure if the '*4' is needed)
54 #define MAX_BLOCK_SIZE (4 * 4096)
56 #define FLOAT_EXISTS(x) (!((x) - (x)))
58 struct channel
60 struct jack_mixer * mixer_ptr;
61 char * name;
62 bool stereo;
63 bool out_mute;
64 float volume_transition_seconds;
65 unsigned int num_volume_transition_steps;
66 float volume;
67 jack_nframes_t volume_idx;
68 float volume_new;
69 float balance;
70 jack_nframes_t balance_idx;
71 float balance_new;
72 float volume_left;
73 float volume_left_new;
74 float volume_right;
75 float volume_right_new;
76 float meter_left;
77 float meter_right;
78 float abspeak;
79 jack_port_t * port_left;
80 jack_port_t * port_right;
82 jack_nframes_t peak_frames;
83 float peak_left;
84 float peak_right;
86 jack_default_audio_sample_t * tmp_mixed_frames_left;
87 jack_default_audio_sample_t * tmp_mixed_frames_right;
88 jack_default_audio_sample_t * frames_left;
89 jack_default_audio_sample_t * frames_right;
90 jack_default_audio_sample_t * prefader_frames_left;
91 jack_default_audio_sample_t * prefader_frames_right;
93 bool NaN_detected;
95 int midi_cc_volume_index;
96 int midi_cc_balance_index;
97 int midi_cc_mute_index;
98 int midi_cc_solo_index;
99 bool midi_cc_volume_picked_up;
100 bool midi_cc_balance_picked_up;
102 jack_default_audio_sample_t * left_buffer_ptr;
103 jack_default_audio_sample_t * right_buffer_ptr;
105 bool midi_in_got_events;
106 void (*midi_change_callback) (void*);
107 void *midi_change_callback_data;
108 bool midi_out_has_events;
110 jack_mixer_scale_t midi_scale;
113 struct output_channel {
114 struct channel channel;
115 GSList *soloed_channels;
116 GSList *muted_channels;
117 GSList *prefader_channels;
119 bool system; /* system channel, without any associated UI */
120 bool prefader;
123 struct jack_mixer
125 pthread_mutex_t mutex;
126 jack_client_t * jack_client;
127 GSList *input_channels_list;
128 GSList *output_channels_list;
129 GSList *soloed_channels;
131 jack_port_t * port_midi_in;
132 jack_port_t * port_midi_out;
133 int last_midi_channel;
134 enum midi_behavior_mode midi_behavior;
136 struct channel* midi_cc_map[128];
139 static jack_mixer_output_channel_t create_output_channel(
140 jack_mixer_t mixer,
141 const char * channel_name,
142 bool stereo,
143 bool system);
145 static inline void
146 update_channel_buffers(
147 struct channel * channel_ptr,
148 jack_nframes_t nframes);
151 float
152 value_to_db(
153 float value)
155 if (value <= 0)
157 return -INFINITY;
160 return 20.0 * log10f(value);
163 float
164 db_to_value(
165 float db)
167 return powf(10.0, db/20.0);
170 #define channel_ptr ((struct channel *)channel)
172 const char*
173 channel_get_name(
174 jack_mixer_channel_t channel)
176 return channel_ptr->name;
179 void
180 channel_rename(
181 jack_mixer_channel_t channel,
182 const char * name)
184 char * new_name;
185 size_t channel_name_size;
186 char * port_name;
187 int ret;
189 new_name = strdup(name);
190 if (new_name == NULL)
192 return;
195 if (channel_ptr->name)
197 free(channel_ptr->name);
200 channel_ptr->name = new_name;
202 if (channel_ptr->stereo)
204 channel_name_size = strlen(name);
205 port_name = malloc(channel_name_size + 3);
206 memcpy(port_name, name, channel_name_size);
208 port_name[channel_name_size] = ' ';
209 port_name[channel_name_size+1] = 'L';
210 port_name[channel_name_size+2] = 0;
212 ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left, port_name);
213 if (ret != 0)
215 /* what could we do here? */
218 port_name[channel_name_size+1] = 'R';
220 ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right, port_name);
221 if (ret != 0)
223 /* what could we do here? */
226 free(port_name);
228 else
230 ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left, name);
231 if (ret != 0)
233 /* what could we do here? */
238 bool
239 channel_is_stereo(
240 jack_mixer_channel_t channel)
242 return channel_ptr->stereo;
246 channel_get_balance_midi_cc(
247 jack_mixer_channel_t channel)
249 return channel_ptr->midi_cc_balance_index;
252 static void
253 channel_unset_midi_cc_map(
254 jack_mixer_channel_t channel,
255 int new_cc) {
256 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_volume_index == new_cc) {
257 channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_volume_index = -1;
258 } else if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_balance_index == new_cc) {
259 channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_balance_index = -1;
260 } else if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_mute_index == new_cc) {
261 channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_mute_index = -1;
262 } else if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_solo_index == new_cc) {
263 channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_solo_index = -1;
267 unsigned int
268 channel_set_balance_midi_cc(
269 jack_mixer_channel_t channel,
270 int new_cc)
272 if (new_cc < 0 || new_cc > 127) {
273 return 2; /* error: outside limit CC */
275 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
276 channel_unset_midi_cc_map(channel, new_cc);
278 if (channel_ptr->midi_cc_balance_index != -1) {
279 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
281 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
282 channel_ptr->midi_cc_balance_index = new_cc;
283 return 0;
287 channel_get_volume_midi_cc(
288 jack_mixer_channel_t channel)
290 return channel_ptr->midi_cc_volume_index;
293 unsigned int
294 channel_set_volume_midi_cc(
295 jack_mixer_channel_t channel, int new_cc)
297 if (new_cc< 0 || new_cc > 127) {
298 return 2; /* error: outside limit CC */
300 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
301 channel_unset_midi_cc_map(channel, new_cc);
303 if (channel_ptr->midi_cc_volume_index != -1) {
304 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
306 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
307 channel_ptr->midi_cc_volume_index = new_cc;
308 return 0;
312 channel_get_mute_midi_cc(
313 jack_mixer_channel_t channel)
315 return channel_ptr->midi_cc_mute_index;
318 unsigned int
319 channel_set_mute_midi_cc(
320 jack_mixer_channel_t channel,
321 int new_cc)
323 if (new_cc < 0 || new_cc > 127) {
324 return 2; /* error: outside limit CC */
326 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
327 channel_unset_midi_cc_map(channel, new_cc);
330 if (channel_ptr->midi_cc_mute_index != -1) {
331 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
333 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
334 channel_ptr->midi_cc_mute_index = new_cc;
335 return 0;
339 channel_get_solo_midi_cc(
340 jack_mixer_channel_t channel)
342 return channel_ptr->midi_cc_solo_index;
345 void channel_set_midi_cc_volume_picked_up(jack_mixer_channel_t channel, bool status)
347 LOG_DEBUG("Setting channel %s volume picked up to %d", channel_ptr->name, status);
348 channel_ptr->midi_cc_volume_picked_up = status;
351 void channel_set_midi_cc_balance_picked_up(jack_mixer_channel_t channel, bool status)
353 LOG_DEBUG("Setting channel %s balance picked up to %d", channel_ptr->name, status);
354 channel_ptr->midi_cc_balance_picked_up = status;
357 unsigned int
358 channel_set_solo_midi_cc(
359 jack_mixer_channel_t channel,
360 int new_cc)
362 if (new_cc < 0 || new_cc > 127) {
363 return 2; /* error: outside limit CC */
365 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
366 channel_unset_midi_cc_map(channel, new_cc);
368 if (channel_ptr->midi_cc_solo_index != -1) {
369 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
371 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
372 channel_ptr->midi_cc_solo_index = new_cc;
373 return 0;
376 void
377 channel_autoset_volume_midi_cc(
378 jack_mixer_channel_t channel)
380 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
382 for (int i = 11 ; i < 128 ; i++)
384 if (mixer_ptr->midi_cc_map[i] == NULL)
386 mixer_ptr->midi_cc_map[i] = channel_ptr;
387 channel_ptr->midi_cc_volume_index = i;
389 LOG_DEBUG("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
391 break;
396 void
397 channel_autoset_balance_midi_cc(
398 jack_mixer_channel_t channel)
400 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
402 for (int i = 11; i < 128 ; i++)
404 if (mixer_ptr->midi_cc_map[i] == NULL)
406 mixer_ptr->midi_cc_map[i] = channel_ptr;
407 channel_ptr->midi_cc_balance_index = i;
409 LOG_DEBUG("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
411 break;
416 void
417 channel_autoset_mute_midi_cc(
418 jack_mixer_channel_t channel)
420 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
422 for (int i = 11; i < 128 ; i++)
424 if (mixer_ptr->midi_cc_map[i] == NULL)
426 mixer_ptr->midi_cc_map[i] = channel_ptr;
427 channel_ptr->midi_cc_mute_index = i;
429 LOG_DEBUG("New channel \"%s\" mute mapped to CC#%i", channel_ptr->name, i);
431 break;
436 void
437 channel_autoset_solo_midi_cc(
438 jack_mixer_channel_t channel)
440 struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
442 for (int i = 11; i < 128 ; i++)
444 if (mixer_ptr->midi_cc_map[i] == NULL)
446 mixer_ptr->midi_cc_map[i] = channel_ptr;
447 channel_ptr->midi_cc_solo_index = i;
449 LOG_DEBUG("New channel \"%s\" solo mapped to CC#%i", channel_ptr->name, i);
451 break;
456 void
457 remove_channel(
458 jack_mixer_channel_t channel)
460 GSList *list_ptr;
461 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
462 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
463 free(channel_ptr->name);
465 /* remove references to input channel from all output channels */
466 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
468 struct output_channel *output_channel_ptr = list_ptr->data;
469 output_channel_set_solo(output_channel_ptr, channel, false);
470 output_channel_set_muted(output_channel_ptr, channel, false);
473 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
474 if (channel_ptr->stereo)
476 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
479 if (channel_ptr->midi_cc_volume_index != -1)
481 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
482 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
485 if (channel_ptr->midi_cc_balance_index != -1)
487 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
488 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
491 if (channel_ptr->midi_cc_mute_index != -1)
493 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
494 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
496 if (channel_ptr->midi_cc_solo_index != -1)
498 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
499 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
502 free(channel_ptr->frames_left);
503 free(channel_ptr->frames_right);
504 free(channel_ptr->prefader_frames_left);
505 free(channel_ptr->prefader_frames_right);
507 free(channel_ptr);
510 void
511 channel_stereo_meter_read(
512 jack_mixer_channel_t channel,
513 double * left_ptr,
514 double * right_ptr)
516 assert(channel_ptr);
517 *left_ptr = value_to_db(channel_ptr->meter_left);
518 *right_ptr = value_to_db(channel_ptr->meter_right);
521 double interpolate(double start, double end, int step, int steps) {
522 double ret;
523 double frac = 0.01;
524 LOG_DEBUG("%f -> %f, %d", start, end, step);
525 if (start <= 0) {
526 if (step <= frac * steps) {
527 ret = frac * end * step / steps;
528 } else {
529 ret = db_to_value(value_to_db(frac * end) + (value_to_db(end) - value_to_db(frac * end)) * step / steps);;
531 } else if (end <= 0) {
532 if (step >= (1 - frac) * steps) {
533 ret = frac * start - frac * start * step / steps;
534 } else {
535 ret = db_to_value(value_to_db(start) + (value_to_db(frac * start) - value_to_db(start)) * step / steps);
537 } else {
538 ret = db_to_value(value_to_db(start) + (value_to_db(end) - value_to_db(start)) *step /steps);
540 LOG_DEBUG("interpolate: %f", ret);
541 return ret;
544 void
545 channel_mono_meter_read(
546 jack_mixer_channel_t channel,
547 double * mono_ptr)
549 *mono_ptr = value_to_db(channel_ptr->meter_left);
552 void
553 channel_volume_write(
554 jack_mixer_channel_t channel,
555 double volume)
557 assert(channel_ptr);
558 /*If changing volume and find we're in the middle of a previous transition,
559 *then set current volume to place in transition to avoid a jump.*/
560 if (channel_ptr->volume_new != channel_ptr->volume) {
561 channel_ptr->volume = interpolate(channel_ptr->volume, channel_ptr->volume_new, channel_ptr->volume_idx,
562 channel_ptr->num_volume_transition_steps);
564 channel_ptr->volume_idx = 0;
565 channel_ptr->volume_new = db_to_value(volume);
566 channel_ptr->midi_out_has_events = true;
569 double
570 channel_volume_read(
571 jack_mixer_channel_t channel)
573 assert(channel_ptr);
574 return value_to_db(channel_ptr->volume_new);
577 void
578 channels_volumes_read(jack_mixer_t mixer_ptr)
580 GSList *node_ptr;
581 struct channel *pChannel;
582 struct jack_mixer * pMixer = (struct jack_mixer *)mixer_ptr;
584 for (node_ptr = pMixer->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
586 pChannel = (struct channel *)node_ptr->data;
587 double vol = channel_volume_read( (jack_mixer_channel_t)pChannel);
588 printf("%s : volume is %f dbFS for mixer channel: %s\n", jack_get_client_name(pMixer->jack_client), vol, pChannel->name);
592 void
593 channel_balance_write(
594 jack_mixer_channel_t channel,
595 double balance)
597 assert(channel_ptr);
598 if (channel_ptr->balance != channel_ptr->balance_new) {
599 channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
600 (channel_ptr->balance_new - channel_ptr->balance) /
601 channel_ptr->num_volume_transition_steps;
603 channel_ptr->balance_idx = 0;
604 channel_ptr->balance_new = balance;
607 double
608 channel_balance_read(
609 jack_mixer_channel_t channel)
611 assert(channel_ptr);
612 return channel_ptr->balance_new;
615 double
616 channel_abspeak_read(
617 jack_mixer_channel_t channel)
619 assert(channel_ptr);
620 if (channel_ptr->NaN_detected)
622 return sqrt(-1);
624 else
626 return value_to_db(channel_ptr->abspeak);
630 void
631 channel_abspeak_reset(
632 jack_mixer_channel_t channel)
634 channel_ptr->abspeak = 0;
635 channel_ptr->NaN_detected = false;
638 void
639 channel_out_mute(
640 jack_mixer_channel_t channel)
642 channel_ptr->out_mute = true;
645 void
646 channel_out_unmute(
647 jack_mixer_channel_t channel)
649 channel_ptr->out_mute = false;
652 bool
653 channel_is_out_muted(
654 jack_mixer_channel_t channel)
656 return channel_ptr->out_mute;
659 void
660 channel_solo(
661 jack_mixer_channel_t channel)
663 if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) != NULL)
664 return;
665 channel_ptr->mixer_ptr->soloed_channels = g_slist_prepend(channel_ptr->mixer_ptr->soloed_channels, channel);
668 void
669 channel_unsolo(
670 jack_mixer_channel_t channel)
672 if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) == NULL)
673 return;
674 channel_ptr->mixer_ptr->soloed_channels = g_slist_remove(channel_ptr->mixer_ptr->soloed_channels, channel);
677 bool
678 channel_is_soloed(
679 jack_mixer_channel_t channel)
681 if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel))
682 return true;
683 return false;
686 void
687 channel_set_midi_scale(
688 jack_mixer_channel_t channel,
689 jack_mixer_scale_t scale)
691 channel_ptr->midi_scale = scale;
694 void
695 channel_set_midi_change_callback(
696 jack_mixer_channel_t channel,
697 void (*midi_change_callback) (void*),
698 void *user_data)
700 channel_ptr->midi_change_callback = midi_change_callback;
701 channel_ptr->midi_change_callback_data = user_data;
704 bool
705 channel_get_midi_in_got_events(
706 jack_mixer_channel_t channel)
708 bool t = channel_ptr->midi_in_got_events;
709 channel_ptr->midi_in_got_events = false;
710 return t;
713 #undef channel_ptr
715 /* process input channels and mix them into main mix */
716 static inline void
717 mix_one(
718 struct output_channel *output_mix_channel,
719 GSList *channels_list,
720 jack_nframes_t start, /* index of first sample to process */
721 jack_nframes_t end) /* index of sample to stop processing before */
723 jack_nframes_t i;
724 GSList *node_ptr;
725 struct channel * channel_ptr;
726 jack_default_audio_sample_t frame_left;
727 jack_default_audio_sample_t frame_right;
728 struct channel *mix_channel = (struct channel*)output_mix_channel;
730 for (i = start; i < end; i++)
732 mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i] = 0.0;
733 if (mix_channel->stereo)
734 mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i] = 0.0;
737 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
739 channel_ptr = node_ptr->data;
741 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL || channel_ptr->out_mute) {
742 /* skip muted channels */
743 continue;
746 if ((!channel_ptr->mixer_ptr->soloed_channels && !output_mix_channel->soloed_channels) ||
747 (channel_ptr->mixer_ptr->soloed_channels &&
748 g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel_ptr) != NULL) ||
749 (output_mix_channel->soloed_channels &&
750 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) != NULL)) {
752 for (i = start ; i < end ; i++)
754 if (! output_mix_channel->prefader &&
755 g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL) {
756 frame_left = channel_ptr->frames_left[i-start];
757 } else {
758 frame_left = channel_ptr->prefader_frames_left[i-start];
760 if (frame_left == NAN)
761 break;
762 mix_channel->tmp_mixed_frames_left[i] += frame_left;
763 if (mix_channel->stereo)
765 if (! output_mix_channel->prefader &&
766 g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL) {
767 frame_right = channel_ptr->frames_right[i-start];
768 } else {
769 frame_right = channel_ptr->prefader_frames_right[i-start];
771 if (frame_right == NAN)
772 break;
773 mix_channel->tmp_mixed_frames_right[i] += frame_right;
779 /* process main mix channel */
780 unsigned int steps = mix_channel->num_volume_transition_steps;
781 for (i = start ; i < end ; i++)
783 if (! output_mix_channel->prefader) {
784 float volume = mix_channel->volume;
785 float volume_new = mix_channel->volume_new;
786 float vol = volume;
787 float balance = mix_channel->balance;
788 float balance_new = mix_channel->balance_new;
789 float bal = balance;
790 if (volume != volume_new) {
791 vol = interpolate(volume, volume_new, mix_channel->volume_idx, steps);
793 if (balance != balance_new) {
794 bal = mix_channel->balance_idx * (balance_new - balance) / steps + balance;
797 float vol_l;
798 float vol_r;
799 if (mix_channel->stereo) {
800 if (bal > 0) {
801 vol_l = vol * (1 - bal);
802 vol_r = vol;
803 } else {
804 vol_l = vol;
805 vol_r = vol * (1 + bal);
807 } else {
808 vol_l = vol * (1 - bal);
809 vol_r = vol * (1 + bal);
811 mix_channel->tmp_mixed_frames_left[i] *= vol_l;
812 mix_channel->tmp_mixed_frames_right[i] *= vol_r;
815 frame_left = fabsf(mix_channel->tmp_mixed_frames_left[i]);
816 if (mix_channel->peak_left < frame_left)
818 mix_channel->peak_left = frame_left;
820 if (frame_left > mix_channel->abspeak)
822 mix_channel->abspeak = frame_left;
826 if (mix_channel->stereo)
828 frame_right = fabsf(mix_channel->tmp_mixed_frames_right[i]);
829 if (mix_channel->peak_right < frame_right)
831 mix_channel->peak_right = frame_right;
833 if (frame_right > mix_channel->abspeak)
835 mix_channel->abspeak = frame_right;
840 mix_channel->peak_frames++;
841 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
843 mix_channel->meter_left = mix_channel->peak_left;
844 mix_channel->peak_left = 0.0;
846 if (mix_channel->stereo)
848 mix_channel->meter_right = mix_channel->peak_right;
849 mix_channel->peak_right = 0.0;
852 mix_channel->peak_frames = 0;
854 mix_channel->volume_idx++;
855 if ((mix_channel->volume != mix_channel->volume_new) && (mix_channel->volume_idx == steps)) {
856 mix_channel->volume = mix_channel->volume_new;
857 mix_channel->volume_idx = 0;
859 mix_channel->balance_idx++;
860 if ((mix_channel->balance != mix_channel->balance_new) && (mix_channel->balance_idx == steps)) {
861 mix_channel->balance = mix_channel->balance_new;
862 mix_channel->balance_idx = 0;
865 if (!mix_channel->out_mute) {
866 mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i];
867 if (mix_channel->stereo)
868 mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i];
873 static inline void
874 calc_channel_frames(
875 struct channel *channel_ptr,
876 jack_nframes_t start,
877 jack_nframes_t end)
879 jack_nframes_t i;
880 jack_default_audio_sample_t frame_left = 0.0f;
881 jack_default_audio_sample_t frame_right = 0.0f;
882 unsigned int steps = channel_ptr->num_volume_transition_steps;
883 for (i = start ; i < end ; i++)
885 if (i-start >= MAX_BLOCK_SIZE)
887 fprintf(stderr, "i-start too high: %d - %d\n", i, start);
889 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
890 if (channel_ptr->stereo)
891 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
893 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
895 channel_ptr->NaN_detected = true;
896 channel_ptr->frames_left[i-start] = NAN;
897 break;
899 float volume = channel_ptr->volume;
900 float volume_new = channel_ptr->volume_new;
901 float vol = volume;
902 float balance = channel_ptr->balance;
903 float balance_new = channel_ptr->balance_new;
904 float bal = balance;
905 if (channel_ptr->volume != channel_ptr->volume_new) {
906 vol = interpolate(volume, volume_new, channel_ptr->volume_idx, steps);
908 if (channel_ptr->balance != channel_ptr->balance_new) {
909 bal = channel_ptr->balance_idx * (balance_new - balance) / steps + balance;
911 float vol_l;
912 float vol_r;
913 if (channel_ptr->stereo) {
914 if (bal > 0) {
915 vol_l = vol * (1 - bal);
916 vol_r = vol;
917 } else {
918 vol_l = vol;
919 vol_r = vol * (1 + bal);
921 } else {
922 vol_l = vol * (1 - bal);
923 vol_r = vol * (1 + bal);
925 frame_left = channel_ptr->left_buffer_ptr[i] * vol_l;
926 if (channel_ptr->stereo)
928 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
930 channel_ptr->NaN_detected = true;
931 channel_ptr->frames_right[i-start] = NAN;
932 break;
935 frame_right = channel_ptr->right_buffer_ptr[i] * vol_r;
938 channel_ptr->frames_left[i-start] = frame_left;
939 channel_ptr->frames_right[i-start] = frame_right;
941 if (channel_ptr->stereo)
943 frame_left = fabsf(frame_left);
944 frame_right = fabsf(frame_right);
946 if (channel_ptr->peak_left < frame_left)
948 channel_ptr->peak_left = frame_left;
950 if (frame_left > channel_ptr->abspeak)
952 channel_ptr->abspeak = frame_left;
956 if (channel_ptr->peak_right < frame_right)
958 channel_ptr->peak_right = frame_right;
960 if (frame_right > channel_ptr->abspeak)
962 channel_ptr->abspeak = frame_right;
966 else
968 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
970 if (channel_ptr->peak_left < frame_left)
972 channel_ptr->peak_left = frame_left;
974 if (frame_left > channel_ptr->abspeak)
976 channel_ptr->abspeak = frame_left;
981 channel_ptr->peak_frames++;
982 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
984 channel_ptr->meter_left = channel_ptr->peak_left;
985 channel_ptr->peak_left = 0.0;
987 if (channel_ptr->stereo)
989 channel_ptr->meter_right = channel_ptr->peak_right;
990 channel_ptr->peak_right = 0.0;
993 channel_ptr->peak_frames = 0;
995 channel_ptr->volume_idx++;
996 if ((channel_ptr->volume != channel_ptr->volume_new) &&
997 (channel_ptr->volume_idx == steps)) {
998 channel_ptr->volume = channel_ptr->volume_new;
999 channel_ptr->volume_idx = 0;
1001 channel_ptr->balance_idx++;
1002 if ((channel_ptr->balance != channel_ptr->balance_new) &&
1003 (channel_ptr->balance_idx >= steps)) {
1004 channel_ptr->balance = channel_ptr->balance_new;
1005 channel_ptr->balance_idx = 0;
1010 static inline void
1011 mix(
1012 struct jack_mixer * mixer_ptr,
1013 jack_nframes_t start, /* index of first sample to process */
1014 jack_nframes_t end) /* index of sample to stop processing before */
1016 GSList *node_ptr;
1017 struct output_channel * output_channel_ptr;
1018 struct channel *channel_ptr;
1020 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1022 channel_ptr = (struct channel*)node_ptr->data;
1023 calc_channel_frames(channel_ptr, start, end);
1026 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1028 output_channel_ptr = node_ptr->data;
1029 channel_ptr = (struct channel*)output_channel_ptr;
1031 if (output_channel_ptr->system)
1033 /* Don't bother mixing the channels if we are not connected */
1034 if (channel_ptr->stereo)
1036 if (jack_port_connected(channel_ptr->port_left) == 0 &&
1037 jack_port_connected(channel_ptr->port_right) == 0)
1038 continue;
1039 } else {
1040 if (jack_port_connected(channel_ptr->port_left) == 0)
1041 continue;
1045 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
1049 static inline void
1050 update_channel_buffers(
1051 struct channel * channel_ptr,
1052 jack_nframes_t nframes)
1054 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
1056 if (channel_ptr->stereo)
1058 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
1062 #define mixer_ptr ((struct jack_mixer *)context)
1064 static int
1065 process(
1066 jack_nframes_t nframes,
1067 void * context)
1069 GSList *node_ptr;
1070 struct channel * channel_ptr;
1071 #if defined(HAVE_JACK_MIDI)
1072 jack_nframes_t i;
1073 jack_nframes_t event_count;
1074 jack_midi_event_t in_event;
1075 unsigned char* midi_out_buffer;
1076 void * midi_buffer;
1077 signed char byte;
1078 unsigned int cc_channel_index;
1079 #endif
1081 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1083 channel_ptr = node_ptr->data;
1084 update_channel_buffers(channel_ptr, nframes);
1087 // Fill output buffers with the input
1088 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1090 channel_ptr = node_ptr->data;
1091 update_channel_buffers(channel_ptr, nframes);
1094 #if defined(HAVE_JACK_MIDI)
1095 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
1096 event_count = jack_midi_get_event_count(midi_buffer);
1098 for (i = 0 ; i < event_count; i++)
1100 jack_midi_event_get(&in_event, midi_buffer, i);
1102 if (in_event.size != 3 ||
1103 (in_event.buffer[0] & 0xF0) != 0xB0 ||
1104 in_event.buffer[1] > 127 ||
1105 in_event.buffer[2] > 127)
1107 continue;
1110 assert(in_event.time < nframes);
1112 LOG_DEBUG(
1113 "%u: CC#%u -> %u",
1114 (unsigned int)(in_event.buffer[0]),
1115 (unsigned int)in_event.buffer[1],
1116 (unsigned int)in_event.buffer[2]);
1118 mixer_ptr->last_midi_channel = (int)in_event.buffer[1];
1119 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
1121 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
1122 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
1124 if (channel_ptr->midi_cc_balance_index == (char)in_event.buffer[1])
1126 byte = in_event.buffer[2];
1127 if (byte == 0)
1129 byte = 1;
1131 byte -= 64;
1132 int current_midi = (int)63 * channel_ptr->balance + 64;
1133 current_midi = current_midi == 1 ? 0 : current_midi;
1134 if (mixer_ptr->midi_behavior == Pick_Up &&
1135 !channel_ptr->midi_cc_balance_picked_up) {
1136 if (in_event.buffer[2] == current_midi) {
1137 channel_set_midi_cc_balance_picked_up(channel_ptr, true);
1140 if ((mixer_ptr->midi_behavior == Pick_Up &&
1141 channel_ptr->midi_cc_balance_picked_up) ||
1142 mixer_ptr->midi_behavior == Jump_To_Value) {
1143 if (channel_ptr->balance != channel_ptr->balance_new) {
1144 channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
1145 (channel_ptr->balance_new - channel_ptr->balance) /
1146 channel_ptr->num_volume_transition_steps;
1148 channel_ptr->balance_idx = 0;
1149 channel_ptr->balance_new = (float)byte / 63;
1150 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance_new);
1153 else if (channel_ptr->midi_cc_volume_index == in_event.buffer[1])
1155 int current_midi = (int)(127 *
1156 scale_db_to_scale(channel_ptr->midi_scale,
1157 value_to_db(channel_ptr->volume)));
1158 if (mixer_ptr->midi_behavior == Pick_Up &&
1159 !channel_ptr->midi_cc_volume_picked_up ) {
1160 if (in_event.buffer[2] == current_midi) {
1161 channel_set_midi_cc_volume_picked_up(channel_ptr, true);
1164 if ((mixer_ptr->midi_behavior == Pick_Up &&
1165 channel_ptr->midi_cc_volume_picked_up) ||
1166 mixer_ptr->midi_behavior == Jump_To_Value) {
1167 if (channel_ptr->volume_new != channel_ptr->volume) {
1168 channel_ptr->volume = interpolate(channel_ptr->volume, channel_ptr->volume_new, channel_ptr->volume_idx,
1169 channel_ptr->num_volume_transition_steps);
1171 channel_ptr->volume_idx = 0;
1172 channel_ptr->volume_new = db_to_value(scale_scale_to_db(channel_ptr->midi_scale,
1173 (double)in_event.buffer[2] / 127));
1174 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume_new);
1177 else if (channel_ptr->midi_cc_mute_index == in_event.buffer[1])
1179 if ((unsigned int)in_event.buffer[2] == 127) {
1180 channel_ptr->out_mute = !channel_ptr->out_mute;
1182 LOG_DEBUG("\"%s\" out_mute %d", channel_ptr->name, channel_ptr->out_mute);
1184 else if (channel_ptr->midi_cc_solo_index == in_event.buffer[1])
1186 if ((unsigned int)in_event.buffer[2] == 127) {
1187 if (channel_is_soloed(channel_ptr)) {
1188 channel_unsolo(channel_ptr);
1189 } else {
1190 channel_solo(channel_ptr);
1193 LOG_DEBUG("\"%s\" solo %d", channel_ptr->name, channel_is_soloed(channel_ptr));
1195 channel_ptr->midi_in_got_events = true;
1196 if (channel_ptr->midi_change_callback)
1197 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
1203 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_out, nframes);
1204 jack_midi_clear_buffer(midi_buffer);
1206 for(i=0; i<nframes; i++)
1208 for (cc_channel_index=0; cc_channel_index<128; cc_channel_index++)
1210 channel_ptr = mixer_ptr->midi_cc_map[cc_channel_index];
1211 if (channel_ptr == NULL || channel_ptr->midi_scale == NULL)
1213 continue;
1215 if (channel_ptr->midi_out_has_events == false)
1217 continue;
1219 if (channel_ptr->midi_cc_balance_index == (int)cc_channel_index)
1221 continue;
1223 midi_out_buffer = jack_midi_event_reserve(midi_buffer, i, 3);
1224 if (midi_out_buffer == NULL)
1226 continue;
1228 midi_out_buffer[0] = 0xB0; /* control change */
1229 midi_out_buffer[1] = cc_channel_index;
1230 midi_out_buffer[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr->midi_scale, value_to_db(channel_ptr->volume_new)));
1232 LOG_DEBUG(
1233 "%u: CC#%u <- %u",
1234 (unsigned int)(midi_out_buffer[0]),
1235 (unsigned int)midi_out_buffer[1],
1236 (unsigned int)midi_out_buffer[2]);
1238 channel_ptr->midi_out_has_events = false;
1242 #endif
1244 mix(mixer_ptr, 0, nframes);
1246 return 0;
1249 #undef mixer_ptr
1251 jack_mixer_t
1252 create(
1253 const char * jack_client_name_ptr,
1254 bool stereo)
1256 (void) stereo;
1257 int ret;
1258 struct jack_mixer * mixer_ptr;
1259 int i;
1262 mixer_ptr = malloc(sizeof(struct jack_mixer));
1263 if (mixer_ptr == NULL)
1265 goto exit;
1268 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
1269 if (ret != 0)
1271 goto exit_free;
1274 mixer_ptr->input_channels_list = NULL;
1275 mixer_ptr->output_channels_list = NULL;
1277 mixer_ptr->soloed_channels = NULL;
1279 mixer_ptr->last_midi_channel = -1;
1281 mixer_ptr->midi_behavior = Jump_To_Value;
1283 for (i = 0 ; i < 128 ; i++)
1285 mixer_ptr->midi_cc_map[i] = NULL;
1288 LOG_DEBUG("Initializing JACK");
1289 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
1290 if (mixer_ptr->jack_client == NULL)
1292 LOG_ERROR("Cannot create JACK client.");
1293 LOG_NOTICE("Please make sure JACK daemon is running.");
1294 goto exit_destroy_mutex;
1297 LOG_DEBUG("JACK client created");
1299 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
1302 #if defined(HAVE_JACK_MIDI)
1303 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1304 if (mixer_ptr->port_midi_in == NULL)
1306 LOG_ERROR("Cannot create JACK MIDI in port");
1307 goto close_jack;
1310 mixer_ptr->port_midi_out = jack_port_register(mixer_ptr->jack_client, "midi out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
1311 if (mixer_ptr->port_midi_out == NULL)
1313 LOG_ERROR("Cannot create JACK MIDI out port");
1314 goto close_jack;
1317 #endif
1319 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1320 if (ret != 0)
1322 LOG_ERROR("Cannot set JACK process callback");
1323 goto close_jack;
1326 ret = jack_activate(mixer_ptr->jack_client);
1327 if (ret != 0)
1329 LOG_ERROR("Cannot activate JACK client");
1330 goto close_jack;
1333 return mixer_ptr;
1335 close_jack:
1336 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1338 exit_destroy_mutex:
1339 pthread_mutex_destroy(&mixer_ptr->mutex);
1341 exit_free:
1342 free(mixer_ptr);
1344 exit:
1345 return NULL;
1348 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1350 void
1351 destroy(
1352 jack_mixer_t mixer)
1354 LOG_DEBUG("Uninitializing JACK");
1356 assert(mixer_ctx_ptr->jack_client != NULL);
1358 jack_client_close(mixer_ctx_ptr->jack_client);
1360 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1362 free(mixer_ctx_ptr);
1366 unsigned int
1367 get_channels_count(
1368 jack_mixer_t mixer)
1370 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1373 const char*
1374 get_client_name(
1375 jack_mixer_t mixer)
1377 return jack_get_client_name(mixer_ctx_ptr->jack_client);
1381 get_last_midi_channel(
1382 jack_mixer_t mixer)
1384 return mixer_ctx_ptr->last_midi_channel;
1387 unsigned int
1388 set_last_midi_channel(
1389 jack_mixer_t mixer,
1390 int new_channel) {
1391 mixer_ctx_ptr->last_midi_channel = new_channel;
1392 return 0;
1396 get_midi_behavior_mode(
1397 jack_mixer_t mixer)
1399 return mixer_ctx_ptr->midi_behavior;
1402 unsigned int
1403 set_midi_behavior_mode(
1404 jack_mixer_t mixer,
1405 enum midi_behavior_mode mode)
1407 mixer_ctx_ptr->midi_behavior = mode;
1408 return 0;
1411 jack_mixer_channel_t
1412 add_channel(
1413 jack_mixer_t mixer,
1414 const char * channel_name,
1415 bool stereo)
1417 struct channel * channel_ptr;
1418 char * port_name = NULL;
1419 size_t channel_name_size;
1421 channel_ptr = malloc(sizeof(struct channel));
1422 if (channel_ptr == NULL)
1424 goto fail;
1427 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1429 channel_ptr->name = strdup(channel_name);
1430 if (channel_ptr->name == NULL)
1432 goto fail_free_channel;
1435 channel_name_size = strlen(channel_name);
1437 if (stereo)
1439 port_name = malloc(channel_name_size + 3);
1440 if (port_name == NULL)
1442 goto fail_free_channel_name;
1445 memcpy(port_name, channel_name, channel_name_size);
1446 port_name[channel_name_size] = ' ';
1447 port_name[channel_name_size+1] = 'L';
1448 port_name[channel_name_size+2] = 0;
1450 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1451 if (channel_ptr->port_left == NULL)
1453 goto fail_free_port_name;
1456 port_name[channel_name_size+1] = 'R';
1458 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1459 if (channel_ptr->port_right == NULL)
1461 goto fail_unregister_left_channel;
1464 else
1466 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1467 if (channel_ptr->port_left == NULL)
1469 goto fail_free_channel_name;
1473 channel_ptr->stereo = stereo;
1475 channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1476 channel_ptr->num_volume_transition_steps =
1477 channel_ptr->volume_transition_seconds *
1478 jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client) + 1;
1479 channel_ptr->volume = 0.0;
1480 channel_ptr->volume_new = 0.0;
1481 channel_ptr->balance = 0.0;
1482 channel_ptr->balance_new = 0.0;
1483 channel_ptr->meter_left = -1.0;
1484 channel_ptr->meter_right = -1.0;
1485 channel_ptr->abspeak = 0.0;
1486 channel_ptr->out_mute = false;
1488 channel_ptr->peak_left = 0.0;
1489 channel_ptr->peak_right = 0.0;
1490 channel_ptr->peak_frames = 0;
1492 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1493 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1494 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1495 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1497 channel_ptr->NaN_detected = false;
1499 channel_ptr->midi_cc_volume_index = -1;
1500 channel_ptr->midi_cc_balance_index = -1;
1501 channel_ptr->midi_cc_mute_index = -1;
1502 channel_ptr->midi_cc_solo_index = -1;
1503 channel_ptr->midi_cc_volume_picked_up = false;
1504 channel_ptr->midi_cc_balance_picked_up = false;
1506 channel_ptr->midi_change_callback = NULL;
1507 channel_ptr->midi_change_callback_data = NULL;
1508 channel_ptr->midi_out_has_events = false;
1510 channel_ptr->midi_scale = NULL;
1512 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1513 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1515 free(port_name);
1516 return channel_ptr;
1518 fail_unregister_left_channel:
1519 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1521 fail_free_port_name:
1522 free(port_name);
1524 fail_free_channel_name:
1525 free(channel_ptr->name);
1527 fail_free_channel:
1528 free(channel_ptr);
1529 channel_ptr = NULL;
1531 fail:
1532 return NULL;
1535 static jack_mixer_output_channel_t
1536 create_output_channel(
1537 jack_mixer_t mixer,
1538 const char * channel_name,
1539 bool stereo,
1540 bool system)
1542 struct channel * channel_ptr;
1543 struct output_channel * output_channel_ptr;
1544 char * port_name = NULL;
1545 size_t channel_name_size;
1547 output_channel_ptr = malloc(sizeof(struct output_channel));
1548 channel_ptr = (struct channel*)output_channel_ptr;
1549 if (channel_ptr == NULL)
1551 goto fail;
1554 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1556 channel_ptr->name = strdup(channel_name);
1557 if (channel_ptr->name == NULL)
1559 goto fail_free_channel;
1562 if (stereo)
1564 channel_name_size = strlen(channel_name);
1566 port_name = malloc(channel_name_size + 4);
1567 if (port_name == NULL)
1569 goto fail_free_channel_name;
1572 memcpy(port_name, channel_name, channel_name_size);
1573 port_name[channel_name_size] = ' ';
1574 port_name[channel_name_size+1] = 'L';
1575 port_name[channel_name_size+2] = 0;
1577 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1578 if (channel_ptr->port_left == NULL)
1580 goto fail_free_port_name;
1583 port_name[channel_name_size+1] = 'R';
1585 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1586 if (channel_ptr->port_right == NULL)
1588 goto fail_unregister_left_channel;
1591 else
1593 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1594 if (channel_ptr->port_left == NULL)
1596 goto fail_free_channel_name;
1600 channel_ptr->stereo = stereo;
1601 channel_ptr->out_mute = false;
1603 channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1604 channel_ptr->num_volume_transition_steps =
1605 channel_ptr->volume_transition_seconds *
1606 jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client) + 1;
1607 channel_ptr->volume = 0.0;
1608 channel_ptr->volume_new = 0.0;
1609 channel_ptr->balance = 0.0;
1610 channel_ptr->balance_new = 0.0;
1611 channel_ptr->meter_left = -1.0;
1612 channel_ptr->meter_right = -1.0;
1613 channel_ptr->abspeak = 0.0;
1615 channel_ptr->peak_left = 0.0;
1616 channel_ptr->peak_right = 0.0;
1617 channel_ptr->peak_frames = 0;
1619 channel_ptr->tmp_mixed_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1620 channel_ptr->tmp_mixed_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1621 channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1622 channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1623 channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1624 channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1626 channel_ptr->NaN_detected = false;
1628 channel_ptr->midi_cc_volume_index = -1;
1629 channel_ptr->midi_cc_balance_index = -1;
1630 channel_ptr->midi_cc_mute_index = -1;
1631 channel_ptr->midi_cc_solo_index = -1;
1632 channel_ptr->midi_cc_volume_picked_up = false;
1633 channel_ptr->midi_cc_balance_picked_up = false;
1635 channel_ptr->midi_change_callback = NULL;
1636 channel_ptr->midi_change_callback_data = NULL;
1638 channel_ptr->midi_scale = NULL;
1640 output_channel_ptr->soloed_channels = NULL;
1641 output_channel_ptr->muted_channels = NULL;
1642 output_channel_ptr->prefader_channels = NULL;
1643 output_channel_ptr->system = system;
1644 output_channel_ptr->prefader = false;
1646 free(port_name);
1647 return output_channel_ptr;
1649 fail_unregister_left_channel:
1650 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1652 fail_free_port_name:
1653 free(port_name);
1655 fail_free_channel_name:
1656 free(channel_ptr->name);
1658 fail_free_channel:
1659 free(channel_ptr);
1660 channel_ptr = NULL;
1662 fail:
1663 return NULL;
1666 jack_mixer_output_channel_t
1667 add_output_channel(
1668 jack_mixer_t mixer,
1669 const char * channel_name,
1670 bool stereo,
1671 bool system)
1673 struct output_channel *output_channel_ptr;
1674 struct channel *channel_ptr;
1676 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1677 if (output_channel_ptr == NULL) {
1678 return NULL;
1680 channel_ptr = (struct channel*)output_channel_ptr;
1682 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1683 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1685 return output_channel_ptr;
1688 void
1689 remove_channels(
1690 jack_mixer_t mixer)
1692 GSList *list_ptr;
1693 for (list_ptr = mixer_ctx_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
1695 struct channel *input_channel_ptr = list_ptr->data;
1696 remove_channel((jack_mixer_channel_t)input_channel_ptr);
1700 void
1701 remove_output_channel(
1702 jack_mixer_output_channel_t output_channel)
1704 struct output_channel *output_channel_ptr = output_channel;
1705 struct channel *channel_ptr = output_channel;
1707 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1708 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1709 free(channel_ptr->name);
1711 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1712 if (channel_ptr->stereo)
1714 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1717 if (channel_ptr->midi_cc_volume_index != -1)
1719 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1720 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1723 if (channel_ptr->midi_cc_balance_index != -1)
1725 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1726 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1729 if (channel_ptr->midi_cc_mute_index != -1)
1731 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
1732 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
1735 if (channel_ptr->midi_cc_solo_index != -1)
1737 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
1738 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
1741 g_slist_free(output_channel_ptr->soloed_channels);
1742 g_slist_free(output_channel_ptr->muted_channels);
1743 g_slist_free(output_channel_ptr->prefader_channels);
1745 free(channel_ptr->tmp_mixed_frames_left);
1746 free(channel_ptr->tmp_mixed_frames_right);
1747 free(channel_ptr->frames_left);
1748 free(channel_ptr->frames_right);
1749 free(channel_ptr->prefader_frames_left);
1750 free(channel_ptr->prefader_frames_right);
1752 free(channel_ptr);
1755 void
1756 output_channel_set_solo(
1757 jack_mixer_output_channel_t output_channel,
1758 jack_mixer_channel_t channel,
1759 bool solo_value)
1761 struct output_channel *output_channel_ptr = output_channel;
1763 if (solo_value) {
1764 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1765 return;
1766 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1767 } else {
1768 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1769 return;
1770 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1774 void
1775 output_channel_set_muted(
1776 jack_mixer_output_channel_t output_channel,
1777 jack_mixer_channel_t channel,
1778 bool muted_value)
1780 struct output_channel *output_channel_ptr = output_channel;
1782 if (muted_value) {
1783 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1784 return;
1785 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1786 } else {
1787 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1788 return;
1789 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1793 bool
1794 output_channel_is_muted(
1795 jack_mixer_output_channel_t output_channel,
1796 jack_mixer_channel_t channel)
1798 struct output_channel *output_channel_ptr = output_channel;
1800 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1801 return true;
1802 return false;
1805 bool
1806 output_channel_is_solo(
1807 jack_mixer_output_channel_t output_channel,
1808 jack_mixer_channel_t channel)
1810 struct output_channel *output_channel_ptr = output_channel;
1812 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1813 return true;
1814 return false;
1817 void
1818 output_channel_set_prefader(
1819 jack_mixer_output_channel_t output_channel,
1820 bool pfl_value)
1822 struct output_channel *output_channel_ptr = output_channel;
1823 output_channel_ptr->prefader = pfl_value;
1826 bool
1827 output_channel_is_prefader(
1828 jack_mixer_output_channel_t output_channel)
1830 struct output_channel *output_channel_ptr = output_channel;
1831 return output_channel_ptr->prefader;
1834 void
1835 output_channel_set_in_prefader(
1836 jack_mixer_output_channel_t output_channel,
1837 jack_mixer_channel_t channel,
1838 bool prefader_value)
1840 struct output_channel *output_channel_ptr = output_channel;
1842 if (prefader_value) {
1843 if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
1844 return;
1845 output_channel_ptr->prefader_channels = g_slist_prepend(output_channel_ptr->prefader_channels, channel);
1846 } else {
1847 if (g_slist_find(output_channel_ptr->prefader_channels, channel) == NULL)
1848 return;
1849 output_channel_ptr->prefader_channels = g_slist_remove(output_channel_ptr->prefader_channels, channel);
1853 bool
1854 output_channel_is_in_prefader(
1855 jack_mixer_output_channel_t output_channel,
1856 jack_mixer_channel_t channel)
1858 struct output_channel *output_channel_ptr = output_channel;
1860 if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
1861 return true;
1862 return false;