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