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 *****************************************************************************/
32 #include <jack/jack.h>
33 #if defined(HAVE_JACK_MIDI)
34 #include <jack/midiport.h>
41 #include "jack_mixer.h"
42 //#define LOG_LEVEL LOG_LEVEL_DEBUG
45 #include "jack_compat.h"
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.
61 struct jack_mixer
* mixer_ptr
;
65 float volume_transition_seconds
;
66 unsigned int num_volume_transition_steps
;
68 jack_nframes_t volume_idx
;
71 jack_nframes_t balance_idx
;
74 float volume_left_new
;
76 float volume_right_new
;
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
;
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
;
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 */
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
;
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(
145 const char * channel_name
,
150 update_channel_buffers(
151 struct channel
* channel_ptr
,
152 jack_nframes_t nframes
);
155 unset_midi_cc_mapping(
156 struct jack_mixer
* mixer
,
168 return 20.0 * log10f(value
);
175 return powf(10.0, db
/ 20.0);
187 LOG_DEBUG("%f -> %f, %d", start
, end
, step
);
189 if (step
<= frac
* steps
) {
190 ret
= frac
* end
* step
/ steps
;
193 ret
= db_to_value(value_to_db(frac
* end
) + (value_to_db(end
) - value_to_db(frac
* end
)) * step
/ steps
);;
197 if (step
>= (1 - frac
) * steps
) {
198 ret
= frac
* start
- frac
* start
* step
/ steps
;
201 ret
= db_to_value(value_to_db(start
) + (value_to_db(frac
* start
) - value_to_db(start
)) * step
/ steps
);
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
);
211 #define channel_ptr ((struct channel *)channel)
215 jack_mixer_channel_t channel
)
217 return channel_ptr
->name
;
222 jack_mixer_channel_t channel
,
226 size_t channel_name_size
;
230 new_name
= strdup(name
);
231 if (new_name
== NULL
)
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
);
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
);
264 /* what could we do here? */
271 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
, name
);
274 /* what could we do here? */
281 jack_mixer_channel_t channel
)
283 return channel_ptr
->stereo
;
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
299 unset_midi_cc_mapping(
300 struct jack_mixer
* mixer
,
303 struct channel
*channel
= mixer
->midi_cc_map
[cc
];
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
;
325 channel_set_balance_midi_cc(
326 jack_mixer_channel_t channel
,
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
;
345 channel_get_volume_midi_cc(
346 jack_mixer_channel_t channel
)
348 return channel_ptr
->midi_cc_volume_index
;
352 channel_set_volume_midi_cc(
353 jack_mixer_channel_t channel
,
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
;
372 channel_get_mute_midi_cc(
373 jack_mixer_channel_t channel
)
375 return channel_ptr
->midi_cc_mute_index
;
379 channel_set_mute_midi_cc(
380 jack_mixer_channel_t channel
,
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
;
399 channel_get_solo_midi_cc(
400 jack_mixer_channel_t channel
)
402 return channel_ptr
->midi_cc_solo_index
;
406 channel_set_midi_cc_volume_picked_up(
407 jack_mixer_channel_t channel
,
410 LOG_DEBUG("Setting channel %s volume picked up to %d", channel_ptr
->name
, status
);
411 channel_ptr
->midi_cc_volume_picked_up
= status
;
415 channel_set_midi_cc_balance_picked_up(
416 jack_mixer_channel_t channel
,
419 LOG_DEBUG("Setting channel %s balance picked up to %d", channel_ptr
->name
, status
);
420 channel_ptr
->midi_cc_balance_picked_up
= status
;
424 channel_set_solo_midi_cc(
425 jack_mixer_channel_t channel
,
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
;
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
);
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
);
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
);
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
);
529 jack_mixer_channel_t channel
)
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
);
582 channel_stereo_meter_read(
583 jack_mixer_channel_t channel
,
588 *left_ptr
= value_to_db(channel_ptr
->meter_left
);
589 *right_ptr
= value_to_db(channel_ptr
->meter_right
);
594 channel_mono_meter_read(
595 jack_mixer_channel_t channel
,
598 *mono_ptr
= value_to_db(channel_ptr
->meter_left
);
602 channel_stereo_kmeter_read(
603 jack_mixer_channel_t channel
,
606 double * left_rms_ptr
,
607 double * right_rms_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;
619 channel_mono_kmeter_read(
620 jack_mixer_channel_t channel
,
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;
630 channel_volume_write(
631 jack_mixer_channel_t channel
,
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
);
654 jack_mixer_channel_t channel
)
657 return value_to_db(channel_ptr
->volume_new
);
661 channels_volumes_read(
662 jack_mixer_t mixer_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
);
677 channel_balance_write(
678 jack_mixer_channel_t channel
,
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
);
696 channel_balance_read(
697 jack_mixer_channel_t channel
)
700 return channel_ptr
->balance_new
;
704 channel_abspeak_read(
705 jack_mixer_channel_t channel
)
708 if (channel_ptr
->NaN_detected
)
714 return value_to_db(channel_ptr
->abspeak
);
719 channel_abspeak_reset(
720 jack_mixer_channel_t channel
)
722 channel_ptr
->abspeak
= 0;
723 channel_ptr
->NaN_detected
= false;
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
);
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
);
749 channel_is_out_muted(
750 jack_mixer_channel_t channel
)
752 return channel_ptr
->out_mute
;
757 jack_mixer_channel_t channel
)
759 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
) != NULL
)
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
);
768 jack_mixer_channel_t channel
)
770 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
) == NULL
)
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
);
779 jack_mixer_channel_t channel
)
781 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
))
787 channel_set_midi_scale(
788 jack_mixer_channel_t channel
,
789 jack_mixer_scale_t scale
)
791 channel_ptr
->midi_scale
= scale
;
795 channel_set_midi_change_callback(
796 jack_mixer_channel_t channel
,
797 void (*midi_change_callback
) (void*),
800 channel_ptr
->midi_change_callback
= midi_change_callback
;
801 channel_ptr
->midi_change_callback_data
= user_data
;
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;
816 * Process input channels and mix them into one output channel signal
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 */
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
) {
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;
857 * - or if the input channel is soloed for this output channel.
860 if ((!channel_ptr
->mixer_ptr
->soloed_channels
&& !output_mix_channel
->soloed_channels
) ||
861 (channel_ptr
->mixer_ptr
->soloed_channels
&&
862 g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel_ptr
) != NULL
) ||
863 (output_mix_channel
->soloed_channels
&&
864 g_slist_find(output_mix_channel
->soloed_channels
, channel_ptr
) != NULL
)) {
866 /* Get either post or pre-fader signal */
867 for (i
= start
; i
< end
; i
++)
869 /* Left/mono signal */
870 if (! output_mix_channel
->prefader
&&
871 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
)
873 frame_left
= channel_ptr
->frames_left
[i
-start
];
876 /* Output channel is globally set to pre-fader routing or
877 * input channel has pre-fader routing set for this output channel
879 frame_left
= channel_ptr
->prefader_frames_left
[i
-start
];
882 if (frame_left
== NAN
)
885 mix_channel
->tmp_mixed_frames_left
[i
] += frame_left
;
888 if (mix_channel
->stereo
)
890 if (! output_mix_channel
->prefader
&&
891 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
)
893 frame_right
= channel_ptr
->frames_right
[i
-start
];
896 /* Pre-fader routing */
897 frame_right
= channel_ptr
->prefader_frames_right
[i
-start
];
900 if (frame_right
== NAN
)
903 mix_channel
->tmp_mixed_frames_right
[i
] += frame_right
;
909 /* Apply output channel volume and compute meter signal and peak values */
910 unsigned int steps
= mix_channel
->num_volume_transition_steps
;
912 for (i
= start
; i
< end
; i
++)
914 /** Apply fader volume if output channel is not set to pre-fader routing */
915 if (! output_mix_channel
->prefader
) {
916 float volume
= mix_channel
->volume
;
917 float volume_new
= mix_channel
->volume_new
;
919 float balance
= mix_channel
->balance
;
920 float balance_new
= mix_channel
->balance_new
;
923 /* Do interpolation during transition to target volume level */
924 if (volume
!= volume_new
) {
925 vol
= interpolate(volume
, volume_new
, mix_channel
->volume_idx
, steps
);
928 /* Do interpolation during transition to target balance */
929 if (balance
!= balance_new
) {
930 bal
= mix_channel
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
936 /* Calculate left+right gain from volume and balance levels */
937 if (mix_channel
->stereo
) {
939 vol_l
= vol
* (1 - bal
);
944 vol_r
= vol
* (1 + bal
);
948 vol_l
= vol
* (1 - bal
);
949 vol_r
= vol
* (1 + bal
);
952 /* Apply gain to output mix */
953 mix_channel
->tmp_mixed_frames_left
[i
] *= vol_l
;
954 mix_channel
->tmp_mixed_frames_right
[i
] *= vol_r
;
957 /* Get peak signal, left/right and combined */
958 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
959 if (mix_channel
->peak_left
< frame_left
)
961 mix_channel
->peak_left
= frame_left
;
963 if (frame_left
> mix_channel
->abspeak
)
965 mix_channel
->abspeak
= frame_left
;
969 if (mix_channel
->stereo
)
971 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
972 if (mix_channel
->peak_right
< frame_right
)
974 mix_channel
->peak_right
= frame_right
;
976 if (frame_right
> mix_channel
->abspeak
)
978 mix_channel
->abspeak
= frame_right
;
983 /* This seems to duplicate what was already done right above? */
984 if (mix_channel
->stereo
)
986 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
987 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
989 if (mix_channel
->peak_left
< frame_left
)
991 mix_channel
->peak_left
= frame_left
;
993 if (frame_left
> mix_channel
->abspeak
)
995 mix_channel
->abspeak
= frame_left
;
999 if (mix_channel
->peak_right
< frame_right
)
1001 mix_channel
->peak_right
= frame_right
;
1003 if (frame_right
> mix_channel
->abspeak
)
1005 mix_channel
->abspeak
= frame_right
;
1011 if (mix_channel
->peak_left
< frame_left
)
1013 mix_channel
->peak_left
= frame_left
;
1015 if (frame_left
> mix_channel
->abspeak
)
1017 mix_channel
->abspeak
= frame_left
;
1022 /* update left/right peak values every so often */
1023 mix_channel
->peak_frames
++;
1024 if (mix_channel
->peak_frames
>= PEAK_FRAMES_CHUNK
)
1026 mix_channel
->meter_left
= mix_channel
->peak_left
;
1027 mix_channel
->peak_left
= 0.0;
1029 if (mix_channel
->stereo
)
1031 mix_channel
->meter_right
= mix_channel
->peak_right
;
1032 mix_channel
->peak_right
= 0.0;
1035 mix_channel
->peak_frames
= 0;
1038 /* Finish off volume interpolation */
1039 mix_channel
->volume_idx
++;
1040 if ((mix_channel
->volume
!= mix_channel
->volume_new
) && (mix_channel
->volume_idx
== steps
)) {
1041 mix_channel
->volume
= mix_channel
->volume_new
;
1042 mix_channel
->volume_idx
= 0;
1044 /* Finish off volume interpolation */
1045 mix_channel
->balance_idx
++;
1046 if ((mix_channel
->balance
!= mix_channel
->balance_new
) && (mix_channel
->balance_idx
== steps
)) {
1047 mix_channel
->balance
= mix_channel
->balance_new
;
1048 mix_channel
->balance_idx
= 0;
1051 /* Finally, if output channel is not muted, put signal into output buffer */
1052 if (!mix_channel
->out_mute
) {
1053 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
];
1054 if (mix_channel
->stereo
)
1055 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
];
1058 /* Calculate k-metering for output channel*/
1059 kmeter_process(&mix_channel
->kmeter_left
, mix_channel
->tmp_mixed_frames_left
, start
, end
);
1060 kmeter_process(&mix_channel
->kmeter_right
, mix_channel
->tmp_mixed_frames_right
, start
, end
);
1064 calc_channel_frames(
1065 struct channel
*channel_ptr
,
1066 jack_nframes_t start
,
1070 jack_default_audio_sample_t frame_left
= 0.0f
;
1071 jack_default_audio_sample_t frame_right
= 0.0f
;
1072 unsigned int steps
= channel_ptr
->num_volume_transition_steps
;
1074 for (i
= start
; i
< end
; i
++)
1076 if (i
-start
>= MAX_BLOCK_SIZE
)
1078 fprintf(stderr
, "i-start too high: %d - %d\n", i
, start
);
1081 /* Save pre-fader signal */
1082 channel_ptr
->prefader_frames_left
[i
-start
] = channel_ptr
->left_buffer_ptr
[i
];
1083 if (channel_ptr
->stereo
)
1084 channel_ptr
->prefader_frames_right
[i
-start
] = channel_ptr
->right_buffer_ptr
[i
];
1086 /* Detect de-normals */
1087 if (!FLOAT_EXISTS(channel_ptr
->left_buffer_ptr
[i
]))
1089 channel_ptr
->NaN_detected
= true;
1090 channel_ptr
->frames_left
[i
-start
] = NAN
;
1094 /* Get current and target channel volume and balance. */
1095 float volume
= channel_ptr
->volume
;
1096 float volume_new
= channel_ptr
->volume_new
;
1098 float balance
= channel_ptr
->balance
;
1099 float balance_new
= channel_ptr
->balance_new
;
1100 float bal
= balance
;
1102 /* During transition do interpolation to target volume level */
1103 if (channel_ptr
->volume
!= channel_ptr
->volume_new
) {
1104 vol
= interpolate(volume
, volume_new
, channel_ptr
->volume_idx
, steps
);
1107 /* During transition do interpolation to target balance */
1108 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
1109 bal
= channel_ptr
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
1112 /* Calculate left+right gain from volume and balance levels */
1115 if (channel_ptr
->stereo
) {
1117 vol_l
= vol
* (1 - bal
);
1122 vol_r
= vol
* (1 + bal
);
1126 vol_l
= vol
* (1 - bal
);
1127 vol_r
= vol
* (1 + bal
);
1130 /* Calculate left channel post-fader sample */
1131 frame_left
= channel_ptr
->left_buffer_ptr
[i
] * vol_l
;
1132 /* Calculate right channel post-fader sample */
1133 if (channel_ptr
->stereo
)
1135 if (!FLOAT_EXISTS(channel_ptr
->right_buffer_ptr
[i
]))
1137 channel_ptr
->NaN_detected
= true;
1138 channel_ptr
->frames_right
[i
-start
] = NAN
;
1142 frame_right
= channel_ptr
->right_buffer_ptr
[i
] * vol_r
;
1146 frame_right
= channel_ptr
->left_buffer_ptr
[i
] * vol_r
;
1148 channel_ptr
->frames_left
[i
-start
] = frame_left
;
1149 channel_ptr
->frames_right
[i
-start
] = frame_right
;
1151 /* Calculate left+right peak-level and, if need be,
1152 * update abspeak level
1154 if (channel_ptr
->stereo
)
1156 frame_left
= fabsf(frame_left
);
1157 frame_right
= fabsf(frame_right
);
1159 if (channel_ptr
->peak_left
< frame_left
)
1161 channel_ptr
->peak_left
= frame_left
;
1163 if (frame_left
> channel_ptr
->abspeak
)
1165 channel_ptr
->abspeak
= frame_left
;
1169 if (channel_ptr
->peak_right
< frame_right
)
1171 channel_ptr
->peak_right
= frame_right
;
1173 if (frame_right
> channel_ptr
->abspeak
)
1175 channel_ptr
->abspeak
= frame_right
;
1182 frame_left
= (fabsf(frame_left
) + fabsf(frame_right
)) / 2;
1184 if (channel_ptr
->peak_left
< frame_left
)
1186 channel_ptr
->peak_left
= frame_left
;
1188 if (frame_left
> channel_ptr
->abspeak
)
1190 channel_ptr
->abspeak
= frame_left
;
1195 /* Update input channel volume meter every so often */
1196 channel_ptr
->peak_frames
++;
1197 if (channel_ptr
->peak_frames
>= PEAK_FRAMES_CHUNK
)
1199 channel_ptr
->meter_left
= channel_ptr
->peak_left
;
1200 channel_ptr
->peak_left
= 0.0;
1202 if (channel_ptr
->stereo
)
1204 channel_ptr
->meter_right
= channel_ptr
->peak_right
;
1205 channel_ptr
->peak_right
= 0.0;
1208 channel_ptr
->peak_frames
= 0;
1211 /* Finish off volume & balance level interpolation */
1212 channel_ptr
->volume_idx
++;
1213 if ((channel_ptr
->volume
!= channel_ptr
->volume_new
) &&
1214 (channel_ptr
->volume_idx
== steps
)) {
1215 channel_ptr
->volume
= channel_ptr
->volume_new
;
1216 channel_ptr
->volume_idx
= 0;
1218 channel_ptr
->balance_idx
++;
1219 if ((channel_ptr
->balance
!= channel_ptr
->balance_new
) &&
1220 (channel_ptr
->balance_idx
>= steps
)) {
1221 channel_ptr
->balance
= channel_ptr
->balance_new
;
1222 channel_ptr
->balance_idx
= 0;
1226 /* Calculate k-metering for input channel */
1227 kmeter_process(&channel_ptr
->kmeter_left
, channel_ptr
->frames_left
, start
, end
);
1228 if (channel_ptr
->stereo
)
1229 kmeter_process(&channel_ptr
->kmeter_right
, channel_ptr
->frames_right
, start
, end
);
1234 struct jack_mixer
* mixer_ptr
,
1235 jack_nframes_t start
, /* Index of first sample to process */
1236 jack_nframes_t end
) /* Index of sample to stop processing before */
1239 struct output_channel
* output_channel_ptr
;
1240 struct channel
*channel_ptr
;
1242 /* Calculate pre/post-fader output and peak values for each input channel */
1243 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1245 channel_ptr
= (struct channel
*)node_ptr
->data
;
1246 calc_channel_frames(channel_ptr
, start
, end
);
1249 /* For all output channels: */
1250 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1252 output_channel_ptr
= node_ptr
->data
;
1253 channel_ptr
= (struct channel
*)output_channel_ptr
;
1255 if (output_channel_ptr
->system
)
1257 /* Don't bother mixing the channels if we are not connected */
1258 if (channel_ptr
->stereo
)
1260 if (jack_port_connected(channel_ptr
->port_left
) == 0 &&
1261 jack_port_connected(channel_ptr
->port_right
) == 0)
1265 if (jack_port_connected(channel_ptr
->port_left
) == 0)
1270 /* Mix this output channel */
1271 mix_one(output_channel_ptr
, mixer_ptr
->input_channels_list
, start
, end
);
1276 update_channel_buffers(
1277 struct channel
* channel_ptr
,
1278 jack_nframes_t nframes
)
1280 channel_ptr
->left_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_left
, nframes
);
1282 if (channel_ptr
->stereo
)
1284 channel_ptr
->right_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_right
, nframes
);
1288 #define mixer_ptr ((struct jack_mixer *)context)
1292 jack_nframes_t nframes
,
1296 struct channel
* channel_ptr
;
1297 #if defined(HAVE_JACK_MIDI)
1299 jack_nframes_t event_count
;
1300 jack_midi_event_t in_event
;
1301 unsigned char* midi_out_buffer
;
1303 double volume
, balance
;
1304 uint8_t cc_channel_index
;
1305 uint8_t cc_num
, cc_val
, cur_cc_val
;
1308 /* Get input ports buffer pointers */
1309 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1311 channel_ptr
= node_ptr
->data
;
1312 update_channel_buffers(channel_ptr
, nframes
);
1315 /* Get output ports buffer pointer */
1316 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1318 channel_ptr
= node_ptr
->data
;
1319 update_channel_buffers(channel_ptr
, nframes
);
1322 #if defined(HAVE_JACK_MIDI)
1323 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_in
, nframes
);
1324 event_count
= jack_midi_get_event_count(midi_buffer
);
1326 for (i
= 0 ; i
< event_count
; i
++)
1328 jack_midi_event_get(&in_event
, midi_buffer
, i
);
1330 if (in_event
.size
!= 3 ||
1331 (in_event
.buffer
[0] & 0xF0) != 0xB0 ||
1332 in_event
.buffer
[1] > 127 ||
1333 in_event
.buffer
[2] > 127)
1338 assert(in_event
.time
< nframes
);
1340 cc_num
= (uint8_t)(in_event
.buffer
[1] & 0x7F);
1341 cc_val
= (uint8_t)(in_event
.buffer
[2] & 0x7F);
1342 mixer_ptr
->last_midi_cc
= (int8_t)cc_num
;
1344 LOG_DEBUG("%u: CC#%u -> %u", (unsigned int)(in_event
.buffer
[0]), cc_num
, cc_val
);
1346 /* Do we have a mapping for particular CC? */
1347 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_num
];
1350 if (channel_ptr
->midi_cc_balance_index
== cc_num
)
1353 balance
= MAP(cc_val
, 0.0, 63.0, -1.0, -0.015625);
1356 balance
= MAP(cc_val
, 64.0, 127.0, 0.0, 1.0);
1358 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1359 !channel_ptr
->midi_cc_balance_picked_up
&&
1360 channel_balance_read(channel_ptr
) - balance
< BALANCE_PICKUP_THRESHOLD
)
1362 channel_set_midi_cc_balance_picked_up(channel_ptr
, true);
1364 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1365 channel_ptr
->midi_cc_balance_picked_up
) ||
1366 mixer_ptr
->midi_behavior
== Jump_To_Value
)
1368 channel_balance_write(channel_ptr
, balance
);
1372 else if (channel_ptr
->midi_cc_volume_index
== cc_num
)
1374 /* Is a MIDI scale set for corresponding channel? */
1375 if (channel_ptr
->midi_scale
) {
1376 volume
= scale_scale_to_db(channel_ptr
->midi_scale
,
1377 (double)cc_val
/ 127);
1378 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1379 !channel_ptr
->midi_cc_volume_picked_up
)
1381 /* MIDI control in pick-up mode but not picked up yet */
1382 cur_cc_val
= (uint8_t)(127 * scale_db_to_scale(
1383 channel_ptr
->midi_scale
,
1384 value_to_db(channel_ptr
->volume
)));
1385 if (cc_val
== cur_cc_val
)
1387 /* Incoming MIDI CC value matches current volume level
1388 * --> MIDI control is picked up
1390 channel_set_midi_cc_volume_picked_up(channel_ptr
, true);
1393 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1394 channel_ptr
->midi_cc_volume_picked_up
) ||
1395 mixer_ptr
->midi_behavior
== Jump_To_Value
)
1397 channel_volume_write(channel_ptr
, volume
);
1401 else if (channel_ptr
->midi_cc_mute_index
== cc_num
)
1404 channel_out_mute(channel_ptr
);
1407 channel_out_unmute(channel_ptr
);
1411 else if (channel_ptr
->midi_cc_solo_index
== cc_num
)
1414 channel_solo(channel_ptr
);
1417 channel_unsolo(channel_ptr
);
1420 channel_ptr
->midi_in_got_events
= true;
1421 if (channel_ptr
->midi_change_callback
) {
1422 channel_ptr
->midi_change_callback(channel_ptr
->midi_change_callback_data
);
1428 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_out
, nframes
);
1429 jack_midi_clear_buffer(midi_buffer
);
1431 for(i
=0; i
<nframes
; i
++)
1433 for (cc_channel_index
=0; cc_channel_index
<128; cc_channel_index
++)
1435 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_channel_index
];
1440 if (!channel_ptr
->midi_out_has_events
)
1444 if (channel_ptr
->midi_out_has_events
& CHANNEL_VOLUME
&& channel_ptr
->midi_scale
)
1446 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1447 if (!midi_out_buffer
)
1450 midi_out_buffer
[0] = 0xB0; /* control change */
1451 midi_out_buffer
[1] = channel_ptr
->midi_cc_volume_index
;
1452 midi_out_buffer
[2] = (unsigned char)(127 * scale_db_to_scale(channel_ptr
->midi_scale
,
1453 value_to_db(channel_ptr
->volume_new
)));
1457 (unsigned int)midi_out_buffer
[0],
1458 (unsigned int)midi_out_buffer
[1],
1459 (unsigned int)midi_out_buffer
[2]);
1461 if (channel_ptr
->midi_out_has_events
& CHANNEL_BALANCE
)
1463 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1464 if (!midi_out_buffer
)
1467 midi_out_buffer
[0] = 0xB0; /* control change */
1468 midi_out_buffer
[1] = channel_ptr
->midi_cc_balance_index
;
1469 balance
= channel_balance_read(channel_ptr
);
1470 if (balance
< 0.0) {
1471 midi_out_buffer
[2] = (unsigned char)(MAP(balance
, -1.0, -0.015625, 0.0, 63.0) + 0.5);
1474 midi_out_buffer
[2] = (unsigned char)(MAP(balance
, 0.0, 1.0, 64.0, 127.0) + 0.5);
1479 (unsigned int)midi_out_buffer
[0],
1480 (unsigned int)midi_out_buffer
[1],
1481 (unsigned int)midi_out_buffer
[2]);
1483 if (channel_ptr
->midi_out_has_events
& CHANNEL_MUTE
)
1485 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1486 if (!midi_out_buffer
)
1489 midi_out_buffer
[0] = 0xB0; /* control change */
1490 midi_out_buffer
[1] = channel_ptr
->midi_cc_mute_index
;
1491 midi_out_buffer
[2] = (unsigned char)(channel_is_out_muted(channel_ptr
) ? 127 : 0);
1495 (unsigned int)midi_out_buffer
[0],
1496 (unsigned int)midi_out_buffer
[1],
1497 (unsigned int)midi_out_buffer
[2]);
1499 if (channel_ptr
->midi_out_has_events
& CHANNEL_SOLO
)
1501 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1502 if (!midi_out_buffer
)
1505 midi_out_buffer
[0] = 0xB0; /* control change */
1506 midi_out_buffer
[1] = channel_ptr
->midi_cc_solo_index
;
1507 midi_out_buffer
[2] = (unsigned char)(channel_is_soloed(channel_ptr
) ? 127 : 0);
1511 (unsigned int)midi_out_buffer
[0],
1512 (unsigned int)midi_out_buffer
[1],
1513 (unsigned int)midi_out_buffer
[2]);
1515 channel_ptr
->midi_out_has_events
= 0;
1521 mix(mixer_ptr
, 0, nframes
);
1530 const char * jack_client_name_ptr
,
1535 struct jack_mixer
* mixer_ptr
;
1539 mixer_ptr
= malloc(sizeof(struct jack_mixer
));
1540 if (mixer_ptr
== NULL
)
1545 ret
= pthread_mutex_init(&mixer_ptr
->mutex
, NULL
);
1551 mixer_ptr
->input_channels_list
= NULL
;
1552 mixer_ptr
->output_channels_list
= NULL
;
1554 mixer_ptr
->soloed_channels
= NULL
;
1556 mixer_ptr
->last_midi_cc
= -1;
1558 mixer_ptr
->midi_behavior
= Jump_To_Value
;
1560 for (i
= 0 ; i
< 128 ; i
++)
1562 mixer_ptr
->midi_cc_map
[i
] = NULL
;
1565 LOG_DEBUG("Initializing JACK");
1566 mixer_ptr
->jack_client
= jack_client_open(jack_client_name_ptr
, 0, NULL
);
1567 if (mixer_ptr
->jack_client
== NULL
)
1569 LOG_ERROR("Cannot create JACK client.");
1570 LOG_NOTICE("Please make sure JACK daemon is running.");
1571 goto exit_destroy_mutex
;
1574 LOG_DEBUG("JACK client created");
1576 LOG_DEBUG("Sample rate: %" PRIu32
, jack_get_sample_rate(mixer_ptr
->jack_client
));
1579 #if defined(HAVE_JACK_MIDI)
1580 mixer_ptr
->port_midi_in
= jack_port_register(mixer_ptr
->jack_client
, "midi in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1581 if (mixer_ptr
->port_midi_in
== NULL
)
1583 LOG_ERROR("Cannot create JACK MIDI in port");
1587 mixer_ptr
->port_midi_out
= jack_port_register(mixer_ptr
->jack_client
, "midi out", JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1588 if (mixer_ptr
->port_midi_out
== NULL
)
1590 LOG_ERROR("Cannot create JACK MIDI out port");
1596 ret
= jack_set_process_callback(mixer_ptr
->jack_client
, process
, mixer_ptr
);
1599 LOG_ERROR("Cannot set JACK process callback");
1603 ret
= jack_activate(mixer_ptr
->jack_client
);
1606 LOG_ERROR("Cannot activate JACK client");
1613 jack_client_close(mixer_ptr
->jack_client
); /* this should clear all other resources we obtained through the client handle */
1616 pthread_mutex_destroy(&mixer_ptr
->mutex
);
1625 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1631 LOG_DEBUG("Uninitializing JACK");
1633 assert(mixer_ctx_ptr
->jack_client
!= NULL
);
1635 jack_client_close(mixer_ctx_ptr
->jack_client
);
1637 pthread_mutex_destroy(&mixer_ctx_ptr
->mutex
);
1639 free(mixer_ctx_ptr
);
1647 return g_slist_length(mixer_ctx_ptr
->input_channels_list
);
1654 return jack_get_client_name(mixer_ctx_ptr
->jack_client
);
1661 return mixer_ctx_ptr
->last_midi_cc
;
1668 mixer_ctx_ptr
->last_midi_cc
= new_cc
;
1673 get_midi_behavior_mode(
1676 return mixer_ctx_ptr
->midi_behavior
;
1680 set_midi_behavior_mode(
1682 enum midi_behavior_mode mode
)
1684 mixer_ctx_ptr
->midi_behavior
= mode
;
1688 jack_mixer_channel_t
1691 const char * channel_name
,
1694 struct channel
* channel_ptr
;
1695 char * port_name
= NULL
;
1696 size_t channel_name_size
;
1698 channel_ptr
= malloc(sizeof(struct channel
));
1699 if (channel_ptr
== NULL
)
1704 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1706 channel_ptr
->name
= strdup(channel_name
);
1707 if (channel_ptr
->name
== NULL
)
1709 goto fail_free_channel
;
1712 channel_name_size
= strlen(channel_name
);
1716 port_name
= malloc(channel_name_size
+ 3);
1717 if (port_name
== NULL
)
1719 goto fail_free_channel_name
;
1722 memcpy(port_name
, channel_name
, channel_name_size
);
1723 port_name
[channel_name_size
] = ' ';
1724 port_name
[channel_name_size
+1] = 'L';
1725 port_name
[channel_name_size
+2] = 0;
1727 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1728 if (channel_ptr
->port_left
== NULL
)
1730 goto fail_free_port_name
;
1733 port_name
[channel_name_size
+1] = 'R';
1735 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1736 if (channel_ptr
->port_right
== NULL
)
1738 goto fail_unregister_left_channel
;
1743 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1744 if (channel_ptr
->port_left
== NULL
)
1746 goto fail_free_channel_name
;
1750 channel_ptr
->stereo
= stereo
;
1752 int sr
= jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
);
1753 int fsize
= jack_get_buffer_size(channel_ptr
->mixer_ptr
->jack_client
);
1755 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1756 channel_ptr
->num_volume_transition_steps
=
1757 channel_ptr
->volume_transition_seconds
* sr
+ 1;
1758 channel_ptr
->volume
= 0.0;
1759 channel_ptr
->volume_new
= 0.0;
1760 channel_ptr
->balance
= 0.0;
1761 channel_ptr
->balance_new
= 0.0;
1762 channel_ptr
->meter_left
= -1.0;
1763 channel_ptr
->meter_right
= -1.0;
1764 channel_ptr
->abspeak
= 0.0;
1765 channel_ptr
->out_mute
= false;
1767 kmeter_init(&channel_ptr
->kmeter_left
, sr
, fsize
, .5f
, 15.0f
);
1768 kmeter_init(&channel_ptr
->kmeter_right
, sr
, fsize
, .5f
, 15.0f
);
1770 channel_ptr
->peak_left
= 0.0;
1771 channel_ptr
->peak_right
= 0.0;
1772 channel_ptr
->peak_frames
= 0;
1774 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1775 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1776 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1777 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1779 channel_ptr
->NaN_detected
= false;
1781 channel_ptr
->midi_cc_volume_index
= -1;
1782 channel_ptr
->midi_cc_balance_index
= -1;
1783 channel_ptr
->midi_cc_mute_index
= -1;
1784 channel_ptr
->midi_cc_solo_index
= -1;
1785 channel_ptr
->midi_cc_volume_picked_up
= false;
1786 channel_ptr
->midi_cc_balance_picked_up
= false;
1788 channel_ptr
->midi_change_callback
= NULL
;
1789 channel_ptr
->midi_change_callback_data
= NULL
;
1790 channel_ptr
->midi_out_has_events
= 0;
1792 channel_ptr
->midi_scale
= NULL
;
1794 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_prepend(
1795 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
1800 fail_unregister_left_channel
:
1801 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1803 fail_free_port_name
:
1806 fail_free_channel_name
:
1807 free(channel_ptr
->name
);
1817 static jack_mixer_output_channel_t
1818 create_output_channel(
1820 const char * channel_name
,
1824 struct channel
* channel_ptr
;
1825 struct output_channel
* output_channel_ptr
;
1826 char * port_name
= NULL
;
1827 size_t channel_name_size
;
1829 output_channel_ptr
= malloc(sizeof(struct output_channel
));
1830 channel_ptr
= (struct channel
*)output_channel_ptr
;
1831 if (channel_ptr
== NULL
)
1836 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1838 channel_ptr
->name
= strdup(channel_name
);
1839 if (channel_ptr
->name
== NULL
)
1841 goto fail_free_channel
;
1846 channel_name_size
= strlen(channel_name
);
1848 port_name
= malloc(channel_name_size
+ 4);
1849 if (port_name
== NULL
)
1851 goto fail_free_channel_name
;
1854 memcpy(port_name
, channel_name
, channel_name_size
);
1855 port_name
[channel_name_size
] = ' ';
1856 port_name
[channel_name_size
+1] = 'L';
1857 port_name
[channel_name_size
+2] = 0;
1859 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1860 if (channel_ptr
->port_left
== NULL
)
1862 goto fail_free_port_name
;
1865 port_name
[channel_name_size
+1] = 'R';
1867 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1868 if (channel_ptr
->port_right
== NULL
)
1870 goto fail_unregister_left_channel
;
1875 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1876 if (channel_ptr
->port_left
== NULL
)
1878 goto fail_free_channel_name
;
1882 channel_ptr
->stereo
= stereo
;
1883 channel_ptr
->out_mute
= false;
1885 int sr
= jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
);
1886 int fsize
= jack_get_buffer_size(channel_ptr
->mixer_ptr
->jack_client
);
1888 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1889 channel_ptr
->num_volume_transition_steps
=
1890 channel_ptr
->volume_transition_seconds
* sr
+ 1;
1891 channel_ptr
->volume
= 0.0;
1892 channel_ptr
->volume_new
= 0.0;
1893 channel_ptr
->balance
= 0.0;
1894 channel_ptr
->balance_new
= 0.0;
1895 channel_ptr
->meter_left
= -1.0;
1896 channel_ptr
->meter_right
= -1.0;
1897 channel_ptr
->abspeak
= 0.0;
1898 kmeter_init(&channel_ptr
->kmeter_left
, sr
, fsize
, 0.5f
, 15.0f
);
1899 kmeter_init(&channel_ptr
->kmeter_right
, sr
, fsize
, 0.5f
, 15.0f
);
1901 channel_ptr
->peak_left
= 0.0;
1902 channel_ptr
->peak_right
= 0.0;
1903 channel_ptr
->peak_frames
= 0;
1905 channel_ptr
->tmp_mixed_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1906 channel_ptr
->tmp_mixed_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1907 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1908 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1909 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1910 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1912 channel_ptr
->NaN_detected
= false;
1914 channel_ptr
->midi_cc_volume_index
= -1;
1915 channel_ptr
->midi_cc_balance_index
= -1;
1916 channel_ptr
->midi_cc_mute_index
= -1;
1917 channel_ptr
->midi_cc_solo_index
= -1;
1918 channel_ptr
->midi_cc_volume_picked_up
= false;
1919 channel_ptr
->midi_cc_balance_picked_up
= false;
1921 channel_ptr
->midi_change_callback
= NULL
;
1922 channel_ptr
->midi_change_callback_data
= NULL
;
1924 channel_ptr
->midi_scale
= NULL
;
1926 output_channel_ptr
->soloed_channels
= NULL
;
1927 output_channel_ptr
->muted_channels
= NULL
;
1928 output_channel_ptr
->prefader_channels
= NULL
;
1929 output_channel_ptr
->system
= system
;
1930 output_channel_ptr
->prefader
= false;
1933 return output_channel_ptr
;
1935 fail_unregister_left_channel
:
1936 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1938 fail_free_port_name
:
1941 fail_free_channel_name
:
1942 free(channel_ptr
->name
);
1952 jack_mixer_output_channel_t
1955 const char * channel_name
,
1959 struct output_channel
*output_channel_ptr
;
1960 struct channel
*channel_ptr
;
1962 output_channel_ptr
= create_output_channel(mixer
, channel_name
, stereo
, system
);
1963 if (output_channel_ptr
== NULL
) {
1966 channel_ptr
= (struct channel
*)output_channel_ptr
;
1968 ((struct jack_mixer
*)mixer
)->output_channels_list
= g_slist_prepend(
1969 ((struct jack_mixer
*)mixer
)->output_channels_list
, channel_ptr
);
1971 return output_channel_ptr
;
1979 for (list_ptr
= mixer_ctx_ptr
->input_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
1981 struct channel
*input_channel_ptr
= list_ptr
->data
;
1982 remove_channel((jack_mixer_channel_t
)input_channel_ptr
);
1986 #define km ((struct kmeter *) kmeter)
1990 jack_mixer_kmeter_t kmeter
,
2004 km
->_omega
= 9.72f
/ sr
;
2005 t
= (float) fsize
/ sr
;
2006 km
->_hold
= (int)(hold
/ t
+ 0.5f
);
2007 km
->_fall
= powf(10.0f
, -0.05f
* fall
* t
);
2012 jack_mixer_kmeter_t kmeter
,
2013 jack_default_audio_sample_t
*p
,
2018 jack_default_audio_sample_t s
, t
, z1
, z2
;
2030 for (i
= start
; i
< end
; i
++) {
2034 z1
+= km
->_omega
* (s
- z1
);
2035 z2
+= km
->_omega
* (z1
- z2
);
2039 km
->_z1
= z1
+ 1e-20f
;
2040 km
->_z2
= z2
+ 1e-20f
;
2043 if (s
> km
->_rms
) km
->_rms
= s
;
2047 km
->_cnt
= km
->_hold
;
2049 else if (km
->_cnt
) {
2053 km
->_dpk
*= km
->_fall
;
2059 remove_output_channel(
2060 jack_mixer_output_channel_t output_channel
)
2062 struct output_channel
*output_channel_ptr
= output_channel
;
2063 struct channel
*channel_ptr
= output_channel
;
2065 channel_ptr
->mixer_ptr
->output_channels_list
= g_slist_remove(
2066 channel_ptr
->mixer_ptr
->output_channels_list
, channel_ptr
);
2067 free(channel_ptr
->name
);
2069 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
2070 if (channel_ptr
->stereo
)
2072 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
2075 if (channel_ptr
->midi_cc_volume_index
!= -1)
2077 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
2078 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
2081 if (channel_ptr
->midi_cc_balance_index
!= -1)
2083 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
2084 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
2087 if (channel_ptr
->midi_cc_mute_index
!= -1)
2089 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] == channel_ptr
);
2090 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
2093 if (channel_ptr
->midi_cc_solo_index
!= -1)
2095 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] == channel_ptr
);
2096 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
2099 g_slist_free(output_channel_ptr
->soloed_channels
);
2100 g_slist_free(output_channel_ptr
->muted_channels
);
2101 g_slist_free(output_channel_ptr
->prefader_channels
);
2103 free(channel_ptr
->tmp_mixed_frames_left
);
2104 free(channel_ptr
->tmp_mixed_frames_right
);
2105 free(channel_ptr
->frames_left
);
2106 free(channel_ptr
->frames_right
);
2107 free(channel_ptr
->prefader_frames_left
);
2108 free(channel_ptr
->prefader_frames_right
);
2114 output_channel_set_solo(
2115 jack_mixer_output_channel_t output_channel
,
2116 jack_mixer_channel_t channel
,
2119 struct output_channel
*output_channel_ptr
= output_channel
;
2122 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
2124 output_channel_ptr
->soloed_channels
= g_slist_prepend(output_channel_ptr
->soloed_channels
, channel
);
2127 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) == NULL
)
2129 output_channel_ptr
->soloed_channels
= g_slist_remove(output_channel_ptr
->soloed_channels
, channel
);
2134 output_channel_set_muted(
2135 jack_mixer_output_channel_t output_channel
,
2136 jack_mixer_channel_t channel
,
2139 struct output_channel
*output_channel_ptr
= output_channel
;
2142 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
2144 output_channel_ptr
->muted_channels
= g_slist_prepend(output_channel_ptr
->muted_channels
, channel
);
2147 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) == NULL
)
2149 output_channel_ptr
->muted_channels
= g_slist_remove(output_channel_ptr
->muted_channels
, channel
);
2154 output_channel_is_muted(
2155 jack_mixer_output_channel_t output_channel
,
2156 jack_mixer_channel_t channel
)
2158 struct output_channel
*output_channel_ptr
= output_channel
;
2160 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
2166 output_channel_is_solo(
2167 jack_mixer_output_channel_t output_channel
,
2168 jack_mixer_channel_t channel
)
2170 struct output_channel
*output_channel_ptr
= output_channel
;
2172 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
2178 output_channel_set_prefader(
2179 jack_mixer_output_channel_t output_channel
,
2182 struct output_channel
*output_channel_ptr
= output_channel
;
2183 output_channel_ptr
->prefader
= pfl_value
;
2187 output_channel_is_prefader(
2188 jack_mixer_output_channel_t output_channel
)
2190 struct output_channel
*output_channel_ptr
= output_channel
;
2191 return output_channel_ptr
->prefader
;
2195 output_channel_set_in_prefader(
2196 jack_mixer_output_channel_t output_channel
,
2197 jack_mixer_channel_t channel
,
2198 bool prefader_value
)
2200 struct output_channel
*output_channel_ptr
= output_channel
;
2202 if (prefader_value
) {
2203 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)
2205 output_channel_ptr
->prefader_channels
= g_slist_prepend(output_channel_ptr
->prefader_channels
, channel
);
2208 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) == NULL
)
2210 output_channel_ptr
->prefader_channels
= g_slist_remove(output_channel_ptr
->prefader_channels
, channel
);
2215 output_channel_is_in_prefader(
2216 jack_mixer_output_channel_t output_channel
,
2217 jack_mixer_channel_t channel
)
2219 struct output_channel
*output_channel_ptr
= output_channel
;
2221 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)