1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of jack_mixer
6 * Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
7 * Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 *****************************************************************************/
31 #include <jack/jack.h>
32 #if defined(HAVE_JACK_MIDI)
33 #include <jack/midiport.h>
40 #include "jack_mixer.h"
41 //#define LOG_LEVEL LOG_LEVEL_DEBUG
44 #include "jack_compat.h"
46 #define VOLUME_TRANSITION_SECONDS 0.01
48 #define PEAK_FRAMES_CHUNK 4800
50 // we don't know how much to allocate, but we don't want to wait with
51 // allocating until we're in the process() callback, so we just take a
52 // fairly big chunk: 4 periods per buffer, 4096 samples per period.
53 // (not sure if the '*4' is needed)
54 #define MAX_BLOCK_SIZE (4 * 4096)
56 #define FLOAT_EXISTS(x) (!((x) - (x)))
58 float _z1
; // filter state
59 float _z2
; // filter state
60 float _rms
; // max rms value since last read()
61 float _dpk
; // current digital peak value
62 int _cnt
; // digital peak hold counter
63 bool _flag
; // flag set by read(), resets _rms
65 int _hold
; // number of JACK periods to hold peak value
66 float _fall
; // per period fallback multiplier for peak value
67 float _omega
; // ballistics filter constant.
72 struct jack_mixer
* mixer_ptr
;
76 float volume_transition_seconds
;
77 unsigned int num_volume_transition_steps
;
79 jack_nframes_t volume_idx
;
82 jack_nframes_t balance_idx
;
85 float volume_left_new
;
87 float volume_right_new
;
91 struct kmeter kmeter_left
;
92 struct kmeter kmeter_right
;
94 jack_port_t
* port_left
;
95 jack_port_t
* port_right
;
97 jack_nframes_t peak_frames
;
101 jack_default_audio_sample_t
* tmp_mixed_frames_left
;
102 jack_default_audio_sample_t
* tmp_mixed_frames_right
;
103 jack_default_audio_sample_t
* frames_left
;
104 jack_default_audio_sample_t
* frames_right
;
105 jack_default_audio_sample_t
* prefader_frames_left
;
106 jack_default_audio_sample_t
* prefader_frames_right
;
110 int midi_cc_volume_index
;
111 int midi_cc_balance_index
;
112 int midi_cc_mute_index
;
113 int midi_cc_solo_index
;
114 bool midi_cc_volume_picked_up
;
115 bool midi_cc_balance_picked_up
;
117 jack_default_audio_sample_t
* left_buffer_ptr
;
118 jack_default_audio_sample_t
* right_buffer_ptr
;
120 bool midi_in_got_events
;
121 void (*midi_change_callback
) (void*);
122 void *midi_change_callback_data
;
123 bool midi_out_has_events
;
125 jack_mixer_scale_t midi_scale
;
128 struct output_channel
{
129 struct channel channel
;
130 GSList
*soloed_channels
;
131 GSList
*muted_channels
;
132 GSList
*prefader_channels
;
134 bool system
; /* system channel, without any associated UI */
140 pthread_mutex_t mutex
;
141 jack_client_t
* jack_client
;
142 GSList
*input_channels_list
;
143 GSList
*output_channels_list
;
144 GSList
*soloed_channels
;
146 jack_port_t
* port_midi_in
;
147 jack_port_t
* port_midi_out
;
148 int last_midi_channel
;
149 enum midi_behavior_mode midi_behavior
;
151 struct channel
* midi_cc_map
[128];
154 static jack_mixer_output_channel_t
create_output_channel(
156 const char * channel_name
,
161 update_channel_buffers(
162 struct channel
* channel_ptr
,
163 jack_nframes_t nframes
);
175 return 20.0 * log10f(value
);
182 return powf(10.0, db
/20.0);
185 double interpolate(double start
, double end
, int step
, int steps
) {
188 LOG_DEBUG("%f -> %f, %d", start
, end
, step
);
190 if (step
<= frac
* steps
) {
191 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
);;
195 } else if (end
<= 0) {
196 if (step
>= (1 - frac
) * steps
) {
197 ret
= frac
* start
- frac
* start
* step
/ steps
;
199 ret
= db_to_value(value_to_db(start
) + (value_to_db(frac
* start
) - value_to_db(start
)) * step
/ steps
);
202 ret
= db_to_value(value_to_db(start
) + (value_to_db(end
) - value_to_db(start
)) *step
/steps
);
204 LOG_DEBUG("interpolate: %f", ret
);
208 #define channel_ptr ((struct channel *)channel)
212 jack_mixer_channel_t channel
)
214 return channel_ptr
->name
;
219 jack_mixer_channel_t channel
,
223 size_t channel_name_size
;
227 new_name
= strdup(name
);
228 if (new_name
== NULL
)
233 if (channel_ptr
->name
)
235 free(channel_ptr
->name
);
238 channel_ptr
->name
= new_name
;
240 if (channel_ptr
->stereo
)
242 channel_name_size
= strlen(name
);
243 port_name
= malloc(channel_name_size
+ 3);
244 memcpy(port_name
, name
, channel_name_size
);
246 port_name
[channel_name_size
] = ' ';
247 port_name
[channel_name_size
+1] = 'L';
248 port_name
[channel_name_size
+2] = 0;
250 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
, port_name
);
253 /* what could we do here? */
256 port_name
[channel_name_size
+1] = 'R';
258 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
, port_name
);
261 /* what could we do here? */
268 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
, name
);
271 /* what could we do here? */
278 jack_mixer_channel_t channel
)
280 return channel_ptr
->stereo
;
284 channel_get_balance_midi_cc(
285 jack_mixer_channel_t channel
)
287 return channel_ptr
->midi_cc_balance_index
;
291 channel_unset_midi_cc_map(
292 jack_mixer_channel_t channel
,
294 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_volume_index
== new_cc
) {
295 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_volume_index
= -1;
296 } else if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_balance_index
== new_cc
) {
297 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_balance_index
= -1;
298 } else if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_mute_index
== new_cc
) {
299 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_mute_index
= -1;
300 } else if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_solo_index
== new_cc
) {
301 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_solo_index
= -1;
306 channel_set_balance_midi_cc(
307 jack_mixer_channel_t channel
,
310 if (new_cc
< 0 || new_cc
> 127) {
311 return 2; /* error: outside limit CC */
313 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
314 channel_unset_midi_cc_map(channel
, new_cc
);
316 if (channel_ptr
->midi_cc_balance_index
!= -1) {
317 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
319 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
320 channel_ptr
->midi_cc_balance_index
= new_cc
;
325 channel_get_volume_midi_cc(
326 jack_mixer_channel_t channel
)
328 return channel_ptr
->midi_cc_volume_index
;
332 channel_set_volume_midi_cc(
333 jack_mixer_channel_t channel
, int new_cc
)
335 if (new_cc
< 0 || new_cc
> 127) {
336 return 2; /* error: outside limit CC */
338 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
339 channel_unset_midi_cc_map(channel
, new_cc
);
341 if (channel_ptr
->midi_cc_volume_index
!= -1) {
342 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
344 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
345 channel_ptr
->midi_cc_volume_index
= new_cc
;
350 channel_get_mute_midi_cc(
351 jack_mixer_channel_t channel
)
353 return channel_ptr
->midi_cc_mute_index
;
357 channel_set_mute_midi_cc(
358 jack_mixer_channel_t channel
,
361 if (new_cc
< 0 || new_cc
> 127) {
362 return 2; /* error: outside limit CC */
364 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
365 channel_unset_midi_cc_map(channel
, new_cc
);
368 if (channel_ptr
->midi_cc_mute_index
!= -1) {
369 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
371 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
372 channel_ptr
->midi_cc_mute_index
= new_cc
;
377 channel_get_solo_midi_cc(
378 jack_mixer_channel_t channel
)
380 return channel_ptr
->midi_cc_solo_index
;
383 void channel_set_midi_cc_volume_picked_up(jack_mixer_channel_t channel
, bool status
)
385 LOG_DEBUG("Setting channel %s volume picked up to %d", channel_ptr
->name
, status
);
386 channel_ptr
->midi_cc_volume_picked_up
= status
;
389 void channel_set_midi_cc_balance_picked_up(jack_mixer_channel_t channel
, bool status
)
391 LOG_DEBUG("Setting channel %s balance picked up to %d", channel_ptr
->name
, status
);
392 channel_ptr
->midi_cc_balance_picked_up
= status
;
396 channel_set_solo_midi_cc(
397 jack_mixer_channel_t channel
,
400 if (new_cc
< 0 || new_cc
> 127) {
401 return 2; /* error: outside limit CC */
403 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
404 channel_unset_midi_cc_map(channel
, new_cc
);
406 if (channel_ptr
->midi_cc_solo_index
!= -1) {
407 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
409 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
410 channel_ptr
->midi_cc_solo_index
= new_cc
;
415 channel_autoset_volume_midi_cc(
416 jack_mixer_channel_t channel
)
418 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
420 for (int i
= 11 ; i
< 128 ; i
++)
422 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
424 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
425 channel_ptr
->midi_cc_volume_index
= i
;
427 LOG_DEBUG("New channel \"%s\" volume mapped to CC#%i", channel_ptr
->name
, i
);
435 channel_autoset_balance_midi_cc(
436 jack_mixer_channel_t channel
)
438 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
440 for (int i
= 11; i
< 128 ; i
++)
442 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
444 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
445 channel_ptr
->midi_cc_balance_index
= i
;
447 LOG_DEBUG("New channel \"%s\" balance mapped to CC#%i", channel_ptr
->name
, i
);
455 channel_autoset_mute_midi_cc(
456 jack_mixer_channel_t channel
)
458 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
460 for (int i
= 11; i
< 128 ; i
++)
462 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
464 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
465 channel_ptr
->midi_cc_mute_index
= i
;
467 LOG_DEBUG("New channel \"%s\" mute mapped to CC#%i", channel_ptr
->name
, i
);
475 channel_autoset_solo_midi_cc(
476 jack_mixer_channel_t channel
)
478 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
480 for (int i
= 11; i
< 128 ; i
++)
482 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
484 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
485 channel_ptr
->midi_cc_solo_index
= i
;
487 LOG_DEBUG("New channel \"%s\" solo mapped to CC#%i", channel_ptr
->name
, i
);
496 jack_mixer_channel_t channel
)
499 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_remove(
500 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
501 free(channel_ptr
->name
);
503 /* remove references to input channel from all output channels */
504 for (list_ptr
= channel_ptr
->mixer_ptr
->output_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
506 struct output_channel
*output_channel_ptr
= list_ptr
->data
;
507 output_channel_set_solo(output_channel_ptr
, channel
, false);
508 output_channel_set_muted(output_channel_ptr
, channel
, false);
511 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
512 if (channel_ptr
->stereo
)
514 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
517 if (channel_ptr
->midi_cc_volume_index
!= -1)
519 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
520 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
523 if (channel_ptr
->midi_cc_balance_index
!= -1)
525 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
526 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
529 if (channel_ptr
->midi_cc_mute_index
!= -1)
531 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] == channel_ptr
);
532 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
534 if (channel_ptr
->midi_cc_solo_index
!= -1)
536 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] == channel_ptr
);
537 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
540 free(channel_ptr
->frames_left
);
541 free(channel_ptr
->frames_right
);
542 free(channel_ptr
->prefader_frames_left
);
543 free(channel_ptr
->prefader_frames_right
);
549 channel_stereo_meter_read(
550 jack_mixer_channel_t channel
,
555 *left_ptr
= value_to_db(channel_ptr
->meter_left
);
556 *right_ptr
= value_to_db(channel_ptr
->meter_right
);
561 channel_mono_meter_read(
562 jack_mixer_channel_t channel
,
565 *mono_ptr
= value_to_db(channel_ptr
->meter_left
);
569 channel_stereo_kmeter_read(
570 jack_mixer_channel_t channel
,
573 double * left_rms_ptr
,
574 double * right_rms_ptr
)
577 *left_ptr
= value_to_db(channel_ptr
->kmeter_left
._dpk
);
578 *right_ptr
= value_to_db(channel_ptr
->kmeter_right
._dpk
);
579 *left_rms_ptr
= value_to_db(channel_ptr
->kmeter_left
._rms
);
580 *right_rms_ptr
= value_to_db(channel_ptr
->kmeter_right
._rms
);
581 channel_ptr
->kmeter_left
._flag
= true;
582 channel_ptr
->kmeter_right
._flag
= true;
586 channel_mono_kmeter_read(
587 jack_mixer_channel_t channel
,
589 double * mono_rms_ptr
)
591 *mono_ptr
= value_to_db(channel_ptr
->kmeter_left
._dpk
);
592 *mono_rms_ptr
= value_to_db(channel_ptr
->kmeter_left
._rms
);
593 channel_ptr
->kmeter_left
._flag
= true;
597 channel_volume_write(
598 jack_mixer_channel_t channel
,
602 /*If changing volume and find we're in the middle of a previous transition,
603 *then set current volume to place in transition to avoid a jump.*/
604 if (channel_ptr
->volume_new
!= channel_ptr
->volume
) {
605 channel_ptr
->volume
= interpolate(channel_ptr
->volume
, channel_ptr
->volume_new
, channel_ptr
->volume_idx
,
606 channel_ptr
->num_volume_transition_steps
);
608 channel_ptr
->volume_idx
= 0;
609 channel_ptr
->volume_new
= db_to_value(volume
);
610 channel_ptr
->midi_out_has_events
= true;
615 jack_mixer_channel_t channel
)
618 return value_to_db(channel_ptr
->volume_new
);
622 channels_volumes_read(jack_mixer_t mixer_ptr
)
625 struct channel
*pChannel
;
626 struct jack_mixer
* pMixer
= (struct jack_mixer
*)mixer_ptr
;
628 for (node_ptr
= pMixer
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
630 pChannel
= (struct channel
*)node_ptr
->data
;
631 double vol
= channel_volume_read( (jack_mixer_channel_t
)pChannel
);
632 printf("%s : volume is %f dbFS for mixer channel: %s\n", jack_get_client_name(pMixer
->jack_client
), vol
, pChannel
->name
);
637 channel_balance_write(
638 jack_mixer_channel_t channel
,
642 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
643 channel_ptr
->balance
= channel_ptr
->balance
+ channel_ptr
->balance_idx
*
644 (channel_ptr
->balance_new
- channel_ptr
->balance
) /
645 channel_ptr
->num_volume_transition_steps
;
647 channel_ptr
->balance_idx
= 0;
648 channel_ptr
->balance_new
= balance
;
652 channel_balance_read(
653 jack_mixer_channel_t channel
)
656 return channel_ptr
->balance_new
;
660 channel_abspeak_read(
661 jack_mixer_channel_t channel
)
664 if (channel_ptr
->NaN_detected
)
670 return value_to_db(channel_ptr
->abspeak
);
675 channel_abspeak_reset(
676 jack_mixer_channel_t channel
)
678 channel_ptr
->abspeak
= 0;
679 channel_ptr
->NaN_detected
= false;
684 jack_mixer_channel_t channel
)
686 channel_ptr
->out_mute
= true;
691 jack_mixer_channel_t channel
)
693 channel_ptr
->out_mute
= false;
697 channel_is_out_muted(
698 jack_mixer_channel_t channel
)
700 return channel_ptr
->out_mute
;
705 jack_mixer_channel_t channel
)
707 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
) != NULL
)
709 channel_ptr
->mixer_ptr
->soloed_channels
= g_slist_prepend(channel_ptr
->mixer_ptr
->soloed_channels
, channel
);
714 jack_mixer_channel_t channel
)
716 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
) == NULL
)
718 channel_ptr
->mixer_ptr
->soloed_channels
= g_slist_remove(channel_ptr
->mixer_ptr
->soloed_channels
, channel
);
723 jack_mixer_channel_t channel
)
725 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
))
731 channel_set_midi_scale(
732 jack_mixer_channel_t channel
,
733 jack_mixer_scale_t scale
)
735 channel_ptr
->midi_scale
= scale
;
739 channel_set_midi_change_callback(
740 jack_mixer_channel_t channel
,
741 void (*midi_change_callback
) (void*),
744 channel_ptr
->midi_change_callback
= midi_change_callback
;
745 channel_ptr
->midi_change_callback_data
= user_data
;
749 channel_get_midi_in_got_events(
750 jack_mixer_channel_t channel
)
752 bool t
= channel_ptr
->midi_in_got_events
;
753 channel_ptr
->midi_in_got_events
= false;
759 /* process input channels and mix them into main mix */
762 struct output_channel
*output_mix_channel
,
763 GSList
*channels_list
,
764 jack_nframes_t start
, /* index of first sample to process */
765 jack_nframes_t end
) /* index of sample to stop processing before */
770 struct channel
* channel_ptr
;
771 jack_default_audio_sample_t frame_left
;
772 jack_default_audio_sample_t frame_right
;
773 struct channel
*mix_channel
= (struct channel
*)output_mix_channel
;
775 for (i
= start
; i
< end
; i
++)
777 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
] = 0.0;
778 if (mix_channel
->stereo
)
779 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
] = 0.0;
782 for (node_ptr
= channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
784 channel_ptr
= node_ptr
->data
;
786 if (g_slist_find(output_mix_channel
->muted_channels
, channel_ptr
) != NULL
|| channel_ptr
->out_mute
) {
787 /* skip muted channels */
791 if ((!channel_ptr
->mixer_ptr
->soloed_channels
&& !output_mix_channel
->soloed_channels
) ||
792 (channel_ptr
->mixer_ptr
->soloed_channels
&&
793 g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel_ptr
) != NULL
) ||
794 (output_mix_channel
->soloed_channels
&&
795 g_slist_find(output_mix_channel
->soloed_channels
, channel_ptr
) != NULL
)) {
797 for (i
= start
; i
< end
; i
++)
799 if (! output_mix_channel
->prefader
&&
800 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
) {
801 frame_left
= channel_ptr
->frames_left
[i
-start
];
803 frame_left
= channel_ptr
->prefader_frames_left
[i
-start
];
805 if (frame_left
== NAN
)
807 mix_channel
->tmp_mixed_frames_left
[i
] += frame_left
;
808 if (mix_channel
->stereo
)
810 if (! output_mix_channel
->prefader
&&
811 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
) {
812 frame_right
= channel_ptr
->frames_right
[i
-start
];
814 frame_right
= channel_ptr
->prefader_frames_right
[i
-start
];
816 if (frame_right
== NAN
)
818 mix_channel
->tmp_mixed_frames_right
[i
] += frame_right
;
824 /* process main mix channel */
825 unsigned int steps
= mix_channel
->num_volume_transition_steps
;
827 for (i
= start
; i
< end
; i
++)
829 if (! output_mix_channel
->prefader
) {
830 float volume
= mix_channel
->volume
;
831 float volume_new
= mix_channel
->volume_new
;
833 float balance
= mix_channel
->balance
;
834 float balance_new
= mix_channel
->balance_new
;
836 if (volume
!= volume_new
) {
837 vol
= interpolate(volume
, volume_new
, mix_channel
->volume_idx
, steps
);
839 if (balance
!= balance_new
) {
840 bal
= mix_channel
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
845 if (mix_channel
->stereo
) {
847 vol_l
= vol
* (1 - bal
);
851 vol_r
= vol
* (1 + bal
);
854 vol_l
= vol
* (1 - bal
);
855 vol_r
= vol
* (1 + bal
);
857 mix_channel
->tmp_mixed_frames_left
[i
] *= vol_l
;
858 mix_channel
->tmp_mixed_frames_right
[i
] *= vol_r
;
861 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
862 if (mix_channel
->peak_left
< frame_left
)
864 mix_channel
->peak_left
= frame_left
;
866 if (frame_left
> mix_channel
->abspeak
)
868 mix_channel
->abspeak
= frame_left
;
872 if (mix_channel
->stereo
)
874 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
875 if (mix_channel
->peak_right
< frame_right
)
877 mix_channel
->peak_right
= frame_right
;
879 if (frame_right
> mix_channel
->abspeak
)
881 mix_channel
->abspeak
= frame_right
;
886 if (mix_channel
->stereo
)
888 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
889 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
891 if (mix_channel
->peak_left
< frame_left
)
893 mix_channel
->peak_left
= frame_left
;
895 if (frame_left
> mix_channel
->abspeak
)
897 mix_channel
->abspeak
= frame_left
;
901 if (mix_channel
->peak_right
< frame_right
)
903 mix_channel
->peak_right
= frame_right
;
905 if (frame_right
> mix_channel
->abspeak
)
907 mix_channel
->abspeak
= frame_right
;
913 if (mix_channel
->peak_left
< frame_left
)
915 mix_channel
->peak_left
= frame_left
;
917 if (frame_left
> mix_channel
->abspeak
)
919 mix_channel
->abspeak
= frame_left
;
924 mix_channel
->peak_frames
++;
925 if (mix_channel
->peak_frames
>= PEAK_FRAMES_CHUNK
)
927 mix_channel
->meter_left
= mix_channel
->peak_left
;
928 mix_channel
->peak_left
= 0.0;
930 if (mix_channel
->stereo
)
932 mix_channel
->meter_right
= mix_channel
->peak_right
;
933 mix_channel
->peak_right
= 0.0;
936 mix_channel
->peak_frames
= 0;
938 mix_channel
->volume_idx
++;
939 if ((mix_channel
->volume
!= mix_channel
->volume_new
) && (mix_channel
->volume_idx
== steps
)) {
940 mix_channel
->volume
= mix_channel
->volume_new
;
941 mix_channel
->volume_idx
= 0;
943 mix_channel
->balance_idx
++;
944 if ((mix_channel
->balance
!= mix_channel
->balance_new
) && (mix_channel
->balance_idx
== steps
)) {
945 mix_channel
->balance
= mix_channel
->balance_new
;
946 mix_channel
->balance_idx
= 0;
949 if (!mix_channel
->out_mute
) {
950 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
];
951 if (mix_channel
->stereo
)
952 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
];
955 kmeter_process(&mix_channel
->kmeter_left
, mix_channel
->tmp_mixed_frames_left
, start
, end
);
956 kmeter_process(&mix_channel
->kmeter_right
, mix_channel
->tmp_mixed_frames_right
, start
, end
);
961 struct channel
*channel_ptr
,
962 jack_nframes_t start
,
966 jack_default_audio_sample_t frame_left
= 0.0f
;
967 jack_default_audio_sample_t frame_right
= 0.0f
;
968 unsigned int steps
= channel_ptr
->num_volume_transition_steps
;
970 for (i
= start
; i
< end
; i
++)
972 if (i
-start
>= MAX_BLOCK_SIZE
)
974 fprintf(stderr
, "i-start too high: %d - %d\n", i
, start
);
976 channel_ptr
->prefader_frames_left
[i
-start
] = channel_ptr
->left_buffer_ptr
[i
];
977 if (channel_ptr
->stereo
)
978 channel_ptr
->prefader_frames_right
[i
-start
] = channel_ptr
->right_buffer_ptr
[i
];
980 if (!FLOAT_EXISTS(channel_ptr
->left_buffer_ptr
[i
]))
982 channel_ptr
->NaN_detected
= true;
983 channel_ptr
->frames_left
[i
-start
] = NAN
;
986 float volume
= channel_ptr
->volume
;
987 float volume_new
= channel_ptr
->volume_new
;
989 float balance
= channel_ptr
->balance
;
990 float balance_new
= channel_ptr
->balance_new
;
992 if (channel_ptr
->volume
!= channel_ptr
->volume_new
) {
993 vol
= interpolate(volume
, volume_new
, channel_ptr
->volume_idx
, steps
);
995 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
996 bal
= channel_ptr
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
1000 if (channel_ptr
->stereo
) {
1002 vol_l
= vol
* (1 - bal
);
1006 vol_r
= vol
* (1 + bal
);
1009 vol_l
= vol
* (1 - bal
);
1010 vol_r
= vol
* (1 + bal
);
1012 frame_left
= channel_ptr
->left_buffer_ptr
[i
] * vol_l
;
1013 if (channel_ptr
->stereo
)
1015 if (!FLOAT_EXISTS(channel_ptr
->right_buffer_ptr
[i
]))
1017 channel_ptr
->NaN_detected
= true;
1018 channel_ptr
->frames_right
[i
-start
] = NAN
;
1022 frame_right
= channel_ptr
->right_buffer_ptr
[i
] * vol_r
;
1026 frame_right
= channel_ptr
->left_buffer_ptr
[i
] * vol_r
;
1028 channel_ptr
->frames_left
[i
-start
] = frame_left
;
1029 channel_ptr
->frames_right
[i
-start
] = frame_right
;
1031 if (channel_ptr
->stereo
)
1033 frame_left
= fabsf(frame_left
);
1034 frame_right
= fabsf(frame_right
);
1036 if (channel_ptr
->peak_left
< frame_left
)
1038 channel_ptr
->peak_left
= frame_left
;
1040 if (frame_left
> channel_ptr
->abspeak
)
1042 channel_ptr
->abspeak
= frame_left
;
1046 if (channel_ptr
->peak_right
< frame_right
)
1048 channel_ptr
->peak_right
= frame_right
;
1050 if (frame_right
> channel_ptr
->abspeak
)
1052 channel_ptr
->abspeak
= frame_right
;
1059 frame_left
= (fabsf(frame_left
) + fabsf(frame_right
)) / 2;
1061 if (channel_ptr
->peak_left
< frame_left
)
1063 channel_ptr
->peak_left
= frame_left
;
1065 if (frame_left
> channel_ptr
->abspeak
)
1067 channel_ptr
->abspeak
= frame_left
;
1072 channel_ptr
->peak_frames
++;
1073 if (channel_ptr
->peak_frames
>= PEAK_FRAMES_CHUNK
)
1075 channel_ptr
->meter_left
= channel_ptr
->peak_left
;
1076 channel_ptr
->peak_left
= 0.0;
1078 if (channel_ptr
->stereo
)
1080 channel_ptr
->meter_right
= channel_ptr
->peak_right
;
1081 channel_ptr
->peak_right
= 0.0;
1084 channel_ptr
->peak_frames
= 0;
1087 channel_ptr
->volume_idx
++;
1088 if ((channel_ptr
->volume
!= channel_ptr
->volume_new
) &&
1089 (channel_ptr
->volume_idx
== steps
)) {
1090 channel_ptr
->volume
= channel_ptr
->volume_new
;
1091 channel_ptr
->volume_idx
= 0;
1093 channel_ptr
->balance_idx
++;
1094 if ((channel_ptr
->balance
!= channel_ptr
->balance_new
) &&
1095 (channel_ptr
->balance_idx
>= steps
)) {
1096 channel_ptr
->balance
= channel_ptr
->balance_new
;
1097 channel_ptr
->balance_idx
= 0;
1101 kmeter_process(&channel_ptr
->kmeter_left
, channel_ptr
->frames_left
, start
, end
);
1102 if (channel_ptr
->stereo
) kmeter_process(&channel_ptr
->kmeter_right
, channel_ptr
->frames_right
, start
, end
);
1108 struct jack_mixer
* mixer_ptr
,
1109 jack_nframes_t start
, /* index of first sample to process */
1110 jack_nframes_t end
) /* index of sample to stop processing before */
1113 struct output_channel
* output_channel_ptr
;
1114 struct channel
*channel_ptr
;
1116 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1118 channel_ptr
= (struct channel
*)node_ptr
->data
;
1119 calc_channel_frames(channel_ptr
, start
, end
);
1122 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1124 output_channel_ptr
= node_ptr
->data
;
1125 channel_ptr
= (struct channel
*)output_channel_ptr
;
1127 if (output_channel_ptr
->system
)
1129 /* Don't bother mixing the channels if we are not connected */
1130 if (channel_ptr
->stereo
)
1132 if (jack_port_connected(channel_ptr
->port_left
) == 0 &&
1133 jack_port_connected(channel_ptr
->port_right
) == 0)
1136 if (jack_port_connected(channel_ptr
->port_left
) == 0)
1141 mix_one(output_channel_ptr
, mixer_ptr
->input_channels_list
, start
, end
);
1146 update_channel_buffers(
1147 struct channel
* channel_ptr
,
1148 jack_nframes_t nframes
)
1150 channel_ptr
->left_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_left
, nframes
);
1152 if (channel_ptr
->stereo
)
1154 channel_ptr
->right_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_right
, nframes
);
1158 #define mixer_ptr ((struct jack_mixer *)context)
1162 jack_nframes_t nframes
,
1166 struct channel
* channel_ptr
;
1167 #if defined(HAVE_JACK_MIDI)
1169 jack_nframes_t event_count
;
1170 jack_midi_event_t in_event
;
1171 unsigned char* midi_out_buffer
;
1174 unsigned int cc_channel_index
;
1177 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1179 channel_ptr
= node_ptr
->data
;
1180 update_channel_buffers(channel_ptr
, nframes
);
1183 // Fill output buffers with the input
1184 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1186 channel_ptr
= node_ptr
->data
;
1187 update_channel_buffers(channel_ptr
, nframes
);
1190 #if defined(HAVE_JACK_MIDI)
1191 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_in
, nframes
);
1192 event_count
= jack_midi_get_event_count(midi_buffer
);
1194 for (i
= 0 ; i
< event_count
; i
++)
1196 jack_midi_event_get(&in_event
, midi_buffer
, i
);
1198 if (in_event
.size
!= 3 ||
1199 (in_event
.buffer
[0] & 0xF0) != 0xB0 ||
1200 in_event
.buffer
[1] > 127 ||
1201 in_event
.buffer
[2] > 127)
1206 assert(in_event
.time
< nframes
);
1210 (unsigned int)(in_event
.buffer
[0]),
1211 (unsigned int)in_event
.buffer
[1],
1212 (unsigned int)in_event
.buffer
[2]);
1214 mixer_ptr
->last_midi_channel
= (int)in_event
.buffer
[1];
1215 channel_ptr
= mixer_ptr
->midi_cc_map
[in_event
.buffer
[1]];
1217 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
1218 if (channel_ptr
!= NULL
&& channel_ptr
->midi_scale
!= NULL
)
1220 if (channel_ptr
->midi_cc_balance_index
== (char)in_event
.buffer
[1])
1222 byte
= in_event
.buffer
[2];
1228 int current_midi
= (int)63 * channel_ptr
->balance
+ 64;
1229 current_midi
= current_midi
== 1 ? 0 : current_midi
;
1230 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1231 !channel_ptr
->midi_cc_balance_picked_up
) {
1232 if (in_event
.buffer
[2] == current_midi
) {
1233 channel_set_midi_cc_balance_picked_up(channel_ptr
, true);
1236 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1237 channel_ptr
->midi_cc_balance_picked_up
) ||
1238 mixer_ptr
->midi_behavior
== Jump_To_Value
) {
1239 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
1240 channel_ptr
->balance
= channel_ptr
->balance
+ channel_ptr
->balance_idx
*
1241 (channel_ptr
->balance_new
- channel_ptr
->balance
) /
1242 channel_ptr
->num_volume_transition_steps
;
1244 channel_ptr
->balance_idx
= 0;
1245 channel_ptr
->balance_new
= (float)byte
/ 63;
1246 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr
->name
, channel_ptr
->balance_new
);
1249 else if (channel_ptr
->midi_cc_volume_index
== in_event
.buffer
[1])
1251 int current_midi
= (int)(127 *
1252 scale_db_to_scale(channel_ptr
->midi_scale
,
1253 value_to_db(channel_ptr
->volume
)));
1254 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1255 !channel_ptr
->midi_cc_volume_picked_up
) {
1256 if (in_event
.buffer
[2] == current_midi
) {
1257 channel_set_midi_cc_volume_picked_up(channel_ptr
, true);
1260 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1261 channel_ptr
->midi_cc_volume_picked_up
) ||
1262 mixer_ptr
->midi_behavior
== Jump_To_Value
) {
1263 if (channel_ptr
->volume_new
!= channel_ptr
->volume
) {
1264 channel_ptr
->volume
= interpolate(channel_ptr
->volume
, channel_ptr
->volume_new
, channel_ptr
->volume_idx
,
1265 channel_ptr
->num_volume_transition_steps
);
1267 channel_ptr
->volume_idx
= 0;
1268 channel_ptr
->volume_new
= db_to_value(scale_scale_to_db(channel_ptr
->midi_scale
,
1269 (double)in_event
.buffer
[2] / 127));
1270 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr
->name
, channel_ptr
->volume_new
);
1273 else if (channel_ptr
->midi_cc_mute_index
== in_event
.buffer
[1])
1275 if ((unsigned int)in_event
.buffer
[2] == 127) {
1276 channel_ptr
->out_mute
= !channel_ptr
->out_mute
;
1278 LOG_DEBUG("\"%s\" out_mute %d", channel_ptr
->name
, channel_ptr
->out_mute
);
1280 else if (channel_ptr
->midi_cc_solo_index
== in_event
.buffer
[1])
1282 if ((unsigned int)in_event
.buffer
[2] == 127) {
1283 if (channel_is_soloed(channel_ptr
)) {
1284 channel_unsolo(channel_ptr
);
1286 channel_solo(channel_ptr
);
1289 LOG_DEBUG("\"%s\" solo %d", channel_ptr
->name
, channel_is_soloed(channel_ptr
));
1291 channel_ptr
->midi_in_got_events
= true;
1292 if (channel_ptr
->midi_change_callback
)
1293 channel_ptr
->midi_change_callback(channel_ptr
->midi_change_callback_data
);
1299 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_out
, nframes
);
1300 jack_midi_clear_buffer(midi_buffer
);
1302 for(i
=0; i
<nframes
; i
++)
1304 for (cc_channel_index
=0; cc_channel_index
<128; cc_channel_index
++)
1306 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_channel_index
];
1307 if (channel_ptr
== NULL
|| channel_ptr
->midi_scale
== NULL
)
1311 if (channel_ptr
->midi_out_has_events
== false)
1315 if (channel_ptr
->midi_cc_balance_index
== (int)cc_channel_index
)
1319 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, i
, 3);
1320 if (midi_out_buffer
== NULL
)
1324 midi_out_buffer
[0] = 0xB0; /* control change */
1325 midi_out_buffer
[1] = cc_channel_index
;
1326 midi_out_buffer
[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr
->midi_scale
, value_to_db(channel_ptr
->volume_new
)));
1330 (unsigned int)(midi_out_buffer
[0]),
1331 (unsigned int)midi_out_buffer
[1],
1332 (unsigned int)midi_out_buffer
[2]);
1334 channel_ptr
->midi_out_has_events
= false;
1340 mix(mixer_ptr
, 0, nframes
);
1349 const char * jack_client_name_ptr
,
1354 struct jack_mixer
* mixer_ptr
;
1358 mixer_ptr
= malloc(sizeof(struct jack_mixer
));
1359 if (mixer_ptr
== NULL
)
1364 ret
= pthread_mutex_init(&mixer_ptr
->mutex
, NULL
);
1370 mixer_ptr
->input_channels_list
= NULL
;
1371 mixer_ptr
->output_channels_list
= NULL
;
1373 mixer_ptr
->soloed_channels
= NULL
;
1375 mixer_ptr
->last_midi_channel
= -1;
1377 mixer_ptr
->midi_behavior
= Jump_To_Value
;
1379 for (i
= 0 ; i
< 128 ; i
++)
1381 mixer_ptr
->midi_cc_map
[i
] = NULL
;
1384 LOG_DEBUG("Initializing JACK");
1385 mixer_ptr
->jack_client
= jack_client_open(jack_client_name_ptr
, 0, NULL
);
1386 if (mixer_ptr
->jack_client
== NULL
)
1388 LOG_ERROR("Cannot create JACK client.");
1389 LOG_NOTICE("Please make sure JACK daemon is running.");
1390 goto exit_destroy_mutex
;
1393 LOG_DEBUG("JACK client created");
1395 LOG_DEBUG("Sample rate: %" PRIu32
, jack_get_sample_rate(mixer_ptr
->jack_client
));
1398 #if defined(HAVE_JACK_MIDI)
1399 mixer_ptr
->port_midi_in
= jack_port_register(mixer_ptr
->jack_client
, "midi in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1400 if (mixer_ptr
->port_midi_in
== NULL
)
1402 LOG_ERROR("Cannot create JACK MIDI in port");
1406 mixer_ptr
->port_midi_out
= jack_port_register(mixer_ptr
->jack_client
, "midi out", JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1407 if (mixer_ptr
->port_midi_out
== NULL
)
1409 LOG_ERROR("Cannot create JACK MIDI out port");
1415 ret
= jack_set_process_callback(mixer_ptr
->jack_client
, process
, mixer_ptr
);
1418 LOG_ERROR("Cannot set JACK process callback");
1422 ret
= jack_activate(mixer_ptr
->jack_client
);
1425 LOG_ERROR("Cannot activate JACK client");
1432 jack_client_close(mixer_ptr
->jack_client
); /* this should clear all other resources we obtained through the client handle */
1435 pthread_mutex_destroy(&mixer_ptr
->mutex
);
1444 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1450 LOG_DEBUG("Uninitializing JACK");
1452 assert(mixer_ctx_ptr
->jack_client
!= NULL
);
1454 jack_client_close(mixer_ctx_ptr
->jack_client
);
1456 pthread_mutex_destroy(&mixer_ctx_ptr
->mutex
);
1458 free(mixer_ctx_ptr
);
1466 return g_slist_length(mixer_ctx_ptr
->input_channels_list
);
1473 return jack_get_client_name(mixer_ctx_ptr
->jack_client
);
1477 get_last_midi_channel(
1480 return mixer_ctx_ptr
->last_midi_channel
;
1484 set_last_midi_channel(
1487 mixer_ctx_ptr
->last_midi_channel
= new_channel
;
1492 get_midi_behavior_mode(
1495 return mixer_ctx_ptr
->midi_behavior
;
1499 set_midi_behavior_mode(
1501 enum midi_behavior_mode mode
)
1503 mixer_ctx_ptr
->midi_behavior
= mode
;
1507 jack_mixer_channel_t
1510 const char * channel_name
,
1513 struct channel
* channel_ptr
;
1514 char * port_name
= NULL
;
1515 size_t channel_name_size
;
1517 channel_ptr
= malloc(sizeof(struct channel
));
1518 if (channel_ptr
== NULL
)
1523 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1525 channel_ptr
->name
= strdup(channel_name
);
1526 if (channel_ptr
->name
== NULL
)
1528 goto fail_free_channel
;
1531 channel_name_size
= strlen(channel_name
);
1535 port_name
= malloc(channel_name_size
+ 3);
1536 if (port_name
== NULL
)
1538 goto fail_free_channel_name
;
1541 memcpy(port_name
, channel_name
, channel_name_size
);
1542 port_name
[channel_name_size
] = ' ';
1543 port_name
[channel_name_size
+1] = 'L';
1544 port_name
[channel_name_size
+2] = 0;
1546 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1547 if (channel_ptr
->port_left
== NULL
)
1549 goto fail_free_port_name
;
1552 port_name
[channel_name_size
+1] = 'R';
1554 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1555 if (channel_ptr
->port_right
== NULL
)
1557 goto fail_unregister_left_channel
;
1562 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1563 if (channel_ptr
->port_left
== NULL
)
1565 goto fail_free_channel_name
;
1569 channel_ptr
->stereo
= stereo
;
1571 int sr
= jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
);
1572 int fsize
= jack_get_buffer_size(channel_ptr
->mixer_ptr
->jack_client
);
1574 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1575 channel_ptr
->num_volume_transition_steps
=
1576 channel_ptr
->volume_transition_seconds
* sr
+ 1;
1577 channel_ptr
->volume
= 0.0;
1578 channel_ptr
->volume_new
= 0.0;
1579 channel_ptr
->balance
= 0.0;
1580 channel_ptr
->balance_new
= 0.0;
1581 channel_ptr
->meter_left
= -1.0;
1582 channel_ptr
->meter_right
= -1.0;
1583 channel_ptr
->abspeak
= 0.0;
1584 channel_ptr
->out_mute
= false;
1586 kmeter_init(&channel_ptr
->kmeter_left
, sr
, fsize
, .5f
, 15.0f
);
1587 kmeter_init(&channel_ptr
->kmeter_right
, sr
, fsize
, .5f
, 15.0f
);
1589 channel_ptr
->peak_left
= 0.0;
1590 channel_ptr
->peak_right
= 0.0;
1591 channel_ptr
->peak_frames
= 0;
1593 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1594 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1595 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1596 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1598 channel_ptr
->NaN_detected
= false;
1600 channel_ptr
->midi_cc_volume_index
= -1;
1601 channel_ptr
->midi_cc_balance_index
= -1;
1602 channel_ptr
->midi_cc_mute_index
= -1;
1603 channel_ptr
->midi_cc_solo_index
= -1;
1604 channel_ptr
->midi_cc_volume_picked_up
= false;
1605 channel_ptr
->midi_cc_balance_picked_up
= false;
1607 channel_ptr
->midi_change_callback
= NULL
;
1608 channel_ptr
->midi_change_callback_data
= NULL
;
1609 channel_ptr
->midi_out_has_events
= false;
1611 channel_ptr
->midi_scale
= NULL
;
1613 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_prepend(
1614 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
1619 fail_unregister_left_channel
:
1620 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1622 fail_free_port_name
:
1625 fail_free_channel_name
:
1626 free(channel_ptr
->name
);
1636 static jack_mixer_output_channel_t
1637 create_output_channel(
1639 const char * channel_name
,
1643 struct channel
* channel_ptr
;
1644 struct output_channel
* output_channel_ptr
;
1645 char * port_name
= NULL
;
1646 size_t channel_name_size
;
1648 output_channel_ptr
= malloc(sizeof(struct output_channel
));
1649 channel_ptr
= (struct channel
*)output_channel_ptr
;
1650 if (channel_ptr
== NULL
)
1655 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1657 channel_ptr
->name
= strdup(channel_name
);
1658 if (channel_ptr
->name
== NULL
)
1660 goto fail_free_channel
;
1665 channel_name_size
= strlen(channel_name
);
1667 port_name
= malloc(channel_name_size
+ 4);
1668 if (port_name
== NULL
)
1670 goto fail_free_channel_name
;
1673 memcpy(port_name
, channel_name
, channel_name_size
);
1674 port_name
[channel_name_size
] = ' ';
1675 port_name
[channel_name_size
+1] = 'L';
1676 port_name
[channel_name_size
+2] = 0;
1678 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1679 if (channel_ptr
->port_left
== NULL
)
1681 goto fail_free_port_name
;
1684 port_name
[channel_name_size
+1] = 'R';
1686 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1687 if (channel_ptr
->port_right
== NULL
)
1689 goto fail_unregister_left_channel
;
1694 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1695 if (channel_ptr
->port_left
== NULL
)
1697 goto fail_free_channel_name
;
1701 channel_ptr
->stereo
= stereo
;
1702 channel_ptr
->out_mute
= false;
1704 int sr
= jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
);
1705 int fsize
= jack_get_buffer_size(channel_ptr
->mixer_ptr
->jack_client
);
1707 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1708 channel_ptr
->num_volume_transition_steps
=
1709 channel_ptr
->volume_transition_seconds
* sr
+ 1;
1710 channel_ptr
->volume
= 0.0;
1711 channel_ptr
->volume_new
= 0.0;
1712 channel_ptr
->balance
= 0.0;
1713 channel_ptr
->balance_new
= 0.0;
1714 channel_ptr
->meter_left
= -1.0;
1715 channel_ptr
->meter_right
= -1.0;
1716 channel_ptr
->abspeak
= 0.0;
1717 kmeter_init(&channel_ptr
->kmeter_left
, sr
, fsize
, 0.5f
, 15.0f
);
1718 kmeter_init(&channel_ptr
->kmeter_right
, sr
, fsize
, 0.5f
, 15.0f
);
1720 channel_ptr
->peak_left
= 0.0;
1721 channel_ptr
->peak_right
= 0.0;
1722 channel_ptr
->peak_frames
= 0;
1724 channel_ptr
->tmp_mixed_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1725 channel_ptr
->tmp_mixed_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1726 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1727 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1728 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1729 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1731 channel_ptr
->NaN_detected
= false;
1733 channel_ptr
->midi_cc_volume_index
= -1;
1734 channel_ptr
->midi_cc_balance_index
= -1;
1735 channel_ptr
->midi_cc_mute_index
= -1;
1736 channel_ptr
->midi_cc_solo_index
= -1;
1737 channel_ptr
->midi_cc_volume_picked_up
= false;
1738 channel_ptr
->midi_cc_balance_picked_up
= false;
1740 channel_ptr
->midi_change_callback
= NULL
;
1741 channel_ptr
->midi_change_callback_data
= NULL
;
1743 channel_ptr
->midi_scale
= NULL
;
1745 output_channel_ptr
->soloed_channels
= NULL
;
1746 output_channel_ptr
->muted_channels
= NULL
;
1747 output_channel_ptr
->prefader_channels
= NULL
;
1748 output_channel_ptr
->system
= system
;
1749 output_channel_ptr
->prefader
= false;
1752 return output_channel_ptr
;
1754 fail_unregister_left_channel
:
1755 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1757 fail_free_port_name
:
1760 fail_free_channel_name
:
1761 free(channel_ptr
->name
);
1771 jack_mixer_output_channel_t
1774 const char * channel_name
,
1778 struct output_channel
*output_channel_ptr
;
1779 struct channel
*channel_ptr
;
1781 output_channel_ptr
= create_output_channel(mixer
, channel_name
, stereo
, system
);
1782 if (output_channel_ptr
== NULL
) {
1785 channel_ptr
= (struct channel
*)output_channel_ptr
;
1787 ((struct jack_mixer
*)mixer
)->output_channels_list
= g_slist_prepend(
1788 ((struct jack_mixer
*)mixer
)->output_channels_list
, channel_ptr
);
1790 return output_channel_ptr
;
1798 for (list_ptr
= mixer_ctx_ptr
->input_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
1800 struct channel
*input_channel_ptr
= list_ptr
->data
;
1801 remove_channel((jack_mixer_channel_t
)input_channel_ptr
);
1805 #define km ((struct kmeter *) kmeter)
1807 void kmeter_init(jack_mixer_kmeter_t kmeter
, int sr
, int fsize
, float hold
, float fall
) {
1816 km
->_omega
= 9.72f
/ sr
;
1817 t
= (float) fsize
/ sr
;
1818 km
->_hold
= (int)(hold
/ t
+ 0.5f
);
1819 km
->_fall
= powf(10.0f
, -0.05f
* fall
* t
);
1822 void kmeter_process(jack_mixer_kmeter_t kmeter
, jack_default_audio_sample_t
*p
, int start
, int end
) {
1824 jack_default_audio_sample_t s
, t
, z1
, z2
;
1836 for (i
= start
; i
< end
; i
++) {
1840 z1
+= km
->_omega
* (s
- z1
);
1841 z2
+= km
->_omega
* (z1
- z2
);
1845 km
->_z1
= z1
+ 1e-20f
;
1846 km
->_z2
= z2
+ 1e-20f
;
1849 if (s
> km
->_rms
) km
->_rms
= s
;
1853 km
->_cnt
= km
->_hold
;
1854 } else if (km
->_cnt
) {
1857 km
->_dpk
*= km
->_fall
;
1863 remove_output_channel(
1864 jack_mixer_output_channel_t output_channel
)
1866 struct output_channel
*output_channel_ptr
= output_channel
;
1867 struct channel
*channel_ptr
= output_channel
;
1869 channel_ptr
->mixer_ptr
->output_channels_list
= g_slist_remove(
1870 channel_ptr
->mixer_ptr
->output_channels_list
, channel_ptr
);
1871 free(channel_ptr
->name
);
1873 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1874 if (channel_ptr
->stereo
)
1876 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
1879 if (channel_ptr
->midi_cc_volume_index
!= -1)
1881 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
1882 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
1885 if (channel_ptr
->midi_cc_balance_index
!= -1)
1887 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
1888 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
1891 if (channel_ptr
->midi_cc_mute_index
!= -1)
1893 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] == channel_ptr
);
1894 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
1897 if (channel_ptr
->midi_cc_solo_index
!= -1)
1899 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] == channel_ptr
);
1900 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
1903 g_slist_free(output_channel_ptr
->soloed_channels
);
1904 g_slist_free(output_channel_ptr
->muted_channels
);
1905 g_slist_free(output_channel_ptr
->prefader_channels
);
1907 free(channel_ptr
->tmp_mixed_frames_left
);
1908 free(channel_ptr
->tmp_mixed_frames_right
);
1909 free(channel_ptr
->frames_left
);
1910 free(channel_ptr
->frames_right
);
1911 free(channel_ptr
->prefader_frames_left
);
1912 free(channel_ptr
->prefader_frames_right
);
1918 output_channel_set_solo(
1919 jack_mixer_output_channel_t output_channel
,
1920 jack_mixer_channel_t channel
,
1923 struct output_channel
*output_channel_ptr
= output_channel
;
1926 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
1928 output_channel_ptr
->soloed_channels
= g_slist_prepend(output_channel_ptr
->soloed_channels
, channel
);
1930 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) == NULL
)
1932 output_channel_ptr
->soloed_channels
= g_slist_remove(output_channel_ptr
->soloed_channels
, channel
);
1937 output_channel_set_muted(
1938 jack_mixer_output_channel_t output_channel
,
1939 jack_mixer_channel_t channel
,
1942 struct output_channel
*output_channel_ptr
= output_channel
;
1945 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
1947 output_channel_ptr
->muted_channels
= g_slist_prepend(output_channel_ptr
->muted_channels
, channel
);
1949 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) == NULL
)
1951 output_channel_ptr
->muted_channels
= g_slist_remove(output_channel_ptr
->muted_channels
, channel
);
1956 output_channel_is_muted(
1957 jack_mixer_output_channel_t output_channel
,
1958 jack_mixer_channel_t channel
)
1960 struct output_channel
*output_channel_ptr
= output_channel
;
1962 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
1968 output_channel_is_solo(
1969 jack_mixer_output_channel_t output_channel
,
1970 jack_mixer_channel_t channel
)
1972 struct output_channel
*output_channel_ptr
= output_channel
;
1974 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
1980 output_channel_set_prefader(
1981 jack_mixer_output_channel_t output_channel
,
1984 struct output_channel
*output_channel_ptr
= output_channel
;
1985 output_channel_ptr
->prefader
= pfl_value
;
1989 output_channel_is_prefader(
1990 jack_mixer_output_channel_t output_channel
)
1992 struct output_channel
*output_channel_ptr
= output_channel
;
1993 return output_channel_ptr
->prefader
;
1997 output_channel_set_in_prefader(
1998 jack_mixer_output_channel_t output_channel
,
1999 jack_mixer_channel_t channel
,
2000 bool prefader_value
)
2002 struct output_channel
*output_channel_ptr
= output_channel
;
2004 if (prefader_value
) {
2005 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)
2007 output_channel_ptr
->prefader_channels
= g_slist_prepend(output_channel_ptr
->prefader_channels
, channel
);
2009 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) == NULL
)
2011 output_channel_ptr
->prefader_channels
= g_slist_remove(output_channel_ptr
->prefader_channels
, channel
);
2016 output_channel_is_in_prefader(
2017 jack_mixer_output_channel_t output_channel
,
2018 jack_mixer_channel_t channel
)
2020 struct output_channel
*output_channel_ptr
= output_channel
;
2022 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)