]> git.0d.be Git - jack_mixer.git/blob - jack_mix_box.c
Merge branch 'master' into mergebranch
[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 "jack_mixer.h"
37
38 jack_mixer_t mixer;
39
40 void
41 usage()
42 {
43
44         printf("Usage:\n");
45         printf("\tjack_mix_box [ -n|--name <jack client name> ] [ -s|--stereo ] [ -v|--volume <initial vol> ] MIDI_CC_1 MIDI_CC_2 ...\n");
46         printf("\tsend SIGUSR1 to the process to have the current columes reported per input channel\n\n");
47 }
48
49 void
50 reportVolume(int sig)
51 {
52         (void)sig;
53         channels_volumes_read(mixer);
54 }
55
56 int
57 main(int argc, char *argv[])
58 {
59         jack_mixer_scale_t scale;
60         jack_mixer_channel_t main_mix_channel;
61         char *jack_cli_name = NULL;
62         int channel_index;
63         bool bStereo = false;
64         double initialVolume = 0.0f; //in dbFS
65
66         while (1) {
67                 int c;
68                 static struct option long_options[] =
69                 {
70                         {"name",  required_argument, 0, 'n'},
71                         {"help",  required_argument, 0, 'h'},
72                         {"stereo",  required_argument, 0, 's'},
73                         {"volume",  required_argument, 0, 'v'},
74                         {0, 0, 0, 0}
75                 };
76                 int option_index = 0;
77
78                 c = getopt_long (argc, argv, "shn:v:", long_options, &option_index);
79                 if (c == -1)
80                         break;
81
82                 switch (c) {
83                         case 'n':
84                                 jack_cli_name = strdup(optarg);
85                                 break;
86                         case 's':
87                                 bStereo = true;
88                                 break;
89                         case 'v':
90                                 initialVolume = strtod(optarg, NULL);
91                                 break;
92                         case 'h':
93                                 usage();
94                                 exit(0);
95                                 break;
96                         default:
97                                 fprintf(stderr, "Unknown argument, aborting.\n");
98                                 exit(1);
99                 }
100         }
101
102         if (optind == argc) {
103                 fprintf(stderr, "You must specify at least one input channel\n");
104                 exit(1);
105         }
106
107         scale = scale_create();
108         scale_add_threshold(scale, -70.0, 0.0);
109         scale_add_threshold(scale, 0.0, 1.0);
110         scale_calculate_coefficients(scale);
111
112         if (jack_cli_name == NULL) {
113                 jack_cli_name = strdup("jack_mix_box");
114         }
115
116         mixer = create(jack_cli_name, false);
117         main_mix_channel = add_output_channel(mixer, "MAIN", true, false);
118         channel_set_midi_scale(main_mix_channel, scale);
119         channel_volume_write(main_mix_channel, 0.0);
120
121         channel_index = 0;
122         while (optind < argc) {
123                 char *channel_name;
124                 jack_mixer_channel_t channel;
125
126                 channel_index += 1;
127                 channel_name = malloc(15);
128                 if (snprintf(channel_name, 15, "Channel %d", channel_index) >= 15) {
129                         abort();
130                 }
131                 channel = add_channel(mixer, channel_name, bStereo);
132                 if (channel == NULL) {
133                         fprintf(stderr, "Failed to add channel %d, aborting\n", channel_index);
134                         exit(1);
135                 }
136                 channel_set_volume_midi_cc(channel, atoi(argv[optind++]));
137                 channel_set_midi_scale(channel, scale);
138                 channel_volume_write(channel, initialVolume);
139         }
140
141         signal(SIGUSR1, reportVolume);
142
143         while (true) {
144                 sleep(1);
145         }
146
147         return 0;
148 }