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