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