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 PEAK_FRAMES_CHUNK 4800
48 // we don't know how much to allocate, but we don't want to wait with
49 // allocating until we're in the process() callback, so we just take a
50 // fairly big chunk: 4 periods per buffer, 4096 samples per period.
51 // (not sure if the '*4' is needed)
52 #define MAX_BLOCK_SIZE (4 * 4096)
54 #define FLOAT_EXISTS(x) (!((x) - (x)))
58 struct jack_mixer
* mixer_ptr
;
68 jack_port_t
* port_left
;
69 jack_port_t
* port_right
;
71 jack_nframes_t peak_frames
;
75 jack_default_audio_sample_t
* frames_left
;
76 jack_default_audio_sample_t
* frames_right
;
77 jack_default_audio_sample_t
* prefader_frames_left
;
78 jack_default_audio_sample_t
* prefader_frames_right
;
82 int midi_cc_volume_index
;
83 int midi_cc_balance_index
;
85 jack_default_audio_sample_t
* left_buffer_ptr
;
86 jack_default_audio_sample_t
* right_buffer_ptr
;
88 bool midi_in_got_events
;
89 void (*midi_change_callback
) (void*);
90 void *midi_change_callback_data
;
91 bool midi_out_has_events
;
93 jack_mixer_scale_t midi_scale
;
96 struct output_channel
{
97 struct channel channel
;
98 GSList
*soloed_channels
;
99 GSList
*muted_channels
;
100 bool system
; /* system channel, without any associated UI */
106 pthread_mutex_t mutex
;
107 jack_client_t
* jack_client
;
108 GSList
*input_channels_list
;
109 GSList
*output_channels_list
;
110 struct output_channel
*main_mix_channel
;
112 jack_port_t
* port_midi_in
;
113 jack_port_t
* port_midi_out
;
114 unsigned int last_midi_channel
;
116 struct channel
* midi_cc_map
[128];
119 static jack_mixer_output_channel_t
create_output_channel(
121 const char * channel_name
,
126 update_channel_buffers(
127 struct channel
* channel_ptr
,
128 jack_nframes_t nframes
);
140 return 20.0 * log10f(value
);
147 return powf(10.0, db
/20.0);
151 calc_channel_volumes(
152 struct channel
* channel_ptr
)
154 if (channel_ptr
->stereo
)
156 if (channel_ptr
->balance
> 0)
158 channel_ptr
->volume_left
= channel_ptr
->volume
* (1 - channel_ptr
->balance
);
159 channel_ptr
->volume_right
= channel_ptr
->volume
;
163 channel_ptr
->volume_left
= channel_ptr
->volume
;
164 channel_ptr
->volume_right
= channel_ptr
->volume
* (1 + channel_ptr
->balance
);
169 channel_ptr
->volume_left
= channel_ptr
->volume
* (1 - channel_ptr
->balance
);
170 channel_ptr
->volume_right
= channel_ptr
->volume
* (1 + channel_ptr
->balance
);
175 calc_all_channel_volumes(
176 struct jack_mixer
* mixer_ptr
)
178 struct channel
* channel_ptr
;
181 for (list_ptr
= mixer_ptr
->input_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
183 channel_ptr
= list_ptr
->data
;
184 calc_channel_volumes(channel_ptr
);
188 #define channel_ptr ((struct channel *)channel)
192 jack_mixer_channel_t channel
)
194 return channel_ptr
->name
;
199 jack_mixer_channel_t channel
,
203 size_t channel_name_size
;
207 new_name
= strdup(name
);
208 if (new_name
== NULL
)
213 if (channel_ptr
->name
)
215 free(channel_ptr
->name
);
218 channel_ptr
->name
= new_name
;
220 if (channel_ptr
->stereo
)
222 channel_name_size
= strlen(name
);
223 port_name
= malloc(channel_name_size
+ 3);
224 memcpy(port_name
, name
, channel_name_size
);
226 port_name
[channel_name_size
] = ' ';
227 port_name
[channel_name_size
+1] = 'L';
228 port_name
[channel_name_size
+2] = 0;
230 ret
= jack_port_set_name(channel_ptr
->port_left
, port_name
);
233 /* what could we do here? */
236 port_name
[channel_name_size
+1] = 'R';
238 ret
= jack_port_set_name(channel_ptr
->port_right
, port_name
);
241 /* what could we do here? */
248 ret
= jack_port_set_name(channel_ptr
->port_left
, name
);
251 /* what could we do here? */
258 jack_mixer_channel_t channel
)
260 return channel_ptr
->stereo
;
264 channel_get_balance_midi_cc(
265 jack_mixer_channel_t channel
)
267 return channel_ptr
->midi_cc_balance_index
;
271 channel_set_balance_midi_cc(
272 jack_mixer_channel_t channel
,
276 return 2; /* error: over limit CC */
278 if (channel_ptr
->midi_cc_balance_index
== new_cc
) {
283 /* 0 is special, it removes the link */
284 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
285 channel_ptr
->midi_cc_balance_index
= 0;
287 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
288 return 1; /* error: cc in use */
290 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
291 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
292 channel_ptr
->midi_cc_balance_index
= new_cc
;
298 channel_get_volume_midi_cc(
299 jack_mixer_channel_t channel
)
301 return channel_ptr
->midi_cc_volume_index
;
305 channel_set_volume_midi_cc(
306 jack_mixer_channel_t channel
, unsigned int new_cc
)
309 return 2; /* error: over limit CC */
311 if (channel_ptr
->midi_cc_volume_index
== new_cc
) {
316 /* 0 is special, it removes the link */
317 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
318 channel_ptr
->midi_cc_volume_index
= 0;
320 if (channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] != NULL
) {
321 return 1; /* error: cc in use */
323 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
324 channel_ptr
->mixer_ptr
->midi_cc_map
[new_cc
] = channel_ptr
;
325 channel_ptr
->midi_cc_volume_index
= new_cc
;
331 channel_autoset_midi_cc(
332 jack_mixer_channel_t channel
)
334 struct jack_mixer
*mixer_ptr
;
337 mixer_ptr
= channel_ptr
->mixer_ptr
;
339 for (i
= 11 ; i
< 128 ; i
++)
341 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
343 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
344 channel_ptr
->midi_cc_volume_index
= i
;
346 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr
->name
, i
);
352 for (; i
< 128 ; i
++)
354 if (mixer_ptr
->midi_cc_map
[i
] == NULL
)
356 mixer_ptr
->midi_cc_map
[i
] = channel_ptr
;
357 channel_ptr
->midi_cc_balance_index
= i
;
359 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr
->name
, i
);
368 jack_mixer_channel_t channel
)
372 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_remove(
373 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
374 free(channel_ptr
->name
);
376 /* remove references to input channel from all output channels */
377 channel_unmute(channel_ptr
);
378 channel_unsolo(channel_ptr
);
379 for (list_ptr
= channel_ptr
->mixer_ptr
->output_channels_list
; list_ptr
; list_ptr
= g_slist_next(list_ptr
))
381 struct output_channel
*output_channel_ptr
= list_ptr
->data
;
382 output_channel_set_solo(output_channel_ptr
, channel
, false);
383 output_channel_set_muted(output_channel_ptr
, channel
, false);
386 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
387 if (channel_ptr
->stereo
)
389 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
392 if (channel_ptr
->midi_cc_volume_index
!= 0)
394 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
395 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
398 if (channel_ptr
->midi_cc_balance_index
!= 0)
400 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
401 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
408 channel_stereo_meter_read(
409 jack_mixer_channel_t channel
,
414 *left_ptr
= value_to_db(channel_ptr
->meter_left
);
415 *right_ptr
= value_to_db(channel_ptr
->meter_right
);
419 channel_mono_meter_read(
420 jack_mixer_channel_t channel
,
423 *mono_ptr
= value_to_db(channel_ptr
->meter_left
);
427 channel_volume_write(
428 jack_mixer_channel_t channel
,
432 channel_ptr
->volume
= db_to_value(volume
);
433 channel_ptr
->midi_out_has_events
= true;
434 calc_channel_volumes(channel_ptr
);
439 jack_mixer_channel_t channel
)
442 return value_to_db(channel_ptr
->volume
);
446 channel_balance_write(
447 jack_mixer_channel_t channel
,
451 channel_ptr
->balance
= balance
;
452 calc_channel_volumes(channel_ptr
);
456 channel_balance_read(
457 jack_mixer_channel_t channel
)
460 return channel_ptr
->balance
;
464 channel_abspeak_read(
465 jack_mixer_channel_t channel
)
468 if (channel_ptr
->NaN_detected
)
474 return value_to_db(channel_ptr
->abspeak
);
479 channel_abspeak_reset(
480 jack_mixer_channel_t channel
)
482 channel_ptr
->abspeak
= 0;
483 channel_ptr
->NaN_detected
= false;
488 jack_mixer_channel_t channel
)
490 output_channel_set_muted(channel_ptr
->mixer_ptr
->main_mix_channel
, channel
, true);
495 jack_mixer_channel_t channel
)
497 output_channel_set_muted(channel_ptr
->mixer_ptr
->main_mix_channel
, channel
, false);
502 jack_mixer_channel_t channel
)
504 output_channel_set_solo(channel_ptr
->mixer_ptr
->main_mix_channel
, channel
, true);
509 jack_mixer_channel_t channel
)
511 output_channel_set_solo(channel_ptr
->mixer_ptr
->main_mix_channel
, channel
, false);
516 jack_mixer_channel_t channel
)
518 if (g_slist_find(channel_ptr
->mixer_ptr
->main_mix_channel
->muted_channels
, channel
))
525 jack_mixer_channel_t channel
)
527 if (g_slist_find(channel_ptr
->mixer_ptr
->main_mix_channel
->soloed_channels
, channel
))
533 channel_set_midi_scale(
534 jack_mixer_channel_t channel
,
535 jack_mixer_scale_t scale
)
537 channel_ptr
->midi_scale
= scale
;
541 channel_set_midi_change_callback(
542 jack_mixer_channel_t channel
,
543 void (*midi_change_callback
) (void*),
546 channel_ptr
->midi_change_callback
= midi_change_callback
;
547 channel_ptr
->midi_change_callback_data
= user_data
;
551 channel_get_midi_in_got_events(
552 jack_mixer_channel_t channel
)
554 bool t
= channel_ptr
->midi_in_got_events
;
555 channel_ptr
->midi_in_got_events
= false;
561 /* process input channels and mix them into main mix */
564 struct output_channel
*output_mix_channel
,
565 GSList
*channels_list
,
566 jack_nframes_t start
, /* index of first sample to process */
567 jack_nframes_t end
) /* index of sample to stop processing before */
571 struct channel
* channel_ptr
;
572 jack_default_audio_sample_t frame_left
;
573 jack_default_audio_sample_t frame_right
;
574 struct channel
*mix_channel
= (struct channel
*)output_mix_channel
;
576 for (i
= start
; i
< end
; i
++)
578 mix_channel
->left_buffer_ptr
[i
] = 0.0;
579 if (mix_channel
->stereo
)
580 mix_channel
->right_buffer_ptr
[i
] = 0.0;
584 for (node_ptr
= channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
586 channel_ptr
= node_ptr
->data
;
588 if (g_slist_find(output_mix_channel
->muted_channels
, channel_ptr
) != NULL
) {
589 /* skip muted channels */
593 if (output_mix_channel
->soloed_channels
&&
594 g_slist_find(output_mix_channel
->soloed_channels
, channel_ptr
) == NULL
) {
595 /* skip channels that are not soloed, when some are */
599 for (i
= start
; i
< end
; i
++)
601 if (! output_mix_channel
->prefader
) {
602 frame_left
= channel_ptr
->frames_left
[i
-start
];
604 frame_left
= channel_ptr
->prefader_frames_left
[i
-start
];
606 if (frame_left
== NAN
)
608 mix_channel
->left_buffer_ptr
[i
] += frame_left
;
610 if (mix_channel
->stereo
)
612 if (! output_mix_channel
->prefader
) {
613 frame_right
= channel_ptr
->frames_right
[i
-start
];
615 frame_right
= channel_ptr
->prefader_frames_right
[i
-start
];
617 if (frame_right
== NAN
)
620 mix_channel
->right_buffer_ptr
[i
] += frame_right
;
627 /* process main mix channel */
628 for (i
= start
; i
< end
; i
++)
630 if (! output_mix_channel
->prefader
) {
631 mix_channel
->left_buffer_ptr
[i
] *= mix_channel
->volume_left
;
632 if (mix_channel
->stereo
)
634 mix_channel
->right_buffer_ptr
[i
] *= mix_channel
->volume_right
;
638 frame_left
= fabsf(mix_channel
->left_buffer_ptr
[i
]);
639 if (mix_channel
->peak_left
< frame_left
)
641 mix_channel
->peak_left
= frame_left
;
643 if (frame_left
> mix_channel
->abspeak
)
645 mix_channel
->abspeak
= frame_left
;
649 if (mix_channel
->stereo
)
651 frame_right
= fabsf(mix_channel
->right_buffer_ptr
[i
]);
652 if (mix_channel
->peak_right
< frame_right
)
654 mix_channel
->peak_right
= frame_right
;
656 if (frame_right
> mix_channel
->abspeak
)
658 mix_channel
->abspeak
= frame_right
;
663 mix_channel
->peak_frames
++;
664 if (mix_channel
->peak_frames
>= PEAK_FRAMES_CHUNK
)
666 mix_channel
->meter_left
= mix_channel
->peak_left
;
667 mix_channel
->peak_left
= 0.0;
669 if (mix_channel
->stereo
)
671 mix_channel
->meter_right
= mix_channel
->peak_right
;
672 mix_channel
->peak_right
= 0.0;
675 mix_channel
->peak_frames
= 0;
682 struct channel
*channel_ptr
,
683 jack_nframes_t start
,
687 jack_default_audio_sample_t frame_left
;
688 jack_default_audio_sample_t frame_right
;
690 for (i
= start
; i
< end
; i
++)
692 if (i
-start
>= MAX_BLOCK_SIZE
)
694 fprintf(stderr
, "i-start too high: %d - %d\n", i
, start
);
696 channel_ptr
->prefader_frames_left
[i
-start
] = channel_ptr
->left_buffer_ptr
[i
];
697 if (channel_ptr
->stereo
)
698 channel_ptr
->prefader_frames_right
[i
-start
] = channel_ptr
->right_buffer_ptr
[i
];
700 if (!FLOAT_EXISTS(channel_ptr
->left_buffer_ptr
[i
]))
702 channel_ptr
->NaN_detected
= true;
703 channel_ptr
->frames_left
[i
-start
] = NAN
;
707 frame_left
= channel_ptr
->left_buffer_ptr
[i
] * channel_ptr
->volume_left
;
709 if (channel_ptr
->stereo
)
711 if (!FLOAT_EXISTS(channel_ptr
->right_buffer_ptr
[i
]))
713 channel_ptr
->NaN_detected
= true;
714 channel_ptr
->frames_right
[i
-start
] = NAN
;
718 frame_right
= channel_ptr
->right_buffer_ptr
[i
] * channel_ptr
->volume_right
;
722 frame_right
= channel_ptr
->left_buffer_ptr
[i
] * channel_ptr
->volume_right
;
725 channel_ptr
->frames_left
[i
-start
] = frame_left
;
726 channel_ptr
->frames_right
[i
-start
] = frame_right
;
728 if (channel_ptr
->stereo
)
730 frame_left
= fabsf(frame_left
);
731 frame_right
= fabsf(frame_right
);
733 if (channel_ptr
->peak_left
< frame_left
)
735 channel_ptr
->peak_left
= frame_left
;
737 if (frame_left
> channel_ptr
->abspeak
)
739 channel_ptr
->abspeak
= frame_left
;
743 if (channel_ptr
->peak_right
< frame_right
)
745 channel_ptr
->peak_right
= frame_right
;
747 if (frame_right
> channel_ptr
->abspeak
)
749 channel_ptr
->abspeak
= frame_right
;
755 frame_left
= (fabsf(frame_left
) + fabsf(frame_right
)) / 2;
757 if (channel_ptr
->peak_left
< frame_left
)
759 channel_ptr
->peak_left
= frame_left
;
761 if (frame_left
> channel_ptr
->abspeak
)
763 channel_ptr
->abspeak
= frame_left
;
768 channel_ptr
->peak_frames
++;
769 if (channel_ptr
->peak_frames
>= PEAK_FRAMES_CHUNK
)
771 channel_ptr
->meter_left
= channel_ptr
->peak_left
;
772 channel_ptr
->peak_left
= 0.0;
774 if (channel_ptr
->stereo
)
776 channel_ptr
->meter_right
= channel_ptr
->peak_right
;
777 channel_ptr
->peak_right
= 0.0;
780 channel_ptr
->peak_frames
= 0;
788 struct jack_mixer
* mixer_ptr
,
789 jack_nframes_t start
, /* index of first sample to process */
790 jack_nframes_t end
) /* index of sample to stop processing before */
793 struct output_channel
* output_channel_ptr
;
794 struct channel
*channel_ptr
;
796 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
798 channel_ptr
= (struct channel
*)node_ptr
->data
;
799 calc_channel_frames(channel_ptr
, start
, end
);
802 mix_one((struct output_channel
*)mixer_ptr
->main_mix_channel
, mixer_ptr
->input_channels_list
, start
, end
);
804 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
806 output_channel_ptr
= node_ptr
->data
;
807 channel_ptr
= (struct channel
*)output_channel_ptr
;
809 if (output_channel_ptr
->system
)
811 /* Don't bother mixing the channels if we are not connected */
812 if (channel_ptr
->stereo
)
814 if (jack_port_connected(channel_ptr
->port_left
) == 0 &&
815 jack_port_connected(channel_ptr
->port_right
) == 0)
818 if (jack_port_connected(channel_ptr
->port_left
) == 0)
823 mix_one(output_channel_ptr
, mixer_ptr
->input_channels_list
, start
, end
);
828 update_channel_buffers(
829 struct channel
* channel_ptr
,
830 jack_nframes_t nframes
)
832 channel_ptr
->left_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_left
, nframes
);
834 if (channel_ptr
->stereo
)
836 channel_ptr
->right_buffer_ptr
= jack_port_get_buffer(channel_ptr
->port_right
, nframes
);
840 #define mixer_ptr ((struct jack_mixer *)context)
844 jack_nframes_t nframes
,
849 struct channel
* channel_ptr
;
850 #if defined(HAVE_JACK_MIDI)
851 jack_nframes_t event_count
;
852 jack_midi_event_t in_event
;
853 unsigned char* midi_out_buffer
;
856 unsigned int cc_channel_index
;
858 jack_nframes_t offset
;
860 for (node_ptr
= mixer_ptr
->input_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
862 channel_ptr
= node_ptr
->data
;
863 update_channel_buffers(channel_ptr
, nframes
);
866 // Fill output buffers with the input
867 update_channel_buffers((struct channel
*)mixer_ptr
->main_mix_channel
, nframes
);
868 for (node_ptr
= mixer_ptr
->output_channels_list
; node_ptr
; node_ptr
= g_slist_next(node_ptr
))
870 channel_ptr
= node_ptr
->data
;
871 update_channel_buffers(channel_ptr
, nframes
);
876 #if defined(HAVE_JACK_MIDI)
877 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_in
, nframes
);
878 event_count
= jack_midi_get_event_count(midi_buffer
);
880 for (i
= 0 ; i
< event_count
; i
++)
882 jack_midi_event_get(&in_event
, midi_buffer
, i
);
884 if (in_event
.size
!= 3 ||
885 (in_event
.buffer
[0] & 0xF0) != 0xB0 ||
886 in_event
.buffer
[1] > 127 ||
887 in_event
.buffer
[2] > 127)
892 assert(in_event
.time
< nframes
);
896 (unsigned int)(in_event
.buffer
[0]),
897 (unsigned int)in_event
.buffer
[1],
898 (unsigned int)in_event
.buffer
[2]);
900 mixer_ptr
->last_midi_channel
= (unsigned int)in_event
.buffer
[1];
901 channel_ptr
= mixer_ptr
->midi_cc_map
[in_event
.buffer
[1]];
903 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
904 if (channel_ptr
!= NULL
&& channel_ptr
->midi_scale
!= NULL
)
906 assert(in_event
.time
>= offset
);
908 if (in_event
.time
> offset
)
910 // Perform the mixing of the part between the previous volume change
911 // (or the start of the block) up until this one.
912 mix(mixer_ptr
, offset
, in_event
.time
);
913 offset
= in_event
.time
;
916 if (channel_ptr
->midi_cc_balance_index
== (unsigned int)in_event
.buffer
[1])
918 byte
= in_event
.buffer
[2];
925 channel_ptr
->balance
= (float)byte
/ 63;
926 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr
->name
, channel_ptr
->balance
);
930 channel_ptr
->volume
= db_to_value(scale_scale_to_db(channel_ptr
->midi_scale
, (double)in_event
.buffer
[2] / 127));
931 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr
->name
, channel_ptr
->volume
);
934 calc_channel_volumes(channel_ptr
);
936 channel_ptr
->midi_in_got_events
= true;
937 if (channel_ptr
->midi_change_callback
)
938 channel_ptr
->midi_change_callback(channel_ptr
->midi_change_callback_data
);
944 midi_buffer
= jack_port_get_buffer(mixer_ptr
->port_midi_out
, nframes
);
945 jack_midi_clear_buffer(midi_buffer
);
947 for(i
=0; i
<nframes
; i
++)
949 for (cc_channel_index
=0; cc_channel_index
<128; cc_channel_index
++)
951 channel_ptr
= mixer_ptr
->midi_cc_map
[cc_channel_index
];
952 if (channel_ptr
== NULL
)
956 if (channel_ptr
->midi_out_has_events
== false)
960 if (channel_ptr
->midi_cc_balance_index
== (unsigned int)cc_channel_index
)
964 midi_out_buffer
= jack_midi_event_reserve(midi_buffer
, i
, 3);
965 if (midi_out_buffer
== NULL
)
969 midi_out_buffer
[0] = 0xB0; /* control change */
970 midi_out_buffer
[1] = cc_channel_index
;
971 midi_out_buffer
[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr
->midi_scale
, value_to_db(channel_ptr
->volume
)));
975 (unsigned int)(midi_out_buffer
[0]),
976 (unsigned int)midi_out_buffer
[1],
977 (unsigned int)midi_out_buffer
[2]);
979 channel_ptr
->midi_out_has_events
= false;
985 mix(mixer_ptr
, offset
, nframes
);
994 const char * jack_client_name_ptr
)
997 struct jack_mixer
* mixer_ptr
;
1001 mixer_ptr
= malloc(sizeof(struct jack_mixer
));
1002 if (mixer_ptr
== NULL
)
1007 ret
= pthread_mutex_init(&mixer_ptr
->mutex
, NULL
);
1013 mixer_ptr
->input_channels_list
= NULL
;
1014 mixer_ptr
->output_channels_list
= NULL
;
1016 mixer_ptr
->last_midi_channel
= 0;
1018 for (i
= 0 ; i
< 128 ; i
++)
1020 mixer_ptr
->midi_cc_map
[i
] = NULL
;
1023 LOG_DEBUG("Initializing JACK");
1024 mixer_ptr
->jack_client
= jack_client_open(jack_client_name_ptr
, 0, NULL
);
1025 if (mixer_ptr
->jack_client
== NULL
)
1027 LOG_ERROR("Cannot create JACK client.");
1028 LOG_NOTICE("Please make sure JACK daemon is running.");
1029 goto exit_destroy_mutex
;
1032 LOG_DEBUG("JACK client created");
1034 LOG_DEBUG("Sample rate: %" PRIu32
, jack_get_sample_rate(mixer_ptr
->jack_client
));
1036 mixer_ptr
->main_mix_channel
= create_output_channel(mixer_ptr
, "MAIN", true, false);
1037 if (mixer_ptr
->main_mix_channel
== NULL
) {
1038 LOG_ERROR("Cannot create main mix channel");
1041 channel_set_volume_midi_cc(mixer_ptr
->main_mix_channel
, 7);
1042 channel_set_balance_midi_cc(mixer_ptr
->main_mix_channel
, 8);
1044 ((struct channel
*)(mixer_ptr
->main_mix_channel
))->mixer_ptr
= mixer_ptr
;
1046 #if defined(HAVE_JACK_MIDI)
1047 mixer_ptr
->port_midi_in
= jack_port_register(mixer_ptr
->jack_client
, "midi in", JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1048 if (mixer_ptr
->port_midi_in
== NULL
)
1050 LOG_ERROR("Cannot create JACK MIDI in port");
1054 mixer_ptr
->port_midi_out
= jack_port_register(mixer_ptr
->jack_client
, "midi out", JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1055 if (mixer_ptr
->port_midi_out
== NULL
)
1057 LOG_ERROR("Cannot create JACK MIDI out port");
1063 calc_channel_volumes((struct channel
*)mixer_ptr
->main_mix_channel
);
1065 ret
= jack_set_process_callback(mixer_ptr
->jack_client
, process
, mixer_ptr
);
1068 LOG_ERROR("Cannot set JACK process callback");
1072 ret
= jack_activate(mixer_ptr
->jack_client
);
1075 LOG_ERROR("Cannot activate JACK client");
1082 jack_client_close(mixer_ptr
->jack_client
); /* this should clear all other resources we obtained through the client handle */
1085 pthread_mutex_destroy(&mixer_ptr
->mutex
);
1094 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1100 LOG_DEBUG("Uninitializing JACK");
1102 assert(mixer_ctx_ptr
->jack_client
!= NULL
);
1104 jack_client_close(mixer_ctx_ptr
->jack_client
);
1106 pthread_mutex_destroy(&mixer_ctx_ptr
->mutex
);
1108 free(mixer_ctx_ptr
->main_mix_channel
);
1109 free(mixer_ctx_ptr
);
1112 jack_mixer_channel_t
1113 get_main_mix_channel(
1116 return (struct channel
*)mixer_ctx_ptr
->main_mix_channel
;
1123 return g_slist_length(mixer_ctx_ptr
->input_channels_list
);
1127 get_last_midi_channel(
1130 return mixer_ctx_ptr
->last_midi_channel
;
1133 jack_mixer_channel_t
1136 const char * channel_name
,
1139 struct channel
* channel_ptr
;
1141 size_t channel_name_size
;
1143 channel_ptr
= malloc(sizeof(struct channel
));
1144 if (channel_ptr
== NULL
)
1149 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1151 channel_ptr
->name
= strdup(channel_name
);
1152 if (channel_ptr
->name
== NULL
)
1154 goto fail_free_channel
;
1157 channel_name_size
= strlen(channel_name
);
1161 port_name
= malloc(channel_name_size
+ 3);
1162 if (port_name
== NULL
)
1164 goto fail_free_channel_name
;
1167 memcpy(port_name
, channel_name
, channel_name_size
);
1168 port_name
[channel_name_size
] = ' ';
1169 port_name
[channel_name_size
+1] = 'L';
1170 port_name
[channel_name_size
+2] = 0;
1172 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1173 if (channel_ptr
->port_left
== NULL
)
1175 goto fail_free_port_name
;
1178 port_name
[channel_name_size
+1] = 'R';
1180 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1181 if (channel_ptr
->port_right
== NULL
)
1183 goto fail_unregister_left_channel
;
1188 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
1189 if (channel_ptr
->port_left
== NULL
)
1191 goto fail_free_channel_name
;
1195 channel_ptr
->stereo
= stereo
;
1197 channel_ptr
->volume
= 0.0;
1198 channel_ptr
->balance
= 0.0;
1199 channel_ptr
->meter_left
= -1.0;
1200 channel_ptr
->meter_right
= -1.0;
1201 channel_ptr
->abspeak
= 0.0;
1203 channel_ptr
->peak_left
= 0.0;
1204 channel_ptr
->peak_right
= 0.0;
1205 channel_ptr
->peak_frames
= 0;
1207 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1208 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1209 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1210 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1212 channel_ptr
->NaN_detected
= false;
1214 channel_ptr
->midi_cc_volume_index
= 0;
1215 channel_ptr
->midi_cc_balance_index
= 0;
1216 channel_ptr
->midi_change_callback
= NULL
;
1217 channel_ptr
->midi_change_callback_data
= NULL
;
1218 channel_ptr
->midi_out_has_events
= false;
1220 channel_ptr
->midi_scale
= NULL
;
1222 calc_channel_volumes(channel_ptr
);
1224 channel_ptr
->mixer_ptr
->input_channels_list
= g_slist_prepend(
1225 channel_ptr
->mixer_ptr
->input_channels_list
, channel_ptr
);
1229 fail_unregister_left_channel
:
1230 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1232 fail_free_port_name
:
1235 fail_free_channel_name
:
1236 free(channel_ptr
->name
);
1246 static jack_mixer_output_channel_t
1247 create_output_channel(
1249 const char * channel_name
,
1253 struct channel
* channel_ptr
;
1254 struct output_channel
* output_channel_ptr
;
1256 size_t channel_name_size
;
1258 output_channel_ptr
= malloc(sizeof(struct output_channel
));
1259 channel_ptr
= (struct channel
*)output_channel_ptr
;
1260 if (channel_ptr
== NULL
)
1265 channel_ptr
->mixer_ptr
= mixer_ctx_ptr
;
1267 channel_ptr
->name
= strdup(channel_name
);
1268 if (channel_ptr
->name
== NULL
)
1270 goto fail_free_channel
;
1275 channel_name_size
= strlen(channel_name
);
1277 port_name
= malloc(channel_name_size
+ 4);
1278 if (port_name
== NULL
)
1280 goto fail_free_channel_name
;
1283 memcpy(port_name
, channel_name
, channel_name_size
);
1284 port_name
[channel_name_size
] = ' ';
1285 port_name
[channel_name_size
+1] = 'L';
1286 port_name
[channel_name_size
+2] = 0;
1288 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1289 if (channel_ptr
->port_left
== NULL
)
1291 goto fail_free_port_name
;
1294 port_name
[channel_name_size
+1] = 'R';
1296 channel_ptr
->port_right
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1297 if (channel_ptr
->port_right
== NULL
)
1299 goto fail_unregister_left_channel
;
1304 channel_ptr
->port_left
= jack_port_register(channel_ptr
->mixer_ptr
->jack_client
, channel_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
1305 if (channel_ptr
->port_left
== NULL
)
1307 goto fail_free_channel_name
;
1311 channel_ptr
->stereo
= stereo
;
1313 channel_ptr
->volume
= 0.0;
1314 channel_ptr
->balance
= 0.0;
1315 channel_ptr
->meter_left
= -1.0;
1316 channel_ptr
->meter_right
= -1.0;
1317 channel_ptr
->abspeak
= 0.0;
1319 channel_ptr
->peak_left
= 0.0;
1320 channel_ptr
->peak_right
= 0.0;
1321 channel_ptr
->peak_frames
= 0;
1323 channel_ptr
->frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1324 channel_ptr
->frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1325 channel_ptr
->prefader_frames_left
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1326 channel_ptr
->prefader_frames_right
= calloc(MAX_BLOCK_SIZE
, sizeof(jack_default_audio_sample_t
));
1328 channel_ptr
->NaN_detected
= false;
1330 channel_ptr
->midi_cc_volume_index
= 0;
1331 channel_ptr
->midi_cc_balance_index
= 0;
1332 channel_ptr
->midi_change_callback
= NULL
;
1333 channel_ptr
->midi_change_callback_data
= NULL
;
1335 channel_ptr
->midi_scale
= NULL
;
1337 output_channel_ptr
->soloed_channels
= NULL
;
1338 output_channel_ptr
->muted_channels
= NULL
;
1339 output_channel_ptr
->system
= system
;
1340 output_channel_ptr
->prefader
= false;
1342 return output_channel_ptr
;
1344 fail_unregister_left_channel
:
1345 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1347 fail_free_port_name
:
1350 fail_free_channel_name
:
1351 free(channel_ptr
->name
);
1361 jack_mixer_output_channel_t
1364 const char * channel_name
,
1368 struct output_channel
*output_channel_ptr
;
1369 struct channel
*channel_ptr
;
1371 output_channel_ptr
= create_output_channel(mixer
, channel_name
, stereo
, system
);
1372 if (output_channel_ptr
== NULL
) {
1375 channel_ptr
= (struct channel
*)output_channel_ptr
;
1377 ((struct jack_mixer
*)mixer
)->output_channels_list
= g_slist_prepend(
1378 ((struct jack_mixer
*)mixer
)->output_channels_list
, channel_ptr
);
1380 return output_channel_ptr
;
1384 remove_output_channel(
1385 jack_mixer_output_channel_t output_channel
)
1387 struct output_channel
*output_channel_ptr
= output_channel
;
1388 struct channel
*channel_ptr
= output_channel
;
1390 channel_ptr
->mixer_ptr
->output_channels_list
= g_slist_remove(
1391 channel_ptr
->mixer_ptr
->output_channels_list
, channel_ptr
);
1392 free(channel_ptr
->name
);
1394 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_left
);
1395 if (channel_ptr
->stereo
)
1397 jack_port_unregister(channel_ptr
->mixer_ptr
->jack_client
, channel_ptr
->port_right
);
1400 if (channel_ptr
->midi_cc_volume_index
!= 0)
1402 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] == channel_ptr
);
1403 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_volume_index
] = NULL
;
1406 if (channel_ptr
->midi_cc_balance_index
!= 0)
1408 assert(channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] == channel_ptr
);
1409 channel_ptr
->mixer_ptr
->midi_cc_map
[channel_ptr
->midi_cc_balance_index
] = NULL
;
1412 g_slist_free(output_channel_ptr
->soloed_channels
);
1413 g_slist_free(output_channel_ptr
->muted_channels
);
1419 output_channel_set_solo(
1420 jack_mixer_output_channel_t output_channel
,
1421 jack_mixer_channel_t channel
,
1424 struct output_channel
*output_channel_ptr
= output_channel
;
1427 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
1429 output_channel_ptr
->soloed_channels
= g_slist_prepend(output_channel_ptr
->soloed_channels
, channel
);
1431 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) == NULL
)
1433 output_channel_ptr
->soloed_channels
= g_slist_remove(output_channel_ptr
->soloed_channels
, channel
);
1438 output_channel_set_muted(
1439 jack_mixer_output_channel_t output_channel
,
1440 jack_mixer_channel_t channel
,
1443 struct output_channel
*output_channel_ptr
= output_channel
;
1446 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
1448 output_channel_ptr
->muted_channels
= g_slist_prepend(output_channel_ptr
->muted_channels
, channel
);
1450 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) == NULL
)
1452 output_channel_ptr
->muted_channels
= g_slist_remove(output_channel_ptr
->muted_channels
, channel
);
1457 output_channel_is_muted(
1458 jack_mixer_output_channel_t output_channel
,
1459 jack_mixer_channel_t channel
)
1461 struct output_channel
*output_channel_ptr
= output_channel
;
1463 if (g_slist_find(output_channel_ptr
->muted_channels
, channel
) != NULL
)
1469 output_channel_is_solo(
1470 jack_mixer_output_channel_t output_channel
,
1471 jack_mixer_channel_t channel
)
1473 struct output_channel
*output_channel_ptr
= output_channel
;
1475 if (g_slist_find(output_channel_ptr
->soloed_channels
, channel
) != NULL
)
1481 output_channel_set_prefader(
1482 jack_mixer_output_channel_t output_channel
,
1485 struct output_channel
*output_channel_ptr
= output_channel
;
1486 output_channel_ptr
->prefader
= pfl_value
;
1490 output_channel_is_prefader(
1491 jack_mixer_output_channel_t output_channel
)
1493 struct output_channel
*output_channel_ptr
= output_channel
;
1494 return output_channel_ptr
->prefader
;