]> git.0d.be Git - jack_mixer.git/blob - jack_mix_box.c
Fix new output channel not creating from dialog
[jack_mixer.git] / jack_mix_box.c
1 /*****************************************************************************
2  *
3  *   This file is part of jack_mixer
4  *
5  *   Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
6  *   Copyright (C) 2009-2011 Frederic Peters <fpeters@0d.be>
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
11  *
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.
16  *
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.
20  *
21  *****************************************************************************/
22
23 /*
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.
27  *
28  */
29
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"
38
39 jack_mixer_t mixer;
40 bool keepRunning = true;
41
42 void
43 usage()
44 {
45
46         printf("Usage:\n");
47         printf("\tjack_mix_box [ -n|--name <jack client name> ] [ -s|--stereo ] [ -v|--volume <initial vol> ] MIDI_CC_1 MIDI_CC_2 ...\n");
48         printf("\tsend SIGUSR1 to the process to have the current columes reported per input channel\n\n");
49 }
50
51 void
52 reportVolume(int sig)
53 {
54         (void)sig;
55         channels_volumes_read(mixer);
56 }
57
58 void
59 triggerShutDown(int sig)
60 {
61         (void)sig;
62         keepRunning = false;
63 }
64
65 int
66 main(int argc, char *argv[])
67 {
68         jack_mixer_scale_t scale;
69         jack_mixer_channel_t main_mix_channel;
70         char *jack_cli_name = NULL;
71         int channel_index;
72         bool bStereo = false;
73         double initialVolume = 0.0f; //in dbFS
74
75         while (1) {
76                 int c;
77                 static struct option long_options[] =
78                 {
79                         {"name",  required_argument, 0, 'n'},
80                         {"help",  required_argument, 0, 'h'},
81                         {"stereo",  required_argument, 0, 's'},
82                         {"volume",  required_argument, 0, 'v'},
83                         {0, 0, 0, 0}
84                 };
85                 int option_index = 0;
86
87                 c = getopt_long (argc, argv, "shn:v:", long_options, &option_index);
88                 if (c == -1)
89                         break;
90
91                 switch (c) {
92                         case 'n':
93                                 jack_cli_name = strdup(optarg);
94                                 break;
95                         case 's':
96                                 bStereo = true;
97                                 break;
98                         case 'v':
99                                 initialVolume = strtod(optarg, NULL);
100                                 break;
101                         case 'h':
102                                 usage();
103                                 exit(0);
104                                 break;
105                         default:
106                                 fprintf(stderr, "Unknown argument, aborting.\n");
107                                 exit(1);
108                 }
109         }
110
111         if (optind == argc) {
112                 fprintf(stderr, "You must specify at least one input channel\n");
113                 exit(1);
114         }
115
116         scale = scale_create();
117         scale_add_threshold(scale, -70.0, 0.0);
118         scale_add_threshold(scale, 0.0, 1.0);
119         scale_calculate_coefficients(scale);
120
121         if (jack_cli_name == NULL) {
122                 jack_cli_name = strdup("jack_mix_box");
123         }
124
125         mixer = create(jack_cli_name, false);
126         main_mix_channel = add_output_channel(mixer, "MAIN", true, false);
127         channel_set_midi_scale(main_mix_channel, scale);
128         channel_volume_write(main_mix_channel, 0.0);
129
130         channel_index = 0;
131         while (optind < argc) {
132                 char *channel_name;
133                 jack_mixer_channel_t channel;
134
135                 channel_index += 1;
136                 channel_name = malloc(15);
137                 if (snprintf(channel_name, 15, "Channel %d", channel_index) >= 15) {
138                         free(channel_name);
139                         abort();
140                 }
141                 channel = add_channel(mixer, channel_name, bStereo);
142                 if (channel == NULL) {
143                         fprintf(stderr, "Failed to add channel %d, aborting\n", channel_index);
144                         exit(1);
145                 }
146                 channel_set_volume_midi_cc(channel, atoi(argv[optind++]));
147                 channel_set_midi_scale(channel, scale);
148                 channel_volume_write(channel, initialVolume);
149                 free(channel_name);
150         }
151
152         signal(SIGUSR1, reportVolume);
153         signal(SIGTERM, triggerShutDown);
154         signal(SIGHUP, triggerShutDown);
155         signal(SIGINT, triggerShutDown);
156
157         while (keepRunning) {
158                 usleep(500u * 1000u); //500msec
159         }
160
161         remove_channels(mixer);
162         remove_output_channel(main_mix_channel);
163         destroy(mixer);
164         scale_destroy(scale);
165         free(jack_cli_name);
166         return 0;
167 }