Initialize threshold before using it
[jack_mixer.git] / jack_mixer.c
blob8ddd811d335e0486f50c191aaad7c6d1cbe0fcc1
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 *****************************************************************************/
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <math.h>
31 #include <jack/jack.h>
32 #if defined(HAVE_JACK_MIDI)
33 #include <jack/midiport.h>
34 #endif
35 #include <assert.h>
36 #include <pthread.h>
38 #include <glib.h>
40 #include "jack_mixer.h"
41 //#define LOG_LEVEL LOG_LEVEL_DEBUG
42 #include "log.h"
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)))
56 struct channel
58 struct jack_mixer * mixer_ptr;
59 char * name;
60 bool stereo;
61 float volume;
62 float balance;
63 float volume_left;
64 float volume_right;
65 float meter_left;
66 float meter_right;
67 float abspeak;
68 jack_port_t * port_left;
69 jack_port_t * port_right;
71 jack_nframes_t peak_frames;
72 float peak_left;
73 float peak_right;
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;
80 bool NaN_detected;
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 */
101 bool prefader;
104 struct jack_mixer
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(
120 jack_mixer_t mixer,
121 const char * channel_name,
122 bool stereo,
123 bool system);
125 static inline void
126 update_channel_buffers(
127 struct channel * channel_ptr,
128 jack_nframes_t nframes);
131 float
132 value_to_db(
133 float value)
135 if (value <= 0)
137 return -INFINITY;
140 return 20.0 * log10f(value);
143 float
144 db_to_value(
145 float db)
147 return powf(10.0, db/20.0);
150 void
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;
161 else
163 channel_ptr->volume_left = channel_ptr->volume;
164 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
167 else
169 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
170 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
174 void
175 calc_all_channel_volumes(
176 struct jack_mixer * mixer_ptr)
178 struct channel * channel_ptr;
179 GSList *list_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)
190 const char*
191 channel_get_name(
192 jack_mixer_channel_t channel)
194 return channel_ptr->name;
197 void
198 channel_rename(
199 jack_mixer_channel_t channel,
200 const char * name)
202 char * new_name;
203 size_t channel_name_size;
204 char * port_name;
205 int ret;
207 new_name = strdup(name);
208 if (new_name == NULL)
210 return;
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);
231 if (ret != 0)
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);
239 if (ret != 0)
241 /* what could we do here? */
244 free(port_name);
246 else
248 ret = jack_port_set_name(channel_ptr->port_left, name);
249 if (ret != 0)
251 /* what could we do here? */
256 bool
257 channel_is_stereo(
258 jack_mixer_channel_t channel)
260 return channel_ptr->stereo;
263 unsigned int
264 channel_get_balance_midi_cc(
265 jack_mixer_channel_t channel)
267 return channel_ptr->midi_cc_balance_index;
270 unsigned int
271 channel_set_balance_midi_cc(
272 jack_mixer_channel_t channel,
273 unsigned int new_cc)
275 if (new_cc > 127) {
276 return 2; /* error: over limit CC */
278 if (channel_ptr->midi_cc_balance_index == new_cc) {
279 /* no change */
280 return 0;
282 if (new_cc == 0) {
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;
286 } else {
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;
294 return 0;
297 unsigned int
298 channel_get_volume_midi_cc(
299 jack_mixer_channel_t channel)
301 return channel_ptr->midi_cc_volume_index;
304 unsigned int
305 channel_set_volume_midi_cc(
306 jack_mixer_channel_t channel, unsigned int new_cc)
308 if (new_cc > 127) {
309 return 2; /* error: over limit CC */
311 if (channel_ptr->midi_cc_volume_index == new_cc) {
312 /* no change */
313 return 0;
315 if (new_cc == 0) {
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;
319 } else {
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;
327 return 0;
330 void
331 channel_autoset_midi_cc(
332 jack_mixer_channel_t channel)
334 struct jack_mixer *mixer_ptr;
335 int i;
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);
348 break;
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);
361 break;
366 void
367 remove_channel(
368 jack_mixer_channel_t channel)
370 GSList *list_ptr;
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;
404 free(channel_ptr);
407 void
408 channel_stereo_meter_read(
409 jack_mixer_channel_t channel,
410 double * left_ptr,
411 double * right_ptr)
413 assert(channel_ptr);
414 *left_ptr = value_to_db(channel_ptr->meter_left);
415 *right_ptr = value_to_db(channel_ptr->meter_right);
418 void
419 channel_mono_meter_read(
420 jack_mixer_channel_t channel,
421 double * mono_ptr)
423 *mono_ptr = value_to_db(channel_ptr->meter_left);
426 void
427 channel_volume_write(
428 jack_mixer_channel_t channel,
429 double volume)
431 assert(channel_ptr);
432 channel_ptr->volume = db_to_value(volume);
433 channel_ptr->midi_out_has_events = true;
434 calc_channel_volumes(channel_ptr);
437 double
438 channel_volume_read(
439 jack_mixer_channel_t channel)
441 assert(channel_ptr);
442 return value_to_db(channel_ptr->volume);
445 void
446 channel_balance_write(
447 jack_mixer_channel_t channel,
448 double balance)
450 assert(channel_ptr);
451 channel_ptr->balance = balance;
452 calc_channel_volumes(channel_ptr);
455 double
456 channel_balance_read(
457 jack_mixer_channel_t channel)
459 assert(channel_ptr);
460 return channel_ptr->balance;
463 double
464 channel_abspeak_read(
465 jack_mixer_channel_t channel)
467 assert(channel_ptr);
468 if (channel_ptr->NaN_detected)
470 return sqrt(-1);
472 else
474 return value_to_db(channel_ptr->abspeak);
478 void
479 channel_abspeak_reset(
480 jack_mixer_channel_t channel)
482 channel_ptr->abspeak = 0;
483 channel_ptr->NaN_detected = false;
486 void
487 channel_mute(
488 jack_mixer_channel_t channel)
490 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
493 void
494 channel_unmute(
495 jack_mixer_channel_t channel)
497 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
500 void
501 channel_solo(
502 jack_mixer_channel_t channel)
504 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
507 void
508 channel_unsolo(
509 jack_mixer_channel_t channel)
511 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
514 bool
515 channel_is_muted(
516 jack_mixer_channel_t channel)
518 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
519 return true;
520 return false;
523 bool
524 channel_is_soloed(
525 jack_mixer_channel_t channel)
527 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
528 return true;
529 return false;
532 void
533 channel_set_midi_scale(
534 jack_mixer_channel_t channel,
535 jack_mixer_scale_t scale)
537 channel_ptr->midi_scale = scale;
540 void
541 channel_set_midi_change_callback(
542 jack_mixer_channel_t channel,
543 void (*midi_change_callback) (void*),
544 void *user_data)
546 channel_ptr->midi_change_callback = midi_change_callback;
547 channel_ptr->midi_change_callback_data = user_data;
550 bool
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;
556 return t;
559 #undef channel_ptr
561 /* process input channels and mix them into main mix */
562 static inline void
563 mix_one(
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 */
569 jack_nframes_t i;
570 GSList *node_ptr;
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 */
590 continue;
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 */
596 continue;
599 for (i = start ; i < end ; i++)
601 if (! output_mix_channel->prefader) {
602 frame_left = channel_ptr->frames_left[i-start];
603 } else {
604 frame_left = channel_ptr->prefader_frames_left[i-start];
606 if (frame_left == NAN)
607 break;
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];
614 } else {
615 frame_right = channel_ptr->prefader_frames_right[i-start];
617 if (frame_right == NAN)
618 break;
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;
680 static inline void
681 calc_channel_frames(
682 struct channel *channel_ptr,
683 jack_nframes_t start,
684 jack_nframes_t end)
686 jack_nframes_t i;
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;
704 break;
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;
715 break;
718 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
720 else
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;
753 else
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;
786 static inline void
787 mix(
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 */
792 GSList *node_ptr;
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)
816 continue;
817 } else {
818 if (jack_port_connected(channel_ptr->port_left) == 0)
819 continue;
823 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
827 static inline void
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)
842 static int
843 process(
844 jack_nframes_t nframes,
845 void * context)
847 jack_nframes_t i;
848 GSList *node_ptr;
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;
854 void * midi_buffer;
855 signed char byte;
856 unsigned int cc_channel_index;
857 #endif
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);
874 offset = 0;
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)
889 continue;
892 assert(in_event.time < nframes);
894 LOG_DEBUG(
895 "%u: CC#%u -> %u",
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];
919 if (byte == 0)
921 byte = 1;
923 byte -= 64;
925 channel_ptr->balance = (float)byte / 63;
926 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
928 else
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)
954 continue;
956 if (channel_ptr->midi_out_has_events == false)
958 continue;
960 if (channel_ptr->midi_cc_balance_index == (unsigned int)cc_channel_index)
962 continue;
964 midi_out_buffer = jack_midi_event_reserve(midi_buffer, i, 3);
965 if (midi_out_buffer == NULL)
967 continue;
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)));
973 LOG_DEBUG(
974 "%u: CC#%u <- %u",
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;
983 #endif
985 mix(mixer_ptr, offset, nframes);
987 return 0;
990 #undef mixer_ptr
992 jack_mixer_t
993 create(
994 const char * jack_client_name_ptr)
996 int ret;
997 struct jack_mixer * mixer_ptr;
998 int i;
1001 mixer_ptr = malloc(sizeof(struct jack_mixer));
1002 if (mixer_ptr == NULL)
1004 goto exit;
1007 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
1008 if (ret != 0)
1010 goto exit_free;
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");
1039 goto close_jack;
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");
1051 goto close_jack;
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");
1058 goto close_jack;
1061 #endif
1063 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
1065 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1066 if (ret != 0)
1068 LOG_ERROR("Cannot set JACK process callback");
1069 goto close_jack;
1072 ret = jack_activate(mixer_ptr->jack_client);
1073 if (ret != 0)
1075 LOG_ERROR("Cannot activate JACK client");
1076 goto close_jack;
1079 return mixer_ptr;
1081 close_jack:
1082 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1084 exit_destroy_mutex:
1085 pthread_mutex_destroy(&mixer_ptr->mutex);
1087 exit_free:
1088 free(mixer_ptr);
1090 exit:
1091 return NULL;
1094 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1096 void
1097 destroy(
1098 jack_mixer_t 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(
1114 jack_mixer_t mixer)
1116 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1119 unsigned int
1120 get_channels_count(
1121 jack_mixer_t mixer)
1123 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1126 unsigned int
1127 get_last_midi_channel(
1128 jack_mixer_t mixer)
1130 return mixer_ctx_ptr->last_midi_channel;
1133 jack_mixer_channel_t
1134 add_channel(
1135 jack_mixer_t mixer,
1136 const char * channel_name,
1137 bool stereo)
1139 struct channel * channel_ptr;
1140 char * port_name;
1141 size_t channel_name_size;
1143 channel_ptr = malloc(sizeof(struct channel));
1144 if (channel_ptr == NULL)
1146 goto fail;
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);
1159 if (stereo)
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;
1186 else
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);
1227 return 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:
1233 free(port_name);
1235 fail_free_channel_name:
1236 free(channel_ptr->name);
1238 fail_free_channel:
1239 free(channel_ptr);
1240 channel_ptr = NULL;
1242 fail:
1243 return NULL;
1246 static jack_mixer_output_channel_t
1247 create_output_channel(
1248 jack_mixer_t mixer,
1249 const char * channel_name,
1250 bool stereo,
1251 bool system)
1253 struct channel * channel_ptr;
1254 struct output_channel * output_channel_ptr;
1255 char * port_name;
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)
1262 goto fail;
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;
1273 if (stereo)
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;
1302 else
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:
1348 free(port_name);
1350 fail_free_channel_name:
1351 free(channel_ptr->name);
1353 fail_free_channel:
1354 free(channel_ptr);
1355 channel_ptr = NULL;
1357 fail:
1358 return NULL;
1361 jack_mixer_output_channel_t
1362 add_output_channel(
1363 jack_mixer_t mixer,
1364 const char * channel_name,
1365 bool stereo,
1366 bool system)
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) {
1373 return 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;
1383 void
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);
1415 free(channel_ptr);
1418 void
1419 output_channel_set_solo(
1420 jack_mixer_output_channel_t output_channel,
1421 jack_mixer_channel_t channel,
1422 bool solo_value)
1424 struct output_channel *output_channel_ptr = output_channel;
1426 if (solo_value) {
1427 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1428 return;
1429 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1430 } else {
1431 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1432 return;
1433 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1437 void
1438 output_channel_set_muted(
1439 jack_mixer_output_channel_t output_channel,
1440 jack_mixer_channel_t channel,
1441 bool muted_value)
1443 struct output_channel *output_channel_ptr = output_channel;
1445 if (muted_value) {
1446 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1447 return;
1448 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1449 } else {
1450 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1451 return;
1452 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1456 bool
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)
1464 return true;
1465 return false;
1468 bool
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)
1476 return true;
1477 return false;
1480 void
1481 output_channel_set_prefader(
1482 jack_mixer_output_channel_t output_channel,
1483 bool pfl_value)
1485 struct output_channel *output_channel_ptr = output_channel;
1486 output_channel_ptr->prefader = pfl_value;
1489 bool
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;