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 limit CC */
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 limit CC */
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;
815 /* process input channels and mix them into main mix */
818 struct output_channel
*output_mix_channel
,
819 GSList
*channels_list
,
820 jack_nframes_t start
, /* index of first sample to process */
821 jack_nframes_t end
) /* index of sample to stop processing before */
826 struct channel
* channel_ptr
;
827 jack_default_audio_sample_t frame_left
;
828 jack_default_audio_sample_t frame_right
;
829 struct channel
*mix_channel
= (struct channel
*)output_mix_channel
;
831 for (i
= start
; i
< end
; i
++)
833 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
] = 0.0;
834 if (mix_channel
->stereo
)
835 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
] = 0.0;
838 for (node_ptr
= channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
840 channel_ptr
= node_ptr
->data
;
842 if (g_slist_find(output_mix_channel
->muted_channels
, channel_ptr
) != NULL
|| channel_ptr
->out_mute
) {
843 /* skip muted channels */
847 if ((!channel_ptr
->mixer_ptr
->soloed_channels
&& !output_mix_channel
->soloed_channels
) ||
848 (channel_ptr
->mixer_ptr
->soloed_channels
&&
849 g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel_ptr
) != NULL
) ||
850 (output_mix_channel
->soloed_channels
&&
851 g_slist_find(output_mix_channel
->soloed_channels
, channel_ptr
) != NULL
)) {
853 for (i
= start
; i
< end
; i
++)
855 if (! output_mix_channel
->prefader
&&
856 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
)
858 frame_left
= channel_ptr
->frames_left
[i
-start
];
861 frame_left
= channel_ptr
->prefader_frames_left
[i
-start
];
863 if (frame_left
== NAN
)
865 mix_channel
->tmp_mixed_frames_left
[i
] += frame_left
;
866 if (mix_channel
->stereo
)
868 if (! output_mix_channel
->prefader
&&
869 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
)
871 frame_right
= channel_ptr
->frames_right
[i
-start
];
874 frame_right
= channel_ptr
->prefader_frames_right
[i
-start
];
876 if (frame_right
== NAN
)
878 mix_channel
->tmp_mixed_frames_right
[i
] += frame_right
;
884 /* process main mix channel */
885 unsigned int steps
= mix_channel
->num_volume_transition_steps
;
887 for (i
= start
; i
< end
; i
++)
889 if (! output_mix_channel
->prefader
) {
890 float volume
= mix_channel
->volume
;
891 float volume_new
= mix_channel
->volume_new
;
893 float balance
= mix_channel
->balance
;
894 float balance_new
= mix_channel
->balance_new
;
896 if (volume
!= volume_new
) {
897 vol
= interpolate(volume
, volume_new
, mix_channel
->volume_idx
, steps
);
899 if (balance
!= balance_new
) {
900 bal
= mix_channel
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
905 if (mix_channel
->stereo
) {
907 vol_l
= vol
* (1 - bal
);
912 vol_r
= vol
* (1 + bal
);
916 vol_l
= vol
* (1 - bal
);
917 vol_r
= vol
* (1 + bal
);
919 mix_channel
->tmp_mixed_frames_left
[i
] *= vol_l
;
920 mix_channel
->tmp_mixed_frames_right
[i
] *= vol_r
;
923 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
924 if (mix_channel
->peak_left
< frame_left
)
926 mix_channel
->peak_left
= frame_left
;
928 if (frame_left
> mix_channel
->abspeak
)
930 mix_channel
->abspeak
= frame_left
;
934 if (mix_channel
->stereo
)
936 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
937 if (mix_channel
->peak_right
< frame_right
)
939 mix_channel
->peak_right
= frame_right
;
941 if (frame_right
> mix_channel
->abspeak
)
943 mix_channel
->abspeak
= frame_right
;
948 if (mix_channel
->stereo
)
950 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
951 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
953 if (mix_channel
->peak_left
< frame_left
)
955 mix_channel
->peak_left
= frame_left
;
957 if (frame_left
> mix_channel
->abspeak
)
959 mix_channel
->abspeak
= frame_left
;
963 if (mix_channel
->peak_right
< frame_right
)
965 mix_channel
->peak_right
= frame_right
;
967 if (frame_right
> mix_channel
->abspeak
)
969 mix_channel
->abspeak
= frame_right
;
975 if (mix_channel
->peak_left
< frame_left
)
977 mix_channel
->peak_left
= frame_left
;
979 if (frame_left
> mix_channel
->abspeak
)
981 mix_channel
->abspeak
= frame_left
;
986 mix_channel
->peak_frames
++;
987 if (mix_channel
->peak_frames
>= PEAK_FRAMES_CHUNK
)
989 mix_channel
->meter_left
= mix_channel
->peak_left
;
990 mix_channel
->peak_left
= 0.0;
992 if (mix_channel
->stereo
)
994 mix_channel
->meter_right
= mix_channel
->peak_right
;
995 mix_channel
->peak_right
= 0.0;
998 mix_channel
->peak_frames
= 0;
1000 mix_channel
->volume_idx
++;
1001 if ((mix_channel
->volume
!= mix_channel
->volume_new
) && (mix_channel
->volume_idx
== steps
)) {
1002 mix_channel
->volume
= mix_channel
->volume_new
;
1003 mix_channel
->volume_idx
= 0;
1005 mix_channel
->balance_idx
++;
1006 if ((mix_channel
->balance
!= mix_channel
->balance_new
) && (mix_channel
->balance_idx
== steps
)) {
1007 mix_channel
->balance
= mix_channel
->balance_new
;
1008 mix_channel
->balance_idx
= 0;
1011 if (!mix_channel
->out_mute
) {
1012 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
];
1013 if (mix_channel
->stereo
)
1014 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
];
1017 kmeter_process(&mix_channel
->kmeter_left
, mix_channel
->tmp_mixed_frames_left
, start
, end
);
1018 kmeter_process(&mix_channel
->kmeter_right
, mix_channel
->tmp_mixed_frames_right
, start
, end
);
1022 calc_channel_frames(
1023 struct channel
*channel_ptr
,
1024 jack_nframes_t start
,
1028 jack_default_audio_sample_t frame_left
= 0.0f
;
1029 jack_default_audio_sample_t frame_right
= 0.0f
;
1030 unsigned int steps
= channel_ptr
->num_volume_transition_steps
;
1032 for (i
= start
; i
< end
; i
++)
1034 if (i
-start
>= MAX_BLOCK_SIZE
)
1036 fprintf(stderr
, "i-start too high: %d - %d\n", i
, start
);
1038 channel_ptr
->prefader_frames_left
[i
-start
] = channel_ptr
->left_buffer_ptr
[i
];
1039 if (channel_ptr
->stereo
)
1040 channel_ptr
->prefader_frames_right
[i
-start
] = channel_ptr
->right_buffer_ptr
[i
];
1042 if (!FLOAT_EXISTS(channel_ptr
->left_buffer_ptr
[i
]))
1044 channel_ptr
->NaN_detected
= true;
1045 channel_ptr
->frames_left
[i
-start
] = NAN
;
1048 float volume
= channel_ptr
->volume
;
1049 float volume_new
= channel_ptr
->volume_new
;
1051 float balance
= channel_ptr
->balance
;
1052 float balance_new
= channel_ptr
->balance_new
;
1053 float bal
= balance
;
1054 if (channel_ptr
->volume
!= channel_ptr
->volume_new
) {
1055 vol
= interpolate(volume
, volume_new
, channel_ptr
->volume_idx
, steps
);
1057 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
1058 bal
= channel_ptr
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
1062 if (channel_ptr
->stereo
) {
1064 vol_l
= vol
* (1 - bal
);
1069 vol_r
= vol
* (1 + bal
);
1073 vol_l
= vol
* (1 - bal
);
1074 vol_r
= vol
* (1 + bal
);
1076 frame_left
= channel_ptr
->left_buffer_ptr
[i
] * vol_l
;
1077 if (channel_ptr
->stereo
)
1079 if (!FLOAT_EXISTS(channel_ptr
->right_buffer_ptr
[i
]))
1081 channel_ptr
->NaN_detected
= true;
1082 channel_ptr
->frames_right
[i
-start
] = NAN
;
1086 frame_right
= channel_ptr
->right_buffer_ptr
[i
] * vol_r
;
1090 frame_right
= channel_ptr
->left_buffer_ptr
[i
] * vol_r
;
1092 channel_ptr
->frames_left
[i
-start
] = frame_left
;
1093 channel_ptr
->frames_right
[i
-start
] = frame_right
;
1095 if (channel_ptr
->stereo
)
1097 frame_left
= fabsf(frame_left
);
1098 frame_right
= fabsf(frame_right
);
1100 if (channel_ptr
->peak_left
< frame_left
)
1102 channel_ptr
->peak_left
= frame_left
;
1104 if (frame_left
> channel_ptr
->abspeak
)
1106 channel_ptr
->abspeak
= frame_left
;
1110 if (channel_ptr
->peak_right
< frame_right
)
1112 channel_ptr
->peak_right
= frame_right
;
1114 if (frame_right
> channel_ptr
->abspeak
)
1116 channel_ptr
->abspeak
= frame_right
;
1123 frame_left
= (fabsf(frame_left
) + fabsf(frame_right
)) / 2;
1125 if (channel_ptr
->peak_left
< frame_left
)
1127 channel_ptr
->peak_left
= frame_left
;
1129 if (frame_left
> channel_ptr
->abspeak
)
1131 channel_ptr
->abspeak
= frame_left
;
1136 channel_ptr
->peak_frames
++;
1137 if (channel_ptr
->peak_frames
>= PEAK_FRAMES_CHUNK
)
1139 channel_ptr
->meter_left
= channel_ptr
->peak_left
;
1140 channel_ptr
->peak_left
= 0.0;
1142 if (channel_ptr
->stereo
)
1144 channel_ptr
->meter_right
= channel_ptr
->peak_right
;
1145 channel_ptr
->peak_right
= 0.0;
1148 channel_ptr
->peak_frames
= 0;
1151 channel_ptr
->volume_idx
++;
1152 if ((channel_ptr
->volume
!= channel_ptr
->volume_new
) &&
1153 (channel_ptr
->volume_idx
== steps
)) {
1154 channel_ptr
->volume
= channel_ptr
->volume_new
;
1155 channel_ptr
->volume_idx
= 0;
1157 channel_ptr
->balance_idx
++;
1158 if ((channel_ptr
->balance
!= channel_ptr
->balance_new
) &&
1159 (channel_ptr
->balance_idx
>= steps
)) {
1160 channel_ptr
->balance
= channel_ptr
->balance_new
;
1161 channel_ptr
->balance_idx
= 0;
1165 kmeter_process(&channel_ptr
->kmeter_left
, channel_ptr
->frames_left
, start
, end
);
1166 if (channel_ptr
->stereo
) kmeter_process(&channel_ptr
->kmeter_right
, channel_ptr
->frames_right
, start
, end
);
1172 struct jack_mixer
* mixer_ptr
,
1173 jack_nframes_t start
, /* index of first sample to process */
1174 jack_nframes_t end
) /* index of sample to stop processing before */
1177 struct output_channel
* output_channel_ptr
;
1178 struct channel
*channel_ptr
;
1180 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1182 channel_ptr
= (struct channel
*)node_ptr
->data
;
1183 calc_channel_frames(channel_ptr
, start
, end
);
1186 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1188 output_channel_ptr
= node_ptr
->data
;
1189 channel_ptr
= (struct channel
*)output_channel_ptr
;
1191 if (output_channel_ptr
->system
)
1193 /* Don't bother mixing the channels if we are not connected */
1194 if (channel_ptr
->stereo
)
1196 if (jack_port_connected(channel_ptr
->port_left
) == 0 &&
1197 jack_port_connected(channel_ptr
->port_right
) == 0)
1201 if (jack_port_connected(channel_ptr
->port_left
) == 0)
1206 mix_one(output_channel_ptr
, mixer_ptr
->input_channels_list
, start
, end
);
1211 update_channel_buffers(
1212 struct channel
* channel_ptr
,
1213 jack_nframes_t nframes
)
1215 channel_ptr
->left_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_left
, nframes
);
1217 if (channel_ptr
->stereo
)
1219 channel_ptr
->right_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_right
, nframes
);
1223 #define mixer_ptr ((struct jack_mixer *)context)
1227 jack_nframes_t nframes
,
1231 struct channel
* channel_ptr
;
1232 #if defined(HAVE_JACK_MIDI)
1234 jack_nframes_t event_count
;
1235 jack_midi_event_t in_event
;
1236 unsigned char* midi_out_buffer
;
1238 double volume
, balance
;
1239 uint8_t cc_channel_index
;
1240 uint8_t cc_num
, cc_val
, cur_cc_val
;
1243 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1245 channel_ptr
= node_ptr
->data
;
1246 update_channel_buffers(channel_ptr
, nframes
);
1249 // Fill output buffers with the input
1250 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1252 channel_ptr
= node_ptr
->data
;
1253 update_channel_buffers(channel_ptr
, nframes
);
1256 #if defined(HAVE_JACK_MIDI)
1257 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_in
, nframes
);
1258 event_count
= jack_midi_get_event_count(midi_buffer
);
1260 for (i
= 0 ; i
< event_count
; i
++)
1262 jack_midi_event_get(&in_event
, midi_buffer
, i
);
1264 if (in_event
.size
!= 3 ||
1265 (in_event
.buffer
[0] & 0xF0) != 0xB0 ||
1266 in_event
.buffer
[1] > 127 ||
1267 in_event
.buffer
[2] > 127)
1272 assert(in_event
.time
< nframes
);
1274 cc_num
= (uint8_t)(in_event
.buffer
[1] & 0x7F);
1275 cc_val
= (uint8_t)(in_event
.buffer
[2] & 0x7F);
1276 mixer_ptr
->last_midi_cc
= (int8_t)cc_num
;
1278 LOG_DEBUG("%u: CC#%u -> %u", (unsigned int)(in_event
.buffer
[0]), cc_num
, cc_val
);
1280 /* do we have a mapping for particular CC? */
1281 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_num
];
1284 if (channel_ptr
->midi_cc_balance_index
== cc_num
)
1287 balance
= MAP(cc_val
, 0.0, 63.0, -1.0, -0.015625);
1290 balance
= MAP(cc_val
, 64.0, 127.0, 0.0, 1.0);
1292 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1293 !channel_ptr
->midi_cc_balance_picked_up
&&
1294 channel_balance_read(channel_ptr
) - balance
< BALANCE_PICKUP_THRESHOLD
)
1296 channel_set_midi_cc_balance_picked_up(channel_ptr
, true);
1298 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1299 channel_ptr
->midi_cc_balance_picked_up
) ||
1300 mixer_ptr
->midi_behavior
== Jump_To_Value
)
1302 channel_balance_write(channel_ptr
, balance
);
1306 else if (channel_ptr
->midi_cc_volume_index
== cc_num
)
1308 // Is a MIDI scale set for corresponding channel?
1309 if (channel_ptr
->midi_scale
) {
1310 volume
= scale_scale_to_db(channel_ptr
->midi_scale
,
1311 (double)cc_val
/ 127);
1312 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1313 !channel_ptr
->midi_cc_volume_picked_up
)
1315 // MIDI control in pick-up mode but not picked up yet
1316 cur_cc_val
= (uint8_t)(127 * scale_db_to_scale(
1317 channel_ptr
->midi_scale
,
1318 value_to_db(channel_ptr
->volume
)));
1319 if (cc_val
== cur_cc_val
)
1321 // Incoming MIDI CC value matches current volume level
1322 // --> MIDI control is picked up
1323 channel_set_midi_cc_volume_picked_up(channel_ptr
, true);
1326 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1327 channel_ptr
->midi_cc_volume_picked_up
) ||
1328 mixer_ptr
->midi_behavior
== Jump_To_Value
)
1330 channel_volume_write(channel_ptr
, volume
);
1334 else if (channel_ptr
->midi_cc_mute_index
== cc_num
)
1337 channel_out_mute(channel_ptr
);
1340 channel_out_unmute(channel_ptr
);
1344 else if (channel_ptr
->midi_cc_solo_index
== cc_num
)
1347 channel_solo(channel_ptr
);
1350 channel_unsolo(channel_ptr
);
1353 channel_ptr
->midi_in_got_events
= true;
1354 if (channel_ptr
->midi_change_callback
) {
1355 channel_ptr
->midi_change_callback(channel_ptr
->midi_change_callback_data
);
1361 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_out
, nframes
);
1362 jack_midi_clear_buffer(midi_buffer
);
1364 for(i
=0; i
<nframes
; i
++)
1366 for (cc_channel_index
=0; cc_channel_index
<128; cc_channel_index
++)
1368 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_channel_index
];
1373 if (!channel_ptr
->midi_out_has_events
)
1377 if (channel_ptr
->midi_out_has_events
& CHANNEL_VOLUME
&& channel_ptr
->midi_scale
)
1379 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1380 if (!midi_out_buffer
)
1383 midi_out_buffer
[0] = 0xB0; /* control change */
1384 midi_out_buffer
[1] = channel_ptr
->midi_cc_volume_index
;
1385 midi_out_buffer
[2] = (unsigned char)(127 * scale_db_to_scale(channel_ptr
->midi_scale
,
1386 value_to_db(channel_ptr
->volume_new
)));
1390 (unsigned int)midi_out_buffer
[0],
1391 (unsigned int)midi_out_buffer
[1],
1392 (unsigned int)midi_out_buffer
[2]);
1394 if (channel_ptr
->midi_out_has_events
& CHANNEL_BALANCE
)
1396 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1397 if (!midi_out_buffer
)
1400 midi_out_buffer
[0] = 0xB0; /* control change */
1401 midi_out_buffer
[1] = channel_ptr
->midi_cc_balance_index
;
1402 balance
= channel_balance_read(channel_ptr
);
1403 if (balance
< 0.0) {
1404 midi_out_buffer
[2] = (unsigned char)(MAP(balance
, -1.0, -0.015625, 0.0, 63.0) + 0.5);
1407 midi_out_buffer
[2] = (unsigned char)(MAP(balance
, 0.0, 1.0, 64.0, 127.0) + 0.5);
1412 (unsigned int)midi_out_buffer
[0],
1413 (unsigned int)midi_out_buffer
[1],
1414 (unsigned int)midi_out_buffer
[2]);
1416 if (channel_ptr
->midi_out_has_events
& CHANNEL_MUTE
)
1418 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1419 if (!midi_out_buffer
)
1422 midi_out_buffer
[0] = 0xB0; /* control change */
1423 midi_out_buffer
[1] = channel_ptr
->midi_cc_mute_index
;
1424 midi_out_buffer
[2] = (unsigned char)(channel_is_out_muted(channel_ptr
) ? 127 : 0);
1428 (unsigned int)midi_out_buffer
[0],
1429 (unsigned int)midi_out_buffer
[1],
1430 (unsigned int)midi_out_buffer
[2]);
1432 if (channel_ptr
->midi_out_has_events
& CHANNEL_SOLO
)
1434 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, 0, 3);
1435 if (!midi_out_buffer
)
1438 midi_out_buffer
[0] = 0xB0; /* control change */
1439 midi_out_buffer
[1] = channel_ptr
->midi_cc_solo_index
;
1440 midi_out_buffer
[2] = (unsigned char)(channel_is_soloed(channel_ptr
) ? 127 : 0);
1444 (unsigned int)midi_out_buffer
[0],
1445 (unsigned int)midi_out_buffer
[1],
1446 (unsigned int)midi_out_buffer
[2]);
1448 channel_ptr
->midi_out_has_events
= 0;
1454 mix(mixer_ptr
, 0, nframes
);
1463 const char * jack_client_name_ptr
,
1468 struct jack_mixer
* mixer_ptr
;
1472 mixer_ptr
= malloc(sizeof(struct jack_mixer
));
1473 if (mixer_ptr
== NULL
)
1478 ret
= pthread_mutex_init(&mixer_ptr
->mutex
, NULL
);
1484 mixer_ptr
->input_channels_list
= NULL
;
1485 mixer_ptr
->output_channels_list
= NULL
;
1487 mixer_ptr
->soloed_channels
= NULL
;
1489 mixer_ptr
->last_midi_cc
= -1;
1491 mixer_ptr
->midi_behavior
= Jump_To_Value
;
1493 for (i
= 0 ; i
< 128 ; i
++)
1495 mixer_ptr
->midi_cc_map
[i
] = NULL
;
1498 LOG_DEBUG("Initializing JACK");
1499 mixer_ptr
->jack_client
= jack_client_open(jack_client_name_ptr
, 0, NULL
);
1500 if (mixer_ptr
->jack_client
== NULL
)
1502 LOG_ERROR("Cannot create JACK client.");
1503 LOG_NOTICE("Please make sure JACK daemon is running.");
1504 goto exit_destroy_mutex
;
1507 LOG_DEBUG("JACK client created");
1509 LOG_DEBUG("Sample rate: %" PRIu32
, jack_get_sample_rate(mixer_ptr
->jack_client
));
1512 #if defined(HAVE_JACK_MIDI)
1513 mixer_ptr
->port_midi_in
= jack_port_register(mixer_ptr
->jack_client
, "midi in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1514 if (mixer_ptr
->port_midi_in
== NULL
)
1516 LOG_ERROR("Cannot create JACK MIDI in port");
1520 mixer_ptr
->port_midi_out
= jack_port_register(mixer_ptr
->jack_client
, "midi out", JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1521 if (mixer_ptr
->port_midi_out
== NULL
)
1523 LOG_ERROR("Cannot create JACK MIDI out port");
1529 ret
= jack_set_process_callback(mixer_ptr
->jack_client
, process
, mixer_ptr
);
1532 LOG_ERROR("Cannot set JACK process callback");
1536 ret
= jack_activate(mixer_ptr
->jack_client
);
1539 LOG_ERROR("Cannot activate JACK client");
1546 jack_client_close(mixer_ptr
->jack_client
); /* this should clear all other resources we obtained through the client handle */
1549 pthread_mutex_destroy(&mixer_ptr
->mutex
);
1558 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1564 LOG_DEBUG("Uninitializing JACK");
1566 assert(mixer_ctx_ptr
->jack_client
!= NULL
);
1568 jack_client_close(mixer_ctx_ptr
->jack_client
);
1570 pthread_mutex_destroy(&mixer_ctx_ptr
->mutex
);
1572 free(mixer_ctx_ptr
);
1580 return g_slist_length(mixer_ctx_ptr
->input_channels_list
);
1587 return jack_get_client_name(mixer_ctx_ptr
->jack_client
);
1594 return mixer_ctx_ptr
->last_midi_cc
;
1601 mixer_ctx_ptr
->last_midi_cc
= new_cc
;
1606 get_midi_behavior_mode(
1609 return mixer_ctx_ptr
->midi_behavior
;
1613 set_midi_behavior_mode(
1615 enum midi_behavior_mode mode
)
1617 mixer_ctx_ptr
->midi_behavior
= mode
;
1621 jack_mixer_channel_t
1624 const char * channel_name
,
1627 struct channel
* channel_ptr
;
1628 char * port_name
= NULL
;
1629 size_t channel_name_size
;
1631 channel_ptr
= malloc(sizeof(struct channel
));
1632 if (channel_ptr
== NULL
)
1637 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1639 channel_ptr
->name
= strdup(channel_name
);
1640 if (channel_ptr
->name
== NULL
)
1642 goto fail_free_channel
;
1645 channel_name_size
= strlen(channel_name
);
1649 port_name
= malloc(channel_name_size
+ 3);
1650 if (port_name
== NULL
)
1652 goto fail_free_channel_name
;
1655 memcpy(port_name
, channel_name
, channel_name_size
);
1656 port_name
[channel_name_size
] = ' ';
1657 port_name
[channel_name_size
+1] = 'L';
1658 port_name
[channel_name_size
+2] = 0;
1660 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1661 if (channel_ptr
->port_left
== NULL
)
1663 goto fail_free_port_name
;
1666 port_name
[channel_name_size
+1] = 'R';
1668 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1669 if (channel_ptr
->port_right
== NULL
)
1671 goto fail_unregister_left_channel
;
1676 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1677 if (channel_ptr
->port_left
== NULL
)
1679 goto fail_free_channel_name
;
1683 channel_ptr
->stereo
= stereo
;
1685 int sr
= jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
);
1686 int fsize
= jack_get_buffer_size(channel_ptr
->mixer_ptr
->jack_client
);
1688 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1689 channel_ptr
->num_volume_transition_steps
=
1690 channel_ptr
->volume_transition_seconds
* sr
+ 1;
1691 channel_ptr
->volume
= 0.0;
1692 channel_ptr
->volume_new
= 0.0;
1693 channel_ptr
->balance
= 0.0;
1694 channel_ptr
->balance_new
= 0.0;
1695 channel_ptr
->meter_left
= -1.0;
1696 channel_ptr
->meter_right
= -1.0;
1697 channel_ptr
->abspeak
= 0.0;
1698 channel_ptr
->out_mute
= false;
1700 kmeter_init(&channel_ptr
->kmeter_left
, sr
, fsize
, .5f
, 15.0f
);
1701 kmeter_init(&channel_ptr
->kmeter_right
, sr
, fsize
, .5f
, 15.0f
);
1703 channel_ptr
->peak_left
= 0.0;
1704 channel_ptr
->peak_right
= 0.0;
1705 channel_ptr
->peak_frames
= 0;
1707 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1708 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1709 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1710 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1712 channel_ptr
->NaN_detected
= false;
1714 channel_ptr
->midi_cc_volume_index
= -1;
1715 channel_ptr
->midi_cc_balance_index
= -1;
1716 channel_ptr
->midi_cc_mute_index
= -1;
1717 channel_ptr
->midi_cc_solo_index
= -1;
1718 channel_ptr
->midi_cc_volume_picked_up
= false;
1719 channel_ptr
->midi_cc_balance_picked_up
= false;
1721 channel_ptr
->midi_change_callback
= NULL
;
1722 channel_ptr
->midi_change_callback_data
= NULL
;
1723 channel_ptr
->midi_out_has_events
= 0;
1725 channel_ptr
->midi_scale
= NULL
;
1727 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_prepend(
1728 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
1733 fail_unregister_left_channel
:
1734 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1736 fail_free_port_name
:
1739 fail_free_channel_name
:
1740 free(channel_ptr
->name
);
1750 static jack_mixer_output_channel_t
1751 create_output_channel(
1753 const char * channel_name
,
1757 struct channel
* channel_ptr
;
1758 struct output_channel
* output_channel_ptr
;
1759 char * port_name
= NULL
;
1760 size_t channel_name_size
;
1762 output_channel_ptr
= malloc(sizeof(struct output_channel
));
1763 channel_ptr
= (struct channel
*)output_channel_ptr
;
1764 if (channel_ptr
== NULL
)
1769 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1771 channel_ptr
->name
= strdup(channel_name
);
1772 if (channel_ptr
->name
== NULL
)
1774 goto fail_free_channel
;
1779 channel_name_size
= strlen(channel_name
);
1781 port_name
= malloc(channel_name_size
+ 4);
1782 if (port_name
== NULL
)
1784 goto fail_free_channel_name
;
1787 memcpy(port_name
, channel_name
, channel_name_size
);
1788 port_name
[channel_name_size
] = ' ';
1789 port_name
[channel_name_size
+1] = 'L';
1790 port_name
[channel_name_size
+2] = 0;
1792 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1793 if (channel_ptr
->port_left
== NULL
)
1795 goto fail_free_port_name
;
1798 port_name
[channel_name_size
+1] = 'R';
1800 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1801 if (channel_ptr
->port_right
== NULL
)
1803 goto fail_unregister_left_channel
;
1808 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1809 if (channel_ptr
->port_left
== NULL
)
1811 goto fail_free_channel_name
;
1815 channel_ptr
->stereo
= stereo
;
1816 channel_ptr
->out_mute
= false;
1818 int sr
= jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
);
1819 int fsize
= jack_get_buffer_size(channel_ptr
->mixer_ptr
->jack_client
);
1821 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1822 channel_ptr
->num_volume_transition_steps
=
1823 channel_ptr
->volume_transition_seconds
* sr
+ 1;
1824 channel_ptr
->volume
= 0.0;
1825 channel_ptr
->volume_new
= 0.0;
1826 channel_ptr
->balance
= 0.0;
1827 channel_ptr
->balance_new
= 0.0;
1828 channel_ptr
->meter_left
= -1.0;
1829 channel_ptr
->meter_right
= -1.0;
1830 channel_ptr
->abspeak
= 0.0;
1831 kmeter_init(&channel_ptr
->kmeter_left
, sr
, fsize
, 0.5f
, 15.0f
);
1832 kmeter_init(&channel_ptr
->kmeter_right
, sr
, fsize
, 0.5f
, 15.0f
);
1834 channel_ptr
->peak_left
= 0.0;
1835 channel_ptr
->peak_right
= 0.0;
1836 channel_ptr
->peak_frames
= 0;
1838 channel_ptr
->tmp_mixed_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1839 channel_ptr
->tmp_mixed_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1840 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1841 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1842 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1843 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1845 channel_ptr
->NaN_detected
= false;
1847 channel_ptr
->midi_cc_volume_index
= -1;
1848 channel_ptr
->midi_cc_balance_index
= -1;
1849 channel_ptr
->midi_cc_mute_index
= -1;
1850 channel_ptr
->midi_cc_solo_index
= -1;
1851 channel_ptr
->midi_cc_volume_picked_up
= false;
1852 channel_ptr
->midi_cc_balance_picked_up
= false;
1854 channel_ptr
->midi_change_callback
= NULL
;
1855 channel_ptr
->midi_change_callback_data
= NULL
;
1857 channel_ptr
->midi_scale
= NULL
;
1859 output_channel_ptr
->soloed_channels
= NULL
;
1860 output_channel_ptr
->muted_channels
= NULL
;
1861 output_channel_ptr
->prefader_channels
= NULL
;
1862 output_channel_ptr
->system
= system
;
1863 output_channel_ptr
->prefader
= false;
1866 return output_channel_ptr
;
1868 fail_unregister_left_channel
:
1869 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1871 fail_free_port_name
:
1874 fail_free_channel_name
:
1875 free(channel_ptr
->name
);
1885 jack_mixer_output_channel_t
1888 const char * channel_name
,
1892 struct output_channel
*output_channel_ptr
;
1893 struct channel
*channel_ptr
;
1895 output_channel_ptr
= create_output_channel(mixer
, channel_name
, stereo
, system
);
1896 if (output_channel_ptr
== NULL
) {
1899 channel_ptr
= (struct channel
*)output_channel_ptr
;
1901 ((struct jack_mixer
*)mixer
)->output_channels_list
= g_slist_prepend(
1902 ((struct jack_mixer
*)mixer
)->output_channels_list
, channel_ptr
);
1904 return output_channel_ptr
;
1912 for (list_ptr
= mixer_ctx_ptr
->input_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
1914 struct channel
*input_channel_ptr
= list_ptr
->data
;
1915 remove_channel((jack_mixer_channel_t
)input_channel_ptr
);
1919 #define km ((struct kmeter *) kmeter)
1923 jack_mixer_kmeter_t kmeter
,
1937 km
->_omega
= 9.72f
/ sr
;
1938 t
= (float) fsize
/ sr
;
1939 km
->_hold
= (int)(hold
/ t
+ 0.5f
);
1940 km
->_fall
= powf(10.0f
, -0.05f
* fall
* t
);
1945 jack_mixer_kmeter_t kmeter
,
1946 jack_default_audio_sample_t
*p
,
1951 jack_default_audio_sample_t s
, t
, z1
, z2
;
1963 for (i
= start
; i
< end
; i
++) {
1967 z1
+= km
->_omega
* (s
- z1
);
1968 z2
+= km
->_omega
* (z1
- z2
);
1972 km
->_z1
= z1
+ 1e-20f
;
1973 km
->_z2
= z2
+ 1e-20f
;
1976 if (s
> km
->_rms
) km
->_rms
= s
;
1980 km
->_cnt
= km
->_hold
;
1982 else if (km
->_cnt
) {
1986 km
->_dpk
*= km
->_fall
;
1992 remove_output_channel(
1993 jack_mixer_output_channel_t output_channel
)
1995 struct output_channel
*output_channel_ptr
= output_channel
;
1996 struct channel
*channel_ptr
= output_channel
;
1998 channel_ptr
->mixer_ptr
->output_channels_list
= g_slist_remove(
1999 channel_ptr
->mixer_ptr
->output_channels_list
, channel_ptr
);
2000 free(channel_ptr
->name
);
2002 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
2003 if (channel_ptr
->stereo
)
2005 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
2008 if (channel_ptr
->midi_cc_volume_index
!= -1)
2010 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
2011 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
2014 if (channel_ptr
->midi_cc_balance_index
!= -1)
2016 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
2017 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
2020 if (channel_ptr
->midi_cc_mute_index
!= -1)
2022 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] == channel_ptr
);
2023 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
2026 if (channel_ptr
->midi_cc_solo_index
!= -1)
2028 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] == channel_ptr
);
2029 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
2032 g_slist_free(output_channel_ptr
->soloed_channels
);
2033 g_slist_free(output_channel_ptr
->muted_channels
);
2034 g_slist_free(output_channel_ptr
->prefader_channels
);
2036 free(channel_ptr
->tmp_mixed_frames_left
);
2037 free(channel_ptr
->tmp_mixed_frames_right
);
2038 free(channel_ptr
->frames_left
);
2039 free(channel_ptr
->frames_right
);
2040 free(channel_ptr
->prefader_frames_left
);
2041 free(channel_ptr
->prefader_frames_right
);
2047 output_channel_set_solo(
2048 jack_mixer_output_channel_t output_channel
,
2049 jack_mixer_channel_t channel
,
2052 struct output_channel
*output_channel_ptr
= output_channel
;
2055 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
2057 output_channel_ptr
->soloed_channels
= g_slist_prepend(output_channel_ptr
->soloed_channels
, channel
);
2060 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) == NULL
)
2062 output_channel_ptr
->soloed_channels
= g_slist_remove(output_channel_ptr
->soloed_channels
, channel
);
2067 output_channel_set_muted(
2068 jack_mixer_output_channel_t output_channel
,
2069 jack_mixer_channel_t channel
,
2072 struct output_channel
*output_channel_ptr
= output_channel
;
2075 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
2077 output_channel_ptr
->muted_channels
= g_slist_prepend(output_channel_ptr
->muted_channels
, channel
);
2080 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) == NULL
)
2082 output_channel_ptr
->muted_channels
= g_slist_remove(output_channel_ptr
->muted_channels
, channel
);
2087 output_channel_is_muted(
2088 jack_mixer_output_channel_t output_channel
,
2089 jack_mixer_channel_t channel
)
2091 struct output_channel
*output_channel_ptr
= output_channel
;
2093 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
2099 output_channel_is_solo(
2100 jack_mixer_output_channel_t output_channel
,
2101 jack_mixer_channel_t channel
)
2103 struct output_channel
*output_channel_ptr
= output_channel
;
2105 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
2111 output_channel_set_prefader(
2112 jack_mixer_output_channel_t output_channel
,
2115 struct output_channel
*output_channel_ptr
= output_channel
;
2116 output_channel_ptr
->prefader
= pfl_value
;
2120 output_channel_is_prefader(
2121 jack_mixer_output_channel_t output_channel
)
2123 struct output_channel
*output_channel_ptr
= output_channel
;
2124 return output_channel_ptr
->prefader
;
2128 output_channel_set_in_prefader(
2129 jack_mixer_output_channel_t output_channel
,
2130 jack_mixer_channel_t channel
,
2131 bool prefader_value
)
2133 struct output_channel
*output_channel_ptr
= output_channel
;
2135 if (prefader_value
) {
2136 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)
2138 output_channel_ptr
->prefader_channels
= g_slist_prepend(output_channel_ptr
->prefader_channels
, channel
);
2141 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) == NULL
)
2143 output_channel_ptr
->prefader_channels
= g_slist_remove(output_channel_ptr
->prefader_channels
, channel
);
2148 output_channel_is_in_prefader(
2149 jack_mixer_output_channel_t output_channel
,
2150 jack_mixer_channel_t channel
)
2152 struct output_channel
*output_channel_ptr
= output_channel
;
2154 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)