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