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