This repository was archived by the owner on Jun 16, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdirt.c
More file actions
229 lines (198 loc) · 7.64 KB
/
Copy pathdirt.c
File metadata and controls
229 lines (198 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <math.h>
#ifdef linux
#include <signal.h>
#endif
#include "common.h"
#include "audio.h"
#include "server.h"
static int dirty_compressor_flag = 1;
#ifdef JACK
static int jack_auto_connect_flag = 1;
#endif
static int late_trigger_flag = 1;
static int shape_gain_comp_flag = 0;
static int preload_flag = 0;
#ifdef linux
void sigint_handler(int sig) {
printf("\nCTRL-C detected\n");
// explicitly call exit on signit so things registered via atexit() fire
exit(-1);
}
#endif
int main (int argc, char **argv) {
/* Use getopt to parse command-line arguments */
/* see http://www.gnu.org/savannah-checkouts/gnu/libc/manual/html_node/Getopt.html */
int c;
int num_channels;
int samplerate;
float gain = 20.0 * log10(g_gain/16.0);
char *osc_port = DEFAULT_OSC_PORT;
char *sampleroot = "./samples";
char *version = "1.0.0";
unsigned int num_workers = DEFAULT_WORKERS;
#ifdef linux
signal(SIGINT, sigint_handler);
#endif
while (1)
{
static struct option long_options[] =
{
/* Use flags like so:
{"verbose", no_argument, &verbose_flag, 'V'}*/
/* Argument styles: no_argument, required_argument, optional_argument */
{"port", required_argument, 0, 'p'},
{"channels", required_argument, 0, 'c'},
{"samplerate", required_argument, 0, 'r'},
{"dirty-compressor", no_argument, &dirty_compressor_flag, 1},
{"no-dirty-compressor", no_argument, &dirty_compressor_flag, 0},
{"shape-gain-compensation", no_argument, &shape_gain_comp_flag, 1},
{"no-shape-gain-compensation", no_argument, &shape_gain_comp_flag, 0},
#ifdef JACK
{"jack-auto-connect", no_argument, &jack_auto_connect_flag, 1},
{"no-jack-auto-connect", no_argument, &jack_auto_connect_flag, 0},
#endif
{"late-trigger", no_argument, &late_trigger_flag, 1},
{"no-late-trigger", no_argument, &late_trigger_flag, 0},
{"samples-root-path", required_argument, 0, 's'},
{"workers", required_argument, 0, 'w'},
{"gain", required_argument, 0, 'g'},
{"preload", no_argument, &preload_flag, 1},
{"no-preload", no_argument, &preload_flag, 0},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
/* Argument parameters:
no_argument: " "
required_argument: ":"
optional_argument: "::" */
c = getopt_long(argc, argv, "c:s:w:g:vh",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0) break;
case 'v':
printf("%s\n", version);
return 1;
case 'h':
printf("Usage: dirt [OPTION]...\n"
"\n"
"Dirt - a software sampler, mainly used with Tidal: http://yaxu.org/tidal/\n"
"Released as free software under the terms of the GNU Public License version 3.0 and later.\n"
"\n"
"Arguments:\n"
" -p, --port OSC port to listen to (default: %s)\n"
" -c, --channels number of output channels (default: %u)\n"
#ifndef JACK
" -r, --samplerate samplerate (default: %u)\n"
#endif
" --dirty-compressor enable dirty compressor on audio output (default)\n"
" --no-dirty-compressor disable dirty compressor on audio output\n"
" --shape-gain-compensation enable distortion gain compensation\n"
" --no-shape-gain-compensation disable distortion gain compensation (default)\n"
" -g, --gain gain adjustment (default %f db)\n"
#ifdef JACK
" --jack-auto-connect automatically connect to writable clients (default)\n"
" --no-jack-auto-connect do not connect to writable clients \n"
#endif
" --late-trigger enable sample retrigger after loading (default)\n"
" --no-late-trigger disable sample retrigger after loading\n"
" --preload enable sample preloading at startup\n"
" --no-preload disable sample preloading at startup (default)\n"
" -s --samples-root-path set a samples root directory path\n"
" -w, --workers number of sample-reading workers (default: %u)\n"
" -h, --help display this help and exit\n"
" -v, --version output version information and exit\n",
DEFAULT_OSC_PORT, DEFAULT_CHANNELS,
#ifndef JACK
DEFAULT_SAMPLERATE,
#endif
20.0*log10(DEFAULT_GAIN/16.0),
DEFAULT_WORKERS);
return 1;
case 'p':
osc_port = optarg;
break;
case 'c':
num_channels = atoi(optarg);
if (num_channels < MIN_CHANNELS || num_channels > MAX_CHANNELS) {
fprintf(stderr, "invalid number of channels: %u (min: %u, max: %u). resetting to default\n", num_channels, MIN_CHANNELS, MAX_CHANNELS);
num_channels = DEFAULT_CHANNELS;
}
g_num_channels = num_channels;
break;
case 'r':
samplerate = atoi(optarg);
if (samplerate < MIN_SAMPLERATE || samplerate > MAX_SAMPLERATE) {
fprintf(stderr, "invalid number of channels: %u (min: %u, max: %u). resetting to default\n", samplerate, MIN_SAMPLERATE, MAX_SAMPLERATE);
samplerate = DEFAULT_SAMPLERATE;
}
g_samplerate = samplerate;
break;
case 's':
sampleroot = optarg;
break;
case 'w':
num_workers = atoi(optarg);
if (num_workers < 1) {
fprintf(stderr, "invalid number of workers: %u. resetting to default\n", num_workers);
num_workers = DEFAULT_WORKERS;
}
break;
case 'g':
gain = atof(optarg);
gain = (gain > 40)? 40 : gain;
gain = (gain < -40)?(-40): gain;
g_gain = 16.0 * pow(10.0, gain/20.0);
break;
case '?':
/* getopt_long will have already printed an error */
break;
default:
return 1;
}
}
fprintf(stderr, "port: %s\n", osc_port);
fprintf(stderr, "channels: %u\n", g_num_channels);
fprintf(stderr, "samplerate: %u\n", g_samplerate);
fprintf(stderr, "gain (dB): %f\n", gain);
fprintf(stderr, "gain factor: %f\n", g_gain);
if (!dirty_compressor_flag) {
fprintf(stderr, "dirty compressor disabled\n");
}
if (shape_gain_comp_flag) {
fprintf(stderr, "distortion gain compensation enabled\n");
}
#ifdef JACK
if (!jack_auto_connect_flag) {
fprintf(stderr, "port auto-connection disabled\n");
}
#endif
if (!late_trigger_flag) {
fprintf(stderr, "late trigger disabled\n");
}
if (preload_flag) {
fprintf(stderr, "sample preloading enabled\n");
}
fprintf(stderr, "workers: %u\n", num_workers);
fprintf(stderr, "init audio\n");
#ifdef JACK
audio_init(dirty_compressor_flag, jack_auto_connect_flag, late_trigger_flag, num_workers, sampleroot, shape_gain_comp_flag, preload_flag);
#else
audio_init(dirty_compressor_flag, true, late_trigger_flag, num_workers, sampleroot, shape_gain_comp_flag, preload_flag);
#endif
fprintf(stderr, "init open sound control\n");
server_init(osc_port);
sleep(-1);
return(0);
}