]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.c
Set version to 14 in preparation for next release
[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 VOLUME_TRANSITION_SECONDS 0.01
47
48 #define PEAK_FRAMES_CHUNK 4800
49
50 // we don't know how much to allocate, but we don't want to wait with
51 // allocating until we're in the process() callback, so we just take a
52 // fairly big chunk: 4 periods per buffer, 4096 samples per period.
53 // (not sure if the '*4' is needed)
54 #define MAX_BLOCK_SIZE (4 * 4096)
55
56 #define FLOAT_EXISTS(x) (!((x) - (x)))
57
58 struct channel
59 {
60   struct jack_mixer * mixer_ptr;
61   char * name;
62   bool stereo;
63   bool out_mute;
64   float volume_transition_seconds;
65   unsigned int num_volume_transition_steps;
66   float volume;
67   jack_nframes_t volume_idx;
68   float volume_new;
69   float balance;
70   jack_nframes_t balance_idx;
71   float balance_new;
72   float volume_left;
73   float volume_left_new;
74   float volume_right;
75   float volume_right_new;
76   float meter_left;
77   float meter_right;
78   float abspeak;
79   jack_port_t * port_left;
80   jack_port_t * port_right;
81
82   jack_nframes_t peak_frames;
83   float peak_left;
84   float peak_right;
85
86   jack_default_audio_sample_t * tmp_mixed_frames_left;
87   jack_default_audio_sample_t * tmp_mixed_frames_right;
88   jack_default_audio_sample_t * frames_left;
89   jack_default_audio_sample_t * frames_right;
90   jack_default_audio_sample_t * prefader_frames_left;
91   jack_default_audio_sample_t * prefader_frames_right;
92
93   bool NaN_detected;
94
95   int midi_cc_volume_index;
96   int midi_cc_balance_index;
97   int midi_cc_mute_index;
98   int midi_cc_solo_index;
99   bool midi_cc_volume_picked_up;
100   bool midi_cc_balance_picked_up;
101
102   jack_default_audio_sample_t * left_buffer_ptr;
103   jack_default_audio_sample_t * right_buffer_ptr;
104
105   bool midi_in_got_events;
106   void (*midi_change_callback) (void*);
107   void *midi_change_callback_data;
108   bool midi_out_has_events;
109
110   jack_mixer_scale_t midi_scale;
111 };
112
113 struct output_channel {
114   struct channel channel;
115   GSList *soloed_channels;
116   GSList *muted_channels;
117   GSList *prefader_channels;
118
119   bool system; /* system channel, without any associated UI */
120   bool prefader;
121 };
122
123 struct jack_mixer
124 {
125   pthread_mutex_t mutex;
126   jack_client_t * jack_client;
127   GSList *input_channels_list;
128   GSList *output_channels_list;
129   GSList *soloed_channels;
130
131   jack_port_t * port_midi_in;
132   jack_port_t * port_midi_out;
133   int last_midi_channel;
134   enum midi_behavior_mode midi_behavior;
135
136   struct channel* midi_cc_map[128];
137 };
138
139 static jack_mixer_output_channel_t create_output_channel(
140   jack_mixer_t mixer,
141   const char * channel_name,
142   bool stereo,
143   bool system);
144
145 static inline void
146 update_channel_buffers(
147   struct channel * channel_ptr,
148   jack_nframes_t nframes);
149
150
151 float
152 value_to_db(
153   float value)
154 {
155   if (value <= 0)
156   {
157     return -INFINITY;
158   }
159
160   return 20.0 * log10f(value);
161 }
162
163 float
164 db_to_value(
165   float db)
166 {
167   return powf(10.0, db/20.0);
168 }
169
170 #define channel_ptr ((struct channel *)channel)
171
172 const char*
173 channel_get_name(
174   jack_mixer_channel_t channel)
175 {
176   return channel_ptr->name;
177 }
178
179 void
180 channel_rename(
181   jack_mixer_channel_t channel,
182   const char * name)
183 {
184   char * new_name;
185   size_t channel_name_size;
186   char * port_name;
187   int ret;
188
189   new_name = strdup(name);
190   if (new_name == NULL)
191   {
192     return;
193   }
194
195   if (channel_ptr->name)
196   {
197     free(channel_ptr->name);
198   }
199
200   channel_ptr->name = new_name;
201
202   if (channel_ptr->stereo)
203   {
204     channel_name_size = strlen(name);
205     port_name = malloc(channel_name_size + 3);
206     memcpy(port_name, name, channel_name_size);
207
208     port_name[channel_name_size] = ' ';
209     port_name[channel_name_size+1] = 'L';
210     port_name[channel_name_size+2] = 0;
211
212     ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left, port_name);
213     if (ret != 0)
214     {
215       /* what could we do here? */
216     }
217
218     port_name[channel_name_size+1] = 'R';
219
220     ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right, port_name);
221     if (ret != 0)
222     {
223       /* what could we do here? */
224     }
225
226     free(port_name);
227   }
228   else
229   {
230     ret = jack_port_rename(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left, name);
231     if (ret != 0)
232     {
233       /* what could we do here? */
234     }
235   }
236 }
237
238 bool
239 channel_is_stereo(
240   jack_mixer_channel_t channel)
241 {
242   return channel_ptr->stereo;
243 }
244
245 int
246 channel_get_balance_midi_cc(
247   jack_mixer_channel_t channel)
248 {
249   return channel_ptr->midi_cc_balance_index;
250 }
251
252 static void
253 channel_unset_midi_cc_map(
254   jack_mixer_channel_t channel,
255   int new_cc) {
256   if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_volume_index == new_cc) {
257     channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_volume_index = -1;
258   } else if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_balance_index == new_cc) {
259   channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_balance_index = -1;
260   } else if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_mute_index == new_cc) {
261   channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_mute_index = -1;
262   } else if (channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_solo_index == new_cc) {
263   channel_ptr->mixer_ptr->midi_cc_map[new_cc]->midi_cc_solo_index = -1;
264   }
265 }
266
267 unsigned int
268 channel_set_balance_midi_cc(
269   jack_mixer_channel_t channel,
270   int new_cc)
271 {
272   if (new_cc < 0 || new_cc > 127) {
273     return 2; /* error: outside limit CC */
274   }
275   if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
276     channel_unset_midi_cc_map(channel, new_cc);
277   }
278   if (channel_ptr->midi_cc_balance_index != -1) {
279     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
280   }
281   channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
282   channel_ptr->midi_cc_balance_index = new_cc;
283   return 0;
284 }
285
286 int
287 channel_get_volume_midi_cc(
288   jack_mixer_channel_t channel)
289 {
290   return channel_ptr->midi_cc_volume_index;
291 }
292
293 unsigned int
294 channel_set_volume_midi_cc(
295   jack_mixer_channel_t channel, int new_cc)
296 {
297   if (new_cc< 0 || new_cc > 127) {
298     return 2; /* error: outside limit CC */
299   }
300   if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
301     channel_unset_midi_cc_map(channel, new_cc);
302   }
303   if (channel_ptr->midi_cc_volume_index != -1) {
304     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
305   }
306   channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
307   channel_ptr->midi_cc_volume_index = new_cc;
308   return 0;
309 }
310
311 int
312 channel_get_mute_midi_cc(
313   jack_mixer_channel_t channel)
314 {
315   return channel_ptr->midi_cc_mute_index;
316 }
317
318 unsigned int
319 channel_set_mute_midi_cc(
320   jack_mixer_channel_t channel,
321   int new_cc)
322 {
323   if (new_cc < 0 || new_cc > 127) {
324     return 2; /* error: outside limit CC */
325   }
326   if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
327      channel_unset_midi_cc_map(channel, new_cc);
328   }
329
330   if (channel_ptr->midi_cc_mute_index != -1) {
331     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
332   }
333   channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
334   channel_ptr->midi_cc_mute_index = new_cc;
335   return 0;
336 }
337
338 int
339 channel_get_solo_midi_cc(
340   jack_mixer_channel_t channel)
341 {
342   return channel_ptr->midi_cc_solo_index;
343 }
344
345 void channel_set_midi_cc_volume_picked_up(jack_mixer_channel_t channel, bool status)
346 {
347   LOG_DEBUG("Setting channel %s volume picked up to %d", channel_ptr->name, status);
348   channel_ptr->midi_cc_volume_picked_up = status;
349 }
350
351 void channel_set_midi_cc_balance_picked_up(jack_mixer_channel_t channel, bool status)
352 {
353   LOG_DEBUG("Setting channel %s balance picked up to %d", channel_ptr->name, status);
354   channel_ptr->midi_cc_balance_picked_up = status;
355 }
356
357 unsigned int
358 channel_set_solo_midi_cc(
359   jack_mixer_channel_t channel,
360   int new_cc)
361 {
362   if (new_cc < 0 || new_cc > 127) {
363     return 2; /* error: outside limit CC */
364   }
365   if (channel_ptr->mixer_ptr->midi_cc_map[new_cc] != NULL) {
366     channel_unset_midi_cc_map(channel, new_cc);
367   }
368   if (channel_ptr->midi_cc_solo_index != -1) {
369     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
370   }
371   channel_ptr->mixer_ptr->midi_cc_map[new_cc] = channel_ptr;
372   channel_ptr->midi_cc_solo_index = new_cc;
373   return 0;
374 }
375
376 void
377 channel_autoset_volume_midi_cc(
378   jack_mixer_channel_t channel)
379 {
380   struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
381
382   for (int i = 11 ; i < 128 ; i++)
383   {
384     if (mixer_ptr->midi_cc_map[i] == NULL)
385     {
386       mixer_ptr->midi_cc_map[i] = channel_ptr;
387       channel_ptr->midi_cc_volume_index = i;
388
389       LOG_DEBUG("New channel \"%s\" volume mapped to CC#%i", channel_ptr->name, i);
390
391       break;
392     }
393   }
394 }
395
396 void
397 channel_autoset_balance_midi_cc(
398   jack_mixer_channel_t channel)
399 {
400   struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
401
402   for (int i = 11; i < 128 ; i++)
403   {
404     if (mixer_ptr->midi_cc_map[i] == NULL)
405     {
406       mixer_ptr->midi_cc_map[i] = channel_ptr;
407       channel_ptr->midi_cc_balance_index = i;
408
409       LOG_DEBUG("New channel \"%s\" balance mapped to CC#%i", channel_ptr->name, i);
410
411       break;
412     }
413   }
414 }
415
416 void
417 channel_autoset_mute_midi_cc(
418   jack_mixer_channel_t channel)
419 {
420   struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
421
422   for (int i = 11; i < 128 ; i++)
423   {
424     if (mixer_ptr->midi_cc_map[i] == NULL)
425     {
426       mixer_ptr->midi_cc_map[i] = channel_ptr;
427       channel_ptr->midi_cc_mute_index = i;
428
429       LOG_DEBUG("New channel \"%s\" mute mapped to CC#%i", channel_ptr->name, i);
430
431       break;
432     }
433   }
434 }
435
436 void
437 channel_autoset_solo_midi_cc(
438   jack_mixer_channel_t channel)
439 {
440   struct jack_mixer *mixer_ptr = channel_ptr->mixer_ptr;
441
442   for (int i = 11; i < 128 ; i++)
443   {
444     if (mixer_ptr->midi_cc_map[i] == NULL)
445     {
446       mixer_ptr->midi_cc_map[i] = channel_ptr;
447       channel_ptr->midi_cc_solo_index = i;
448
449       LOG_DEBUG("New channel \"%s\" solo mapped to CC#%i", channel_ptr->name, i);
450
451       break;
452     }
453   }
454 }
455
456 void
457 remove_channel(
458   jack_mixer_channel_t channel)
459 {
460   GSList *list_ptr;
461   channel_ptr->mixer_ptr->input_channels_list = g_slist_remove(
462                   channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
463   free(channel_ptr->name);
464
465   /* remove references to input channel from all output channels */
466   for (list_ptr = channel_ptr->mixer_ptr->output_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
467   {
468     struct output_channel *output_channel_ptr = list_ptr->data;
469     output_channel_set_solo(output_channel_ptr, channel, false);
470     output_channel_set_muted(output_channel_ptr, channel, false);
471   }
472
473   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
474   if (channel_ptr->stereo)
475   {
476     jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
477   }
478
479   if (channel_ptr->midi_cc_volume_index != -1)
480   {
481     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
482     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
483   }
484
485   if (channel_ptr->midi_cc_balance_index != -1)
486   {
487     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
488     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
489   }
490
491   if (channel_ptr->midi_cc_mute_index != -1)
492   {
493     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
494     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
495   }
496   if (channel_ptr->midi_cc_solo_index != -1)
497   {
498     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
499     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
500   }
501
502   free(channel_ptr->frames_left);
503   free(channel_ptr->frames_right);
504   free(channel_ptr->prefader_frames_left);
505   free(channel_ptr->prefader_frames_right);
506
507   free(channel_ptr);
508 }
509
510 void
511 channel_stereo_meter_read(
512   jack_mixer_channel_t channel,
513   double * left_ptr,
514   double * right_ptr)
515 {
516   assert(channel_ptr);
517   *left_ptr = value_to_db(channel_ptr->meter_left);
518   *right_ptr = value_to_db(channel_ptr->meter_right);
519 }
520
521 double interpolate(double start, double end, int step, int steps) {
522   double ret;
523   double frac = 0.01;
524   LOG_DEBUG("%f -> %f, %d", start, end, step);
525   if (start <= 0) {
526     if (step <= frac * steps) {
527       ret = frac * end * step / steps;
528     } else {
529       ret = db_to_value(value_to_db(frac * end) + (value_to_db(end) - value_to_db(frac * end)) * step / steps);;
530     }
531   } else if (end <= 0) {
532       if (step >= (1 - frac) * steps) {
533         ret = frac * start - frac * start * step / steps;
534       } else {
535         ret = db_to_value(value_to_db(start) + (value_to_db(frac * start) - value_to_db(start)) * step / steps);
536       }
537     } else {
538     ret = db_to_value(value_to_db(start) + (value_to_db(end) - value_to_db(start)) *step /steps);
539   }
540   LOG_DEBUG("interpolate: %f", ret);
541   return ret;
542 }
543
544 void
545 channel_mono_meter_read(
546   jack_mixer_channel_t channel,
547   double * mono_ptr)
548 {
549   *mono_ptr = value_to_db(channel_ptr->meter_left);
550 }
551
552 void
553 channel_volume_write(
554   jack_mixer_channel_t channel,
555   double volume)
556 {
557   assert(channel_ptr);
558   /*If changing volume and find we're in the middle of a previous transition,
559    *then set current volume to place in transition to avoid a jump.*/
560   if (channel_ptr->volume_new != channel_ptr->volume) {
561     channel_ptr->volume = interpolate(channel_ptr->volume, channel_ptr->volume_new, channel_ptr->volume_idx,
562      channel_ptr->num_volume_transition_steps);
563   }
564   channel_ptr->volume_idx = 0;
565   channel_ptr->volume_new = db_to_value(volume);
566   channel_ptr->midi_out_has_events = true;
567 }
568
569 double
570 channel_volume_read(
571   jack_mixer_channel_t channel)
572 {
573   assert(channel_ptr);
574   return value_to_db(channel_ptr->volume_new);
575 }
576
577 void
578 channels_volumes_read(jack_mixer_t mixer_ptr)
579 {
580     GSList *node_ptr;
581     struct channel *pChannel;
582     struct jack_mixer * pMixer = (struct jack_mixer *)mixer_ptr;
583
584     for (node_ptr = pMixer->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
585     {
586         pChannel = (struct channel *)node_ptr->data;
587         double vol = channel_volume_read( (jack_mixer_channel_t)pChannel);
588         printf("%s : volume is %f dbFS for mixer channel: %s\n", jack_get_client_name(pMixer->jack_client), vol, pChannel->name);
589     }
590 }
591
592 void
593 channel_balance_write(
594   jack_mixer_channel_t channel,
595   double balance)
596 {
597   assert(channel_ptr);
598   if (channel_ptr->balance != channel_ptr->balance_new) {
599     channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
600       (channel_ptr->balance_new - channel_ptr->balance) /
601       channel_ptr->num_volume_transition_steps;
602   }
603   channel_ptr->balance_idx = 0;
604   channel_ptr->balance_new = balance;
605 }
606
607 double
608 channel_balance_read(
609   jack_mixer_channel_t channel)
610 {
611   assert(channel_ptr);
612   return channel_ptr->balance_new;
613 }
614
615 double
616 channel_abspeak_read(
617   jack_mixer_channel_t channel)
618 {
619   assert(channel_ptr);
620   if (channel_ptr->NaN_detected)
621   {
622     return sqrt(-1);
623   }
624   else
625   {
626     return value_to_db(channel_ptr->abspeak);
627   }
628 }
629
630 void
631 channel_abspeak_reset(
632   jack_mixer_channel_t channel)
633 {
634   channel_ptr->abspeak = 0;
635   channel_ptr->NaN_detected = false;
636 }
637
638 void
639 channel_out_mute(
640   jack_mixer_channel_t channel)
641 {
642   channel_ptr->out_mute = true;
643 }
644
645 void
646 channel_out_unmute(
647   jack_mixer_channel_t channel)
648 {
649   channel_ptr->out_mute = false;
650 }
651
652 bool
653 channel_is_out_muted(
654   jack_mixer_channel_t channel)
655 {
656   return channel_ptr->out_mute;
657 }
658
659 void
660 channel_solo(
661   jack_mixer_channel_t channel)
662 {
663   if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) != NULL)
664     return;
665   channel_ptr->mixer_ptr->soloed_channels = g_slist_prepend(channel_ptr->mixer_ptr->soloed_channels, channel);
666 }
667
668 void
669 channel_unsolo(
670   jack_mixer_channel_t channel)
671 {
672   if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) == NULL)
673     return;
674   channel_ptr->mixer_ptr->soloed_channels = g_slist_remove(channel_ptr->mixer_ptr->soloed_channels, channel);
675 }
676
677 bool
678 channel_is_soloed(
679   jack_mixer_channel_t channel)
680 {
681   if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel))
682     return true;
683   return false;
684 }
685
686 void
687 channel_set_midi_scale(
688   jack_mixer_channel_t channel,
689   jack_mixer_scale_t scale)
690 {
691   channel_ptr->midi_scale = scale;
692 }
693
694 void
695 channel_set_midi_change_callback(
696   jack_mixer_channel_t channel,
697   void (*midi_change_callback) (void*),
698   void *user_data)
699 {
700   channel_ptr->midi_change_callback = midi_change_callback;
701   channel_ptr->midi_change_callback_data = user_data;
702 }
703
704 bool
705 channel_get_midi_in_got_events(
706   jack_mixer_channel_t channel)
707 {
708   bool t = channel_ptr->midi_in_got_events;
709   channel_ptr->midi_in_got_events = false;
710   return t;
711 }
712
713 #undef channel_ptr
714
715 /* process input channels and mix them into main mix */
716 static inline void
717 mix_one(
718   struct output_channel *output_mix_channel,
719   GSList *channels_list,
720   jack_nframes_t start,         /* index of first sample to process */
721   jack_nframes_t end)           /* index of sample to stop processing before */
722 {
723   jack_nframes_t i;
724   GSList *node_ptr;
725   struct channel * channel_ptr;
726   jack_default_audio_sample_t frame_left;
727   jack_default_audio_sample_t frame_right;
728   struct channel *mix_channel = (struct channel*)output_mix_channel;
729
730   for (i = start; i < end; i++)
731   {
732     mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i] = 0.0;
733     if (mix_channel->stereo)
734       mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i] = 0.0;
735   }
736
737   for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
738   {
739     channel_ptr = node_ptr->data;
740
741     if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL || channel_ptr->out_mute) {
742       /* skip muted channels */
743       continue;
744     }
745
746     if ((!channel_ptr->mixer_ptr->soloed_channels && !output_mix_channel->soloed_channels) ||
747         (channel_ptr->mixer_ptr->soloed_channels &&
748          g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel_ptr) != NULL) ||
749         (output_mix_channel->soloed_channels &&
750         g_slist_find(output_mix_channel->soloed_channels, channel_ptr) != NULL)) {
751
752       for (i = start ; i < end ; i++)
753       {
754         if (! output_mix_channel->prefader &&
755          g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL) {
756           frame_left = channel_ptr->frames_left[i-start];
757         } else {
758           frame_left = channel_ptr->prefader_frames_left[i-start];
759         }
760         if (frame_left == NAN)
761           break;
762         mix_channel->tmp_mixed_frames_left[i] += frame_left;
763         if (mix_channel->stereo)
764         {
765           if (! output_mix_channel->prefader &&
766               g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL) {
767             frame_right = channel_ptr->frames_right[i-start];
768           } else {
769             frame_right = channel_ptr->prefader_frames_right[i-start];
770           }
771           if (frame_right == NAN)
772             break;
773           mix_channel->tmp_mixed_frames_right[i] += frame_right;
774         }
775       }
776     }
777   }
778
779   /* process main mix channel */
780   unsigned int steps = mix_channel->num_volume_transition_steps;
781   for (i = start ; i < end ; i++)
782   {
783     if (! output_mix_channel->prefader) {
784       float volume = mix_channel->volume;
785       float volume_new = mix_channel->volume_new;
786       float vol = volume;
787       float balance = mix_channel->balance;
788       float balance_new = mix_channel->balance_new;
789       float bal = balance;
790       if (volume != volume_new) {
791         vol = interpolate(volume, volume_new,  mix_channel->volume_idx, steps);
792       }
793       if (balance != balance_new) {
794         bal = mix_channel->balance_idx * (balance_new - balance) / steps + balance;
795       }
796
797       float vol_l;
798       float vol_r;
799       if (mix_channel->stereo) {
800         if (bal > 0) {
801           vol_l = vol * (1 - bal);
802           vol_r = vol;
803         } else {
804           vol_l = vol;
805           vol_r = vol * (1 + bal);
806         }
807       } else {
808         vol_l = vol * (1 - bal);
809         vol_r = vol * (1 + bal);
810       }
811       mix_channel->tmp_mixed_frames_left[i] *= vol_l;
812       mix_channel->tmp_mixed_frames_right[i] *= vol_r;
813     }
814
815     frame_left = fabsf(mix_channel->tmp_mixed_frames_left[i]);
816     if (mix_channel->peak_left < frame_left)
817     {
818       mix_channel->peak_left = frame_left;
819
820       if (frame_left > mix_channel->abspeak)
821       {
822         mix_channel->abspeak = frame_left;
823       }
824     }
825
826     if (mix_channel->stereo)
827     {
828       frame_right = fabsf(mix_channel->tmp_mixed_frames_right[i]);
829       if (mix_channel->peak_right < frame_right)
830       {
831         mix_channel->peak_right = frame_right;
832
833         if (frame_right > mix_channel->abspeak)
834         {
835           mix_channel->abspeak = frame_right;
836         }
837       }
838     }
839
840     mix_channel->peak_frames++;
841     if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
842     {
843       mix_channel->meter_left = mix_channel->peak_left;
844       mix_channel->peak_left = 0.0;
845
846       if (mix_channel->stereo)
847       {
848         mix_channel->meter_right = mix_channel->peak_right;
849         mix_channel->peak_right = 0.0;
850       }
851
852       mix_channel->peak_frames = 0;
853     }
854     mix_channel->volume_idx++;
855     if ((mix_channel->volume != mix_channel->volume_new) && (mix_channel->volume_idx == steps)) {
856       mix_channel->volume = mix_channel->volume_new;
857       mix_channel->volume_idx = 0;
858     }
859     mix_channel->balance_idx++;
860     if ((mix_channel->balance != mix_channel->balance_new) && (mix_channel->balance_idx == steps)) {
861       mix_channel->balance = mix_channel->balance_new;
862       mix_channel->balance_idx = 0;
863     }
864
865     if (!mix_channel->out_mute) {
866         mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i];
867         if (mix_channel->stereo)
868           mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i];
869     }
870   }
871 }
872
873 static inline void
874 calc_channel_frames(
875   struct channel *channel_ptr,
876   jack_nframes_t start,
877   jack_nframes_t end)
878 {
879   jack_nframes_t i;
880   jack_default_audio_sample_t frame_left = 0.0f;
881   jack_default_audio_sample_t frame_right = 0.0f;
882   unsigned int steps = channel_ptr->num_volume_transition_steps;
883   for (i = start ; i < end ; i++)
884   {
885     if (i-start >= MAX_BLOCK_SIZE)
886     {
887       fprintf(stderr, "i-start too high: %d - %d\n", i, start);
888     }
889     channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
890     if (channel_ptr->stereo)
891       channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
892
893     if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
894     {
895       channel_ptr->NaN_detected = true;
896       channel_ptr->frames_left[i-start] = NAN;
897       break;
898     }
899     float volume = channel_ptr->volume;
900     float volume_new = channel_ptr->volume_new;
901     float vol = volume;
902     float balance = channel_ptr->balance;
903     float balance_new = channel_ptr->balance_new;
904     float bal = balance;
905     if (channel_ptr->volume != channel_ptr->volume_new) {
906       vol = interpolate(volume, volume_new, channel_ptr->volume_idx, steps);
907     }
908     if (channel_ptr->balance != channel_ptr->balance_new) {
909       bal = channel_ptr->balance_idx * (balance_new - balance) / steps + balance;
910     }
911     float vol_l;
912     float vol_r;
913     if (channel_ptr->stereo) {
914       if (bal > 0) {
915         vol_l = vol * (1 - bal);
916         vol_r = vol;
917       } else {
918         vol_l = vol;
919         vol_r = vol * (1 + bal);
920       }
921     } else {
922       vol_l = vol * (1 - bal);
923       vol_r = vol * (1 + bal);
924     }
925     frame_left = channel_ptr->left_buffer_ptr[i] * vol_l;
926     if (channel_ptr->stereo)
927     {
928       if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
929       {
930         channel_ptr->NaN_detected = true;
931         channel_ptr->frames_right[i-start] = NAN;
932         break;
933       }
934
935       frame_right = channel_ptr->right_buffer_ptr[i] * vol_r;
936     }
937
938     channel_ptr->frames_left[i-start] = frame_left;
939     channel_ptr->frames_right[i-start] = frame_right;
940
941     if (channel_ptr->stereo)
942     {
943       frame_left = fabsf(frame_left);
944       frame_right = fabsf(frame_right);
945
946       if (channel_ptr->peak_left < frame_left)
947       {
948         channel_ptr->peak_left = frame_left;
949
950         if (frame_left > channel_ptr->abspeak)
951         {
952           channel_ptr->abspeak = frame_left;
953         }
954       }
955
956       if (channel_ptr->peak_right < frame_right)
957       {
958         channel_ptr->peak_right = frame_right;
959
960         if (frame_right > channel_ptr->abspeak)
961         {
962           channel_ptr->abspeak = frame_right;
963         }
964       }
965     }
966     else
967     {
968       frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
969
970       if (channel_ptr->peak_left < frame_left)
971       {
972         channel_ptr->peak_left = frame_left;
973
974         if (frame_left > channel_ptr->abspeak)
975         {
976           channel_ptr->abspeak = frame_left;
977         }
978       }
979     }
980
981     channel_ptr->peak_frames++;
982     if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
983     {
984       channel_ptr->meter_left = channel_ptr->peak_left;
985       channel_ptr->peak_left = 0.0;
986
987       if (channel_ptr->stereo)
988       {
989         channel_ptr->meter_right = channel_ptr->peak_right;
990         channel_ptr->peak_right = 0.0;
991       }
992
993       channel_ptr->peak_frames = 0;
994     }
995     channel_ptr->volume_idx++;
996     if ((channel_ptr->volume != channel_ptr->volume_new) &&
997      (channel_ptr->volume_idx == steps)) {
998       channel_ptr->volume = channel_ptr->volume_new;
999       channel_ptr->volume_idx = 0;
1000     }
1001     channel_ptr->balance_idx++;
1002     if ((channel_ptr->balance != channel_ptr->balance_new) &&
1003      (channel_ptr->balance_idx >= steps)) {
1004       channel_ptr->balance = channel_ptr->balance_new;
1005       channel_ptr->balance_idx = 0;
1006      }
1007   }
1008 }
1009
1010 static inline void
1011 mix(
1012   struct jack_mixer * mixer_ptr,
1013   jack_nframes_t start,         /* index of first sample to process */
1014   jack_nframes_t end)           /* index of sample to stop processing before */
1015 {
1016   GSList *node_ptr;
1017   struct output_channel * output_channel_ptr;
1018   struct channel *channel_ptr;
1019
1020   for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1021   {
1022     channel_ptr = (struct channel*)node_ptr->data;
1023     calc_channel_frames(channel_ptr, start, end);
1024   }
1025
1026   for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1027   {
1028     output_channel_ptr = node_ptr->data;
1029     channel_ptr = (struct channel*)output_channel_ptr;
1030
1031     if (output_channel_ptr->system)
1032     {
1033       /* Don't bother mixing the channels if we are not connected */
1034       if (channel_ptr->stereo)
1035       {
1036         if (jack_port_connected(channel_ptr->port_left) == 0 &&
1037             jack_port_connected(channel_ptr->port_right) == 0)
1038           continue;
1039       } else {
1040          if (jack_port_connected(channel_ptr->port_left) == 0)
1041            continue;
1042       }
1043     }
1044
1045     mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
1046   }
1047 }
1048
1049 static inline void
1050 update_channel_buffers(
1051   struct channel * channel_ptr,
1052   jack_nframes_t nframes)
1053 {
1054   channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
1055
1056   if (channel_ptr->stereo)
1057   {
1058     channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
1059   }
1060 }
1061
1062 #define mixer_ptr ((struct jack_mixer *)context)
1063
1064 static int
1065 process(
1066   jack_nframes_t nframes,
1067   void * context)
1068 {
1069   GSList *node_ptr;
1070   struct channel * channel_ptr;
1071 #if defined(HAVE_JACK_MIDI)
1072   jack_nframes_t i;
1073   jack_nframes_t event_count;
1074   jack_midi_event_t in_event;
1075   unsigned char* midi_out_buffer;
1076   void * midi_buffer;
1077   signed char byte;
1078   unsigned int cc_channel_index;
1079 #endif
1080
1081   for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1082   {
1083     channel_ptr = node_ptr->data;
1084     update_channel_buffers(channel_ptr, nframes);
1085   }
1086
1087   // Fill output buffers with the input
1088   for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1089   {
1090     channel_ptr = node_ptr->data;
1091     update_channel_buffers(channel_ptr, nframes);
1092   }
1093
1094 #if defined(HAVE_JACK_MIDI)
1095   midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
1096   event_count = jack_midi_get_event_count(midi_buffer);
1097
1098   for (i = 0 ; i < event_count; i++)
1099   {
1100     jack_midi_event_get(&in_event, midi_buffer, i);
1101
1102     if (in_event.size != 3 ||
1103         (in_event.buffer[0] & 0xF0) != 0xB0 ||
1104         in_event.buffer[1] > 127 ||
1105         in_event.buffer[2] > 127)
1106     {
1107       continue;
1108     }
1109
1110     assert(in_event.time < nframes);
1111
1112     LOG_DEBUG(
1113       "%u: CC#%u -> %u",
1114       (unsigned int)(in_event.buffer[0]),
1115       (unsigned int)in_event.buffer[1],
1116       (unsigned int)in_event.buffer[2]);
1117
1118     mixer_ptr->last_midi_channel = (int)in_event.buffer[1];
1119     channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
1120
1121     /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
1122     if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
1123     {
1124       if (channel_ptr->midi_cc_balance_index == (char)in_event.buffer[1])
1125       {
1126         byte = in_event.buffer[2];
1127         if (byte == 0)
1128         {
1129           byte = 1;
1130         }
1131         byte -= 64;
1132         int current_midi = (int)63 * channel_ptr->balance + 64;
1133         current_midi = current_midi == 1 ? 0 : current_midi;
1134         if (mixer_ptr->midi_behavior == Pick_Up &&
1135          !channel_ptr->midi_cc_balance_picked_up) {
1136           if (in_event.buffer[2] == current_midi) {
1137             channel_set_midi_cc_balance_picked_up(channel_ptr, true);
1138           }
1139         }
1140         if ((mixer_ptr->midi_behavior == Pick_Up &&
1141          channel_ptr->midi_cc_balance_picked_up) ||
1142          mixer_ptr->midi_behavior == Jump_To_Value) {
1143           if (channel_ptr->balance != channel_ptr->balance_new) {
1144             channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
1145              (channel_ptr->balance_new - channel_ptr->balance) /
1146             channel_ptr->num_volume_transition_steps;
1147           }
1148           channel_ptr->balance_idx = 0;
1149           channel_ptr->balance_new = (float)byte / 63;
1150           LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance_new);
1151         }
1152       }
1153       else if (channel_ptr->midi_cc_volume_index == in_event.buffer[1])
1154       {
1155           int current_midi =  (int)(127 *
1156            scale_db_to_scale(channel_ptr->midi_scale,
1157            value_to_db(channel_ptr->volume)));
1158           if (mixer_ptr->midi_behavior == Pick_Up &&
1159            !channel_ptr->midi_cc_volume_picked_up ) {
1160           if (in_event.buffer[2] == current_midi) {
1161             channel_set_midi_cc_volume_picked_up(channel_ptr, true);
1162           }
1163         }
1164         if ((mixer_ptr->midi_behavior == Pick_Up &&
1165          channel_ptr->midi_cc_volume_picked_up) ||
1166          mixer_ptr->midi_behavior == Jump_To_Value) {
1167             if (channel_ptr->volume_new != channel_ptr->volume) {
1168               channel_ptr->volume = interpolate(channel_ptr->volume, channel_ptr->volume_new, channel_ptr->volume_idx,
1169                channel_ptr->num_volume_transition_steps);
1170             }
1171             channel_ptr->volume_idx = 0;
1172             channel_ptr->volume_new = db_to_value(scale_scale_to_db(channel_ptr->midi_scale,
1173              (double)in_event.buffer[2] / 127));
1174             LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume_new);
1175         }
1176       }
1177       else if (channel_ptr->midi_cc_mute_index == in_event.buffer[1])
1178       {
1179         if ((unsigned int)in_event.buffer[2] == 127) {
1180           channel_ptr->out_mute = !channel_ptr->out_mute;
1181         }
1182         LOG_DEBUG("\"%s\" out_mute %d", channel_ptr->name, channel_ptr->out_mute);
1183       }
1184       else if (channel_ptr->midi_cc_solo_index == in_event.buffer[1])
1185       {
1186         if ((unsigned int)in_event.buffer[2] == 127) {
1187           if (channel_is_soloed(channel_ptr)) {
1188             channel_unsolo(channel_ptr);
1189           } else {
1190             channel_solo(channel_ptr);
1191           }
1192         }
1193         LOG_DEBUG("\"%s\" solo %d", channel_ptr->name, channel_is_soloed(channel_ptr));
1194       }
1195       channel_ptr->midi_in_got_events = true;
1196       if (channel_ptr->midi_change_callback)
1197         channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
1198
1199     }
1200
1201   }
1202
1203   midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_out, nframes);
1204   jack_midi_clear_buffer(midi_buffer);
1205
1206   for(i=0; i<nframes; i++)
1207   {
1208     for (cc_channel_index=0; cc_channel_index<128; cc_channel_index++)
1209     {
1210       channel_ptr = mixer_ptr->midi_cc_map[cc_channel_index];
1211       if (channel_ptr == NULL || channel_ptr->midi_scale == NULL)
1212       {
1213         continue;
1214       }
1215       if (channel_ptr->midi_out_has_events == false)
1216       {
1217         continue;
1218       }
1219       if (channel_ptr->midi_cc_balance_index == (int)cc_channel_index)
1220       {
1221         continue;
1222       }
1223       midi_out_buffer = jack_midi_event_reserve(midi_buffer, i, 3);
1224       if (midi_out_buffer == NULL)
1225       {
1226         continue;
1227       }
1228       midi_out_buffer[0] = 0xB0; /* control change */
1229       midi_out_buffer[1] = cc_channel_index;
1230       midi_out_buffer[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr->midi_scale, value_to_db(channel_ptr->volume_new)));
1231
1232       LOG_DEBUG(
1233         "%u: CC#%u <- %u",
1234         (unsigned int)(midi_out_buffer[0]),
1235         (unsigned int)midi_out_buffer[1],
1236         (unsigned int)midi_out_buffer[2]);
1237
1238       channel_ptr->midi_out_has_events = false;
1239     }
1240   }
1241
1242 #endif
1243
1244   mix(mixer_ptr, 0, nframes);
1245
1246   return 0;
1247 }
1248
1249 #undef mixer_ptr
1250
1251 jack_mixer_t
1252 create(
1253   const char * jack_client_name_ptr,
1254   bool stereo)
1255 {
1256   (void) stereo;
1257   int ret;
1258   struct jack_mixer * mixer_ptr;
1259   int i;
1260
1261
1262   mixer_ptr = malloc(sizeof(struct jack_mixer));
1263   if (mixer_ptr == NULL)
1264   {
1265     goto exit;
1266   }
1267
1268   ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
1269   if (ret != 0)
1270   {
1271     goto exit_free;
1272   }
1273
1274   mixer_ptr->input_channels_list = NULL;
1275   mixer_ptr->output_channels_list = NULL;
1276
1277   mixer_ptr->soloed_channels = NULL;
1278
1279   mixer_ptr->last_midi_channel = -1;
1280
1281   mixer_ptr->midi_behavior = Jump_To_Value;
1282
1283   for (i = 0 ; i < 128 ; i++)
1284   {
1285     mixer_ptr->midi_cc_map[i] = NULL;
1286   }
1287
1288   LOG_DEBUG("Initializing JACK");
1289   mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
1290   if (mixer_ptr->jack_client == NULL)
1291   {
1292     LOG_ERROR("Cannot create JACK client.");
1293     LOG_NOTICE("Please make sure JACK daemon is running.");
1294     goto exit_destroy_mutex;
1295   }
1296
1297   LOG_DEBUG("JACK client created");
1298
1299   LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
1300
1301
1302 #if defined(HAVE_JACK_MIDI)
1303   mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1304   if (mixer_ptr->port_midi_in == NULL)
1305   {
1306     LOG_ERROR("Cannot create JACK MIDI in port");
1307     goto close_jack;
1308   }
1309
1310   mixer_ptr->port_midi_out = jack_port_register(mixer_ptr->jack_client, "midi out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
1311   if (mixer_ptr->port_midi_out == NULL)
1312   {
1313     LOG_ERROR("Cannot create JACK MIDI out port");
1314     goto close_jack;
1315   }
1316
1317 #endif
1318
1319   ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1320   if (ret != 0)
1321   {
1322     LOG_ERROR("Cannot set JACK process callback");
1323     goto close_jack;
1324   }
1325
1326   ret = jack_activate(mixer_ptr->jack_client);
1327   if (ret != 0)
1328   {
1329     LOG_ERROR("Cannot activate JACK client");
1330     goto close_jack;
1331   }
1332
1333   return mixer_ptr;
1334
1335 close_jack:
1336   jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1337
1338 exit_destroy_mutex:
1339   pthread_mutex_destroy(&mixer_ptr->mutex);
1340
1341 exit_free:
1342   free(mixer_ptr);
1343
1344 exit:
1345   return NULL;
1346 }
1347
1348 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1349
1350 void
1351 destroy(
1352   jack_mixer_t mixer)
1353 {
1354   LOG_DEBUG("Uninitializing JACK");
1355
1356   assert(mixer_ctx_ptr->jack_client != NULL);
1357
1358   jack_client_close(mixer_ctx_ptr->jack_client);
1359
1360   pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1361
1362   free(mixer_ctx_ptr);
1363 }
1364
1365
1366 unsigned int
1367 get_channels_count(
1368   jack_mixer_t mixer)
1369 {
1370   return g_slist_length(mixer_ctx_ptr->input_channels_list);
1371 }
1372
1373 const char*
1374 get_client_name(
1375   jack_mixer_t mixer)
1376 {
1377   return jack_get_client_name(mixer_ctx_ptr->jack_client);
1378 }
1379
1380 int
1381 get_last_midi_channel(
1382   jack_mixer_t mixer)
1383 {
1384   return mixer_ctx_ptr->last_midi_channel;
1385 }
1386
1387 unsigned int
1388 set_last_midi_channel(
1389   jack_mixer_t mixer,
1390   int new_channel) {
1391   mixer_ctx_ptr->last_midi_channel = new_channel;
1392   return 0;
1393 }
1394
1395 int
1396 get_midi_behavior_mode(
1397   jack_mixer_t mixer)
1398 {
1399   return mixer_ctx_ptr->midi_behavior;
1400 }
1401
1402 unsigned int
1403 set_midi_behavior_mode(
1404   jack_mixer_t mixer,
1405   enum midi_behavior_mode mode)
1406 {
1407   mixer_ctx_ptr->midi_behavior = mode;
1408   return 0;
1409 }
1410
1411 jack_mixer_channel_t
1412 add_channel(
1413   jack_mixer_t mixer,
1414   const char * channel_name,
1415   bool stereo)
1416 {
1417   struct channel * channel_ptr;
1418   char * port_name = NULL;
1419   size_t channel_name_size;
1420
1421   channel_ptr = malloc(sizeof(struct channel));
1422   if (channel_ptr == NULL)
1423   {
1424     goto fail;
1425   }
1426
1427   channel_ptr->mixer_ptr = mixer_ctx_ptr;
1428
1429   channel_ptr->name = strdup(channel_name);
1430   if (channel_ptr->name == NULL)
1431   {
1432     goto fail_free_channel;
1433   }
1434
1435   channel_name_size = strlen(channel_name);
1436
1437   if (stereo)
1438   {
1439     port_name = malloc(channel_name_size + 3);
1440     if (port_name == NULL)
1441     {
1442         goto fail_free_channel_name;
1443     }
1444
1445     memcpy(port_name, channel_name, channel_name_size);
1446     port_name[channel_name_size] = ' ';
1447     port_name[channel_name_size+1] = 'L';
1448     port_name[channel_name_size+2] = 0;
1449
1450     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1451     if (channel_ptr->port_left == NULL)
1452     {
1453         goto fail_free_port_name;
1454     }
1455
1456     port_name[channel_name_size+1] = 'R';
1457
1458     channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1459     if (channel_ptr->port_right == NULL)
1460     {
1461         goto fail_unregister_left_channel;
1462     }
1463   }
1464   else
1465   {
1466     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1467     if (channel_ptr->port_left == NULL)
1468     {
1469         goto fail_free_channel_name;
1470     }
1471   }
1472
1473   channel_ptr->stereo = stereo;
1474
1475   channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1476   channel_ptr->num_volume_transition_steps =
1477     channel_ptr->volume_transition_seconds *
1478     jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client) + 1;
1479   channel_ptr->volume = 0.0;
1480   channel_ptr->volume_new = 0.0;
1481   channel_ptr->balance = 0.0;
1482   channel_ptr->balance_new = 0.0;
1483   channel_ptr->meter_left = -1.0;
1484   channel_ptr->meter_right = -1.0;
1485   channel_ptr->abspeak = 0.0;
1486   channel_ptr->out_mute = false;
1487
1488   channel_ptr->peak_left = 0.0;
1489   channel_ptr->peak_right = 0.0;
1490   channel_ptr->peak_frames = 0;
1491
1492   channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1493   channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1494   channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1495   channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1496
1497   channel_ptr->NaN_detected = false;
1498
1499   channel_ptr->midi_cc_volume_index = -1;
1500   channel_ptr->midi_cc_balance_index = -1;
1501   channel_ptr->midi_cc_mute_index = -1;
1502   channel_ptr->midi_cc_solo_index = -1;
1503   channel_ptr->midi_cc_volume_picked_up = false;
1504   channel_ptr->midi_cc_balance_picked_up = false;
1505
1506   channel_ptr->midi_change_callback = NULL;
1507   channel_ptr->midi_change_callback_data = NULL;
1508   channel_ptr->midi_out_has_events = false;
1509
1510   channel_ptr->midi_scale = NULL;
1511
1512   channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1513                   channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1514
1515   free(port_name);
1516   return channel_ptr;
1517
1518 fail_unregister_left_channel:
1519   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1520
1521 fail_free_port_name:
1522   free(port_name);
1523
1524 fail_free_channel_name:
1525   free(channel_ptr->name);
1526
1527 fail_free_channel:
1528   free(channel_ptr);
1529   channel_ptr = NULL;
1530
1531 fail:
1532   return NULL;
1533 }
1534
1535 static jack_mixer_output_channel_t
1536 create_output_channel(
1537   jack_mixer_t mixer,
1538   const char * channel_name,
1539   bool stereo,
1540   bool system)
1541 {
1542   struct channel * channel_ptr;
1543   struct output_channel * output_channel_ptr;
1544   char * port_name = NULL;
1545   size_t channel_name_size;
1546
1547   output_channel_ptr = malloc(sizeof(struct output_channel));
1548   channel_ptr = (struct channel*)output_channel_ptr;
1549   if (channel_ptr == NULL)
1550   {
1551     goto fail;
1552   }
1553
1554   channel_ptr->mixer_ptr = mixer_ctx_ptr;
1555
1556   channel_ptr->name = strdup(channel_name);
1557   if (channel_ptr->name == NULL)
1558   {
1559     goto fail_free_channel;
1560   }
1561
1562   if (stereo)
1563   {
1564     channel_name_size = strlen(channel_name);
1565
1566     port_name = malloc(channel_name_size + 4);
1567     if (port_name == NULL)
1568     {
1569         goto fail_free_channel_name;
1570     }
1571
1572     memcpy(port_name, channel_name, channel_name_size);
1573     port_name[channel_name_size] = ' ';
1574     port_name[channel_name_size+1] = 'L';
1575     port_name[channel_name_size+2] = 0;
1576
1577     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1578     if (channel_ptr->port_left == NULL)
1579     {
1580         goto fail_free_port_name;
1581     }
1582
1583     port_name[channel_name_size+1] = 'R';
1584
1585     channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1586     if (channel_ptr->port_right == NULL)
1587     {
1588         goto fail_unregister_left_channel;
1589     }
1590   }
1591   else
1592   {
1593     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1594     if (channel_ptr->port_left == NULL)
1595     {
1596         goto fail_free_channel_name;
1597     }
1598   }
1599
1600   channel_ptr->stereo = stereo;
1601   channel_ptr->out_mute = false;
1602
1603   channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1604   channel_ptr->num_volume_transition_steps =
1605     channel_ptr->volume_transition_seconds *
1606     jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client) + 1;
1607   channel_ptr->volume = 0.0;
1608   channel_ptr->volume_new = 0.0;
1609   channel_ptr->balance = 0.0;
1610   channel_ptr->balance_new = 0.0;
1611   channel_ptr->meter_left = -1.0;
1612   channel_ptr->meter_right = -1.0;
1613   channel_ptr->abspeak = 0.0;
1614
1615   channel_ptr->peak_left = 0.0;
1616   channel_ptr->peak_right = 0.0;
1617   channel_ptr->peak_frames = 0;
1618
1619   channel_ptr->tmp_mixed_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1620   channel_ptr->tmp_mixed_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1621   channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1622   channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1623   channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1624   channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1625
1626   channel_ptr->NaN_detected = false;
1627
1628   channel_ptr->midi_cc_volume_index = -1;
1629   channel_ptr->midi_cc_balance_index = -1;
1630   channel_ptr->midi_cc_mute_index = -1;
1631   channel_ptr->midi_cc_solo_index = -1;
1632   channel_ptr->midi_cc_volume_picked_up = false;
1633   channel_ptr->midi_cc_balance_picked_up = false;
1634
1635   channel_ptr->midi_change_callback = NULL;
1636   channel_ptr->midi_change_callback_data = NULL;
1637
1638   channel_ptr->midi_scale = NULL;
1639
1640   output_channel_ptr->soloed_channels = NULL;
1641   output_channel_ptr->muted_channels = NULL;
1642   output_channel_ptr->prefader_channels = NULL;
1643   output_channel_ptr->system = system;
1644   output_channel_ptr->prefader = false;
1645
1646   free(port_name);
1647   return output_channel_ptr;
1648
1649 fail_unregister_left_channel:
1650   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1651
1652 fail_free_port_name:
1653   free(port_name);
1654
1655 fail_free_channel_name:
1656   free(channel_ptr->name);
1657
1658 fail_free_channel:
1659   free(channel_ptr);
1660   channel_ptr = NULL;
1661
1662 fail:
1663   return NULL;
1664 }
1665
1666 jack_mixer_output_channel_t
1667 add_output_channel(
1668   jack_mixer_t mixer,
1669   const char * channel_name,
1670   bool stereo,
1671   bool system)
1672 {
1673   struct output_channel *output_channel_ptr;
1674   struct channel *channel_ptr;
1675
1676   output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1677   if (output_channel_ptr == NULL) {
1678     return NULL;
1679   }
1680   channel_ptr = (struct channel*)output_channel_ptr;
1681
1682   ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1683                   ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1684
1685   return output_channel_ptr;
1686 }
1687
1688 void
1689 remove_channels(
1690   jack_mixer_t mixer)
1691 {
1692   GSList *list_ptr;
1693   for (list_ptr = mixer_ctx_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
1694   {
1695     struct channel *input_channel_ptr = list_ptr->data;
1696     remove_channel((jack_mixer_channel_t)input_channel_ptr);
1697   }
1698 }
1699
1700 void
1701 remove_output_channel(
1702   jack_mixer_output_channel_t output_channel)
1703 {
1704   struct output_channel *output_channel_ptr = output_channel;
1705   struct channel *channel_ptr = output_channel;
1706
1707   channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1708                   channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1709   free(channel_ptr->name);
1710
1711   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1712   if (channel_ptr->stereo)
1713   {
1714     jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1715   }
1716
1717   if (channel_ptr->midi_cc_volume_index != -1)
1718   {
1719     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1720     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1721   }
1722
1723   if (channel_ptr->midi_cc_balance_index != -1)
1724   {
1725     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1726     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1727   }
1728
1729   if (channel_ptr->midi_cc_mute_index != -1)
1730   {
1731     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
1732     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
1733   }
1734
1735   if (channel_ptr->midi_cc_solo_index != -1)
1736   {
1737     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
1738     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
1739   }
1740
1741   g_slist_free(output_channel_ptr->soloed_channels);
1742   g_slist_free(output_channel_ptr->muted_channels);
1743   g_slist_free(output_channel_ptr->prefader_channels);
1744
1745   free(channel_ptr->tmp_mixed_frames_left);
1746   free(channel_ptr->tmp_mixed_frames_right);
1747   free(channel_ptr->frames_left);
1748   free(channel_ptr->frames_right);
1749   free(channel_ptr->prefader_frames_left);
1750   free(channel_ptr->prefader_frames_right);
1751
1752   free(channel_ptr);
1753 }
1754
1755 void
1756 output_channel_set_solo(
1757   jack_mixer_output_channel_t output_channel,
1758   jack_mixer_channel_t channel,
1759   bool solo_value)
1760 {
1761   struct output_channel *output_channel_ptr = output_channel;
1762
1763   if (solo_value) {
1764     if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1765       return;
1766     output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1767   } else {
1768     if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1769       return;
1770     output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1771   }
1772 }
1773
1774 void
1775 output_channel_set_muted(
1776   jack_mixer_output_channel_t output_channel,
1777   jack_mixer_channel_t channel,
1778   bool muted_value)
1779 {
1780   struct output_channel *output_channel_ptr = output_channel;
1781
1782   if (muted_value) {
1783     if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1784       return;
1785     output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1786   } else {
1787     if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1788       return;
1789     output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1790   }
1791 }
1792
1793 bool
1794 output_channel_is_muted(
1795   jack_mixer_output_channel_t output_channel,
1796   jack_mixer_channel_t channel)
1797 {
1798   struct output_channel *output_channel_ptr = output_channel;
1799
1800   if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1801     return true;
1802   return false;
1803 }
1804
1805 bool
1806 output_channel_is_solo(
1807   jack_mixer_output_channel_t output_channel,
1808   jack_mixer_channel_t channel)
1809 {
1810   struct output_channel *output_channel_ptr = output_channel;
1811
1812   if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1813     return true;
1814   return false;
1815 }
1816
1817 void
1818 output_channel_set_prefader(
1819   jack_mixer_output_channel_t output_channel,
1820   bool pfl_value)
1821 {
1822   struct output_channel *output_channel_ptr = output_channel;
1823   output_channel_ptr->prefader = pfl_value;
1824 }
1825
1826 bool
1827 output_channel_is_prefader(
1828   jack_mixer_output_channel_t output_channel)
1829 {
1830   struct output_channel *output_channel_ptr = output_channel;
1831   return output_channel_ptr->prefader;
1832 }
1833
1834 void
1835 output_channel_set_in_prefader(
1836   jack_mixer_output_channel_t output_channel,
1837   jack_mixer_channel_t channel,
1838   bool prefader_value)
1839 {
1840   struct output_channel *output_channel_ptr = output_channel;
1841
1842   if (prefader_value) {
1843     if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
1844       return;
1845     output_channel_ptr->prefader_channels = g_slist_prepend(output_channel_ptr->prefader_channels, channel);
1846   } else {
1847     if (g_slist_find(output_channel_ptr->prefader_channels, channel) == NULL)
1848       return;
1849     output_channel_ptr->prefader_channels = g_slist_remove(output_channel_ptr->prefader_channels, channel);
1850   }
1851 }
1852
1853 bool
1854 output_channel_is_in_prefader(
1855   jack_mixer_output_channel_t output_channel,
1856   jack_mixer_channel_t channel)
1857 {
1858   struct output_channel *output_channel_ptr = output_channel;
1859
1860   if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
1861     return true;
1862   return false;
1863 }