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)))
60 struct jack_mixer
* mixer_ptr
;
64 float volume_transition_seconds
;
65 unsigned int num_volume_transition_steps
;
67 jack_nframes_t volume_idx
;
70 jack_nframes_t balance_idx
;
73 float volume_left_new
;
75 float volume_right_new
;
79 jack_port_t
* port_left
;
80 jack_port_t
* port_right
;
82 jack_nframes_t peak_frames
;
86 jack_default_audio_sample_t
* tmp_mixed_frames_left
;
87 jack_default_audio_sample_t
* tmp_mixed_frames_right
;
88 jack_default_audio_sample_t
* frames_left
;
89 jack_default_audio_sample_t
* frames_right
;
90 jack_default_audio_sample_t
* prefader_frames_left
;
91 jack_default_audio_sample_t
* prefader_frames_right
;
95 int midi_cc_volume_index
;
96 int midi_cc_balance_index
;
97 int midi_cc_mute_index
;
98 int midi_cc_solo_index
;
99 bool midi_cc_volume_picked_up
;
100 bool midi_cc_balance_picked_up
;
102 jack_default_audio_sample_t
* left_buffer_ptr
;
103 jack_default_audio_sample_t
* right_buffer_ptr
;
105 bool midi_in_got_events
;
106 void (*midi_change_callback
) (void*);
107 void *midi_change_callback_data
;
108 bool midi_out_has_events
;
110 jack_mixer_scale_t midi_scale
;
113 struct output_channel
{
114 struct channel channel
;
115 GSList
*soloed_channels
;
116 GSList
*muted_channels
;
117 GSList
*prefader_channels
;
119 bool system
; /* system channel, without any associated UI */
125 pthread_mutex_t mutex
;
126 jack_client_t
* jack_client
;
127 GSList
*input_channels_list
;
128 GSList
*output_channels_list
;
129 GSList
*soloed_channels
;
131 jack_port_t
* port_midi_in
;
132 jack_port_t
* port_midi_out
;
133 int last_midi_channel
;
134 enum midi_behavior_mode midi_behavior
;
136 struct channel
* midi_cc_map
[128];
139 static jack_mixer_output_channel_t
create_output_channel(
141 const char * channel_name
,
146 update_channel_buffers(
147 struct channel
* channel_ptr
,
148 jack_nframes_t nframes
);
160 return 20.0 * log10f(value
);
167 return powf(10.0, db
/20.0);
170 #define channel_ptr ((struct channel *)channel)
174 jack_mixer_channel_t channel
)
176 return channel_ptr
->name
;
181 jack_mixer_channel_t channel
,
185 size_t channel_name_size
;
189 new_name
= strdup(name
);
190 if (new_name
== NULL
)
195 if (channel_ptr
->name
)
197 free(channel_ptr
->name
);
200 channel_ptr
->name
= new_name
;
202 if (channel_ptr
->stereo
)
204 channel_name_size
= strlen(name
);
205 port_name
= malloc(channel_name_size
+ 3);
206 memcpy(port_name
, name
, channel_name_size
);
208 port_name
[channel_name_size
] = ' ';
209 port_name
[channel_name_size
+1] = 'L';
210 port_name
[channel_name_size
+2] = 0;
212 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
, port_name
);
215 /* what could we do here? */
218 port_name
[channel_name_size
+1] = 'R';
220 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
, port_name
);
223 /* what could we do here? */
230 ret
= jack_port_rename(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
, name
);
233 /* what could we do here? */
240 jack_mixer_channel_t channel
)
242 return channel_ptr
->stereo
;
246 channel_get_balance_midi_cc(
247 jack_mixer_channel_t channel
)
249 return channel_ptr
->midi_cc_balance_index
;
253 channel_unset_midi_cc_map(
254 jack_mixer_channel_t channel
,
256 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_volume_index
== new_cc
) {
257 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_volume_index
= -1;
258 } else if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_balance_index
== new_cc
) {
259 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_balance_index
= -1;
260 } else if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_mute_index
== new_cc
) {
261 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_mute_index
= -1;
262 } else if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_solo_index
== new_cc
) {
263 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
]->midi_cc_solo_index
= -1;
268 channel_set_balance_midi_cc(
269 jack_mixer_channel_t channel
,
272 if (new_cc
< 0 || new_cc
> 127) {
273 return 2; /* error: outside limit CC */
275 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
276 channel_unset_midi_cc_map(channel
, new_cc
);
278 if (channel_ptr
->midi_cc_balance_index
!= -1) {
279 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
281 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
282 channel_ptr
->midi_cc_balance_index
= new_cc
;
287 channel_get_volume_midi_cc(
288 jack_mixer_channel_t channel
)
290 return channel_ptr
->midi_cc_volume_index
;
294 channel_set_volume_midi_cc(
295 jack_mixer_channel_t channel
, int new_cc
)
297 if (new_cc
< 0 || new_cc
> 127) {
298 return 2; /* error: outside limit CC */
300 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
301 channel_unset_midi_cc_map(channel
, new_cc
);
303 if (channel_ptr
->midi_cc_volume_index
!= -1) {
304 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
306 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
307 channel_ptr
->midi_cc_volume_index
= new_cc
;
312 channel_get_mute_midi_cc(
313 jack_mixer_channel_t channel
)
315 return channel_ptr
->midi_cc_mute_index
;
319 channel_set_mute_midi_cc(
320 jack_mixer_channel_t channel
,
323 if (new_cc
< 0 || new_cc
> 127) {
324 return 2; /* error: outside limit CC */
326 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
327 channel_unset_midi_cc_map(channel
, new_cc
);
330 if (channel_ptr
->midi_cc_mute_index
!= -1) {
331 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
333 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
334 channel_ptr
->midi_cc_mute_index
= new_cc
;
339 channel_get_solo_midi_cc(
340 jack_mixer_channel_t channel
)
342 return channel_ptr
->midi_cc_solo_index
;
345 void channel_set_midi_cc_volume_picked_up(jack_mixer_channel_t channel
, bool status
)
347 LOG_DEBUG("Setting channel %s volume picked up to %d", channel_ptr
->name
, status
);
348 channel_ptr
->midi_cc_volume_picked_up
= status
;
351 void channel_set_midi_cc_balance_picked_up(jack_mixer_channel_t channel
, bool status
)
353 LOG_DEBUG("Setting channel %s balance picked up to %d", channel_ptr
->name
, status
);
354 channel_ptr
->midi_cc_balance_picked_up
= status
;
358 channel_set_solo_midi_cc(
359 jack_mixer_channel_t channel
,
362 if (new_cc
< 0 || new_cc
> 127) {
363 return 2; /* error: outside limit CC */
365 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
366 channel_unset_midi_cc_map(channel
, new_cc
);
368 if (channel_ptr
->midi_cc_solo_index
!= -1) {
369 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
371 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
372 channel_ptr
->midi_cc_solo_index
= new_cc
;
377 channel_autoset_volume_midi_cc(
378 jack_mixer_channel_t channel
)
380 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
382 for (int i
= 11 ; i
< 128 ; i
++)
384 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
386 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
387 channel_ptr
->midi_cc_volume_index
= i
;
389 LOG_DEBUG("New channel \"%s\" volume mapped to CC#%i", channel_ptr
->name
, i
);
397 channel_autoset_balance_midi_cc(
398 jack_mixer_channel_t channel
)
400 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
402 for (int i
= 11; i
< 128 ; i
++)
404 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
406 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
407 channel_ptr
->midi_cc_balance_index
= i
;
409 LOG_DEBUG("New channel \"%s\" balance mapped to CC#%i", channel_ptr
->name
, i
);
417 channel_autoset_mute_midi_cc(
418 jack_mixer_channel_t channel
)
420 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
422 for (int i
= 11; i
< 128 ; i
++)
424 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
426 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
427 channel_ptr
->midi_cc_mute_index
= i
;
429 LOG_DEBUG("New channel \"%s\" mute mapped to CC#%i", channel_ptr
->name
, i
);
437 channel_autoset_solo_midi_cc(
438 jack_mixer_channel_t channel
)
440 struct jack_mixer
*mixer_ptr
= channel_ptr
->mixer_ptr
;
442 for (int i
= 11; i
< 128 ; i
++)
444 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
446 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
447 channel_ptr
->midi_cc_solo_index
= i
;
449 LOG_DEBUG("New channel \"%s\" solo mapped to CC#%i", channel_ptr
->name
, i
);
458 jack_mixer_channel_t channel
)
461 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_remove(
462 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
463 free(channel_ptr
->name
);
465 /* remove references to input channel from all output channels */
466 for (list_ptr
= channel_ptr
->mixer_ptr
->output_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
468 struct output_channel
*output_channel_ptr
= list_ptr
->data
;
469 output_channel_set_solo(output_channel_ptr
, channel
, false);
470 output_channel_set_muted(output_channel_ptr
, channel
, false);
473 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
474 if (channel_ptr
->stereo
)
476 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
479 if (channel_ptr
->midi_cc_volume_index
!= -1)
481 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
482 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
485 if (channel_ptr
->midi_cc_balance_index
!= -1)
487 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
488 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
491 if (channel_ptr
->midi_cc_mute_index
!= -1)
493 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] == channel_ptr
);
494 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
496 if (channel_ptr
->midi_cc_solo_index
!= -1)
498 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] == channel_ptr
);
499 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
502 free(channel_ptr
->frames_left
);
503 free(channel_ptr
->frames_right
);
504 free(channel_ptr
->prefader_frames_left
);
505 free(channel_ptr
->prefader_frames_right
);
511 channel_stereo_meter_read(
512 jack_mixer_channel_t channel
,
517 *left_ptr
= value_to_db(channel_ptr
->meter_left
);
518 *right_ptr
= value_to_db(channel_ptr
->meter_right
);
521 double interpolate(double start
, double end
, int step
, int steps
) {
524 LOG_DEBUG("%f -> %f, %d", start
, end
, step
);
526 if (step
<= frac
* steps
) {
527 ret
= frac
* end
* step
/ steps
;
529 ret
= db_to_value(value_to_db(frac
* end
) + (value_to_db(end
) - value_to_db(frac
* end
)) * step
/ steps
);;
531 } else if (end
<= 0) {
532 if (step
>= (1 - frac
) * steps
) {
533 ret
= frac
* start
- frac
* start
* step
/ steps
;
535 ret
= db_to_value(value_to_db(start
) + (value_to_db(frac
* start
) - value_to_db(start
)) * step
/ steps
);
538 ret
= db_to_value(value_to_db(start
) + (value_to_db(end
) - value_to_db(start
)) *step
/steps
);
540 LOG_DEBUG("interpolate: %f", ret
);
545 channel_mono_meter_read(
546 jack_mixer_channel_t channel
,
549 *mono_ptr
= value_to_db(channel_ptr
->meter_left
);
553 channel_volume_write(
554 jack_mixer_channel_t channel
,
558 /*If changing volume and find we're in the middle of a previous transition,
559 *then set current volume to place in transition to avoid a jump.*/
560 if (channel_ptr
->volume_new
!= channel_ptr
->volume
) {
561 channel_ptr
->volume
= interpolate(channel_ptr
->volume
, channel_ptr
->volume_new
, channel_ptr
->volume_idx
,
562 channel_ptr
->num_volume_transition_steps
);
564 channel_ptr
->volume_idx
= 0;
565 channel_ptr
->volume_new
= db_to_value(volume
);
566 channel_ptr
->midi_out_has_events
= true;
571 jack_mixer_channel_t channel
)
574 return value_to_db(channel_ptr
->volume_new
);
578 channels_volumes_read(jack_mixer_t mixer_ptr
)
581 struct channel
*pChannel
;
582 struct jack_mixer
* pMixer
= (struct jack_mixer
*)mixer_ptr
;
584 for (node_ptr
= pMixer
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
586 pChannel
= (struct channel
*)node_ptr
->data
;
587 double vol
= channel_volume_read( (jack_mixer_channel_t
)pChannel
);
588 printf("%s : volume is %f dbFS for mixer channel: %s\n", jack_get_client_name(pMixer
->jack_client
), vol
, pChannel
->name
);
593 channel_balance_write(
594 jack_mixer_channel_t channel
,
598 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
599 channel_ptr
->balance
= channel_ptr
->balance
+ channel_ptr
->balance_idx
*
600 (channel_ptr
->balance_new
- channel_ptr
->balance
) /
601 channel_ptr
->num_volume_transition_steps
;
603 channel_ptr
->balance_idx
= 0;
604 channel_ptr
->balance_new
= balance
;
608 channel_balance_read(
609 jack_mixer_channel_t channel
)
612 return channel_ptr
->balance_new
;
616 channel_abspeak_read(
617 jack_mixer_channel_t channel
)
620 if (channel_ptr
->NaN_detected
)
626 return value_to_db(channel_ptr
->abspeak
);
631 channel_abspeak_reset(
632 jack_mixer_channel_t channel
)
634 channel_ptr
->abspeak
= 0;
635 channel_ptr
->NaN_detected
= false;
640 jack_mixer_channel_t channel
)
642 channel_ptr
->out_mute
= true;
647 jack_mixer_channel_t channel
)
649 channel_ptr
->out_mute
= false;
653 channel_is_out_muted(
654 jack_mixer_channel_t channel
)
656 return channel_ptr
->out_mute
;
661 jack_mixer_channel_t channel
)
663 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
) != NULL
)
665 channel_ptr
->mixer_ptr
->soloed_channels
= g_slist_prepend(channel_ptr
->mixer_ptr
->soloed_channels
, channel
);
670 jack_mixer_channel_t channel
)
672 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
) == NULL
)
674 channel_ptr
->mixer_ptr
->soloed_channels
= g_slist_remove(channel_ptr
->mixer_ptr
->soloed_channels
, channel
);
679 jack_mixer_channel_t channel
)
681 if (g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel
))
687 channel_set_midi_scale(
688 jack_mixer_channel_t channel
,
689 jack_mixer_scale_t scale
)
691 channel_ptr
->midi_scale
= scale
;
695 channel_set_midi_change_callback(
696 jack_mixer_channel_t channel
,
697 void (*midi_change_callback
) (void*),
700 channel_ptr
->midi_change_callback
= midi_change_callback
;
701 channel_ptr
->midi_change_callback_data
= user_data
;
705 channel_get_midi_in_got_events(
706 jack_mixer_channel_t channel
)
708 bool t
= channel_ptr
->midi_in_got_events
;
709 channel_ptr
->midi_in_got_events
= false;
715 /* process input channels and mix them into main mix */
718 struct output_channel
*output_mix_channel
,
719 GSList
*channels_list
,
720 jack_nframes_t start
, /* index of first sample to process */
721 jack_nframes_t end
) /* index of sample to stop processing before */
725 struct channel
* channel_ptr
;
726 jack_default_audio_sample_t frame_left
;
727 jack_default_audio_sample_t frame_right
;
728 struct channel
*mix_channel
= (struct channel
*)output_mix_channel
;
730 for (i
= start
; i
< end
; i
++)
732 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
] = 0.0;
733 if (mix_channel
->stereo
)
734 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
] = 0.0;
737 for (node_ptr
= channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
739 channel_ptr
= node_ptr
->data
;
741 if (g_slist_find(output_mix_channel
->muted_channels
, channel_ptr
) != NULL
|| channel_ptr
->out_mute
) {
742 /* skip muted channels */
746 if ((!channel_ptr
->mixer_ptr
->soloed_channels
&& !output_mix_channel
->soloed_channels
) ||
747 (channel_ptr
->mixer_ptr
->soloed_channels
&&
748 g_slist_find(channel_ptr
->mixer_ptr
->soloed_channels
, channel_ptr
) != NULL
) ||
749 (output_mix_channel
->soloed_channels
&&
750 g_slist_find(output_mix_channel
->soloed_channels
, channel_ptr
) != NULL
)) {
752 for (i
= start
; i
< end
; i
++)
754 if (! output_mix_channel
->prefader
&&
755 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
) {
756 frame_left
= channel_ptr
->frames_left
[i
-start
];
758 frame_left
= channel_ptr
->prefader_frames_left
[i
-start
];
760 if (frame_left
== NAN
)
762 mix_channel
->tmp_mixed_frames_left
[i
] += frame_left
;
763 if (mix_channel
->stereo
)
765 if (! output_mix_channel
->prefader
&&
766 g_slist_find(output_mix_channel
->prefader_channels
, channel_ptr
) == NULL
) {
767 frame_right
= channel_ptr
->frames_right
[i
-start
];
769 frame_right
= channel_ptr
->prefader_frames_right
[i
-start
];
771 if (frame_right
== NAN
)
773 mix_channel
->tmp_mixed_frames_right
[i
] += frame_right
;
779 /* process main mix channel */
780 unsigned int steps
= mix_channel
->num_volume_transition_steps
;
781 for (i
= start
; i
< end
; i
++)
783 if (! output_mix_channel
->prefader
) {
784 float volume
= mix_channel
->volume
;
785 float volume_new
= mix_channel
->volume_new
;
787 float balance
= mix_channel
->balance
;
788 float balance_new
= mix_channel
->balance_new
;
790 if (volume
!= volume_new
) {
791 vol
= interpolate(volume
, volume_new
, mix_channel
->volume_idx
, steps
);
793 if (balance
!= balance_new
) {
794 bal
= mix_channel
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
799 if (mix_channel
->stereo
) {
801 vol_l
= vol
* (1 - bal
);
805 vol_r
= vol
* (1 + bal
);
808 vol_l
= vol
* (1 - bal
);
809 vol_r
= vol
* (1 + bal
);
811 mix_channel
->tmp_mixed_frames_left
[i
] *= vol_l
;
812 mix_channel
->tmp_mixed_frames_right
[i
] *= vol_r
;
815 frame_left
= fabsf(mix_channel
->tmp_mixed_frames_left
[i
]);
816 if (mix_channel
->peak_left
< frame_left
)
818 mix_channel
->peak_left
= frame_left
;
820 if (frame_left
> mix_channel
->abspeak
)
822 mix_channel
->abspeak
= frame_left
;
826 if (mix_channel
->stereo
)
828 frame_right
= fabsf(mix_channel
->tmp_mixed_frames_right
[i
]);
829 if (mix_channel
->peak_right
< frame_right
)
831 mix_channel
->peak_right
= frame_right
;
833 if (frame_right
> mix_channel
->abspeak
)
835 mix_channel
->abspeak
= frame_right
;
840 mix_channel
->peak_frames
++;
841 if (mix_channel
->peak_frames
>= PEAK_FRAMES_CHUNK
)
843 mix_channel
->meter_left
= mix_channel
->peak_left
;
844 mix_channel
->peak_left
= 0.0;
846 if (mix_channel
->stereo
)
848 mix_channel
->meter_right
= mix_channel
->peak_right
;
849 mix_channel
->peak_right
= 0.0;
852 mix_channel
->peak_frames
= 0;
854 mix_channel
->volume_idx
++;
855 if ((mix_channel
->volume
!= mix_channel
->volume_new
) && (mix_channel
->volume_idx
== steps
)) {
856 mix_channel
->volume
= mix_channel
->volume_new
;
857 mix_channel
->volume_idx
= 0;
859 mix_channel
->balance_idx
++;
860 if ((mix_channel
->balance
!= mix_channel
->balance_new
) && (mix_channel
->balance_idx
== steps
)) {
861 mix_channel
->balance
= mix_channel
->balance_new
;
862 mix_channel
->balance_idx
= 0;
865 if (!mix_channel
->out_mute
) {
866 mix_channel
->left_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_left
[i
];
867 if (mix_channel
->stereo
)
868 mix_channel
->right_buffer_ptr
[i
] = mix_channel
->tmp_mixed_frames_right
[i
];
875 struct channel
*channel_ptr
,
876 jack_nframes_t start
,
880 jack_default_audio_sample_t frame_left
= 0.0f
;
881 jack_default_audio_sample_t frame_right
= 0.0f
;
882 unsigned int steps
= channel_ptr
->num_volume_transition_steps
;
883 for (i
= start
; i
< end
; i
++)
885 if (i
-start
>= MAX_BLOCK_SIZE
)
887 fprintf(stderr
, "i-start too high: %d - %d\n", i
, start
);
889 channel_ptr
->prefader_frames_left
[i
-start
] = channel_ptr
->left_buffer_ptr
[i
];
890 if (channel_ptr
->stereo
)
891 channel_ptr
->prefader_frames_right
[i
-start
] = channel_ptr
->right_buffer_ptr
[i
];
893 if (!FLOAT_EXISTS(channel_ptr
->left_buffer_ptr
[i
]))
895 channel_ptr
->NaN_detected
= true;
896 channel_ptr
->frames_left
[i
-start
] = NAN
;
899 float volume
= channel_ptr
->volume
;
900 float volume_new
= channel_ptr
->volume_new
;
902 float balance
= channel_ptr
->balance
;
903 float balance_new
= channel_ptr
->balance_new
;
905 if (channel_ptr
->volume
!= channel_ptr
->volume_new
) {
906 vol
= interpolate(volume
, volume_new
, channel_ptr
->volume_idx
, steps
);
908 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
909 bal
= channel_ptr
->balance_idx
* (balance_new
- balance
) / steps
+ balance
;
913 if (channel_ptr
->stereo
) {
915 vol_l
= vol
* (1 - bal
);
919 vol_r
= vol
* (1 + bal
);
922 vol_l
= vol
* (1 - bal
);
923 vol_r
= vol
* (1 + bal
);
925 frame_left
= channel_ptr
->left_buffer_ptr
[i
] * vol_l
;
926 if (channel_ptr
->stereo
)
928 if (!FLOAT_EXISTS(channel_ptr
->right_buffer_ptr
[i
]))
930 channel_ptr
->NaN_detected
= true;
931 channel_ptr
->frames_right
[i
-start
] = NAN
;
935 frame_right
= channel_ptr
->right_buffer_ptr
[i
] * vol_r
;
938 channel_ptr
->frames_left
[i
-start
] = frame_left
;
939 channel_ptr
->frames_right
[i
-start
] = frame_right
;
941 if (channel_ptr
->stereo
)
943 frame_left
= fabsf(frame_left
);
944 frame_right
= fabsf(frame_right
);
946 if (channel_ptr
->peak_left
< frame_left
)
948 channel_ptr
->peak_left
= frame_left
;
950 if (frame_left
> channel_ptr
->abspeak
)
952 channel_ptr
->abspeak
= frame_left
;
956 if (channel_ptr
->peak_right
< frame_right
)
958 channel_ptr
->peak_right
= frame_right
;
960 if (frame_right
> channel_ptr
->abspeak
)
962 channel_ptr
->abspeak
= frame_right
;
968 frame_left
= (fabsf(frame_left
) + fabsf(frame_right
)) / 2;
970 if (channel_ptr
->peak_left
< frame_left
)
972 channel_ptr
->peak_left
= frame_left
;
974 if (frame_left
> channel_ptr
->abspeak
)
976 channel_ptr
->abspeak
= frame_left
;
981 channel_ptr
->peak_frames
++;
982 if (channel_ptr
->peak_frames
>= PEAK_FRAMES_CHUNK
)
984 channel_ptr
->meter_left
= channel_ptr
->peak_left
;
985 channel_ptr
->peak_left
= 0.0;
987 if (channel_ptr
->stereo
)
989 channel_ptr
->meter_right
= channel_ptr
->peak_right
;
990 channel_ptr
->peak_right
= 0.0;
993 channel_ptr
->peak_frames
= 0;
995 channel_ptr
->volume_idx
++;
996 if ((channel_ptr
->volume
!= channel_ptr
->volume_new
) &&
997 (channel_ptr
->volume_idx
== steps
)) {
998 channel_ptr
->volume
= channel_ptr
->volume_new
;
999 channel_ptr
->volume_idx
= 0;
1001 channel_ptr
->balance_idx
++;
1002 if ((channel_ptr
->balance
!= channel_ptr
->balance_new
) &&
1003 (channel_ptr
->balance_idx
>= steps
)) {
1004 channel_ptr
->balance
= channel_ptr
->balance_new
;
1005 channel_ptr
->balance_idx
= 0;
1012 struct jack_mixer
* mixer_ptr
,
1013 jack_nframes_t start
, /* index of first sample to process */
1014 jack_nframes_t end
) /* index of sample to stop processing before */
1017 struct output_channel
* output_channel_ptr
;
1018 struct channel
*channel_ptr
;
1020 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1022 channel_ptr
= (struct channel
*)node_ptr
->data
;
1023 calc_channel_frames(channel_ptr
, start
, end
);
1026 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1028 output_channel_ptr
= node_ptr
->data
;
1029 channel_ptr
= (struct channel
*)output_channel_ptr
;
1031 if (output_channel_ptr
->system
)
1033 /* Don't bother mixing the channels if we are not connected */
1034 if (channel_ptr
->stereo
)
1036 if (jack_port_connected(channel_ptr
->port_left
) == 0 &&
1037 jack_port_connected(channel_ptr
->port_right
) == 0)
1040 if (jack_port_connected(channel_ptr
->port_left
) == 0)
1045 mix_one(output_channel_ptr
, mixer_ptr
->input_channels_list
, start
, end
);
1050 update_channel_buffers(
1051 struct channel
* channel_ptr
,
1052 jack_nframes_t nframes
)
1054 channel_ptr
->left_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_left
, nframes
);
1056 if (channel_ptr
->stereo
)
1058 channel_ptr
->right_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_right
, nframes
);
1062 #define mixer_ptr ((struct jack_mixer *)context)
1066 jack_nframes_t nframes
,
1070 struct channel
* channel_ptr
;
1071 #if defined(HAVE_JACK_MIDI)
1073 jack_nframes_t event_count
;
1074 jack_midi_event_t in_event
;
1075 unsigned char* midi_out_buffer
;
1078 unsigned int cc_channel_index
;
1081 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1083 channel_ptr
= node_ptr
->data
;
1084 update_channel_buffers(channel_ptr
, nframes
);
1087 // Fill output buffers with the input
1088 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
1090 channel_ptr
= node_ptr
->data
;
1091 update_channel_buffers(channel_ptr
, nframes
);
1094 #if defined(HAVE_JACK_MIDI)
1095 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_in
, nframes
);
1096 event_count
= jack_midi_get_event_count(midi_buffer
);
1098 for (i
= 0 ; i
< event_count
; i
++)
1100 jack_midi_event_get(&in_event
, midi_buffer
, i
);
1102 if (in_event
.size
!= 3 ||
1103 (in_event
.buffer
[0] & 0xF0) != 0xB0 ||
1104 in_event
.buffer
[1] > 127 ||
1105 in_event
.buffer
[2] > 127)
1110 assert(in_event
.time
< nframes
);
1114 (unsigned int)(in_event
.buffer
[0]),
1115 (unsigned int)in_event
.buffer
[1],
1116 (unsigned int)in_event
.buffer
[2]);
1118 mixer_ptr
->last_midi_channel
= (int)in_event
.buffer
[1];
1119 channel_ptr
= mixer_ptr
->midi_cc_map
[in_event
.buffer
[1]];
1121 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
1122 if (channel_ptr
!= NULL
&& channel_ptr
->midi_scale
!= NULL
)
1124 if (channel_ptr
->midi_cc_balance_index
== (char)in_event
.buffer
[1])
1126 byte
= in_event
.buffer
[2];
1132 int current_midi
= (int)63 * channel_ptr
->balance
+ 64;
1133 current_midi
= current_midi
== 1 ? 0 : current_midi
;
1134 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1135 !channel_ptr
->midi_cc_balance_picked_up
) {
1136 if (in_event
.buffer
[2] == current_midi
) {
1137 channel_set_midi_cc_balance_picked_up(channel_ptr
, true);
1140 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1141 channel_ptr
->midi_cc_balance_picked_up
) ||
1142 mixer_ptr
->midi_behavior
== Jump_To_Value
) {
1143 if (channel_ptr
->balance
!= channel_ptr
->balance_new
) {
1144 channel_ptr
->balance
= channel_ptr
->balance
+ channel_ptr
->balance_idx
*
1145 (channel_ptr
->balance_new
- channel_ptr
->balance
) /
1146 channel_ptr
->num_volume_transition_steps
;
1148 channel_ptr
->balance_idx
= 0;
1149 channel_ptr
->balance_new
= (float)byte
/ 63;
1150 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr
->name
, channel_ptr
->balance_new
);
1153 else if (channel_ptr
->midi_cc_volume_index
== in_event
.buffer
[1])
1155 int current_midi
= (int)(127 *
1156 scale_db_to_scale(channel_ptr
->midi_scale
,
1157 value_to_db(channel_ptr
->volume
)));
1158 if (mixer_ptr
->midi_behavior
== Pick_Up
&&
1159 !channel_ptr
->midi_cc_volume_picked_up
) {
1160 if (in_event
.buffer
[2] == current_midi
) {
1161 channel_set_midi_cc_volume_picked_up(channel_ptr
, true);
1164 if ((mixer_ptr
->midi_behavior
== Pick_Up
&&
1165 channel_ptr
->midi_cc_volume_picked_up
) ||
1166 mixer_ptr
->midi_behavior
== Jump_To_Value
) {
1167 if (channel_ptr
->volume_new
!= channel_ptr
->volume
) {
1168 channel_ptr
->volume
= interpolate(channel_ptr
->volume
, channel_ptr
->volume_new
, channel_ptr
->volume_idx
,
1169 channel_ptr
->num_volume_transition_steps
);
1171 channel_ptr
->volume_idx
= 0;
1172 channel_ptr
->volume_new
= db_to_value(scale_scale_to_db(channel_ptr
->midi_scale
,
1173 (double)in_event
.buffer
[2] / 127));
1174 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr
->name
, channel_ptr
->volume_new
);
1177 else if (channel_ptr
->midi_cc_mute_index
== in_event
.buffer
[1])
1179 if ((unsigned int)in_event
.buffer
[2] == 127) {
1180 channel_ptr
->out_mute
= !channel_ptr
->out_mute
;
1182 LOG_DEBUG("\"%s\" out_mute %d", channel_ptr
->name
, channel_ptr
->out_mute
);
1184 else if (channel_ptr
->midi_cc_solo_index
== in_event
.buffer
[1])
1186 if ((unsigned int)in_event
.buffer
[2] == 127) {
1187 if (channel_is_soloed(channel_ptr
)) {
1188 channel_unsolo(channel_ptr
);
1190 channel_solo(channel_ptr
);
1193 LOG_DEBUG("\"%s\" solo %d", channel_ptr
->name
, channel_is_soloed(channel_ptr
));
1195 channel_ptr
->midi_in_got_events
= true;
1196 if (channel_ptr
->midi_change_callback
)
1197 channel_ptr
->midi_change_callback(channel_ptr
->midi_change_callback_data
);
1203 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_out
, nframes
);
1204 jack_midi_clear_buffer(midi_buffer
);
1206 for(i
=0; i
<nframes
; i
++)
1208 for (cc_channel_index
=0; cc_channel_index
<128; cc_channel_index
++)
1210 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_channel_index
];
1211 if (channel_ptr
== NULL
|| channel_ptr
->midi_scale
== NULL
)
1215 if (channel_ptr
->midi_out_has_events
== false)
1219 if (channel_ptr
->midi_cc_balance_index
== (int)cc_channel_index
)
1223 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, i
, 3);
1224 if (midi_out_buffer
== NULL
)
1228 midi_out_buffer
[0] = 0xB0; /* control change */
1229 midi_out_buffer
[1] = cc_channel_index
;
1230 midi_out_buffer
[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr
->midi_scale
, value_to_db(channel_ptr
->volume_new
)));
1234 (unsigned int)(midi_out_buffer
[0]),
1235 (unsigned int)midi_out_buffer
[1],
1236 (unsigned int)midi_out_buffer
[2]);
1238 channel_ptr
->midi_out_has_events
= false;
1244 mix(mixer_ptr
, 0, nframes
);
1253 const char * jack_client_name_ptr
,
1258 struct jack_mixer
* mixer_ptr
;
1262 mixer_ptr
= malloc(sizeof(struct jack_mixer
));
1263 if (mixer_ptr
== NULL
)
1268 ret
= pthread_mutex_init(&mixer_ptr
->mutex
, NULL
);
1274 mixer_ptr
->input_channels_list
= NULL
;
1275 mixer_ptr
->output_channels_list
= NULL
;
1277 mixer_ptr
->soloed_channels
= NULL
;
1279 mixer_ptr
->last_midi_channel
= -1;
1281 mixer_ptr
->midi_behavior
= Jump_To_Value
;
1283 for (i
= 0 ; i
< 128 ; i
++)
1285 mixer_ptr
->midi_cc_map
[i
] = NULL
;
1288 LOG_DEBUG("Initializing JACK");
1289 mixer_ptr
->jack_client
= jack_client_open(jack_client_name_ptr
, 0, NULL
);
1290 if (mixer_ptr
->jack_client
== NULL
)
1292 LOG_ERROR("Cannot create JACK client.");
1293 LOG_NOTICE("Please make sure JACK daemon is running.");
1294 goto exit_destroy_mutex
;
1297 LOG_DEBUG("JACK client created");
1299 LOG_DEBUG("Sample rate: %" PRIu32
, jack_get_sample_rate(mixer_ptr
->jack_client
));
1302 #if defined(HAVE_JACK_MIDI)
1303 mixer_ptr
->port_midi_in
= jack_port_register(mixer_ptr
->jack_client
, "midi in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1304 if (mixer_ptr
->port_midi_in
== NULL
)
1306 LOG_ERROR("Cannot create JACK MIDI in port");
1310 mixer_ptr
->port_midi_out
= jack_port_register(mixer_ptr
->jack_client
, "midi out", JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1311 if (mixer_ptr
->port_midi_out
== NULL
)
1313 LOG_ERROR("Cannot create JACK MIDI out port");
1319 ret
= jack_set_process_callback(mixer_ptr
->jack_client
, process
, mixer_ptr
);
1322 LOG_ERROR("Cannot set JACK process callback");
1326 ret
= jack_activate(mixer_ptr
->jack_client
);
1329 LOG_ERROR("Cannot activate JACK client");
1336 jack_client_close(mixer_ptr
->jack_client
); /* this should clear all other resources we obtained through the client handle */
1339 pthread_mutex_destroy(&mixer_ptr
->mutex
);
1348 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1354 LOG_DEBUG("Uninitializing JACK");
1356 assert(mixer_ctx_ptr
->jack_client
!= NULL
);
1358 jack_client_close(mixer_ctx_ptr
->jack_client
);
1360 pthread_mutex_destroy(&mixer_ctx_ptr
->mutex
);
1362 free(mixer_ctx_ptr
);
1370 return g_slist_length(mixer_ctx_ptr
->input_channels_list
);
1377 return jack_get_client_name(mixer_ctx_ptr
->jack_client
);
1381 get_last_midi_channel(
1384 return mixer_ctx_ptr
->last_midi_channel
;
1388 set_last_midi_channel(
1391 mixer_ctx_ptr
->last_midi_channel
= new_channel
;
1396 get_midi_behavior_mode(
1399 return mixer_ctx_ptr
->midi_behavior
;
1403 set_midi_behavior_mode(
1405 enum midi_behavior_mode mode
)
1407 mixer_ctx_ptr
->midi_behavior
= mode
;
1411 jack_mixer_channel_t
1414 const char * channel_name
,
1417 struct channel
* channel_ptr
;
1418 char * port_name
= NULL
;
1419 size_t channel_name_size
;
1421 channel_ptr
= malloc(sizeof(struct channel
));
1422 if (channel_ptr
== NULL
)
1427 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1429 channel_ptr
->name
= strdup(channel_name
);
1430 if (channel_ptr
->name
== NULL
)
1432 goto fail_free_channel
;
1435 channel_name_size
= strlen(channel_name
);
1439 port_name
= malloc(channel_name_size
+ 3);
1440 if (port_name
== NULL
)
1442 goto fail_free_channel_name
;
1445 memcpy(port_name
, channel_name
, channel_name_size
);
1446 port_name
[channel_name_size
] = ' ';
1447 port_name
[channel_name_size
+1] = 'L';
1448 port_name
[channel_name_size
+2] = 0;
1450 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1451 if (channel_ptr
->port_left
== NULL
)
1453 goto fail_free_port_name
;
1456 port_name
[channel_name_size
+1] = 'R';
1458 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1459 if (channel_ptr
->port_right
== NULL
)
1461 goto fail_unregister_left_channel
;
1466 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1467 if (channel_ptr
->port_left
== NULL
)
1469 goto fail_free_channel_name
;
1473 channel_ptr
->stereo
= stereo
;
1475 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1476 channel_ptr
->num_volume_transition_steps
=
1477 channel_ptr
->volume_transition_seconds
*
1478 jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
) + 1;
1479 channel_ptr
->volume
= 0.0;
1480 channel_ptr
->volume_new
= 0.0;
1481 channel_ptr
->balance
= 0.0;
1482 channel_ptr
->balance_new
= 0.0;
1483 channel_ptr
->meter_left
= -1.0;
1484 channel_ptr
->meter_right
= -1.0;
1485 channel_ptr
->abspeak
= 0.0;
1486 channel_ptr
->out_mute
= false;
1488 channel_ptr
->peak_left
= 0.0;
1489 channel_ptr
->peak_right
= 0.0;
1490 channel_ptr
->peak_frames
= 0;
1492 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1493 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1494 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1495 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1497 channel_ptr
->NaN_detected
= false;
1499 channel_ptr
->midi_cc_volume_index
= -1;
1500 channel_ptr
->midi_cc_balance_index
= -1;
1501 channel_ptr
->midi_cc_mute_index
= -1;
1502 channel_ptr
->midi_cc_solo_index
= -1;
1503 channel_ptr
->midi_cc_volume_picked_up
= false;
1504 channel_ptr
->midi_cc_balance_picked_up
= false;
1506 channel_ptr
->midi_change_callback
= NULL
;
1507 channel_ptr
->midi_change_callback_data
= NULL
;
1508 channel_ptr
->midi_out_has_events
= false;
1510 channel_ptr
->midi_scale
= NULL
;
1512 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_prepend(
1513 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
1518 fail_unregister_left_channel
:
1519 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1521 fail_free_port_name
:
1524 fail_free_channel_name
:
1525 free(channel_ptr
->name
);
1535 static jack_mixer_output_channel_t
1536 create_output_channel(
1538 const char * channel_name
,
1542 struct channel
* channel_ptr
;
1543 struct output_channel
* output_channel_ptr
;
1544 char * port_name
= NULL
;
1545 size_t channel_name_size
;
1547 output_channel_ptr
= malloc(sizeof(struct output_channel
));
1548 channel_ptr
= (struct channel
*)output_channel_ptr
;
1549 if (channel_ptr
== NULL
)
1554 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1556 channel_ptr
->name
= strdup(channel_name
);
1557 if (channel_ptr
->name
== NULL
)
1559 goto fail_free_channel
;
1564 channel_name_size
= strlen(channel_name
);
1566 port_name
= malloc(channel_name_size
+ 4);
1567 if (port_name
== NULL
)
1569 goto fail_free_channel_name
;
1572 memcpy(port_name
, channel_name
, channel_name_size
);
1573 port_name
[channel_name_size
] = ' ';
1574 port_name
[channel_name_size
+1] = 'L';
1575 port_name
[channel_name_size
+2] = 0;
1577 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1578 if (channel_ptr
->port_left
== NULL
)
1580 goto fail_free_port_name
;
1583 port_name
[channel_name_size
+1] = 'R';
1585 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1586 if (channel_ptr
->port_right
== NULL
)
1588 goto fail_unregister_left_channel
;
1593 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1594 if (channel_ptr
->port_left
== NULL
)
1596 goto fail_free_channel_name
;
1600 channel_ptr
->stereo
= stereo
;
1601 channel_ptr
->out_mute
= false;
1603 channel_ptr
->volume_transition_seconds
= VOLUME_TRANSITION_SECONDS
;
1604 channel_ptr
->num_volume_transition_steps
=
1605 channel_ptr
->volume_transition_seconds
*
1606 jack_get_sample_rate(channel_ptr
->mixer_ptr
->jack_client
) + 1;
1607 channel_ptr
->volume
= 0.0;
1608 channel_ptr
->volume_new
= 0.0;
1609 channel_ptr
->balance
= 0.0;
1610 channel_ptr
->balance_new
= 0.0;
1611 channel_ptr
->meter_left
= -1.0;
1612 channel_ptr
->meter_right
= -1.0;
1613 channel_ptr
->abspeak
= 0.0;
1615 channel_ptr
->peak_left
= 0.0;
1616 channel_ptr
->peak_right
= 0.0;
1617 channel_ptr
->peak_frames
= 0;
1619 channel_ptr
->tmp_mixed_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1620 channel_ptr
->tmp_mixed_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1621 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1622 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1623 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1624 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1626 channel_ptr
->NaN_detected
= false;
1628 channel_ptr
->midi_cc_volume_index
= -1;
1629 channel_ptr
->midi_cc_balance_index
= -1;
1630 channel_ptr
->midi_cc_mute_index
= -1;
1631 channel_ptr
->midi_cc_solo_index
= -1;
1632 channel_ptr
->midi_cc_volume_picked_up
= false;
1633 channel_ptr
->midi_cc_balance_picked_up
= false;
1635 channel_ptr
->midi_change_callback
= NULL
;
1636 channel_ptr
->midi_change_callback_data
= NULL
;
1638 channel_ptr
->midi_scale
= NULL
;
1640 output_channel_ptr
->soloed_channels
= NULL
;
1641 output_channel_ptr
->muted_channels
= NULL
;
1642 output_channel_ptr
->prefader_channels
= NULL
;
1643 output_channel_ptr
->system
= system
;
1644 output_channel_ptr
->prefader
= false;
1647 return output_channel_ptr
;
1649 fail_unregister_left_channel
:
1650 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1652 fail_free_port_name
:
1655 fail_free_channel_name
:
1656 free(channel_ptr
->name
);
1666 jack_mixer_output_channel_t
1669 const char * channel_name
,
1673 struct output_channel
*output_channel_ptr
;
1674 struct channel
*channel_ptr
;
1676 output_channel_ptr
= create_output_channel(mixer
, channel_name
, stereo
, system
);
1677 if (output_channel_ptr
== NULL
) {
1680 channel_ptr
= (struct channel
*)output_channel_ptr
;
1682 ((struct jack_mixer
*)mixer
)->output_channels_list
= g_slist_prepend(
1683 ((struct jack_mixer
*)mixer
)->output_channels_list
, channel_ptr
);
1685 return output_channel_ptr
;
1693 for (list_ptr
= mixer_ctx_ptr
->input_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
1695 struct channel
*input_channel_ptr
= list_ptr
->data
;
1696 remove_channel((jack_mixer_channel_t
)input_channel_ptr
);
1701 remove_output_channel(
1702 jack_mixer_output_channel_t output_channel
)
1704 struct output_channel
*output_channel_ptr
= output_channel
;
1705 struct channel
*channel_ptr
= output_channel
;
1707 channel_ptr
->mixer_ptr
->output_channels_list
= g_slist_remove(
1708 channel_ptr
->mixer_ptr
->output_channels_list
, channel_ptr
);
1709 free(channel_ptr
->name
);
1711 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1712 if (channel_ptr
->stereo
)
1714 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
1717 if (channel_ptr
->midi_cc_volume_index
!= -1)
1719 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
1720 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
1723 if (channel_ptr
->midi_cc_balance_index
!= -1)
1725 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
1726 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
1729 if (channel_ptr
->midi_cc_mute_index
!= -1)
1731 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] == channel_ptr
);
1732 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_mute_index
] = NULL
;
1735 if (channel_ptr
->midi_cc_solo_index
!= -1)
1737 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] == channel_ptr
);
1738 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_solo_index
] = NULL
;
1741 g_slist_free(output_channel_ptr
->soloed_channels
);
1742 g_slist_free(output_channel_ptr
->muted_channels
);
1743 g_slist_free(output_channel_ptr
->prefader_channels
);
1745 free(channel_ptr
->tmp_mixed_frames_left
);
1746 free(channel_ptr
->tmp_mixed_frames_right
);
1747 free(channel_ptr
->frames_left
);
1748 free(channel_ptr
->frames_right
);
1749 free(channel_ptr
->prefader_frames_left
);
1750 free(channel_ptr
->prefader_frames_right
);
1756 output_channel_set_solo(
1757 jack_mixer_output_channel_t output_channel
,
1758 jack_mixer_channel_t channel
,
1761 struct output_channel
*output_channel_ptr
= output_channel
;
1764 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
1766 output_channel_ptr
->soloed_channels
= g_slist_prepend(output_channel_ptr
->soloed_channels
, channel
);
1768 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) == NULL
)
1770 output_channel_ptr
->soloed_channels
= g_slist_remove(output_channel_ptr
->soloed_channels
, channel
);
1775 output_channel_set_muted(
1776 jack_mixer_output_channel_t output_channel
,
1777 jack_mixer_channel_t channel
,
1780 struct output_channel
*output_channel_ptr
= output_channel
;
1783 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
1785 output_channel_ptr
->muted_channels
= g_slist_prepend(output_channel_ptr
->muted_channels
, channel
);
1787 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) == NULL
)
1789 output_channel_ptr
->muted_channels
= g_slist_remove(output_channel_ptr
->muted_channels
, channel
);
1794 output_channel_is_muted(
1795 jack_mixer_output_channel_t output_channel
,
1796 jack_mixer_channel_t channel
)
1798 struct output_channel
*output_channel_ptr
= output_channel
;
1800 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
1806 output_channel_is_solo(
1807 jack_mixer_output_channel_t output_channel
,
1808 jack_mixer_channel_t channel
)
1810 struct output_channel
*output_channel_ptr
= output_channel
;
1812 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
1818 output_channel_set_prefader(
1819 jack_mixer_output_channel_t output_channel
,
1822 struct output_channel
*output_channel_ptr
= output_channel
;
1823 output_channel_ptr
->prefader
= pfl_value
;
1827 output_channel_is_prefader(
1828 jack_mixer_output_channel_t output_channel
)
1830 struct output_channel
*output_channel_ptr
= output_channel
;
1831 return output_channel_ptr
->prefader
;
1835 output_channel_set_in_prefader(
1836 jack_mixer_output_channel_t output_channel
,
1837 jack_mixer_channel_t channel
,
1838 bool prefader_value
)
1840 struct output_channel
*output_channel_ptr
= output_channel
;
1842 if (prefader_value
) {
1843 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)
1845 output_channel_ptr
->prefader_channels
= g_slist_prepend(output_channel_ptr
->prefader_channels
, channel
);
1847 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) == NULL
)
1849 output_channel_ptr
->prefader_channels
= g_slist_remove(output_channel_ptr
->prefader_channels
, channel
);
1854 output_channel_is_in_prefader(
1855 jack_mixer_output_channel_t output_channel
,
1856 jack_mixer_channel_t channel
)
1858 struct output_channel
*output_channel_ptr
= output_channel
;
1860 if (g_slist_find(output_channel_ptr
->prefader_channels
, channel
) != NULL
)