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