Update channel edit/remove menu order when channels are re-ordered
[jack_mixer.git] / jack_mix_box.c
blobf092ffcecf742c0cdac20cf1db0c79e061e8a586
1 /*****************************************************************************
3 * This file is part of jack_mixer
5 * Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2009-2011 Frederic Peters <fpeters@0d.be>
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 *****************************************************************************/
24 * jack_mix_box is a most minimalistic jack mixer, a set of mono/sterero input
25 * channels, mixed to a single output channel, with the volume of the
26 * input channels controlled by MIDI control change (CC) codes.
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdbool.h>
34 #include <getopt.h>
35 #include <signal.h>
36 #include <unistd.h>
37 #include "jack_mixer.h"
39 jack_mixer_t mixer;
40 bool keepRunning = true;
42 void
43 usage()
45 printf("Usage: ");
46 printf("jack_mix_box [-n <name>] [-p] [-s] [-v <dB>] MIDI_CC...\n");
47 printf("\n");
48 printf("-h|--help\tprint this help message\n");
49 printf("-n|--name\tset JACK client name\n");
50 printf("-p|--pickup\tenable MIDI pickup mode (default: jump-to-value)\n");
51 printf("-s|--stereo\tmake all input channels stereo with left+right input\n");
52 printf("-v|--volume\tinitial volume gain in dB, default is 0.0 (i.e. unity gain)\n");
53 printf("\n");
54 printf("Each positional argument is interpreted as a MIDI Control Change number and adds\n");
55 printf("a mixer channel with one (mono) or left+right (stereo) inputs, whose volume can\n");
56 printf("be controlled via the given MIDI Control Change.\n");
57 printf("\n");
58 printf("Send SIGUSR1 to the process to have the current volumes reported per input channel.\n\n");
61 void
62 reportVolume(int sig)
64 (void)sig;
65 channels_volumes_read(mixer);
68 void
69 triggerShutDown(int sig)
71 (void)sig;
72 keepRunning = false;
75 int
76 main(int argc, char *argv[])
78 jack_mixer_scale_t scale;
79 jack_mixer_channel_t main_mix_channel;
80 char *jack_cli_name = NULL;
81 int channel_index;
82 bool bStereo = false;
83 enum midi_behavior_mode ePickup = Jump_To_Value;
84 double initialVolume = 0.0f; //in dbFS
86 while (1) {
87 int c;
88 static struct option long_options[] =
90 {"name", required_argument, 0, 'n'},
91 {"help", no_argument, 0, 'h'},
92 {"pickup", no_argument, 0, 'p'},
93 {"stereo", no_argument, 0, 's'},
94 {"volume", required_argument, 0, 'v'},
95 {0, 0, 0, 0}
97 int option_index = 0;
99 c = getopt_long (argc, argv, "sphn:v:", long_options, &option_index);
100 if (c == -1)
101 break;
103 switch (c) {
104 case 'n':
105 jack_cli_name = strdup(optarg);
106 break;
107 case 's':
108 bStereo = true;
109 break;
110 case 'v':
111 initialVolume = strtod(optarg, NULL);
112 break;
113 case 'h':
114 usage();
115 exit(0);
116 break;
117 case 'p':
118 ePickup = Pick_Up;
119 break;
120 default:
121 fprintf(stderr, "Unknown argument, aborting.\n");
122 exit(1);
126 if (optind == argc) {
127 fprintf(stderr, "You must specify at least one input channel\n");
128 exit(1);
131 scale = scale_create();
132 scale_add_threshold(scale, -70.0, 0.0);
133 scale_add_threshold(scale, 0.0, 1.0);
134 scale_calculate_coefficients(scale);
136 if (jack_cli_name == NULL) {
137 jack_cli_name = strdup("jack_mix_box");
140 mixer = create(jack_cli_name, false);
141 main_mix_channel = add_output_channel(mixer, "MAIN", true, false);
142 channel_set_midi_scale(main_mix_channel, scale);
143 channel_volume_write(main_mix_channel, 0.0);
144 set_midi_behavior_mode(mixer, ePickup);
146 channel_index = 0;
147 while (optind < argc) {
148 char *channel_name;
149 jack_mixer_channel_t channel;
151 channel_index += 1;
152 channel_name = malloc(15);
153 if (snprintf(channel_name, 15, "Channel %d", channel_index) >= 15) {
154 free(channel_name);
155 abort();
157 channel = add_channel(mixer, channel_name, bStereo);
158 if (channel == NULL) {
159 fprintf(stderr, "Failed to add channel %d, aborting\n", channel_index);
160 exit(1);
162 channel_set_volume_midi_cc(channel, atoi(argv[optind++]));
163 channel_set_midi_scale(channel, scale);
164 channel_volume_write(channel, initialVolume);
165 free(channel_name);
168 signal(SIGUSR1, reportVolume);
169 signal(SIGTERM, triggerShutDown);
170 signal(SIGHUP, triggerShutDown);
171 signal(SIGINT, triggerShutDown);
173 while (keepRunning) {
174 usleep(500u * 1000u); //500msec
177 remove_channels(mixer);
178 remove_output_channel(main_mix_channel);
179 destroy(mixer);
180 scale_destroy(scale);
181 free(jack_cli_name);
182 return 0;