Replaced serialization_name functions by class methods
[jack_mixer.git] / jack_mixer.c
blob49c0b24e9e78da5198c9fe1baa50f2bfe554ae66
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 #define FLOAT_EXISTS(x) (!((x) - (x)))
50 struct channel
52 struct jack_mixer * mixer_ptr;
53 char * name;
54 bool stereo;
55 float volume;
56 float balance;
57 float volume_left;
58 float volume_right;
59 float meter_left;
60 float meter_right;
61 float abspeak;
62 jack_port_t * port_left;
63 jack_port_t * port_right;
65 jack_nframes_t peak_frames;
66 float peak_left;
67 float peak_right;
69 jack_default_audio_sample_t * frames_left;
70 jack_default_audio_sample_t * frames_right;
71 jack_default_audio_sample_t * prefader_frames_left;
72 jack_default_audio_sample_t * prefader_frames_right;
74 bool NaN_detected;
76 int midi_cc_volume_index;
77 int midi_cc_balance_index;
79 jack_default_audio_sample_t * left_buffer_ptr;
80 jack_default_audio_sample_t * right_buffer_ptr;
82 void (*midi_change_callback) (void*);
83 void *midi_change_callback_data;
85 jack_mixer_scale_t midi_scale;
88 struct output_channel {
89 struct channel channel;
90 GSList *soloed_channels;
91 GSList *muted_channels;
92 bool system; /* system channel, without any associated UI */
93 bool prefader;
96 struct jack_mixer
98 pthread_mutex_t mutex;
99 jack_client_t * jack_client;
100 GSList *input_channels_list;
101 GSList *output_channels_list;
102 struct output_channel *main_mix_channel;
104 jack_port_t * port_midi_in;
105 unsigned int last_midi_channel;
107 struct channel* midi_cc_map[128];
110 static jack_mixer_output_channel_t create_output_channel(
111 jack_mixer_t mixer,
112 const char * channel_name,
113 bool stereo,
114 bool system);
116 static inline void
117 update_channel_buffers(
118 struct channel * channel_ptr,
119 jack_nframes_t nframes);
122 float
123 value_to_db(
124 float value)
126 if (value <= 0)
128 return -INFINITY;
131 return 20.0 * log10f(value);
134 float
135 db_to_value(
136 float db)
138 return powf(10.0, db/20.0);
141 void
142 calc_channel_volumes(
143 struct channel * channel_ptr)
145 if (channel_ptr->stereo)
147 if (channel_ptr->balance > 0)
149 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
150 channel_ptr->volume_right = channel_ptr->volume;
152 else
154 channel_ptr->volume_left = channel_ptr->volume;
155 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
158 else
160 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
161 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
165 void
166 calc_all_channel_volumes(
167 struct jack_mixer * mixer_ptr)
169 struct channel * channel_ptr;
170 GSList *list_ptr;
172 for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
174 channel_ptr = list_ptr->data;
175 calc_channel_volumes(channel_ptr);
179 #define channel_ptr ((struct channel *)channel)
181 const char*
182 channel_get_name(
183 jack_mixer_channel_t channel)
185 return channel_ptr->name;
188 void
189 channel_rename(
190 jack_mixer_channel_t channel,
191 const char * name)
193 char * new_name;
194 size_t channel_name_size;
195 char * port_name;
196 int ret;
198 new_name = strdup(name);
199 if (new_name == NULL)
201 return;
204 if (channel_ptr->name)
206 free(channel_ptr->name);
209 channel_ptr->name = new_name;
211 if (channel_ptr->stereo)
213 channel_name_size = strlen(name);
214 port_name = malloc(channel_name_size + 3);
215 memcpy(port_name, name, channel_name_size);
217 port_name[channel_name_size] = ' ';
218 port_name[channel_name_size+1] = 'L';
219 port_name[channel_name_size+2] = 0;
221 ret = jack_port_set_name(channel_ptr->port_left, port_name);
222 if (ret != 0)
224 /* what could we do here? */
227 port_name[channel_name_size+1] = 'R';
229 ret = jack_port_set_name(channel_ptr->port_right, port_name);
230 if (ret != 0)
232 /* what could we do here? */
235 free(port_name);
237 else
239 ret = jack_port_set_name(channel_ptr->port_left, name);
240 if (ret != 0)
242 /* what could we do here? */
247 bool
248 channel_is_stereo(
249 jack_mixer_channel_t channel)
251 return channel_ptr->stereo;
254 unsigned int
255 channel_get_balance_midi_cc(
256 jack_mixer_channel_t channel)
258 return channel_ptr->midi_cc_balance_index;
261 unsigned int
262 channel_set_balance_midi_cc(
263 jack_mixer_channel_t channel,
264 unsigned int new_cc)
266 if (new_cc > 127) {
267 return 2; /* error: over limit CC */
269 if (channel_ptr->midi_cc_balance_index == new_cc) {
270 /* no change */
271 return 0;
273 if (new_cc == 0) {
274 /* 0 is special, it removes the link */
275 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
276 channel_ptr->midi_cc_balance_index = 0;
277 } else {
278 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
279 return 1; /* error: cc in use */
281 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
282 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
283 channel_ptr->midi_cc_balance_index = new_cc;
285 return 0;
288 unsigned int
289 channel_get_volume_midi_cc(
290 jack_mixer_channel_t channel)
292 return channel_ptr->midi_cc_volume_index;
295 unsigned int
296 channel_set_volume_midi_cc(
297 jack_mixer_channel_t channel, unsigned int new_cc)
299 if (new_cc > 127) {
300 return 2; /* error: over limit CC */
302 if (channel_ptr->midi_cc_volume_index == new_cc) {
303 /* no change */
304 return 0;
306 if (new_cc == 0) {
307 /* 0 is special, it removes the link */
308 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
309 channel_ptr->midi_cc_volume_index = 0;
310 } else {
311 if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
312 return 1; /* error: cc in use */
314 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
315 channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
316 channel_ptr->midi_cc_volume_index = new_cc;
318 return 0;
321 void
322 channel_autoset_midi_cc(
323 jack_mixer_channel_t channel)
325 struct jack_mixer *mixer_ptr;
326 int i;
328 mixer_ptr = channel_ptr->mixer_ptr;
330 for (i = 11 ; i < 128 ; i++)
332 if (mixer_ptr->midi_cc_map[i] == NULL)
334 mixer_ptr->midi_cc_map[i] = channel_ptr;
335 channel_ptr->midi_cc_volume_index = i;
337 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
339 break;
343 for (; i < 128 ; i++)
345 if (mixer_ptr->midi_cc_map[i] == NULL)
347 mixer_ptr->midi_cc_map[i] = channel_ptr;
348 channel_ptr->midi_cc_balance_index = i;
350 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
352 break;
357 void
358 remove_channel(
359 jack_mixer_channel_t channel)
361 GSList *list_ptr;
363 channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
364 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
365 free(channel_ptr->name);
367 /* remove references to input channel from all output channels */
368 channel_unmute(channel_ptr);
369 channel_unsolo(channel_ptr);
370 for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
372 struct output_channel *output_channel_ptr = list_ptr->data;
373 output_channel_set_solo(output_channel_ptr, channel, false);
374 output_channel_set_muted(output_channel_ptr, channel, false);
377 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
378 if (channel_ptr->stereo)
380 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
383 if (channel_ptr->midi_cc_volume_index != 0)
385 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
386 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
389 if (channel_ptr->midi_cc_balance_index != 0)
391 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
392 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
395 free(channel_ptr);
398 void
399 channel_stereo_meter_read(
400 jack_mixer_channel_t channel,
401 double * left_ptr,
402 double * right_ptr)
404 assert(channel_ptr);
405 *left_ptr = value_to_db(channel_ptr->meter_left);
406 *right_ptr = value_to_db(channel_ptr->meter_right);
409 void
410 channel_mono_meter_read(
411 jack_mixer_channel_t channel,
412 double * mono_ptr)
414 *mono_ptr = value_to_db(channel_ptr->meter_left);
417 void
418 channel_volume_write(
419 jack_mixer_channel_t channel,
420 double volume)
422 assert(channel_ptr);
423 channel_ptr->volume = db_to_value(volume);
424 calc_channel_volumes(channel_ptr);
427 double
428 channel_volume_read(
429 jack_mixer_channel_t channel)
431 assert(channel_ptr);
432 return value_to_db(channel_ptr->volume);
435 void
436 channel_balance_write(
437 jack_mixer_channel_t channel,
438 double balance)
440 assert(channel_ptr);
441 channel_ptr->balance = balance;
442 calc_channel_volumes(channel_ptr);
445 double
446 channel_balance_read(
447 jack_mixer_channel_t channel)
449 assert(channel_ptr);
450 return channel_ptr->balance;
453 double
454 channel_abspeak_read(
455 jack_mixer_channel_t channel)
457 assert(channel_ptr);
458 if (channel_ptr->NaN_detected)
460 return sqrt(-1);
462 else
464 return value_to_db(channel_ptr->abspeak);
468 void
469 channel_abspeak_reset(
470 jack_mixer_channel_t channel)
472 channel_ptr->abspeak = 0;
473 channel_ptr->NaN_detected = false;
476 void
477 channel_mute(
478 jack_mixer_channel_t channel)
480 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
483 void
484 channel_unmute(
485 jack_mixer_channel_t channel)
487 output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
490 void
491 channel_solo(
492 jack_mixer_channel_t channel)
494 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
497 void
498 channel_unsolo(
499 jack_mixer_channel_t channel)
501 output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
504 bool
505 channel_is_muted(
506 jack_mixer_channel_t channel)
508 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
509 return true;
510 return false;
513 bool
514 channel_is_soloed(
515 jack_mixer_channel_t channel)
517 if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
518 return true;
519 return false;
522 void
523 channel_set_midi_scale(
524 jack_mixer_channel_t channel,
525 jack_mixer_scale_t scale)
527 channel_ptr->midi_scale = scale;
530 void
531 channel_set_midi_change_callback(
532 jack_mixer_channel_t channel,
533 void (*midi_change_callback) (void*),
534 void *user_data)
536 channel_ptr->midi_change_callback = midi_change_callback;
537 channel_ptr->midi_change_callback_data = user_data;
540 #undef channel_ptr
542 /* process input channels and mix them into main mix */
543 static inline void
544 mix_one(
545 struct output_channel *output_mix_channel,
546 GSList *channels_list,
547 jack_nframes_t start, /* index of first sample to process */
548 jack_nframes_t end) /* index of sample to stop processing before */
550 jack_nframes_t i;
551 GSList *node_ptr;
552 struct channel * channel_ptr;
553 jack_default_audio_sample_t frame_left;
554 jack_default_audio_sample_t frame_right;
555 struct channel *mix_channel = (struct channel*)output_mix_channel;
557 update_channel_buffers(mix_channel, end-start);
558 for (i = 0; i < (end-start); i++)
560 mix_channel->left_buffer_ptr[i] = 0.0;
561 if (mix_channel->stereo)
562 mix_channel->right_buffer_ptr[i] = 0.0;
566 for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
568 channel_ptr = node_ptr->data;
570 if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
571 /* skip muted channels */
572 continue;
575 if (output_mix_channel->soloed_channels &&
576 g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
577 /* skip channels that are not soloed, when some are */
578 continue;
581 for (i = start ; i < end ; i++)
583 if (! output_mix_channel->prefader) {
584 frame_left = channel_ptr->frames_left[i-start];
585 } else {
586 frame_left = channel_ptr->prefader_frames_left[i-start];
588 if (frame_left == NAN)
589 break;
590 mix_channel->left_buffer_ptr[i] += frame_left;
592 if (mix_channel->stereo)
594 if (! output_mix_channel->prefader) {
595 frame_right = channel_ptr->frames_right[i-start];
596 } else {
597 frame_right = channel_ptr->prefader_frames_right[i-start];
599 if (frame_right == NAN)
600 break;
602 mix_channel->right_buffer_ptr[i] += frame_right;
609 /* process main mix channel */
610 for (i = start ; i < end ; i++)
612 if (! output_mix_channel->prefader) {
613 mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
614 if (mix_channel->stereo)
616 mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
620 frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
621 if (mix_channel->peak_left < frame_left)
623 mix_channel->peak_left = frame_left;
625 if (frame_left > mix_channel->abspeak)
627 mix_channel->abspeak = frame_left;
631 if (mix_channel->stereo)
633 frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
634 if (mix_channel->peak_right < frame_right)
636 mix_channel->peak_right = frame_right;
638 if (frame_right > mix_channel->abspeak)
640 mix_channel->abspeak = frame_right;
645 mix_channel->peak_frames++;
646 if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
648 mix_channel->meter_left = mix_channel->peak_left;
649 mix_channel->peak_left = 0.0;
651 if (mix_channel->stereo)
653 mix_channel->meter_right = mix_channel->peak_right;
654 mix_channel->peak_right = 0.0;
657 mix_channel->peak_frames = 0;
662 static inline void
663 calc_channel_frames(
664 struct channel *channel_ptr,
665 jack_nframes_t start,
666 jack_nframes_t end)
668 jack_nframes_t i;
669 jack_default_audio_sample_t frame_left;
670 jack_default_audio_sample_t frame_right;
672 channel_ptr->frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
673 channel_ptr->frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
675 channel_ptr->prefader_frames_left = calloc(end-start, sizeof(jack_default_audio_sample_t));
676 channel_ptr->prefader_frames_right = calloc(end-start, sizeof(jack_default_audio_sample_t));
678 for (i = start ; i < end ; i++)
680 channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
681 if (channel_ptr->stereo)
682 channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
684 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
686 channel_ptr->NaN_detected = true;
687 channel_ptr->frames_left[i-start] = NAN;
688 break;
691 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
693 if (channel_ptr->stereo)
695 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
697 channel_ptr->NaN_detected = true;
698 channel_ptr->frames_right[i-start] = NAN;
699 break;
702 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
704 else
706 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
709 channel_ptr->frames_left[i-start] = frame_left;
710 channel_ptr->frames_right[i-start] = frame_right;
712 if (channel_ptr->stereo)
714 frame_left = fabsf(frame_left);
715 frame_right = fabsf(frame_right);
717 if (channel_ptr->peak_left < frame_left)
719 channel_ptr->peak_left = frame_left;
721 if (frame_left > channel_ptr->abspeak)
723 channel_ptr->abspeak = frame_left;
727 if (channel_ptr->peak_right < frame_right)
729 channel_ptr->peak_right = frame_right;
731 if (frame_right > channel_ptr->abspeak)
733 channel_ptr->abspeak = frame_right;
737 else
739 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
741 if (channel_ptr->peak_left < frame_left)
743 channel_ptr->peak_left = frame_left;
745 if (frame_left > channel_ptr->abspeak)
747 channel_ptr->abspeak = frame_left;
752 channel_ptr->peak_frames++;
753 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
755 channel_ptr->meter_left = channel_ptr->peak_left;
756 channel_ptr->peak_left = 0.0;
758 if (channel_ptr->stereo)
760 channel_ptr->meter_right = channel_ptr->peak_right;
761 channel_ptr->peak_right = 0.0;
764 channel_ptr->peak_frames = 0;
770 static inline void
771 mix(
772 struct jack_mixer * mixer_ptr,
773 jack_nframes_t start, /* index of first sample to process */
774 jack_nframes_t end) /* index of sample to stop processing before */
776 GSList *node_ptr;
777 struct output_channel * output_channel_ptr;
778 struct channel *channel_ptr;
780 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
782 channel_ptr = (struct channel*)node_ptr->data;
783 calc_channel_frames(channel_ptr, start, end);
786 mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
788 for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
790 output_channel_ptr = node_ptr->data;
791 channel_ptr = (struct channel*)output_channel_ptr;
793 if (output_channel_ptr->system)
795 /* Don't bother mixing the channels if we are not connected */
796 if (channel_ptr->stereo)
798 if (jack_port_connected(channel_ptr->port_left) == 0 &&
799 jack_port_connected(channel_ptr->port_right) == 0)
800 continue;
801 } else {
802 if (jack_port_connected(channel_ptr->port_left) == 0)
803 continue;
807 mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
810 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
812 channel_ptr = (struct channel*)node_ptr->data;
813 free(channel_ptr->frames_left);
814 free(channel_ptr->frames_right);
815 free(channel_ptr->prefader_frames_left);
816 free(channel_ptr->prefader_frames_right);
820 static inline void
821 update_channel_buffers(
822 struct channel * channel_ptr,
823 jack_nframes_t nframes)
825 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
827 if (channel_ptr->stereo)
829 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
833 #define mixer_ptr ((struct jack_mixer *)context)
835 static int
836 process(
837 jack_nframes_t nframes,
838 void * context)
840 jack_nframes_t i;
841 GSList *node_ptr;
842 struct channel * channel_ptr;
843 #if defined(HAVE_JACK_MIDI)
844 jack_nframes_t event_count;
845 jack_midi_event_t in_event;
846 void * midi_buffer;
847 signed char byte;
848 #endif
849 jack_nframes_t offset;
851 for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
853 channel_ptr = node_ptr->data;
854 update_channel_buffers(channel_ptr, nframes);
857 offset = 0;
859 #if defined(HAVE_JACK_MIDI)
860 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
861 event_count = jack_midi_get_event_count(midi_buffer);
863 for (i = 0 ; i < event_count; i++)
865 jack_midi_event_get(&in_event, midi_buffer, i);
867 if (in_event.size != 3 ||
868 (in_event.buffer[0] & 0xF0) != 0xB0 ||
869 in_event.buffer[1] > 127 ||
870 in_event.buffer[2] > 127)
872 continue;
875 assert(in_event.time < nframes);
877 LOG_DEBUG(
878 "%u: CC#%u -> %u",
879 (unsigned int)(in_event.buffer[0] & 0x0F),
880 (unsigned int)in_event.buffer[1],
881 (unsigned int)in_event.buffer[2]);
883 mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
884 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
886 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
887 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
889 assert(in_event.time >= offset);
891 if (in_event.time > offset)
893 mix(mixer_ptr, offset, in_event.time);
894 offset = in_event.time;
897 if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
899 byte = in_event.buffer[2];
900 if (byte == 0)
902 byte = 1;
904 byte -= 64;
906 channel_ptr->balance = (float)byte / 63;
907 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
909 else
911 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
912 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
915 calc_channel_volumes(channel_ptr);
917 if (channel_ptr->midi_change_callback)
918 channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
924 #endif
926 mix(mixer_ptr, offset, nframes);
928 return 0;
931 #undef mixer_ptr
933 jack_mixer_t
934 create(
935 const char * jack_client_name_ptr)
937 int ret;
938 struct jack_mixer * mixer_ptr;
939 int i;
942 mixer_ptr = malloc(sizeof(struct jack_mixer));
943 if (mixer_ptr == NULL)
945 goto exit;
948 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
949 if (ret != 0)
951 goto exit_free;
954 mixer_ptr->input_channels_list = NULL;
955 mixer_ptr->output_channels_list = NULL;
957 mixer_ptr->last_midi_channel = 0;
959 for (i = 0 ; i < 128 ; i++)
961 mixer_ptr->midi_cc_map[i] = NULL;
964 LOG_DEBUG("Initializing JACK");
965 mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
966 if (mixer_ptr->jack_client == NULL)
968 LOG_ERROR("Cannot create JACK client.");
969 LOG_NOTICE("Please make sure JACK daemon is running.");
970 goto exit_destroy_mutex;
973 LOG_DEBUG("JACK client created");
975 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
977 mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
978 if (mixer_ptr->main_mix_channel == NULL) {
979 LOG_ERROR("Cannot create main mix channel");
980 goto close_jack;
982 channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
983 channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
985 ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
987 #if defined(HAVE_JACK_MIDI)
988 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
989 if (mixer_ptr->port_midi_in == NULL)
991 LOG_ERROR("Cannot create JACK port");
992 goto close_jack;
994 #endif
996 calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
998 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
999 if (ret != 0)
1001 LOG_ERROR("Cannot set JACK process callback");
1002 goto close_jack;
1005 ret = jack_activate(mixer_ptr->jack_client);
1006 if (ret != 0)
1008 LOG_ERROR("Cannot activate JACK client");
1009 goto close_jack;
1012 return mixer_ptr;
1014 close_jack:
1015 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1017 exit_destroy_mutex:
1018 pthread_mutex_destroy(&mixer_ptr->mutex);
1020 exit_free:
1021 free(mixer_ptr);
1023 exit:
1024 return NULL;
1027 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1029 void
1030 destroy(
1031 jack_mixer_t mixer)
1033 LOG_DEBUG("Uninitializing JACK");
1035 assert(mixer_ctx_ptr->jack_client != NULL);
1037 jack_client_close(mixer_ctx_ptr->jack_client);
1039 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1041 free(mixer_ctx_ptr->main_mix_channel);
1042 free(mixer_ctx_ptr);
1045 jack_mixer_channel_t
1046 get_main_mix_channel(
1047 jack_mixer_t mixer)
1049 return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1052 unsigned int
1053 get_channels_count(
1054 jack_mixer_t mixer)
1056 return g_slist_length(mixer_ctx_ptr->input_channels_list);
1059 unsigned int
1060 get_last_midi_channel(
1061 jack_mixer_t mixer)
1063 return mixer_ctx_ptr->last_midi_channel;
1066 jack_mixer_channel_t
1067 add_channel(
1068 jack_mixer_t mixer,
1069 const char * channel_name,
1070 bool stereo)
1072 struct channel * channel_ptr;
1073 char * port_name;
1074 size_t channel_name_size;
1076 channel_ptr = malloc(sizeof(struct channel));
1077 if (channel_ptr == NULL)
1079 goto fail;
1082 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1084 channel_ptr->name = strdup(channel_name);
1085 if (channel_ptr->name == NULL)
1087 goto fail_free_channel;
1090 channel_name_size = strlen(channel_name);
1092 if (stereo)
1094 port_name = malloc(channel_name_size + 3);
1095 if (port_name == NULL)
1097 goto fail_free_channel_name;
1100 memcpy(port_name, channel_name, channel_name_size);
1101 port_name[channel_name_size] = ' ';
1102 port_name[channel_name_size+1] = 'L';
1103 port_name[channel_name_size+2] = 0;
1105 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1106 if (channel_ptr->port_left == NULL)
1108 goto fail_free_port_name;
1111 port_name[channel_name_size+1] = 'R';
1113 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1114 if (channel_ptr->port_right == NULL)
1116 goto fail_unregister_left_channel;
1119 else
1121 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1122 if (channel_ptr->port_left == NULL)
1124 goto fail_free_channel_name;
1128 channel_ptr->stereo = stereo;
1130 channel_ptr->volume = 0.0;
1131 channel_ptr->balance = 0.0;
1132 channel_ptr->meter_left = -1.0;
1133 channel_ptr->meter_right = -1.0;
1134 channel_ptr->abspeak = 0.0;
1136 channel_ptr->peak_left = 0.0;
1137 channel_ptr->peak_right = 0.0;
1138 channel_ptr->peak_frames = 0;
1140 channel_ptr->NaN_detected = false;
1142 channel_ptr->midi_cc_volume_index = 0;
1143 channel_ptr->midi_cc_balance_index = 0;
1144 channel_ptr->midi_change_callback = NULL;
1145 channel_ptr->midi_change_callback_data = NULL;
1147 channel_ptr->midi_scale = NULL;
1149 calc_channel_volumes(channel_ptr);
1151 channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1152 channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1154 return channel_ptr;
1156 fail_unregister_left_channel:
1157 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1159 fail_free_port_name:
1160 free(port_name);
1162 fail_free_channel_name:
1163 free(channel_ptr->name);
1165 fail_free_channel:
1166 free(channel_ptr);
1167 channel_ptr = NULL;
1169 fail:
1170 return NULL;
1173 static jack_mixer_output_channel_t
1174 create_output_channel(
1175 jack_mixer_t mixer,
1176 const char * channel_name,
1177 bool stereo,
1178 bool system)
1180 struct channel * channel_ptr;
1181 struct output_channel * output_channel_ptr;
1182 char * port_name;
1183 size_t channel_name_size;
1185 output_channel_ptr = malloc(sizeof(struct output_channel));
1186 channel_ptr = (struct channel*)output_channel_ptr;
1187 if (channel_ptr == NULL)
1189 goto fail;
1192 channel_ptr->mixer_ptr = mixer_ctx_ptr;
1194 channel_ptr->name = strdup(channel_name);
1195 if (channel_ptr->name == NULL)
1197 goto fail_free_channel;
1200 if (stereo)
1202 channel_name_size = strlen(channel_name);
1204 port_name = malloc(channel_name_size + 4);
1205 if (port_name == NULL)
1207 goto fail_free_channel_name;
1210 memcpy(port_name, channel_name, channel_name_size);
1211 port_name[channel_name_size] = ' ';
1212 port_name[channel_name_size+1] = 'L';
1213 port_name[channel_name_size+2] = 0;
1215 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1216 if (channel_ptr->port_left == NULL)
1218 goto fail_free_port_name;
1221 port_name[channel_name_size+1] = 'R';
1223 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1224 if (channel_ptr->port_right == NULL)
1226 goto fail_unregister_left_channel;
1229 else
1231 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1232 if (channel_ptr->port_left == NULL)
1234 goto fail_free_channel_name;
1238 channel_ptr->stereo = stereo;
1240 channel_ptr->volume = 0.0;
1241 channel_ptr->balance = 0.0;
1242 channel_ptr->meter_left = -1.0;
1243 channel_ptr->meter_right = -1.0;
1244 channel_ptr->abspeak = 0.0;
1246 channel_ptr->peak_left = 0.0;
1247 channel_ptr->peak_right = 0.0;
1248 channel_ptr->peak_frames = 0;
1250 channel_ptr->NaN_detected = false;
1252 channel_ptr->midi_cc_volume_index = 0;
1253 channel_ptr->midi_cc_balance_index = 0;
1254 channel_ptr->midi_change_callback = NULL;
1255 channel_ptr->midi_change_callback_data = NULL;
1257 channel_ptr->midi_scale = NULL;
1259 output_channel_ptr->soloed_channels = NULL;
1260 output_channel_ptr->muted_channels = NULL;
1261 output_channel_ptr->system = system;
1262 output_channel_ptr->prefader = false;
1264 return output_channel_ptr;
1266 fail_unregister_left_channel:
1267 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1269 fail_free_port_name:
1270 free(port_name);
1272 fail_free_channel_name:
1273 free(channel_ptr->name);
1275 fail_free_channel:
1276 free(channel_ptr);
1277 channel_ptr = NULL;
1279 fail:
1280 return NULL;
1283 jack_mixer_output_channel_t
1284 add_output_channel(
1285 jack_mixer_t mixer,
1286 const char * channel_name,
1287 bool stereo,
1288 bool system)
1290 struct output_channel *output_channel_ptr;
1291 struct channel *channel_ptr;
1293 output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1294 if (output_channel_ptr == NULL) {
1295 return NULL;
1297 channel_ptr = (struct channel*)output_channel_ptr;
1299 ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1300 ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1302 return output_channel_ptr;
1305 void
1306 remove_output_channel(
1307 jack_mixer_output_channel_t output_channel)
1309 struct output_channel *output_channel_ptr = output_channel;
1310 struct channel *channel_ptr = output_channel;
1312 channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1313 channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1314 free(channel_ptr->name);
1316 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1317 if (channel_ptr->stereo)
1319 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1322 if (channel_ptr->midi_cc_volume_index != 0)
1324 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1325 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1328 if (channel_ptr->midi_cc_balance_index != 0)
1330 assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1331 channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1334 g_slist_free(output_channel_ptr->soloed_channels);
1335 g_slist_free(output_channel_ptr->muted_channels);
1337 free(channel_ptr);
1340 void
1341 output_channel_set_solo(
1342 jack_mixer_output_channel_t output_channel,
1343 jack_mixer_channel_t channel,
1344 bool solo_value)
1346 struct output_channel *output_channel_ptr = output_channel;
1348 if (solo_value) {
1349 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1350 return;
1351 output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1352 } else {
1353 if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1354 return;
1355 output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1359 void
1360 output_channel_set_muted(
1361 jack_mixer_output_channel_t output_channel,
1362 jack_mixer_channel_t channel,
1363 bool muted_value)
1365 struct output_channel *output_channel_ptr = output_channel;
1367 if (muted_value) {
1368 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1369 return;
1370 output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1371 } else {
1372 if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1373 return;
1374 output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1378 bool
1379 output_channel_is_muted(
1380 jack_mixer_output_channel_t output_channel,
1381 jack_mixer_channel_t channel)
1383 struct output_channel *output_channel_ptr = output_channel;
1385 if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1386 return true;
1387 return false;
1390 bool
1391 output_channel_is_solo(
1392 jack_mixer_output_channel_t output_channel,
1393 jack_mixer_channel_t channel)
1395 struct output_channel *output_channel_ptr = output_channel;
1397 if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1398 return true;
1399 return false;
1402 void
1403 output_channel_set_prefader(
1404 jack_mixer_output_channel_t output_channel,
1405 bool pfl_value)
1407 struct output_channel *output_channel_ptr = output_channel;
1408 output_channel_ptr->prefader = pfl_value;
1411 bool
1412 output_channel_is_prefader(
1413 jack_mixer_output_channel_t output_channel)
1415 struct output_channel *output_channel_ptr = output_channel;
1416 return output_channel_ptr->prefader;