]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.c
Don't fail applying channel changes on non numeric MIDI CC values (#2554)
[jack_mixer.git] / jack_mixer.c
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
3  *
4  *   This file is part of jack_mixer
5  *
6  *   Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
7  *   Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
8  *
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
12  *
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.
17  *
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.
21  *
22  *****************************************************************************/
23
24 #include "config.h"
25
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>
37
38 #include <glib.h>
39
40 #include "jack_mixer.h"
41 //#define LOG_LEVEL LOG_LEVEL_DEBUG
42 #include "log.h"
43
44 #include "jack_compat.h"
45
46 #define PEAK_FRAMES_CHUNK 4800
47
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)
53
54 #define FLOAT_EXISTS(x) (!((x) - (x)))
55
56 struct channel
57 {
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;
70
71   jack_nframes_t peak_frames;
72   float peak_left;
73   float peak_right;
74
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;
79
80   bool NaN_detected;
81
82   int midi_cc_volume_index;
83   int midi_cc_balance_index;
84
85   jack_default_audio_sample_t * left_buffer_ptr;
86   jack_default_audio_sample_t * right_buffer_ptr;
87
88   bool midi_got_events;
89   void (*midi_change_callback) (void*);
90   void *midi_change_callback_data;
91
92   jack_mixer_scale_t midi_scale;
93 };
94
95 struct output_channel {
96   struct channel channel;
97   GSList *soloed_channels;
98   GSList *muted_channels;
99   bool system; /* system channel, without any associated UI */
100   bool prefader;
101 };
102
103 struct jack_mixer
104 {
105   pthread_mutex_t mutex;
106   jack_client_t * jack_client;
107   GSList *input_channels_list;
108   GSList *output_channels_list;
109   struct output_channel *main_mix_channel;
110
111   jack_port_t * port_midi_in;
112   unsigned int last_midi_channel;
113
114   struct channel* midi_cc_map[128];
115 };
116
117 static jack_mixer_output_channel_t create_output_channel(
118   jack_mixer_t mixer,
119   const char * channel_name,
120   bool stereo,
121   bool system);
122
123 static inline void
124 update_channel_buffers(
125   struct channel * channel_ptr,
126   jack_nframes_t nframes);
127
128
129 float
130 value_to_db(
131   float value)
132 {
133   if (value <= 0)
134   {
135     return -INFINITY;
136   }
137
138   return 20.0 * log10f(value);
139 }
140
141 float
142 db_to_value(
143   float db)
144 {
145   return powf(10.0, db/20.0);
146 }
147
148 void
149 calc_channel_volumes(
150   struct channel * channel_ptr)
151 {
152   if (channel_ptr->stereo)
153   {
154     if (channel_ptr->balance > 0)
155     {
156       channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
157       channel_ptr->volume_right = channel_ptr->volume;
158     }
159     else
160     {
161       channel_ptr->volume_left = channel_ptr->volume;
162       channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
163     }
164   }
165   else
166   {
167     channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
168     channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
169   }
170 }
171
172 void
173 calc_all_channel_volumes(
174   struct jack_mixer * mixer_ptr)
175 {
176   struct channel * channel_ptr;
177   GSList *list_ptr;
178
179   for (list_ptr = mixer_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
180   {
181     channel_ptr = list_ptr->data;
182     calc_channel_volumes(channel_ptr);
183   }
184 }
185
186 #define channel_ptr ((struct channel *)channel)
187
188 const char*
189 channel_get_name(
190   jack_mixer_channel_t channel)
191 {
192   return channel_ptr->name;
193 }
194
195 void
196 channel_rename(
197   jack_mixer_channel_t channel,
198   const char * name)
199 {
200   char * new_name;
201   size_t channel_name_size;
202   char * port_name;
203   int ret;
204
205   new_name = strdup(name);
206   if (new_name == NULL)
207   {
208     return;
209   }
210
211   if (channel_ptr->name)
212   {
213     free(channel_ptr->name);
214   }
215
216   channel_ptr->name = new_name;
217
218   if (channel_ptr->stereo)
219   {
220     channel_name_size = strlen(name);
221     port_name = malloc(channel_name_size + 3);
222     memcpy(port_name, name, channel_name_size);
223
224     port_name[channel_name_size] = ' ';
225     port_name[channel_name_size+1] = 'L';
226     port_name[channel_name_size+2] = 0;
227
228     ret = jack_port_set_name(channel_ptr->port_left, port_name);
229     if (ret != 0)
230     {
231       /* what could we do here? */
232     }
233
234     port_name[channel_name_size+1] = 'R';
235
236     ret = jack_port_set_name(channel_ptr->port_right, port_name);
237     if (ret != 0)
238     {
239       /* what could we do here? */
240     }
241
242     free(port_name);
243   }
244   else
245   {
246     ret = jack_port_set_name(channel_ptr->port_left, name);
247     if (ret != 0)
248     {
249       /* what could we do here? */
250     }
251   }
252 }
253
254 bool
255 channel_is_stereo(
256   jack_mixer_channel_t channel)
257 {
258   return channel_ptr->stereo;
259 }
260
261 unsigned int
262 channel_get_balance_midi_cc(
263   jack_mixer_channel_t channel)
264 {
265   return channel_ptr->midi_cc_balance_index;
266 }
267
268 unsigned int
269 channel_set_balance_midi_cc(
270   jack_mixer_channel_t channel,
271   unsigned int new_cc)
272 {
273   if (new_cc > 127) {
274     return 2; /* error: over limit CC */
275   }
276   if (channel_ptr->midi_cc_balance_index == new_cc) {
277     /* no change */
278     return 0;
279   }
280   if (new_cc == 0) {
281     /* 0 is special, it removes the link */
282     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
283     channel_ptr->midi_cc_balance_index = 0;
284   } else {
285     if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
286       return 1; /* error: cc in use */
287     }
288     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
289     channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
290     channel_ptr->midi_cc_balance_index = new_cc;
291   }
292   return 0;
293 }
294
295 unsigned int
296 channel_get_volume_midi_cc(
297   jack_mixer_channel_t channel)
298 {
299   return channel_ptr->midi_cc_volume_index;
300 }
301
302 unsigned int
303 channel_set_volume_midi_cc(
304   jack_mixer_channel_t channel, unsigned int new_cc)
305 {
306   if (new_cc > 127) {
307     return 2; /* error: over limit CC */
308   }
309   if (channel_ptr->midi_cc_volume_index == new_cc) {
310     /* no change */
311     return 0;
312   }
313   if (new_cc == 0) {
314     /* 0 is special, it removes the link */
315     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
316     channel_ptr->midi_cc_volume_index = 0;
317   } else {
318     if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
319       return 1; /* error: cc in use */
320     }
321     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
322     channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
323     channel_ptr->midi_cc_volume_index = new_cc;
324   }
325   return 0;
326 }
327
328 void
329 channel_autoset_midi_cc(
330   jack_mixer_channel_t channel)
331 {
332   struct jack_mixer *mixer_ptr;
333   int i;
334
335   mixer_ptr = channel_ptr->mixer_ptr;
336
337   for (i = 11 ; i < 128 ; i++)
338   {
339     if (mixer_ptr->midi_cc_map[i] == NULL)
340     {
341       mixer_ptr->midi_cc_map[i] = channel_ptr;
342       channel_ptr->midi_cc_volume_index = i;
343
344       LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
345
346       break;
347     }
348   }
349
350   for (; i < 128 ; i++)
351   {
352     if (mixer_ptr->midi_cc_map[i] == NULL)
353     {
354       mixer_ptr->midi_cc_map[i] = channel_ptr;
355       channel_ptr->midi_cc_balance_index = i;
356
357       LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
358
359       break;
360     }
361   }
362 }
363
364 void
365 remove_channel(
366   jack_mixer_channel_t channel)
367 {
368   GSList *list_ptr;
369
370   channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
371                   channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
372   free(channel_ptr->name);
373
374   /* remove references to input channel from all output channels */
375   channel_unmute(channel_ptr);
376   channel_unsolo(channel_ptr);
377   for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
378   {
379     struct output_channel *output_channel_ptr = list_ptr->data;
380     output_channel_set_solo(output_channel_ptr, channel, false);
381     output_channel_set_muted(output_channel_ptr, channel, false);
382   }
383
384   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
385   if (channel_ptr->stereo)
386   {
387     jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
388   }
389
390   if (channel_ptr->midi_cc_volume_index != 0)
391   {
392     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
393     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
394   }
395
396   if (channel_ptr->midi_cc_balance_index != 0)
397   {
398     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
399     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
400   }
401
402   free(channel_ptr);
403 }
404
405 void
406 channel_stereo_meter_read(
407   jack_mixer_channel_t channel,
408   double * left_ptr,
409   double * right_ptr)
410 {
411   assert(channel_ptr);
412   *left_ptr = value_to_db(channel_ptr->meter_left);
413   *right_ptr = value_to_db(channel_ptr->meter_right);
414 }
415
416 void
417 channel_mono_meter_read(
418   jack_mixer_channel_t channel,
419   double * mono_ptr)
420 {
421   *mono_ptr = value_to_db(channel_ptr->meter_left);
422 }
423
424 void
425 channel_volume_write(
426   jack_mixer_channel_t channel,
427   double volume)
428 {
429   assert(channel_ptr);
430   channel_ptr->volume = db_to_value(volume);
431   calc_channel_volumes(channel_ptr);
432 }
433
434 double
435 channel_volume_read(
436   jack_mixer_channel_t channel)
437 {
438   assert(channel_ptr);
439   return value_to_db(channel_ptr->volume);
440 }
441
442 void
443 channel_balance_write(
444   jack_mixer_channel_t channel,
445   double balance)
446 {
447   assert(channel_ptr);
448   channel_ptr->balance = balance;
449   calc_channel_volumes(channel_ptr);
450 }
451
452 double
453 channel_balance_read(
454   jack_mixer_channel_t channel)
455 {
456   assert(channel_ptr);
457   return channel_ptr->balance;
458 }
459
460 double
461 channel_abspeak_read(
462   jack_mixer_channel_t channel)
463 {
464   assert(channel_ptr);
465   if (channel_ptr->NaN_detected)
466   {
467     return sqrt(-1);
468   }
469   else
470   {
471     return value_to_db(channel_ptr->abspeak);
472   }
473 }
474
475 void
476 channel_abspeak_reset(
477   jack_mixer_channel_t channel)
478 {
479   channel_ptr->abspeak = 0;
480   channel_ptr->NaN_detected = false;
481 }
482
483 void
484 channel_mute(
485   jack_mixer_channel_t channel)
486 {
487   output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
488 }
489
490 void
491 channel_unmute(
492   jack_mixer_channel_t channel)
493 {
494   output_channel_set_muted(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
495 }
496
497 void
498 channel_solo(
499   jack_mixer_channel_t channel)
500 {
501   output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, true);
502 }
503
504 void
505 channel_unsolo(
506   jack_mixer_channel_t channel)
507 {
508   output_channel_set_solo(channel_ptr->mixer_ptr->main_mix_channel, channel, false);
509 }
510
511 bool
512 channel_is_muted(
513   jack_mixer_channel_t channel)
514 {
515   if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->muted_channels, channel))
516     return true;
517   return false;
518 }
519
520 bool
521 channel_is_soloed(
522   jack_mixer_channel_t channel)
523 {
524   if (g_slist_find(channel_ptr->mixer_ptr->main_mix_channel->soloed_channels, channel))
525     return true;
526   return false;
527 }
528
529 void
530 channel_set_midi_scale(
531   jack_mixer_channel_t channel,
532   jack_mixer_scale_t scale)
533 {
534   channel_ptr->midi_scale = scale;
535 }
536
537 void
538 channel_set_midi_change_callback(
539   jack_mixer_channel_t channel,
540   void (*midi_change_callback) (void*),
541   void *user_data)
542 {
543   channel_ptr->midi_change_callback = midi_change_callback;
544   channel_ptr->midi_change_callback_data = user_data;
545 }
546
547 bool
548 channel_get_midi_got_events(
549   jack_mixer_channel_t channel)
550 {
551   bool t = channel_ptr->midi_got_events;
552   channel_ptr->midi_got_events = false;
553   return t;
554 }
555
556 #undef channel_ptr
557
558 /* process input channels and mix them into main mix */
559 static inline void
560 mix_one(
561   struct output_channel *output_mix_channel,
562   GSList *channels_list,
563   jack_nframes_t start,         /* index of first sample to process */
564   jack_nframes_t end)           /* index of sample to stop processing before */
565 {
566   jack_nframes_t i;
567   GSList *node_ptr;
568   struct channel * channel_ptr;
569   jack_default_audio_sample_t frame_left;
570   jack_default_audio_sample_t frame_right;
571   struct channel *mix_channel = (struct channel*)output_mix_channel;
572
573   for (i = start; i < end; i++)
574   {
575     mix_channel->left_buffer_ptr[i] = 0.0;
576     if (mix_channel->stereo)
577       mix_channel->right_buffer_ptr[i] = 0.0;
578   }
579
580
581   for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
582   {
583     channel_ptr = node_ptr->data;
584
585     if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL) {
586       /* skip muted channels */
587       continue;
588     }
589
590     if (output_mix_channel->soloed_channels &&
591         g_slist_find(output_mix_channel->soloed_channels, channel_ptr) == NULL) {
592       /* skip channels that are not soloed, when some are */
593       continue;
594     }
595
596     for (i = start ; i < end ; i++)
597     {
598       if (! output_mix_channel->prefader) {
599         frame_left = channel_ptr->frames_left[i-start];
600       } else {
601         frame_left = channel_ptr->prefader_frames_left[i-start];
602       }
603       if (frame_left == NAN)
604         break;
605       mix_channel->left_buffer_ptr[i] += frame_left;
606
607       if (mix_channel->stereo)
608       {
609         if (! output_mix_channel->prefader) {
610           frame_right = channel_ptr->frames_right[i-start];
611         } else {
612           frame_right = channel_ptr->prefader_frames_right[i-start];
613         }
614         if (frame_right == NAN)
615           break;
616
617         mix_channel->right_buffer_ptr[i] += frame_right;
618       }
619
620     }
621
622   }
623
624   /* process main mix channel */
625   for (i = start ; i < end ; i++)
626   {
627     if (! output_mix_channel->prefader) {
628       mix_channel->left_buffer_ptr[i] *= mix_channel->volume_left;
629       if (mix_channel->stereo)
630       {
631         mix_channel->right_buffer_ptr[i] *= mix_channel->volume_right;
632       }
633     }
634
635     frame_left = fabsf(mix_channel->left_buffer_ptr[i]);
636     if (mix_channel->peak_left < frame_left)
637     {
638       mix_channel->peak_left = frame_left;
639
640       if (frame_left > mix_channel->abspeak)
641       {
642         mix_channel->abspeak = frame_left;
643       }
644     }
645
646     if (mix_channel->stereo)
647     {
648       frame_right = fabsf(mix_channel->right_buffer_ptr[i]);
649       if (mix_channel->peak_right < frame_right)
650       {
651         mix_channel->peak_right = frame_right;
652
653         if (frame_right > mix_channel->abspeak)
654         {
655           mix_channel->abspeak = frame_right;
656         }
657       }
658     }
659
660     mix_channel->peak_frames++;
661     if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
662     {
663       mix_channel->meter_left = mix_channel->peak_left;
664       mix_channel->peak_left = 0.0;
665
666       if (mix_channel->stereo)
667       {
668         mix_channel->meter_right = mix_channel->peak_right;
669         mix_channel->peak_right = 0.0;
670       }
671
672       mix_channel->peak_frames = 0;
673     }
674   }
675 }
676
677 static inline void
678 calc_channel_frames(
679   struct channel *channel_ptr,
680   jack_nframes_t start,
681   jack_nframes_t end)
682 {
683   jack_nframes_t i;
684   jack_default_audio_sample_t frame_left;
685   jack_default_audio_sample_t frame_right;
686
687   for (i = start ; i < end ; i++)
688   {
689     if (i-start >= MAX_BLOCK_SIZE)
690     {
691       fprintf(stderr, "i-start too high: %d - %d\n", i, start);
692     }
693     channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
694     if (channel_ptr->stereo)
695       channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
696
697     if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
698     {
699       channel_ptr->NaN_detected = true;
700       channel_ptr->frames_left[i-start] = NAN;
701       break;
702     }
703
704     frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
705
706     if (channel_ptr->stereo)
707     {
708       if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
709       {
710         channel_ptr->NaN_detected = true;
711         channel_ptr->frames_right[i-start] = NAN;
712         break;
713       }
714
715       frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
716     }
717     else
718     {
719       frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
720     }
721
722     channel_ptr->frames_left[i-start] = frame_left;
723     channel_ptr->frames_right[i-start] = frame_right;
724
725     if (channel_ptr->stereo)
726     {
727       frame_left = fabsf(frame_left);
728       frame_right = fabsf(frame_right);
729
730       if (channel_ptr->peak_left < frame_left)
731       {
732         channel_ptr->peak_left = frame_left;
733
734         if (frame_left > channel_ptr->abspeak)
735         {
736           channel_ptr->abspeak = frame_left;
737         }
738       }
739
740       if (channel_ptr->peak_right < frame_right)
741       {
742         channel_ptr->peak_right = frame_right;
743
744         if (frame_right > channel_ptr->abspeak)
745         {
746           channel_ptr->abspeak = frame_right;
747         }
748       }
749     }
750     else
751     {
752       frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
753
754       if (channel_ptr->peak_left < frame_left)
755       {
756         channel_ptr->peak_left = frame_left;
757
758         if (frame_left > channel_ptr->abspeak)
759         {
760           channel_ptr->abspeak = frame_left;
761         }
762       }
763     }
764
765     channel_ptr->peak_frames++;
766     if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
767     {
768       channel_ptr->meter_left = channel_ptr->peak_left;
769       channel_ptr->peak_left = 0.0;
770
771       if (channel_ptr->stereo)
772       {
773         channel_ptr->meter_right = channel_ptr->peak_right;
774         channel_ptr->peak_right = 0.0;
775       }
776
777       channel_ptr->peak_frames = 0;
778     }
779   }
780
781 }
782
783 static inline void
784 mix(
785   struct jack_mixer * mixer_ptr,
786   jack_nframes_t start,         /* index of first sample to process */
787   jack_nframes_t end)           /* index of sample to stop processing before */
788 {
789   GSList *node_ptr;
790   struct output_channel * output_channel_ptr;
791   struct channel *channel_ptr;
792
793   for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
794   {
795     channel_ptr = (struct channel*)node_ptr->data;
796     calc_channel_frames(channel_ptr, start, end);
797   }
798
799   mix_one((struct output_channel*)mixer_ptr->main_mix_channel, mixer_ptr->input_channels_list, start, end);
800
801   for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
802   {
803     output_channel_ptr = node_ptr->data;
804     channel_ptr = (struct channel*)output_channel_ptr;
805
806     if (output_channel_ptr->system)
807     {
808       /* Don't bother mixing the channels if we are not connected */
809       if (channel_ptr->stereo)
810       {
811         if (jack_port_connected(channel_ptr->port_left) == 0 &&
812             jack_port_connected(channel_ptr->port_right) == 0)
813           continue;
814       } else {
815          if (jack_port_connected(channel_ptr->port_left) == 0)
816            continue;
817       }
818     }
819
820     mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
821   }
822 }
823
824 static inline void
825 update_channel_buffers(
826   struct channel * channel_ptr,
827   jack_nframes_t nframes)
828 {
829   channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
830
831   if (channel_ptr->stereo)
832   {
833     channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
834   }
835 }
836
837 #define mixer_ptr ((struct jack_mixer *)context)
838
839 static int
840 process(
841   jack_nframes_t nframes,
842   void * context)
843 {
844   jack_nframes_t i;
845   GSList *node_ptr;
846   struct channel * channel_ptr;
847 #if defined(HAVE_JACK_MIDI)
848   jack_nframes_t event_count;
849   jack_midi_event_t in_event;
850   void * midi_buffer;
851   signed char byte;
852 #endif
853   jack_nframes_t offset;
854
855   for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
856   {
857     channel_ptr = node_ptr->data;
858     update_channel_buffers(channel_ptr, nframes);
859   }
860
861   // Fill output buffers with the input 
862   update_channel_buffers((struct channel*)mixer_ptr->main_mix_channel, nframes);
863   for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
864   {
865     channel_ptr = node_ptr->data;
866     update_channel_buffers(channel_ptr, nframes);
867   }
868
869   offset = 0;
870
871 #if defined(HAVE_JACK_MIDI)
872   midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
873   event_count = jack_midi_get_event_count(midi_buffer);
874
875   for (i = 0 ; i < event_count; i++)
876   {
877     jack_midi_event_get(&in_event, midi_buffer, i);
878
879     if (in_event.size != 3 ||
880         (in_event.buffer[0] & 0xF0) != 0xB0 ||
881         in_event.buffer[1] > 127 ||
882         in_event.buffer[2] > 127)
883     {
884       continue;
885     }
886
887     assert(in_event.time < nframes);
888
889     LOG_DEBUG(
890       "%u: CC#%u -> %u",
891       (unsigned int)(in_event.buffer[0] & 0x0F),
892       (unsigned int)in_event.buffer[1],
893       (unsigned int)in_event.buffer[2]);
894
895     mixer_ptr->last_midi_channel = (unsigned int)in_event.buffer[1];
896     channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
897
898     /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
899     if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
900     {
901       assert(in_event.time >= offset);
902
903       if (in_event.time > offset)
904       {
905         // Perform the mixing of the part between the previous volume change
906         // (or the start of the block) up until this one.
907         mix(mixer_ptr, offset, in_event.time);
908         offset = in_event.time;
909       }
910
911       if (channel_ptr->midi_cc_balance_index == (unsigned int)in_event.buffer[1])
912       {
913         byte = in_event.buffer[2];
914         if (byte == 0)
915         {
916           byte = 1;
917         }
918         byte -= 64;
919
920         channel_ptr->balance = (float)byte / 63;
921         LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
922       }
923       else
924       {
925         channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
926         LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
927       }
928
929       calc_channel_volumes(channel_ptr);
930
931       channel_ptr->midi_got_events = true;
932       if (channel_ptr->midi_change_callback)
933         channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
934
935     }
936
937   }
938
939 #endif
940
941   mix(mixer_ptr, offset, nframes);
942
943   return 0;
944 }
945
946 #undef mixer_ptr
947
948 jack_mixer_t
949 create(
950   const char * jack_client_name_ptr)
951 {
952   int ret;
953   struct jack_mixer * mixer_ptr;
954   int i;
955
956
957   mixer_ptr = malloc(sizeof(struct jack_mixer));
958   if (mixer_ptr == NULL)
959   {
960     goto exit;
961   }
962
963   ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
964   if (ret != 0)
965   {
966     goto exit_free;
967   }
968
969   mixer_ptr->input_channels_list = NULL;
970   mixer_ptr->output_channels_list = NULL;
971
972   mixer_ptr->last_midi_channel = 0;
973
974   for (i = 0 ; i < 128 ; i++)
975   {
976     mixer_ptr->midi_cc_map[i] = NULL;
977   }
978
979   LOG_DEBUG("Initializing JACK");
980   mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
981   if (mixer_ptr->jack_client == NULL)
982   {
983     LOG_ERROR("Cannot create JACK client.");
984     LOG_NOTICE("Please make sure JACK daemon is running.");
985     goto exit_destroy_mutex;
986   }
987
988   LOG_DEBUG("JACK client created");
989
990   LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
991
992   mixer_ptr->main_mix_channel = create_output_channel(mixer_ptr, "MAIN", true, false);
993   if (mixer_ptr->main_mix_channel == NULL) {
994     LOG_ERROR("Cannot create main mix channel");
995     goto close_jack;
996   }
997   channel_set_volume_midi_cc(mixer_ptr->main_mix_channel, 7);
998   channel_set_balance_midi_cc(mixer_ptr->main_mix_channel, 8);
999
1000   ((struct channel*)(mixer_ptr->main_mix_channel))->mixer_ptr = mixer_ptr;
1001
1002 #if defined(HAVE_JACK_MIDI)
1003   mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1004   if (mixer_ptr->port_midi_in == NULL)
1005   {
1006     LOG_ERROR("Cannot create JACK port");
1007     goto close_jack;
1008   }
1009 #endif
1010
1011   calc_channel_volumes((struct channel*)mixer_ptr->main_mix_channel);
1012
1013   ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1014   if (ret != 0)
1015   {
1016     LOG_ERROR("Cannot set JACK process callback");
1017     goto close_jack;
1018   }
1019
1020   ret = jack_activate(mixer_ptr->jack_client);
1021   if (ret != 0)
1022   {
1023     LOG_ERROR("Cannot activate JACK client");
1024     goto close_jack;
1025   }
1026
1027   return mixer_ptr;
1028
1029 close_jack:
1030   jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1031
1032 exit_destroy_mutex:
1033   pthread_mutex_destroy(&mixer_ptr->mutex);
1034
1035 exit_free:
1036   free(mixer_ptr);
1037
1038 exit:
1039   return NULL;
1040 }
1041
1042 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1043
1044 void
1045 destroy(
1046   jack_mixer_t mixer)
1047 {
1048   LOG_DEBUG("Uninitializing JACK");
1049
1050   assert(mixer_ctx_ptr->jack_client != NULL);
1051
1052   jack_client_close(mixer_ctx_ptr->jack_client);
1053
1054   pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1055
1056   free(mixer_ctx_ptr->main_mix_channel);
1057   free(mixer_ctx_ptr);
1058 }
1059
1060 jack_mixer_channel_t
1061 get_main_mix_channel(
1062   jack_mixer_t mixer)
1063 {
1064   return (struct channel*)mixer_ctx_ptr->main_mix_channel;
1065 }
1066
1067 unsigned int
1068 get_channels_count(
1069   jack_mixer_t mixer)
1070 {
1071   return g_slist_length(mixer_ctx_ptr->input_channels_list);
1072 }
1073
1074 unsigned int
1075 get_last_midi_channel(
1076   jack_mixer_t mixer)
1077 {
1078   return mixer_ctx_ptr->last_midi_channel;
1079 }
1080
1081 jack_mixer_channel_t
1082 add_channel(
1083   jack_mixer_t mixer,
1084   const char * channel_name,
1085   bool stereo)
1086 {
1087   struct channel * channel_ptr;
1088   char * port_name;
1089   size_t channel_name_size;
1090
1091   channel_ptr = malloc(sizeof(struct channel));
1092   if (channel_ptr == NULL)
1093   {
1094     goto fail;
1095   }
1096
1097   channel_ptr->mixer_ptr = mixer_ctx_ptr;
1098
1099   channel_ptr->name = strdup(channel_name);
1100   if (channel_ptr->name == NULL)
1101   {
1102     goto fail_free_channel;
1103   }
1104
1105   channel_name_size = strlen(channel_name);
1106
1107   if (stereo)
1108   {
1109     port_name = malloc(channel_name_size + 3);
1110     if (port_name == NULL)
1111     {
1112         goto fail_free_channel_name;
1113     }
1114
1115     memcpy(port_name, channel_name, channel_name_size);
1116     port_name[channel_name_size] = ' ';
1117     port_name[channel_name_size+1] = 'L';
1118     port_name[channel_name_size+2] = 0;
1119
1120     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1121     if (channel_ptr->port_left == NULL)
1122     {
1123         goto fail_free_port_name;
1124     }
1125
1126     port_name[channel_name_size+1] = 'R';
1127
1128     channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1129     if (channel_ptr->port_right == NULL)
1130     {
1131         goto fail_unregister_left_channel;
1132     }
1133   }
1134   else
1135   {
1136     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1137     if (channel_ptr->port_left == NULL)
1138     {
1139         goto fail_free_channel_name;
1140     }
1141   }
1142
1143   channel_ptr->stereo = stereo;
1144
1145   channel_ptr->volume = 0.0;
1146   channel_ptr->balance = 0.0;
1147   channel_ptr->meter_left = -1.0;
1148   channel_ptr->meter_right = -1.0;
1149   channel_ptr->abspeak = 0.0;
1150
1151   channel_ptr->peak_left = 0.0;
1152   channel_ptr->peak_right = 0.0;
1153   channel_ptr->peak_frames = 0;
1154
1155   channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1156   channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1157   channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1158   channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1159
1160   channel_ptr->NaN_detected = false;
1161
1162   channel_ptr->midi_cc_volume_index = 0;
1163   channel_ptr->midi_cc_balance_index = 0;
1164   channel_ptr->midi_change_callback = NULL;
1165   channel_ptr->midi_change_callback_data = NULL;
1166
1167   channel_ptr->midi_scale = NULL;
1168
1169   calc_channel_volumes(channel_ptr);
1170
1171   channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1172                   channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1173
1174   return channel_ptr;
1175
1176 fail_unregister_left_channel:
1177   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1178
1179 fail_free_port_name:
1180   free(port_name);
1181
1182 fail_free_channel_name:
1183   free(channel_ptr->name);
1184
1185 fail_free_channel:
1186   free(channel_ptr);
1187   channel_ptr = NULL;
1188
1189 fail:
1190   return NULL;
1191 }
1192
1193 static jack_mixer_output_channel_t
1194 create_output_channel(
1195   jack_mixer_t mixer,
1196   const char * channel_name,
1197   bool stereo,
1198   bool system)
1199 {
1200   struct channel * channel_ptr;
1201   struct output_channel * output_channel_ptr;
1202   char * port_name;
1203   size_t channel_name_size;
1204
1205   output_channel_ptr = malloc(sizeof(struct output_channel));
1206   channel_ptr = (struct channel*)output_channel_ptr;
1207   if (channel_ptr == NULL)
1208   {
1209     goto fail;
1210   }
1211
1212   channel_ptr->mixer_ptr = mixer_ctx_ptr;
1213
1214   channel_ptr->name = strdup(channel_name);
1215   if (channel_ptr->name == NULL)
1216   {
1217     goto fail_free_channel;
1218   }
1219
1220   if (stereo)
1221   {
1222     channel_name_size = strlen(channel_name);
1223
1224     port_name = malloc(channel_name_size + 4);
1225     if (port_name == NULL)
1226     {
1227         goto fail_free_channel_name;
1228     }
1229
1230     memcpy(port_name, channel_name, channel_name_size);
1231     port_name[channel_name_size] = ' ';
1232     port_name[channel_name_size+1] = 'L';
1233     port_name[channel_name_size+2] = 0;
1234
1235     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1236     if (channel_ptr->port_left == NULL)
1237     {
1238         goto fail_free_port_name;
1239     }
1240
1241     port_name[channel_name_size+1] = 'R';
1242
1243     channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1244     if (channel_ptr->port_right == NULL)
1245     {
1246         goto fail_unregister_left_channel;
1247     }
1248   }
1249   else
1250   {
1251     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1252     if (channel_ptr->port_left == NULL)
1253     {
1254         goto fail_free_channel_name;
1255     }
1256   }
1257
1258   channel_ptr->stereo = stereo;
1259
1260   channel_ptr->volume = 0.0;
1261   channel_ptr->balance = 0.0;
1262   channel_ptr->meter_left = -1.0;
1263   channel_ptr->meter_right = -1.0;
1264   channel_ptr->abspeak = 0.0;
1265
1266   channel_ptr->peak_left = 0.0;
1267   channel_ptr->peak_right = 0.0;
1268   channel_ptr->peak_frames = 0;
1269
1270   channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1271   channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1272   channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1273   channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1274
1275   channel_ptr->NaN_detected = false;
1276
1277   channel_ptr->midi_cc_volume_index = 0;
1278   channel_ptr->midi_cc_balance_index = 0;
1279   channel_ptr->midi_change_callback = NULL;
1280   channel_ptr->midi_change_callback_data = NULL;
1281
1282   channel_ptr->midi_scale = NULL;
1283
1284   output_channel_ptr->soloed_channels = NULL;
1285   output_channel_ptr->muted_channels = NULL;
1286   output_channel_ptr->system = system;
1287   output_channel_ptr->prefader = false;
1288
1289   return output_channel_ptr;
1290
1291 fail_unregister_left_channel:
1292   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1293
1294 fail_free_port_name:
1295   free(port_name);
1296
1297 fail_free_channel_name:
1298   free(channel_ptr->name);
1299
1300 fail_free_channel:
1301   free(channel_ptr);
1302   channel_ptr = NULL;
1303
1304 fail:
1305   return NULL;
1306 }
1307
1308 jack_mixer_output_channel_t
1309 add_output_channel(
1310   jack_mixer_t mixer,
1311   const char * channel_name,
1312   bool stereo,
1313   bool system)
1314 {
1315   struct output_channel *output_channel_ptr;
1316   struct channel *channel_ptr;
1317
1318   output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1319   if (output_channel_ptr == NULL) {
1320     return NULL;
1321   }
1322   channel_ptr = (struct channel*)output_channel_ptr;
1323
1324   ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1325                   ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1326
1327   return output_channel_ptr;
1328 }
1329
1330 void
1331 remove_output_channel(
1332   jack_mixer_output_channel_t output_channel)
1333 {
1334   struct output_channel *output_channel_ptr = output_channel;
1335   struct channel *channel_ptr = output_channel;
1336
1337   channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1338                   channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1339   free(channel_ptr->name);
1340
1341   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1342   if (channel_ptr->stereo)
1343   {
1344     jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1345   }
1346
1347   if (channel_ptr->midi_cc_volume_index != 0)
1348   {
1349     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1350     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1351   }
1352
1353   if (channel_ptr->midi_cc_balance_index != 0)
1354   {
1355     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1356     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1357   }
1358
1359   g_slist_free(output_channel_ptr->soloed_channels);
1360   g_slist_free(output_channel_ptr->muted_channels);
1361
1362   free(channel_ptr);
1363 }
1364
1365 void
1366 output_channel_set_solo(
1367   jack_mixer_output_channel_t output_channel,
1368   jack_mixer_channel_t channel,
1369   bool solo_value)
1370 {
1371   struct output_channel *output_channel_ptr = output_channel;
1372
1373   if (solo_value) {
1374     if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1375       return;
1376     output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1377   } else {
1378     if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1379       return;
1380     output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1381   }
1382 }
1383
1384 void
1385 output_channel_set_muted(
1386   jack_mixer_output_channel_t output_channel,
1387   jack_mixer_channel_t channel,
1388   bool muted_value)
1389 {
1390   struct output_channel *output_channel_ptr = output_channel;
1391
1392   if (muted_value) {
1393     if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1394       return;
1395     output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1396   } else {
1397     if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1398       return;
1399     output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1400   }
1401 }
1402
1403 bool
1404 output_channel_is_muted(
1405   jack_mixer_output_channel_t output_channel,
1406   jack_mixer_channel_t channel)
1407 {
1408   struct output_channel *output_channel_ptr = output_channel;
1409
1410   if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1411     return true;
1412   return false;
1413 }
1414
1415 bool
1416 output_channel_is_solo(
1417   jack_mixer_output_channel_t output_channel,
1418   jack_mixer_channel_t channel)
1419 {
1420   struct output_channel *output_channel_ptr = output_channel;
1421
1422   if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1423     return true;
1424   return false;
1425 }
1426
1427 void
1428 output_channel_set_prefader(
1429   jack_mixer_output_channel_t output_channel,
1430   bool pfl_value)
1431 {
1432   struct output_channel *output_channel_ptr = output_channel;
1433   output_channel_ptr->prefader = pfl_value;
1434 }
1435
1436 bool
1437 output_channel_is_prefader(
1438   jack_mixer_output_channel_t output_channel)
1439 {
1440   struct output_channel *output_channel_ptr = output_channel;
1441   return output_channel_ptr->prefader;
1442 }