Send the restore event back, as requested in documentation
[jack_mixer.git] / jack_mixer.c
blob8498dcbfcced799e1bf852596b74aac7c49dd541
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of jack_mixer
5 *
6 * Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *****************************************************************************/
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include <math.h>
29 #include <jack/jack.h>
30 #if defined(HAVE_JACK_MIDI)
31 #include <jack/midiport.h>
32 #endif
33 #include <assert.h>
34 #include <pthread.h>
36 #include "jack_mixer.h"
37 #include "list.h"
38 //#define LOG_LEVEL LOG_LEVEL_DEBUG
39 #include "log.h"
41 #include "jack_compat.h"
43 #define PEAK_FRAMES_CHUNK 4800
45 #define FLOAT_EXISTS(x) (!((x) - (x)))
47 struct channel
49 struct list_head siblings;
50 struct jack_mixer * mixer_ptr;
51 char * name;
52 bool stereo;
53 float volume;
54 float balance;
55 bool muted;
56 bool soloed;
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;
65 jack_nframes_t peak_frames;
66 float peak_left;
67 float peak_right;
69 bool NaN_detected;
71 struct channel ** cc_map_volume_ptr_ptr;
72 struct channel ** cc_map_balance_ptr_ptr;
74 jack_default_audio_sample_t * left_buffer_ptr;
75 jack_default_audio_sample_t * right_buffer_ptr;
77 bool midi_modified;
79 jack_mixer_scale_t midi_scale;
82 struct jack_mixer
84 pthread_mutex_t mutex;
85 jack_client_t * jack_client;
86 struct list_head channels_list;
87 struct channel main_mix_channel;
88 unsigned int channels_count;
89 unsigned int soloed_channels_count;
91 jack_port_t * port_midi_in;
93 struct
95 bool balance; /* volume or balance is controlled by this mapping */
96 struct channel * channel_ptr;
97 } midi_cc_map[128];
100 float value_to_db(float value)
102 if (value <= 0)
104 return -INFINITY;
107 return 20.0 * log10f(value);
110 float db_to_value(float db)
112 return powf(10.0, db/20.0);
115 void
116 calc_channel_volumes(struct channel * channel_ptr)
118 if (channel_ptr->muted)
120 channel_ptr->volume_left = 0;
121 channel_ptr->volume_right = 0;
122 return;
125 if (channel_ptr->mixer_ptr->soloed_channels_count > 0 && !channel_ptr->soloed) /* there are soloed channels but we are not one of them */
127 channel_ptr->volume_left = 0;
128 channel_ptr->volume_right = 0;
129 return;
132 if (channel_ptr->stereo)
134 if (channel_ptr->balance > 0)
136 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
137 channel_ptr->volume_right = channel_ptr->volume;
139 else
141 channel_ptr->volume_left = channel_ptr->volume;
142 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
145 else
147 channel_ptr->volume_left = channel_ptr->volume * (1 - channel_ptr->balance);
148 channel_ptr->volume_right = channel_ptr->volume * (1 + channel_ptr->balance);
152 void
153 calc_all_channel_volumes(
154 struct jack_mixer * mixer_ptr)
156 struct list_head * node_ptr;
157 struct channel * channel_ptr;
159 list_for_each(node_ptr, &mixer_ptr->channels_list)
161 channel_ptr = list_entry(node_ptr, struct channel, siblings);
162 calc_channel_volumes(channel_ptr);
166 #define channel_ptr ((struct channel *)channel)
168 const char * channel_get_name(jack_mixer_channel_t channel)
170 return channel_ptr->name;
173 void channel_rename(jack_mixer_channel_t channel, const char * name)
175 char * new_name;
176 size_t channel_name_size;
177 char * port_name;
178 int ret;
180 new_name = strdup(name);
181 if (new_name == NULL)
183 return;
186 if (channel_ptr->name)
188 free(channel_ptr->name);
191 channel_ptr->name = new_name;
193 if (channel_ptr->stereo)
195 channel_name_size = strlen(name);
196 port_name = malloc(channel_name_size + 3);
197 memcpy(port_name, name, channel_name_size);
199 port_name[channel_name_size] = ' ';
200 port_name[channel_name_size+1] = 'L';
201 port_name[channel_name_size+2] = 0;
203 ret = jack_port_set_name(channel_ptr->port_left, port_name);
204 if (ret != 0)
206 /* what could we do here? */
209 port_name[channel_name_size+1] = 'R';
211 ret = jack_port_set_name(channel_ptr->port_right, port_name);
212 if (ret != 0)
214 /* what could we do here? */
217 free(port_name);
219 else
221 ret = jack_port_set_name(channel_ptr->port_left, name);
222 if (ret != 0)
224 /* what could we do here? */
229 bool channel_is_stereo(jack_mixer_channel_t channel)
231 return channel_ptr->stereo;
234 void remove_channel(jack_mixer_channel_t channel)
236 list_del(&channel_ptr->siblings);
237 free(channel_ptr->name);
239 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
240 if (channel_ptr->stereo)
242 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_right);
245 channel_ptr->mixer_ptr->channels_count--;
247 if (channel_ptr->cc_map_volume_ptr_ptr != NULL)
249 assert(*(channel_ptr->cc_map_volume_ptr_ptr) == channel_ptr);
250 *(channel_ptr->cc_map_volume_ptr_ptr) = NULL;
253 if (channel_ptr->cc_map_balance_ptr_ptr != NULL)
255 assert(*(channel_ptr->cc_map_balance_ptr_ptr) == channel_ptr);
256 *(channel_ptr->cc_map_balance_ptr_ptr) = NULL;
259 free(channel_ptr);
262 void channel_stereo_meter_read(jack_mixer_channel_t channel, double * left_ptr, double * right_ptr)
264 assert(channel_ptr);
265 *left_ptr = value_to_db(channel_ptr->meter_left);
266 *right_ptr = value_to_db(channel_ptr->meter_right);
269 void channel_mono_meter_read(jack_mixer_channel_t channel, double * mono_ptr)
271 *mono_ptr = value_to_db(channel_ptr->meter_left);
274 bool
275 channel_is_midi_modified(
276 jack_mixer_channel_t channel)
278 bool midi_modified;
280 assert(channel_ptr);
281 midi_modified = channel_ptr->midi_modified;
282 channel_ptr->midi_modified = false;
284 return midi_modified;
287 void channel_volume_write(jack_mixer_channel_t channel, double volume)
289 assert(channel_ptr);
290 channel_ptr->volume = db_to_value(volume);
291 calc_channel_volumes(channel_ptr);
294 double
295 channel_volume_read(
296 jack_mixer_channel_t channel)
298 assert(channel_ptr);
299 return value_to_db(channel_ptr->volume);
302 void channel_balance_write(jack_mixer_channel_t channel, double balance)
304 assert(channel_ptr);
305 channel_ptr->balance = balance;
306 calc_channel_volumes(channel_ptr);
309 double
310 channel_balance_read(
311 jack_mixer_channel_t channel)
313 assert(channel_ptr);
314 return channel_ptr->balance;
317 double channel_abspeak_read(jack_mixer_channel_t channel)
319 assert(channel_ptr);
320 if (channel_ptr->NaN_detected)
322 return sqrt(-1);
324 else
326 return value_to_db(channel_ptr->abspeak);
330 void channel_abspeak_reset(jack_mixer_channel_t channel)
332 channel_ptr->abspeak = 0;
333 channel_ptr->NaN_detected = false;
336 void channel_mute(jack_mixer_channel_t channel)
338 channel_ptr->muted = true;
339 calc_channel_volumes(channel_ptr);
342 void channel_unmute(jack_mixer_channel_t channel)
344 channel_ptr->muted = false;
345 calc_channel_volumes(channel_ptr);
348 void channel_solo(jack_mixer_channel_t channel)
350 if (!channel_ptr->soloed)
352 channel_ptr->soloed = true;
353 channel_ptr->mixer_ptr->soloed_channels_count++;
355 if (channel_ptr->mixer_ptr->soloed_channels_count == 1)
357 calc_all_channel_volumes(channel_ptr->mixer_ptr);
359 else
361 calc_channel_volumes(channel_ptr);
366 void channel_unsolo(jack_mixer_channel_t channel)
368 if (channel_ptr->soloed)
370 channel_ptr->soloed = false;
371 channel_ptr->mixer_ptr->soloed_channels_count--;
373 if (channel_ptr->mixer_ptr->soloed_channels_count == 0)
375 calc_all_channel_volumes(channel_ptr->mixer_ptr);
377 else
379 calc_channel_volumes(channel_ptr);
384 bool channel_is_muted(jack_mixer_channel_t channel)
386 return channel_ptr->muted;
389 bool channel_is_soloed(jack_mixer_channel_t channel)
391 return channel_ptr->soloed;
394 void
395 channel_set_midi_scale(
396 jack_mixer_channel_t channel,
397 jack_mixer_scale_t scale)
399 channel_ptr->midi_scale = scale;
402 #undef channel_ptr
404 /* process input channels and mix them into main mix */
405 static
406 inline
407 void
408 mix(
409 struct jack_mixer * mixer_ptr,
410 jack_nframes_t start, /* index of first sample to process */
411 jack_nframes_t end) /* index of sample to stop processing before */
413 jack_nframes_t i;
414 struct list_head * node_ptr;
415 struct channel * channel_ptr;
416 jack_default_audio_sample_t frame_left;
417 jack_default_audio_sample_t frame_right;
419 list_for_each(node_ptr, &mixer_ptr->channels_list)
421 channel_ptr = list_entry(node_ptr, struct channel, siblings);
423 for (i = start ; i < end ; i++)
425 if (!FLOAT_EXISTS(channel_ptr->left_buffer_ptr[i]))
427 channel_ptr->NaN_detected = true;
428 break;
431 frame_left = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_left;
432 mixer_ptr->main_mix_channel.left_buffer_ptr[i] += frame_left;
434 if (channel_ptr->stereo)
436 if (!FLOAT_EXISTS(channel_ptr->right_buffer_ptr[i]))
438 channel_ptr->NaN_detected = true;
439 break;
442 frame_right = channel_ptr->right_buffer_ptr[i] * channel_ptr->volume_right;
444 else
446 frame_right = channel_ptr->left_buffer_ptr[i] * channel_ptr->volume_right;
449 mixer_ptr->main_mix_channel.right_buffer_ptr[i] += frame_right;
451 if (channel_ptr->stereo)
453 frame_left = fabsf(frame_left);
454 frame_right = fabsf(frame_right);
456 if (channel_ptr->peak_left < frame_left)
458 channel_ptr->peak_left = frame_left;
460 if (frame_left > channel_ptr->abspeak)
462 channel_ptr->abspeak = frame_left;
466 if (channel_ptr->peak_right < frame_right)
468 channel_ptr->peak_right = frame_right;
470 if (frame_right > channel_ptr->abspeak)
472 channel_ptr->abspeak = frame_right;
476 else
478 frame_left = (fabsf(frame_left) + fabsf(frame_right)) / 2;
480 if (channel_ptr->peak_left < frame_left)
482 channel_ptr->peak_left = frame_left;
484 if (frame_left > channel_ptr->abspeak)
486 channel_ptr->abspeak = frame_left;
491 channel_ptr->peak_frames++;
492 if (channel_ptr->peak_frames >= PEAK_FRAMES_CHUNK)
494 channel_ptr->meter_left = channel_ptr->peak_left;
495 channel_ptr->peak_left = 0.0;
497 if (channel_ptr->stereo)
499 channel_ptr->meter_right = channel_ptr->peak_right;
500 channel_ptr->peak_right = 0.0;
503 channel_ptr->peak_frames = 0;
508 /* process main mix channel */
509 for (i = start ; i < end ; i++)
511 mixer_ptr->main_mix_channel.left_buffer_ptr[i] *= mixer_ptr->main_mix_channel.volume_left;
512 mixer_ptr->main_mix_channel.right_buffer_ptr[i] *= mixer_ptr->main_mix_channel.volume_right;
514 frame_left = fabsf(mixer_ptr->main_mix_channel.left_buffer_ptr[i]);
515 if (mixer_ptr->main_mix_channel.peak_left < frame_left)
517 mixer_ptr->main_mix_channel.peak_left = frame_left;
519 if (frame_left > mixer_ptr->main_mix_channel.abspeak)
521 mixer_ptr->main_mix_channel.abspeak = frame_left;
525 frame_right = fabsf(mixer_ptr->main_mix_channel.right_buffer_ptr[i]);
526 if (mixer_ptr->main_mix_channel.peak_right < frame_right)
528 mixer_ptr->main_mix_channel.peak_right = frame_right;
530 if (frame_right > mixer_ptr->main_mix_channel.abspeak)
532 mixer_ptr->main_mix_channel.abspeak = frame_right;
536 mixer_ptr->main_mix_channel.peak_frames++;
537 if (mixer_ptr->main_mix_channel.peak_frames >= PEAK_FRAMES_CHUNK)
539 mixer_ptr->main_mix_channel.meter_left = mixer_ptr->main_mix_channel.peak_left;
540 mixer_ptr->main_mix_channel.peak_left = 0.0;
542 mixer_ptr->main_mix_channel.meter_right = mixer_ptr->main_mix_channel.peak_right;
543 mixer_ptr->main_mix_channel.peak_right = 0.0;
545 mixer_ptr->main_mix_channel.peak_frames = 0;
550 static
551 inline
552 void
553 update_channel_buffers(
554 struct channel * channel_ptr,
555 jack_nframes_t nframes)
557 channel_ptr->left_buffer_ptr = jack_port_get_buffer(channel_ptr->port_left, nframes);
559 if (channel_ptr->stereo)
561 channel_ptr->right_buffer_ptr = jack_port_get_buffer(channel_ptr->port_right, nframes);
565 #define mixer_ptr ((struct jack_mixer *)context)
568 process(jack_nframes_t nframes, void * context)
570 jack_nframes_t i;
571 struct list_head * node_ptr;
572 struct channel * channel_ptr;
573 #if defined(HAVE_JACK_MIDI)
574 jack_nframes_t event_count;
575 jack_midi_event_t in_event;
576 void * midi_buffer;
577 signed char byte;
578 #endif
579 jack_nframes_t offset;
581 update_channel_buffers(&mixer_ptr->main_mix_channel, nframes);
583 list_for_each(node_ptr, &mixer_ptr->channels_list)
585 channel_ptr = list_entry(node_ptr, struct channel, siblings);
587 update_channel_buffers(channel_ptr, nframes);
590 for (i = 0 ; i < nframes ; i++)
592 mixer_ptr->main_mix_channel.left_buffer_ptr[i] = 0.0;
593 mixer_ptr->main_mix_channel.right_buffer_ptr[i] = 0.0;
596 offset = 0;
598 #if defined(HAVE_JACK_MIDI)
599 midi_buffer = jack_port_get_buffer(mixer_ptr->port_midi_in, nframes);
600 event_count = jack_midi_get_event_count(midi_buffer);
602 for (i = 0 ; i < event_count; i++)
604 jack_midi_event_get(&in_event, midi_buffer, i);
606 if (in_event.size != 3 ||
607 (in_event.buffer[0] & 0xF0) != 0xB0 ||
608 in_event.buffer[1] > 127 ||
609 in_event.buffer[2] > 127)
611 continue;
614 assert(in_event.time < nframes);
616 LOG_DEBUG(
617 "%u: CC#%u -> %u",
618 (unsigned int)(in_event.buffer[0] & 0x0F),
619 (unsigned int)in_event.buffer[1],
620 (unsigned int)in_event.buffer[2]);
622 channel_ptr = mixer_ptr->midi_cc_map[in_event.buffer[1]].channel_ptr;
624 /* if we have mapping for particular CC and MIDI scale is set for corresponding channel */
625 if (channel_ptr != NULL && channel_ptr->midi_scale != NULL)
627 assert(in_event.time >= offset);
629 if (in_event.time > offset)
631 mix(mixer_ptr, offset, in_event.time);
632 offset = in_event.time;
635 if (mixer_ptr->midi_cc_map[in_event.buffer[1]].balance)
637 byte = in_event.buffer[2];
638 if (byte == 0)
640 byte = 1;
642 byte -= 64;
644 channel_ptr->balance = (float)byte / 63;
645 LOG_DEBUG("\"%s\" balance -> %f", channel_ptr->name, channel_ptr->balance);
647 else
649 channel_ptr->volume = db_to_value(scale_scale_to_db(channel_ptr->midi_scale, (double)in_event.buffer[2] / 127));
650 LOG_DEBUG("\"%s\" volume -> %f", channel_ptr->name, channel_ptr->volume);
653 calc_channel_volumes(channel_ptr);
655 channel_ptr->midi_modified = true;
660 #endif
662 mix(mixer_ptr, offset, nframes);
664 return 0;
667 #undef mixer_ptr
669 jack_mixer_t
670 create(
671 const char * jack_client_name_ptr)
673 int ret;
674 struct jack_mixer * mixer_ptr;
675 int i;
678 mixer_ptr = malloc(sizeof(struct jack_mixer));
679 if (mixer_ptr == NULL)
681 goto exit;
684 ret = pthread_mutex_init(&mixer_ptr->mutex, NULL);
685 if (ret != 0)
687 goto exit_free;
690 INIT_LIST_HEAD(&mixer_ptr->channels_list);
692 mixer_ptr->channels_count = 0;
693 mixer_ptr->soloed_channels_count = 0;
695 for (i = 0 ; i < 128 ; i++)
697 mixer_ptr->midi_cc_map[i].channel_ptr = NULL;
700 mixer_ptr->midi_cc_map[7].channel_ptr = &mixer_ptr->main_mix_channel;
701 mixer_ptr->midi_cc_map[7].balance = false;
702 mixer_ptr->main_mix_channel.cc_map_volume_ptr_ptr = &mixer_ptr->midi_cc_map[7].channel_ptr;
704 mixer_ptr->midi_cc_map[8].channel_ptr = &mixer_ptr->main_mix_channel;
705 mixer_ptr->midi_cc_map[8].balance = true;
706 mixer_ptr->main_mix_channel.cc_map_balance_ptr_ptr = &mixer_ptr->midi_cc_map[8].channel_ptr;
708 LOG_DEBUG("Initializing JACK");
709 mixer_ptr->jack_client = jack_client_new(jack_client_name_ptr);
710 if (mixer_ptr->jack_client == NULL)
712 LOG_ERROR("Cannot create JACK client.");
713 LOG_NOTICE("Please make sure JACK daemon is running.");
714 goto exit_destroy_mutex;
717 LOG_DEBUG("JACK client created");
719 LOG_DEBUG("Sample rate: %" PRIu32, jack_get_sample_rate(mixer_ptr->jack_client));
721 mixer_ptr->main_mix_channel.mixer_ptr = mixer_ptr;
723 #if defined(HAVE_JACK_MIDI)
724 mixer_ptr->port_midi_in = jack_port_register(mixer_ptr->jack_client, "midi in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
725 if (mixer_ptr->port_midi_in == NULL)
727 LOG_ERROR("Cannot create JACK port");
728 goto close_jack;
730 #endif
732 mixer_ptr->main_mix_channel.port_left = jack_port_register(mixer_ptr->jack_client, "main out L", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
733 if (mixer_ptr->main_mix_channel.port_left == NULL)
735 LOG_ERROR("Cannot create JACK port");
736 goto close_jack;
739 mixer_ptr->main_mix_channel.port_right = jack_port_register(mixer_ptr->jack_client, "main out R", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
740 if (mixer_ptr->main_mix_channel.port_right == NULL)
742 LOG_ERROR("Cannot create JACK port");
743 goto close_jack;
746 mixer_ptr->main_mix_channel.stereo = true;
748 mixer_ptr->main_mix_channel.name = "MAIN";
750 mixer_ptr->main_mix_channel.volume = 0.0;
751 mixer_ptr->main_mix_channel.balance = 0.0;
752 mixer_ptr->main_mix_channel.muted = false;
753 mixer_ptr->main_mix_channel.soloed = false;
754 mixer_ptr->main_mix_channel.meter_left = 0.0;
755 mixer_ptr->main_mix_channel.meter_right = 0.0;
756 mixer_ptr->main_mix_channel.abspeak = 0.0;
758 mixer_ptr->main_mix_channel.peak_left = 0.0;
759 mixer_ptr->main_mix_channel.peak_right = 0.0;
760 mixer_ptr->main_mix_channel.peak_frames = 0;
762 mixer_ptr->main_mix_channel.NaN_detected = false;
763 mixer_ptr->main_mix_channel.midi_modified = false;
765 mixer_ptr->main_mix_channel.midi_scale = NULL;
767 calc_channel_volumes(&mixer_ptr->main_mix_channel);
769 ret = jack_set_process_callback(mixer_ptr->jack_client, process, mixer_ptr);
770 if (ret != 0)
772 LOG_ERROR("Cannot set JACK process callback");
773 goto close_jack;
776 ret = jack_activate(mixer_ptr->jack_client);
777 if (ret != 0)
779 LOG_ERROR("Cannot activate JACK client");
780 goto close_jack;
783 return mixer_ptr;
785 close_jack:
786 jack_client_close(mixer_ptr->jack_client); /* this should clear all other resources we obtained through the client handle */
788 exit_destroy_mutex:
789 pthread_mutex_destroy(&mixer_ptr->mutex);
791 exit_free:
792 free(mixer_ptr);
794 exit:
795 return NULL;
798 #define mixer_ctx_ptr ((struct jack_mixer *)mixer)
800 void
801 destroy(
802 jack_mixer_t mixer)
804 LOG_DEBUG("Uninitializing JACK");
806 assert(mixer_ctx_ptr->jack_client != NULL);
808 jack_client_close(mixer_ctx_ptr->jack_client);
810 pthread_mutex_destroy(&mixer_ctx_ptr->mutex);
812 free(mixer_ctx_ptr);
815 jack_mixer_channel_t
816 get_main_mix_channel(
817 jack_mixer_t mixer)
819 return &mixer_ctx_ptr->main_mix_channel;
822 unsigned int
823 get_channels_count(
824 jack_mixer_t mixer)
826 return mixer_ctx_ptr->channels_count;
829 jack_mixer_channel_t
830 add_channel(
831 jack_mixer_t mixer,
832 const char * channel_name,
833 bool stereo)
835 struct channel * channel_ptr;
836 char * port_name;
837 size_t channel_name_size;
838 int i;
840 channel_ptr = malloc(sizeof(struct channel));
841 if (channel_ptr == NULL)
843 goto fail;
846 channel_ptr->mixer_ptr = mixer_ctx_ptr;
848 channel_ptr->name = strdup(channel_name);
849 if (channel_ptr->name == NULL)
851 goto fail_free_channel;
854 if (stereo)
856 channel_name_size = strlen(channel_name);
858 port_name = malloc(channel_name_size + 3);
859 if (port_name == NULL)
861 goto fail_free_channel_name;
864 memcpy(port_name, channel_name, channel_name_size);
865 port_name[channel_name_size] = ' ';
866 port_name[channel_name_size+1] = 'L';
867 port_name[channel_name_size+2] = 0;
869 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
870 if (channel_ptr->port_left == NULL)
872 goto fail_free_port_name;
875 port_name[channel_name_size+1] = 'R';
877 channel_ptr->port_right = jack_port_register(channel_ptr->mixer_ptr->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
878 if (channel_ptr->port_right == NULL)
880 goto fail_unregister_left_channel;
883 else
885 channel_ptr->port_left = jack_port_register(channel_ptr->mixer_ptr->jack_client, channel_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
886 if (channel_ptr->port_left == NULL)
888 goto fail_free_channel_name;
892 channel_ptr->stereo = stereo;
894 channel_ptr->volume = 0.0;
895 channel_ptr->balance = 0.0;
896 channel_ptr->muted = false;
897 channel_ptr->soloed = false;
898 channel_ptr->meter_left = -1.0;
899 channel_ptr->meter_right = -1.0;
900 channel_ptr->abspeak = 0.0;
902 channel_ptr->peak_left = 0.0;
903 channel_ptr->peak_right = 0.0;
904 channel_ptr->peak_frames = 0;
906 channel_ptr->NaN_detected = false;
907 channel_ptr->midi_modified = false;
909 channel_ptr->midi_scale = NULL;
911 calc_channel_volumes(channel_ptr);
913 list_add_tail(&channel_ptr->siblings, &channel_ptr->mixer_ptr->channels_list);
914 channel_ptr->mixer_ptr->channels_count++;
916 for (i = 11 ; i < 128 ; i++)
918 if (mixer_ctx_ptr->midi_cc_map[i].channel_ptr == NULL)
920 mixer_ctx_ptr->midi_cc_map[i].channel_ptr = channel_ptr;
921 mixer_ctx_ptr->midi_cc_map[i].balance = false;
922 channel_ptr->cc_map_volume_ptr_ptr = &mixer_ctx_ptr->midi_cc_map[i].channel_ptr;
924 LOG_NOTICE("New channel \"%s\" volume mapped to CC#%i", channel_name, i);
926 break;
930 for (; i < 128 ; i++)
932 if (mixer_ctx_ptr->midi_cc_map[i].channel_ptr == NULL)
934 mixer_ctx_ptr->midi_cc_map[i].channel_ptr = channel_ptr;
935 mixer_ctx_ptr->midi_cc_map[i].balance = true;
936 channel_ptr->cc_map_balance_ptr_ptr = &mixer_ctx_ptr->midi_cc_map[i].channel_ptr;
938 LOG_NOTICE("New channel \"%s\" balance mapped to CC#%i", channel_name, i);
940 break;
944 return channel_ptr;
946 fail_unregister_left_channel:
947 jack_port_unregister(channel_ptr->mixer_ptr->jack_client, channel_ptr->port_left);
949 fail_free_port_name:
950 free(port_name);
952 fail_free_channel_name:
953 free(channel_ptr->name);
955 fail_free_channel:
956 free(channel_ptr);
957 channel_ptr = NULL;
959 fail:
960 return NULL;