]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.c
Serialize prefader button states
[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 void
522 channel_mono_meter_read(
523   jack_mixer_channel_t channel,
524   double * mono_ptr)
525 {
526   *mono_ptr = value_to_db(channel_ptr->meter_left);
527 }
528
529 void
530 channel_volume_write(
531   jack_mixer_channel_t channel,
532   double volume)
533 {
534   assert(channel_ptr);
535   /*If changing volume and find we're in the middle of a previous transition,
536    *then set current volume to place in transition to avoid a jump.*/
537   if (channel_ptr->volume_new != channel_ptr->volume) {
538     channel_ptr->volume = channel_ptr->volume + channel_ptr->volume_idx *
539      (channel_ptr->volume_new - channel_ptr->volume) /
540      channel_ptr->num_volume_transition_steps;
541   }
542   channel_ptr->volume_idx = 0;
543   channel_ptr->volume_new = db_to_value(volume);
544   channel_ptr->midi_out_has_events = true;
545 }
546
547 double
548 channel_volume_read(
549   jack_mixer_channel_t channel)
550 {
551   assert(channel_ptr);
552   return value_to_db(channel_ptr->volume_new);
553 }
554
555 void
556 channels_volumes_read(jack_mixer_t mixer_ptr)
557 {
558     GSList *node_ptr;
559     struct channel *pChannel;
560     struct jack_mixer * pMixer = (struct jack_mixer *)mixer_ptr;
561
562     for (node_ptr = pMixer->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
563     {
564         pChannel = (struct channel *)node_ptr->data;
565         double vol = channel_volume_read( (jack_mixer_channel_t)pChannel);
566         printf("%s : volume is %f dbFS for mixer channel: %s\n", jack_get_client_name(pMixer->jack_client), vol, pChannel->name);
567     }
568 }
569
570 void
571 channel_balance_write(
572   jack_mixer_channel_t channel,
573   double balance)
574 {
575   assert(channel_ptr);
576   if (channel_ptr->balance != channel_ptr->balance_new) {
577     channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
578       (channel_ptr->balance_new - channel_ptr->balance) /
579       channel_ptr->num_volume_transition_steps;
580   }
581   channel_ptr->balance_idx = 0;
582   channel_ptr->balance_new = balance;
583 }
584
585 double
586 channel_balance_read(
587   jack_mixer_channel_t channel)
588 {
589   assert(channel_ptr);
590   return channel_ptr->balance_new;
591 }
592
593 double
594 channel_abspeak_read(
595   jack_mixer_channel_t channel)
596 {
597   assert(channel_ptr);
598   if (channel_ptr->NaN_detected)
599   {
600     return sqrt(-1);
601   }
602   else
603   {
604     return value_to_db(channel_ptr->abspeak);
605   }
606 }
607
608 void
609 channel_abspeak_reset(
610   jack_mixer_channel_t channel)
611 {
612   channel_ptr->abspeak = 0;
613   channel_ptr->NaN_detected = false;
614 }
615
616 void
617 channel_out_mute(
618   jack_mixer_channel_t channel)
619 {
620   channel_ptr->out_mute = true;
621 }
622
623 void
624 channel_out_unmute(
625   jack_mixer_channel_t channel)
626 {
627   channel_ptr->out_mute = false;
628 }
629
630 bool
631 channel_is_out_muted(
632   jack_mixer_channel_t channel)
633 {
634   return channel_ptr->out_mute;
635 }
636
637 void
638 channel_solo(
639   jack_mixer_channel_t channel)
640 {
641   if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) != NULL)
642     return;
643   channel_ptr->mixer_ptr->soloed_channels = g_slist_prepend(channel_ptr->mixer_ptr->soloed_channels, channel);
644 }
645
646 void
647 channel_unsolo(
648   jack_mixer_channel_t channel)
649 {
650   if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel) == NULL)
651     return;
652   channel_ptr->mixer_ptr->soloed_channels = g_slist_remove(channel_ptr->mixer_ptr->soloed_channels, channel);
653 }
654
655 bool
656 channel_is_soloed(
657   jack_mixer_channel_t channel)
658 {
659   if (g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel))
660     return true;
661   return false;
662 }
663
664 void
665 channel_set_midi_scale(
666   jack_mixer_channel_t channel,
667   jack_mixer_scale_t scale)
668 {
669   channel_ptr->midi_scale = scale;
670 }
671
672 void
673 channel_set_midi_change_callback(
674   jack_mixer_channel_t channel,
675   void (*midi_change_callback) (void*),
676   void *user_data)
677 {
678   channel_ptr->midi_change_callback = midi_change_callback;
679   channel_ptr->midi_change_callback_data = user_data;
680 }
681
682 bool
683 channel_get_midi_in_got_events(
684   jack_mixer_channel_t channel)
685 {
686   bool t = channel_ptr->midi_in_got_events;
687   channel_ptr->midi_in_got_events = false;
688   return t;
689 }
690
691 #undef channel_ptr
692
693 /* process input channels and mix them into main mix */
694 static inline void
695 mix_one(
696   struct output_channel *output_mix_channel,
697   GSList *channels_list,
698   jack_nframes_t start,         /* index of first sample to process */
699   jack_nframes_t end)           /* index of sample to stop processing before */
700 {
701   jack_nframes_t i;
702   GSList *node_ptr;
703   struct channel * channel_ptr;
704   jack_default_audio_sample_t frame_left;
705   jack_default_audio_sample_t frame_right;
706   struct channel *mix_channel = (struct channel*)output_mix_channel;
707
708   for (i = start; i < end; i++)
709   {
710     mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i] = 0.0;
711     if (mix_channel->stereo)
712       mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i] = 0.0;
713   }
714
715   for (node_ptr = channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
716   {
717     channel_ptr = node_ptr->data;
718
719     if (g_slist_find(output_mix_channel->muted_channels, channel_ptr) != NULL || channel_ptr->out_mute) {
720       /* skip muted channels */
721       continue;
722     }
723
724     if ((!channel_ptr->mixer_ptr->soloed_channels && !output_mix_channel->soloed_channels) ||
725         (channel_ptr->mixer_ptr->soloed_channels &&
726          g_slist_find(channel_ptr->mixer_ptr->soloed_channels, channel_ptr) != NULL) ||
727         (output_mix_channel->soloed_channels &&
728         g_slist_find(output_mix_channel->soloed_channels, channel_ptr) != NULL)) {
729
730       for (i = start ; i < end ; i++)
731       {
732         if (! output_mix_channel->prefader &&
733          g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL) {
734           frame_left = channel_ptr->frames_left[i-start];
735         } else {
736           frame_left = channel_ptr->prefader_frames_left[i-start];
737         }
738         if (frame_left == NAN)
739           break;
740         mix_channel->tmp_mixed_frames_left[i] += frame_left;
741         if (mix_channel->stereo)
742         {
743           if (! output_mix_channel->prefader &&
744               g_slist_find(output_mix_channel->prefader_channels, channel_ptr) == NULL) {
745             frame_right = channel_ptr->frames_right[i-start];
746           } else {
747             frame_right = channel_ptr->prefader_frames_right[i-start];
748           }
749           if (frame_right == NAN)
750             break;
751           mix_channel->tmp_mixed_frames_right[i] += frame_right;
752         }
753       }
754     }
755   }
756
757   /* process main mix channel */
758   unsigned int steps = mix_channel->num_volume_transition_steps;
759   for (i = start ; i < end ; i++)
760   {
761     if (! output_mix_channel->prefader) {
762       float volume = mix_channel->volume;
763       float volume_new = mix_channel->volume_new;
764       float vol = volume;
765       float balance = mix_channel->balance;
766       float balance_new = mix_channel->balance_new;
767       float bal = balance;
768       if (volume != volume_new) {
769         vol = mix_channel->volume_idx * (volume_new - volume) / steps + volume;
770       }
771       if (balance != balance_new) {
772         bal = mix_channel->balance_idx * (balance_new - balance) / steps + balance;
773       }
774
775       float vol_l;
776       float vol_r;
777       if (mix_channel->stereo) {
778         if (bal > 0) {
779           vol_l = vol * (1 - bal);
780           vol_r = vol;
781         } else {
782           vol_l = vol;
783           vol_r = vol * (1 + bal);
784         }
785       } else {
786         vol_l = vol * (1 - bal);
787         vol_r = vol * (1 + bal);
788       }
789       mix_channel->tmp_mixed_frames_left[i] *= vol_l;
790       mix_channel->tmp_mixed_frames_right[i] *= vol_r;
791     }
792
793     frame_left = fabsf(mix_channel->tmp_mixed_frames_left[i]);
794     if (mix_channel->peak_left < frame_left)
795     {
796       mix_channel->peak_left = frame_left;
797
798       if (frame_left > mix_channel->abspeak)
799       {
800         mix_channel->abspeak = frame_left;
801       }
802     }
803
804     if (mix_channel->stereo)
805     {
806       frame_right = fabsf(mix_channel->tmp_mixed_frames_right[i]);
807       if (mix_channel->peak_right < frame_right)
808       {
809         mix_channel->peak_right = frame_right;
810
811         if (frame_right > mix_channel->abspeak)
812         {
813           mix_channel->abspeak = frame_right;
814         }
815       }
816     }
817
818     mix_channel->peak_frames++;
819     if (mix_channel->peak_frames >= PEAK_FRAMES_CHUNK)
820     {
821       mix_channel->meter_left = mix_channel->peak_left;
822       mix_channel->peak_left = 0.0;
823
824       if (mix_channel->stereo)
825       {
826         mix_channel->meter_right = mix_channel->peak_right;
827         mix_channel->peak_right = 0.0;
828       }
829
830       mix_channel->peak_frames = 0;
831     }
832     mix_channel->volume_idx++;
833     if ((mix_channel->volume != mix_channel->volume_new) && (mix_channel->volume_idx == steps)) {
834       mix_channel->volume = mix_channel->volume_new;
835       mix_channel->volume_idx = 0;
836     }
837     mix_channel->balance_idx++;
838     if ((mix_channel->balance != mix_channel->balance_new) && (mix_channel->balance_idx == steps)) {
839       mix_channel->balance = mix_channel->balance_new;
840       mix_channel->balance_idx = 0;
841     }
842
843     if (!mix_channel->out_mute) {
844         mix_channel->left_buffer_ptr[i] = mix_channel->tmp_mixed_frames_left[i];
845         if (mix_channel->stereo)
846           mix_channel->right_buffer_ptr[i] = mix_channel->tmp_mixed_frames_right[i];
847     }
848   }
849 }
850
851 static inline void
852 calc_channel_frames(
853   struct channel *channel_ptr,
854   jack_nframes_t start,
855   jack_nframes_t end)
856 {
857   jack_nframes_t i;
858   jack_default_audio_sample_t frame_left = 0.0f;
859   jack_default_audio_sample_t frame_right = 0.0f;
860   unsigned int steps = channel_ptr->num_volume_transition_steps;
861   for (i = start ; i < end ; i++)
862   {
863     if (i-start >= MAX_BLOCK_SIZE)
864     {
865       fprintf(stderr, "i-start too high: %d - %d\n", i, start);
866     }
867     channel_ptr->prefader_frames_left[i-start] = channel_ptr->left_buffer_ptr[i];
868     if (channel_ptr->stereo)
869       channel_ptr->prefader_frames_right[i-start] = channel_ptr->right_buffer_ptr[i];
870
871     if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
872     {
873       channel_ptr->NaN_detected = true;
874       channel_ptr->frames_left[i-start] = NAN;
875       break;
876     }
877     float volume = channel_ptr->volume;
878     float volume_new = channel_ptr->volume_new;
879     float vol = volume;
880     float balance = channel_ptr->balance;
881     float balance_new = channel_ptr->balance_new;
882     float bal = balance;
883     if (channel_ptr->volume != channel_ptr->volume_new) {
884       vol = channel_ptr->volume_idx * (volume_new - volume) / steps + volume;
885     }
886     if (channel_ptr->balance != channel_ptr->balance_new) {
887       bal = channel_ptr->balance_idx * (balance_new - balance) / steps + balance;
888     }
889     float vol_l;
890     float vol_r;
891     if (channel_ptr->stereo) {
892       if (bal > 0) {
893         vol_l = vol * (1 - bal);
894         vol_r = vol;
895       } else {
896         vol_l = vol;
897         vol_r = vol * (1 + bal);
898       }
899     } else {
900       vol_l = vol * (1 - bal);
901       vol_r = vol * (1 + bal);
902     }
903     frame_left = channel_ptr->left_buffer_ptr[i] * vol_l;
904     if (channel_ptr->stereo)
905     {
906       if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
907       {
908         channel_ptr->NaN_detected = true;
909         channel_ptr->frames_right[i-start] = NAN;
910         break;
911       }
912
913       frame_right = channel_ptr->right_buffer_ptr[i] * vol_r;
914     }
915
916     channel_ptr->frames_left[i-start] = frame_left;
917     channel_ptr->frames_right[i-start] = frame_right;
918
919     if (channel_ptr->stereo)
920     {
921       frame_left = fabsf(frame_left);
922       frame_right = fabsf(frame_right);
923
924       if (channel_ptr->peak_left < frame_left)
925       {
926         channel_ptr->peak_left = frame_left;
927
928         if (frame_left > channel_ptr->abspeak)
929         {
930           channel_ptr->abspeak = frame_left;
931         }
932       }
933
934       if (channel_ptr->peak_right < frame_right)
935       {
936         channel_ptr->peak_right = frame_right;
937
938         if (frame_right > channel_ptr->abspeak)
939         {
940           channel_ptr->abspeak = frame_right;
941         }
942       }
943     }
944     else
945     {
946       frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
947
948       if (channel_ptr->peak_left < frame_left)
949       {
950         channel_ptr->peak_left = frame_left;
951
952         if (frame_left > channel_ptr->abspeak)
953         {
954           channel_ptr->abspeak = frame_left;
955         }
956       }
957     }
958
959     channel_ptr->peak_frames++;
960     if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
961     {
962       channel_ptr->meter_left = channel_ptr->peak_left;
963       channel_ptr->peak_left = 0.0;
964
965       if (channel_ptr->stereo)
966       {
967         channel_ptr->meter_right = channel_ptr->peak_right;
968         channel_ptr->peak_right = 0.0;
969       }
970
971       channel_ptr->peak_frames = 0;
972     }
973     channel_ptr->volume_idx++;
974     if ((channel_ptr->volume != channel_ptr->volume_new) &&
975      (channel_ptr->volume_idx == steps)) {
976       channel_ptr->volume = channel_ptr->volume_new;
977       channel_ptr->volume_idx = 0;
978     }
979     channel_ptr->balance_idx++;
980     if ((channel_ptr->balance != channel_ptr->balance_new) &&
981      (channel_ptr->balance_idx >= steps)) {
982       channel_ptr->balance = channel_ptr->balance_new;
983       channel_ptr->balance_idx = 0;
984      }
985   }
986 }
987
988 static inline void
989 mix(
990   struct jack_mixer * mixer_ptr,
991   jack_nframes_t start,         /* index of first sample to process */
992   jack_nframes_t end)           /* index of sample to stop processing before */
993 {
994   GSList *node_ptr;
995   struct output_channel * output_channel_ptr;
996   struct channel *channel_ptr;
997
998   for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
999   {
1000     channel_ptr = (struct channel*)node_ptr->data;
1001     calc_channel_frames(channel_ptr, start, end);
1002   }
1003
1004   for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1005   {
1006     output_channel_ptr = node_ptr->data;
1007     channel_ptr = (struct channel*)output_channel_ptr;
1008
1009     if (output_channel_ptr->system)
1010     {
1011       /* Don't bother mixing the channels if we are not connected */
1012       if (channel_ptr->stereo)
1013       {
1014         if (jack_port_connected(channel_ptr->port_left) == 0 &&
1015             jack_port_connected(channel_ptr->port_right) == 0)
1016           continue;
1017       } else {
1018          if (jack_port_connected(channel_ptr->port_left) == 0)
1019            continue;
1020       }
1021     }
1022
1023     mix_one(output_channel_ptr, mixer_ptr->input_channels_list, start, end);
1024   }
1025 }
1026
1027 static inline void
1028 update_channel_buffers(
1029   struct channel * channel_ptr,
1030   jack_nframes_t nframes)
1031 {
1032   channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
1033
1034   if (channel_ptr->stereo)
1035   {
1036     channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
1037   }
1038 }
1039
1040 #define mixer_ptr ((struct jack_mixer *)context)
1041
1042 static int
1043 process(
1044   jack_nframes_t nframes,
1045   void * context)
1046 {
1047   jack_nframes_t i;
1048   GSList *node_ptr;
1049   struct channel * channel_ptr;
1050 #if defined(HAVE_JACK_MIDI)
1051   jack_nframes_t event_count;
1052   jack_midi_event_t in_event;
1053   unsigned char* midi_out_buffer;
1054   void * midi_buffer;
1055   signed char byte;
1056   unsigned int cc_channel_index;
1057 #endif
1058
1059   for (node_ptr = mixer_ptr->input_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1060   {
1061     channel_ptr = node_ptr->data;
1062     update_channel_buffers(channel_ptr, nframes);
1063   }
1064
1065   // Fill output buffers with the input
1066   for (node_ptr = mixer_ptr->output_channels_list; node_ptr; node_ptr = g_slist_next(node_ptr))
1067   {
1068     channel_ptr = node_ptr->data;
1069     update_channel_buffers(channel_ptr, nframes);
1070   }
1071
1072 #if defined(HAVE_JACK_MIDI)
1073   midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
1074   event_count = jack_midi_get_event_count(midi_buffer);
1075
1076   for (i = 0 ; i < event_count; i++)
1077   {
1078     jack_midi_event_get(&in_event, midi_buffer, i);
1079
1080     if (in_event.size != 3 ||
1081         (in_event.buffer[0] & 0xF0) != 0xB0 ||
1082         in_event.buffer[1] > 127 ||
1083         in_event.buffer[2] > 127)
1084     {
1085       continue;
1086     }
1087
1088     assert(in_event.time < nframes);
1089
1090     LOG_DEBUG(
1091       "%u: CC#%u -> %u",
1092       (unsigned int)(in_event.buffer[0]),
1093       (unsigned int)in_event.buffer[1],
1094       (unsigned int)in_event.buffer[2]);
1095
1096     mixer_ptr->last_midi_channel = (int)in_event.buffer[1];
1097     channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]];
1098
1099     /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
1100     if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
1101     {
1102       if (channel_ptr->midi_cc_balance_index == (char)in_event.buffer[1])
1103       {
1104         byte = in_event.buffer[2];
1105         if (byte == 0)
1106         {
1107           byte = 1;
1108         }
1109         byte -= 64;
1110         int current_midi = (int)63 * channel_ptr->balance + 64;
1111         current_midi = current_midi == 1 ? 0 : current_midi;
1112         if (mixer_ptr->midi_behavior == Pick_Up &&
1113          !channel_ptr->midi_cc_balance_picked_up) {
1114           if (in_event.buffer[2] == current_midi) {
1115             channel_set_midi_cc_balance_picked_up(channel_ptr, true);
1116           }
1117         }
1118         if ((mixer_ptr->midi_behavior == Pick_Up &&
1119          channel_ptr->midi_cc_balance_picked_up) ||
1120          mixer_ptr->midi_behavior == Jump_To_Value) {
1121           if (channel_ptr->balance != channel_ptr->balance_new) {
1122             channel_ptr->balance = channel_ptr->balance + channel_ptr->balance_idx *
1123              (channel_ptr->balance_new - channel_ptr->balance) /
1124             channel_ptr->num_volume_transition_steps;
1125           }
1126           channel_ptr->balance_idx = 0;
1127           channel_ptr->balance_new = (float)byte / 63;
1128           LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance_new);
1129         }
1130       }
1131       else if (channel_ptr->midi_cc_volume_index == in_event.buffer[1])
1132       {
1133           int current_midi =  (int)(127 *
1134            scale_db_to_scale(channel_ptr->midi_scale,
1135            value_to_db(channel_ptr->volume)));
1136           if (mixer_ptr->midi_behavior == Pick_Up &&
1137            !channel_ptr->midi_cc_volume_picked_up ) {
1138           if (in_event.buffer[2] == current_midi) {
1139             channel_set_midi_cc_volume_picked_up(channel_ptr, true);
1140           }
1141         }
1142         if ((mixer_ptr->midi_behavior == Pick_Up &&
1143          channel_ptr->midi_cc_volume_picked_up) ||
1144          mixer_ptr->midi_behavior == Jump_To_Value) {
1145             if (channel_ptr->volume_new != channel_ptr->volume) {
1146               channel_ptr->volume = channel_ptr->volume + channel_ptr->volume_idx *
1147                (channel_ptr->volume_new - channel_ptr->volume) /
1148                channel_ptr->num_volume_transition_steps;
1149             }
1150             channel_ptr->volume_idx = 0;
1151             channel_ptr->volume_new = db_to_value(scale_scale_to_db(channel_ptr->midi_scale,
1152              (double)in_event.buffer[2] / 127));
1153             LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume_new);
1154         }
1155       }
1156       else if (channel_ptr->midi_cc_mute_index == in_event.buffer[1])
1157       {
1158         if ((unsigned int)in_event.buffer[2] == 127) {
1159           channel_ptr->out_mute = !channel_ptr->out_mute;
1160         }
1161         LOG_DEBUG("\"%s\" out_mute %d", channel_ptr->name, channel_ptr->out_mute);
1162       }
1163       else if (channel_ptr->midi_cc_solo_index == in_event.buffer[1])
1164       {
1165         if ((unsigned int)in_event.buffer[2] == 127) {
1166           if (channel_is_soloed(channel_ptr)) {
1167             channel_unsolo(channel_ptr);
1168           } else {
1169             channel_solo(channel_ptr);
1170           }
1171         }
1172         LOG_DEBUG("\"%s\" solo %d", channel_ptr->name, channel_is_soloed(channel_ptr));
1173       }
1174       channel_ptr->midi_in_got_events = true;
1175       if (channel_ptr->midi_change_callback)
1176         channel_ptr->midi_change_callback(channel_ptr->midi_change_callback_data);
1177
1178     }
1179
1180   }
1181
1182   midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_out, nframes);
1183   jack_midi_clear_buffer(midi_buffer);
1184
1185   for(i=0; i<nframes; i++)
1186   {
1187     for (cc_channel_index=0; cc_channel_index<128; cc_channel_index++)
1188     {
1189       channel_ptr = mixer_ptr->midi_cc_map[cc_channel_index];
1190       if (channel_ptr == NULL || channel_ptr->midi_scale == NULL)
1191       {
1192         continue;
1193       }
1194       if (channel_ptr->midi_out_has_events == false)
1195       {
1196         continue;
1197       }
1198       if (channel_ptr->midi_cc_balance_index == (int)cc_channel_index)
1199       {
1200         continue;
1201       }
1202       midi_out_buffer = jack_midi_event_reserve(midi_buffer, i, 3);
1203       if (midi_out_buffer == NULL)
1204       {
1205         continue;
1206       }
1207       midi_out_buffer[0] = 0xB0; /* control change */
1208       midi_out_buffer[1] = cc_channel_index;
1209       midi_out_buffer[2] = (unsigned char)(127*scale_db_to_scale(channel_ptr->midi_scale, value_to_db(channel_ptr->volume_new)));
1210
1211       LOG_DEBUG(
1212         "%u: CC#%u <- %u",
1213         (unsigned int)(midi_out_buffer[0]),
1214         (unsigned int)midi_out_buffer[1],
1215         (unsigned int)midi_out_buffer[2]);
1216
1217       channel_ptr->midi_out_has_events = false;
1218     }
1219   }
1220
1221 #endif
1222
1223   mix(mixer_ptr, 0, nframes);
1224
1225   return 0;
1226 }
1227
1228 #undef mixer_ptr
1229
1230 jack_mixer_t
1231 create(
1232   const char * jack_client_name_ptr,
1233   bool stereo)
1234 {
1235   (void) stereo;
1236   int ret;
1237   struct jack_mixer * mixer_ptr;
1238   int i;
1239
1240
1241   mixer_ptr = malloc(sizeof(struct jack_mixer));
1242   if (mixer_ptr == NULL)
1243   {
1244     goto exit;
1245   }
1246
1247   ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
1248   if (ret != 0)
1249   {
1250     goto exit_free;
1251   }
1252
1253   mixer_ptr->input_channels_list = NULL;
1254   mixer_ptr->output_channels_list = NULL;
1255
1256   mixer_ptr->soloed_channels = NULL;
1257
1258   mixer_ptr->last_midi_channel = -1;
1259
1260   mixer_ptr->midi_behavior = Jump_To_Value;
1261
1262   for (i = 0 ; i < 128 ; i++)
1263   {
1264     mixer_ptr->midi_cc_map[i] = NULL;
1265   }
1266
1267   LOG_DEBUG("Initializing JACK");
1268   mixer_ptr->jack_client = jack_client_open(jack_client_name_ptr, 0, NULL);
1269   if (mixer_ptr->jack_client == NULL)
1270   {
1271     LOG_ERROR("Cannot create JACK client.");
1272     LOG_NOTICE("Please make sure JACK daemon is running.");
1273     goto exit_destroy_mutex;
1274   }
1275
1276   LOG_DEBUG("JACK client created");
1277
1278   LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
1279
1280
1281 #if defined(HAVE_JACK_MIDI)
1282   mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
1283   if (mixer_ptr->port_midi_in == NULL)
1284   {
1285     LOG_ERROR("Cannot create JACK MIDI in port");
1286     goto close_jack;
1287   }
1288
1289   mixer_ptr->port_midi_out = jack_port_register(mixer_ptr->jack_client, "midi out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
1290   if (mixer_ptr->port_midi_out == NULL)
1291   {
1292     LOG_ERROR("Cannot create JACK MIDI out port");
1293     goto close_jack;
1294   }
1295
1296 #endif
1297
1298   ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
1299   if (ret != 0)
1300   {
1301     LOG_ERROR("Cannot set JACK process callback");
1302     goto close_jack;
1303   }
1304
1305   ret = jack_activate(mixer_ptr->jack_client);
1306   if (ret != 0)
1307   {
1308     LOG_ERROR("Cannot activate JACK client");
1309     goto close_jack;
1310   }
1311
1312   return mixer_ptr;
1313
1314 close_jack:
1315   jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
1316
1317 exit_destroy_mutex:
1318   pthread_mutex_destroy(&mixer_ptr->mutex);
1319
1320 exit_free:
1321   free(mixer_ptr);
1322
1323 exit:
1324   return NULL;
1325 }
1326
1327 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
1328
1329 void
1330 destroy(
1331   jack_mixer_t mixer)
1332 {
1333   LOG_DEBUG("Uninitializing JACK");
1334
1335   assert(mixer_ctx_ptr->jack_client != NULL);
1336
1337   jack_client_close(mixer_ctx_ptr->jack_client);
1338
1339   pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
1340
1341   free(mixer_ctx_ptr);
1342 }
1343
1344
1345 unsigned int
1346 get_channels_count(
1347   jack_mixer_t mixer)
1348 {
1349   return g_slist_length(mixer_ctx_ptr->input_channels_list);
1350 }
1351
1352 const char*
1353 get_client_name(
1354   jack_mixer_t mixer)
1355 {
1356   return jack_get_client_name(mixer_ctx_ptr->jack_client);
1357 }
1358
1359 int
1360 get_last_midi_channel(
1361   jack_mixer_t mixer)
1362 {
1363   return mixer_ctx_ptr->last_midi_channel;
1364 }
1365
1366 unsigned int
1367 set_last_midi_channel(
1368   jack_mixer_t mixer,
1369   int new_channel) {
1370   mixer_ctx_ptr->last_midi_channel = new_channel;
1371   return 0;
1372 }
1373
1374 int
1375 get_midi_behavior_mode(
1376   jack_mixer_t mixer)
1377 {
1378   return mixer_ctx_ptr->midi_behavior;
1379 }
1380
1381 unsigned int
1382 set_midi_behavior_mode(
1383   jack_mixer_t mixer,
1384   enum midi_behavior_mode mode)
1385 {
1386   mixer_ctx_ptr->midi_behavior = mode;
1387   return 0;
1388 }
1389
1390 jack_mixer_channel_t
1391 add_channel(
1392   jack_mixer_t mixer,
1393   const char * channel_name,
1394   bool stereo)
1395 {
1396   struct channel * channel_ptr;
1397   char * port_name = NULL;
1398   size_t channel_name_size;
1399
1400   channel_ptr = malloc(sizeof(struct channel));
1401   if (channel_ptr == NULL)
1402   {
1403     goto fail;
1404   }
1405
1406   channel_ptr->mixer_ptr = mixer_ctx_ptr;
1407
1408   channel_ptr->name = strdup(channel_name);
1409   if (channel_ptr->name == NULL)
1410   {
1411     goto fail_free_channel;
1412   }
1413
1414   channel_name_size = strlen(channel_name);
1415
1416   if (stereo)
1417   {
1418     port_name = malloc(channel_name_size + 3);
1419     if (port_name == NULL)
1420     {
1421         goto fail_free_channel_name;
1422     }
1423
1424     memcpy(port_name, channel_name, channel_name_size);
1425     port_name[channel_name_size] = ' ';
1426     port_name[channel_name_size+1] = 'L';
1427     port_name[channel_name_size+2] = 0;
1428
1429     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1430     if (channel_ptr->port_left == NULL)
1431     {
1432         goto fail_free_port_name;
1433     }
1434
1435     port_name[channel_name_size+1] = 'R';
1436
1437     channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1438     if (channel_ptr->port_right == NULL)
1439     {
1440         goto fail_unregister_left_channel;
1441     }
1442   }
1443   else
1444   {
1445     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
1446     if (channel_ptr->port_left == NULL)
1447     {
1448         goto fail_free_channel_name;
1449     }
1450   }
1451
1452   channel_ptr->stereo = stereo;
1453
1454   channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1455   channel_ptr->num_volume_transition_steps =
1456     channel_ptr->volume_transition_seconds *
1457     jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client) + 1;
1458   channel_ptr->volume = 0.0;
1459   channel_ptr->volume_new = 0.0;
1460   channel_ptr->balance = 0.0;
1461   channel_ptr->balance_new = 0.0;
1462   channel_ptr->meter_left = -1.0;
1463   channel_ptr->meter_right = -1.0;
1464   channel_ptr->abspeak = 0.0;
1465   channel_ptr->out_mute = false;
1466
1467   channel_ptr->peak_left = 0.0;
1468   channel_ptr->peak_right = 0.0;
1469   channel_ptr->peak_frames = 0;
1470
1471   channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1472   channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1473   channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1474   channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1475
1476   channel_ptr->NaN_detected = false;
1477
1478   channel_ptr->midi_cc_volume_index = -1;
1479   channel_ptr->midi_cc_balance_index = -1;
1480   channel_ptr->midi_cc_mute_index = -1;
1481   channel_ptr->midi_cc_solo_index = -1;
1482   channel_ptr->midi_cc_volume_picked_up = false;
1483   channel_ptr->midi_cc_balance_picked_up = false;
1484
1485   channel_ptr->midi_change_callback = NULL;
1486   channel_ptr->midi_change_callback_data = NULL;
1487   channel_ptr->midi_out_has_events = false;
1488
1489   channel_ptr->midi_scale = NULL;
1490
1491   channel_ptr->mixer_ptr->input_channels_list = g_slist_prepend(
1492                   channel_ptr->mixer_ptr->input_channels_list, channel_ptr);
1493
1494   free(port_name);
1495   return channel_ptr;
1496
1497 fail_unregister_left_channel:
1498   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1499
1500 fail_free_port_name:
1501   free(port_name);
1502
1503 fail_free_channel_name:
1504   free(channel_ptr->name);
1505
1506 fail_free_channel:
1507   free(channel_ptr);
1508   channel_ptr = NULL;
1509
1510 fail:
1511   return NULL;
1512 }
1513
1514 static jack_mixer_output_channel_t
1515 create_output_channel(
1516   jack_mixer_t mixer,
1517   const char * channel_name,
1518   bool stereo,
1519   bool system)
1520 {
1521   struct channel * channel_ptr;
1522   struct output_channel * output_channel_ptr;
1523   char * port_name = NULL;
1524   size_t channel_name_size;
1525
1526   output_channel_ptr = malloc(sizeof(struct output_channel));
1527   channel_ptr = (struct channel*)output_channel_ptr;
1528   if (channel_ptr == NULL)
1529   {
1530     goto fail;
1531   }
1532
1533   channel_ptr->mixer_ptr = mixer_ctx_ptr;
1534
1535   channel_ptr->name = strdup(channel_name);
1536   if (channel_ptr->name == NULL)
1537   {
1538     goto fail_free_channel;
1539   }
1540
1541   if (stereo)
1542   {
1543     channel_name_size = strlen(channel_name);
1544
1545     port_name = malloc(channel_name_size + 4);
1546     if (port_name == NULL)
1547     {
1548         goto fail_free_channel_name;
1549     }
1550
1551     memcpy(port_name, channel_name, channel_name_size);
1552     port_name[channel_name_size] = ' ';
1553     port_name[channel_name_size+1] = 'L';
1554     port_name[channel_name_size+2] = 0;
1555
1556     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1557     if (channel_ptr->port_left == NULL)
1558     {
1559         goto fail_free_port_name;
1560     }
1561
1562     port_name[channel_name_size+1] = 'R';
1563
1564     channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1565     if (channel_ptr->port_right == NULL)
1566     {
1567         goto fail_unregister_left_channel;
1568     }
1569   }
1570   else
1571   {
1572     channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
1573     if (channel_ptr->port_left == NULL)
1574     {
1575         goto fail_free_channel_name;
1576     }
1577   }
1578
1579   channel_ptr->stereo = stereo;
1580   channel_ptr->out_mute = false;
1581
1582   channel_ptr->volume_transition_seconds = VOLUME_TRANSITION_SECONDS;
1583   channel_ptr->num_volume_transition_steps =
1584     channel_ptr->volume_transition_seconds *
1585     jack_get_sample_rate(channel_ptr->mixer_ptr->jack_client) + 1;
1586   channel_ptr->volume = 0.0;
1587   channel_ptr->volume_new = 0.0;
1588   channel_ptr->balance = 0.0;
1589   channel_ptr->balance_new = 0.0;
1590   channel_ptr->meter_left = -1.0;
1591   channel_ptr->meter_right = -1.0;
1592   channel_ptr->abspeak = 0.0;
1593
1594   channel_ptr->peak_left = 0.0;
1595   channel_ptr->peak_right = 0.0;
1596   channel_ptr->peak_frames = 0;
1597
1598   channel_ptr->tmp_mixed_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1599   channel_ptr->tmp_mixed_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1600   channel_ptr->frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1601   channel_ptr->frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1602   channel_ptr->prefader_frames_left = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1603   channel_ptr->prefader_frames_right = calloc(MAX_BLOCK_SIZE, sizeof(jack_default_audio_sample_t));
1604
1605   channel_ptr->NaN_detected = false;
1606
1607   channel_ptr->midi_cc_volume_index = -1;
1608   channel_ptr->midi_cc_balance_index = -1;
1609   channel_ptr->midi_cc_mute_index = -1;
1610   channel_ptr->midi_cc_solo_index = -1;
1611   channel_ptr->midi_cc_volume_picked_up = false;
1612   channel_ptr->midi_cc_balance_picked_up = false;
1613
1614   channel_ptr->midi_change_callback = NULL;
1615   channel_ptr->midi_change_callback_data = NULL;
1616
1617   channel_ptr->midi_scale = NULL;
1618
1619   output_channel_ptr->soloed_channels = NULL;
1620   output_channel_ptr->muted_channels = NULL;
1621   output_channel_ptr->prefader_channels = NULL;
1622   output_channel_ptr->system = system;
1623   output_channel_ptr->prefader = false;
1624
1625   free(port_name);
1626   return output_channel_ptr;
1627
1628 fail_unregister_left_channel:
1629   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1630
1631 fail_free_port_name:
1632   free(port_name);
1633
1634 fail_free_channel_name:
1635   free(channel_ptr->name);
1636
1637 fail_free_channel:
1638   free(channel_ptr);
1639   channel_ptr = NULL;
1640
1641 fail:
1642   return NULL;
1643 }
1644
1645 jack_mixer_output_channel_t
1646 add_output_channel(
1647   jack_mixer_t mixer,
1648   const char * channel_name,
1649   bool stereo,
1650   bool system)
1651 {
1652   struct output_channel *output_channel_ptr;
1653   struct channel *channel_ptr;
1654
1655   output_channel_ptr = create_output_channel(mixer, channel_name, stereo, system);
1656   if (output_channel_ptr == NULL) {
1657     return NULL;
1658   }
1659   channel_ptr = (struct channel*)output_channel_ptr;
1660
1661   ((struct jack_mixer*)mixer)->output_channels_list = g_slist_prepend(
1662                   ((struct jack_mixer*)mixer)->output_channels_list, channel_ptr);
1663
1664   return output_channel_ptr;
1665 }
1666
1667 void
1668 remove_channels(
1669   jack_mixer_t mixer)
1670 {
1671   GSList *list_ptr;
1672   for (list_ptr = mixer_ctx_ptr->input_channels_list; list_ptr; list_ptr = g_slist_next(list_ptr))
1673   {
1674     struct channel *input_channel_ptr = list_ptr->data;
1675     remove_channel((jack_mixer_channel_t)input_channel_ptr);
1676   }
1677 }
1678
1679 void
1680 remove_output_channel(
1681   jack_mixer_output_channel_t output_channel)
1682 {
1683   struct output_channel *output_channel_ptr = output_channel;
1684   struct channel *channel_ptr = output_channel;
1685
1686   channel_ptr->mixer_ptr->output_channels_list = g_slist_remove(
1687                   channel_ptr->mixer_ptr->output_channels_list, channel_ptr);
1688   free(channel_ptr->name);
1689
1690   jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
1691   if (channel_ptr->stereo)
1692   {
1693     jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
1694   }
1695
1696   if (channel_ptr->midi_cc_volume_index != -1)
1697   {
1698     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] == channel_ptr);
1699     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_volume_index] = NULL;
1700   }
1701
1702   if (channel_ptr->midi_cc_balance_index != -1)
1703   {
1704     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] == channel_ptr);
1705     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_balance_index] = NULL;
1706   }
1707
1708   if (channel_ptr->midi_cc_mute_index != -1)
1709   {
1710     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] == channel_ptr);
1711     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_mute_index] = NULL;
1712   }
1713
1714   if (channel_ptr->midi_cc_solo_index != -1)
1715   {
1716     assert(channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] == channel_ptr);
1717     channel_ptr->mixer_ptr->midi_cc_map[channel_ptr->midi_cc_solo_index] = NULL;
1718   }
1719
1720   g_slist_free(output_channel_ptr->soloed_channels);
1721   g_slist_free(output_channel_ptr->muted_channels);
1722   g_slist_free(output_channel_ptr->prefader_channels);
1723
1724   free(channel_ptr->tmp_mixed_frames_left);
1725   free(channel_ptr->tmp_mixed_frames_right);
1726   free(channel_ptr->frames_left);
1727   free(channel_ptr->frames_right);
1728   free(channel_ptr->prefader_frames_left);
1729   free(channel_ptr->prefader_frames_right);
1730
1731   free(channel_ptr);
1732 }
1733
1734 void
1735 output_channel_set_solo(
1736   jack_mixer_output_channel_t output_channel,
1737   jack_mixer_channel_t channel,
1738   bool solo_value)
1739 {
1740   struct output_channel *output_channel_ptr = output_channel;
1741
1742   if (solo_value) {
1743     if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1744       return;
1745     output_channel_ptr->soloed_channels = g_slist_prepend(output_channel_ptr->soloed_channels, channel);
1746   } else {
1747     if (g_slist_find(output_channel_ptr->soloed_channels, channel) == NULL)
1748       return;
1749     output_channel_ptr->soloed_channels = g_slist_remove(output_channel_ptr->soloed_channels, channel);
1750   }
1751 }
1752
1753 void
1754 output_channel_set_muted(
1755   jack_mixer_output_channel_t output_channel,
1756   jack_mixer_channel_t channel,
1757   bool muted_value)
1758 {
1759   struct output_channel *output_channel_ptr = output_channel;
1760
1761   if (muted_value) {
1762     if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1763       return;
1764     output_channel_ptr->muted_channels = g_slist_prepend(output_channel_ptr->muted_channels, channel);
1765   } else {
1766     if (g_slist_find(output_channel_ptr->muted_channels, channel) == NULL)
1767       return;
1768     output_channel_ptr->muted_channels = g_slist_remove(output_channel_ptr->muted_channels, channel);
1769   }
1770 }
1771
1772 bool
1773 output_channel_is_muted(
1774   jack_mixer_output_channel_t output_channel,
1775   jack_mixer_channel_t channel)
1776 {
1777   struct output_channel *output_channel_ptr = output_channel;
1778
1779   if (g_slist_find(output_channel_ptr->muted_channels, channel) != NULL)
1780     return true;
1781   return false;
1782 }
1783
1784 bool
1785 output_channel_is_solo(
1786   jack_mixer_output_channel_t output_channel,
1787   jack_mixer_channel_t channel)
1788 {
1789   struct output_channel *output_channel_ptr = output_channel;
1790
1791   if (g_slist_find(output_channel_ptr->soloed_channels, channel) != NULL)
1792     return true;
1793   return false;
1794 }
1795
1796 void
1797 output_channel_set_prefader(
1798   jack_mixer_output_channel_t output_channel,
1799   bool pfl_value)
1800 {
1801   struct output_channel *output_channel_ptr = output_channel;
1802   output_channel_ptr->prefader = pfl_value;
1803 }
1804
1805 bool
1806 output_channel_is_prefader(
1807   jack_mixer_output_channel_t output_channel)
1808 {
1809   struct output_channel *output_channel_ptr = output_channel;
1810   return output_channel_ptr->prefader;
1811 }
1812
1813 void
1814 output_channel_set_in_prefader(
1815   jack_mixer_output_channel_t output_channel,
1816   jack_mixer_channel_t channel,
1817   bool prefader_value)
1818 {
1819   struct output_channel *output_channel_ptr = output_channel;
1820
1821   if (prefader_value) {
1822     if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
1823       return;
1824     output_channel_ptr->prefader_channels = g_slist_prepend(output_channel_ptr->prefader_channels, channel);
1825   } else {
1826     if (g_slist_find(output_channel_ptr->prefader_channels, channel) == NULL)
1827       return;
1828     output_channel_ptr->prefader_channels = g_slist_remove(output_channel_ptr->prefader_channels, channel);
1829   }
1830 }
1831
1832 bool
1833 output_channel_is_in_prefader(
1834   jack_mixer_output_channel_t output_channel,
1835   jack_mixer_channel_t channel)
1836 {
1837   struct output_channel *output_channel_ptr = output_channel;
1838
1839   if (g_slist_find(output_channel_ptr->prefader_channels, channel) != NULL)
1840     return true;
1841   return false;
1842 }