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