forked from borsboom/vocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
289 lines (257 loc) · 7.81 KB
/
main.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/******************************************************************************
* $Id: main.c,v 1.9 2002/09/20 02:24:14 emanuel Exp $
* Copyright (C) 1996-1999,2002 Emanuel Borsboom <[email protected]>
* Permission is granted to make any use of this code subject to the condition
* that all copies contain this notice and an indication of what has been
* changed.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <limits.h>
#include "error.h"
#include "vocode.h"
#include "fft.h"
#define COPYRIGHT \
"Zerius Vocoder 1.3 Copyright (C) 1996-1999, 2002 Emanuel Borsboom\n" \
"email: [email protected] / web: http://www.nuel.ca/Vocoder\n"
#define DEFAULT_WINDOW_TIME 15 /* 1/15th of a second */
char *prog_name;
static VBOOL got_window_length, got_window_overlap;
static VBOOL quiet;
static void usage()
{
printf("%s\n", COPYRIGHT);
printf("usage: %s [-q] [-N] [-b <band-count>] [-w <window-length] "
"[-o <window-overlap>] [-v <volume>] "
"<modulator-file> <carrier-file> <output-file>\n", prog_name);
exit(1);
}
static void prompt_user(const char *prompt, char *buffer, size_t length)
{
printf("%s? ", prompt);
fgets(buffer, length, stdin);
}
static void message_user(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
static void alert_user(const char *fmt, ...)
{
va_list ap;
printf("! ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
static void ask_user(const char *prompt, char *buffer, size_t length,
VBOOL require_non_empty, VBOOL require_number)
{
char *p;
VBOOL okay;
do
{
prompt_user(prompt, buffer, length);
okay = TRUE;
if (require_non_empty)
{
okay = FALSE;
p = buffer;
while (*p && *p != '\n')
if (!isspace(*p++))
{
okay = TRUE;
break;
}
if (!okay) alert_user("You must enter a value.");
}
if (require_number)
{
p = buffer;
while (*p && *p != '\n')
{
if (!isdigit(*p) && *p != '.')
{
alert_user("You may only enter digits.");
okay = FALSE;
break;
}
p++;
}
}
}
while (!okay);
p = strchr(buffer, '\n');
if (p) *p = '\0';
}
static void ask_user_filename(const char *prompt, char *buffer, size_t length,
VBOOL require_existance)
{
ask_user(prompt, buffer, length, TRUE, FALSE);
if (require_existance)
{
FILE *fp;
fp = fopen(buffer, "rb");
while (fp == NULL)
{
alert_user("Cannot open %s: %s", buffer, strerror(errno));
ask_user(prompt, buffer, length, TRUE, FALSE);
fp = fopen(buffer, "rb");
}
fclose(fp);
}
}
static void parse_args(int argc, char *argv[])
{
int i;
prog_name = argv[0];
got_window_length = FALSE;
got_window_overlap = FALSE;
quiet = FALSE;
vocode_normalize = TRUE;
vocode_volume = 1.0;
vocode_band_count = 16;
if (argc <= 1)
{
char buf[16];
vocode_modulator_filename = error_malloc(PATH_MAX);
vocode_carrier_filename = error_malloc(PATH_MAX);
vocode_output_filename = error_malloc(PATH_MAX);
ask_user_filename("Modulator filename (required)", vocode_modulator_filename,
PATH_MAX, TRUE);
ask_user_filename("Carrier filename (required)", vocode_carrier_filename,
PATH_MAX, TRUE);
ask_user("Window length (empty for default)", buf, sizeof(buf),
FALSE, TRUE);
if (buf[0])
{
vocode_window_length = atoi(buf);
got_window_length = TRUE;
}
ask_user("Window overlap (empty for default)", buf, sizeof(buf),
FALSE, TRUE);
if (buf[0])
{
vocode_window_overlap = atoi(buf);
got_window_overlap = TRUE;
}
ask_user("Band count (empty for default)", buf, sizeof(buf),
FALSE, TRUE);
if (buf[0]) vocode_band_count = atoi(buf);
ask_user("Output volume (empty for default)", buf, sizeof(buf),
FALSE, TRUE);
if (buf[0]) vocode_volume = atof(buf);
ask_user_filename("Output filename (required)", vocode_output_filename,
PATH_MAX, FALSE);
}
else if (argc == 7 &&
argv[1][0] != '-' && argv[2][0] != '-' &&
atoi(argv[3]) != 0 && atoi(argv[4]) != 0 && atoi(argv[5]) != 0 &&
argv[6][0] != '-')
{
vocode_modulator_filename = argv[1];
vocode_carrier_filename = argv[2];
vocode_window_length = atoi(argv[3]);
got_window_length = TRUE;
vocode_window_overlap = atoi(argv[4]);
got_window_overlap = TRUE;
vocode_band_count = atoi(argv[5]);
vocode_output_filename = argv[6];
}
else
{
for (i = 1; i < argc && argv[i][0] == '-'; ++i)
{
if (argv[i][1] == 'v' && i < argc - 1)
vocode_volume = atof(argv[++i]);
else if (argv[i][1] == 'b' && i < argc - 1)
vocode_band_count = atoi(argv[++i]);
else if (argv[i][1] == 'q')
quiet = TRUE;
else if (argv[i][1] == 'N')
vocode_normalize = FALSE;
else if (argv[i][1] == 'w' && i < argc - 1) {
vocode_window_length = atoi(argv[++i]);
got_window_length = TRUE;
}
else if (argv[i][1] == 'o' && i < argc - 1) {
vocode_window_overlap = atoi(argv[++i]);
got_window_overlap = TRUE;
}
else
usage();
}
if (argc != i + 3)
usage();
vocode_modulator_filename = argv[i++];
vocode_carrier_filename = argv[i++];
vocode_output_filename = argv[i++];
}
}
static void check_args(void)
{
if (!got_window_length)
vocode_window_length = ipow(2, ilog2(vocode_modulator_rate / DEFAULT_WINDOW_TIME));
if (!got_window_overlap)
vocode_window_overlap = vocode_window_length / 2;
if (vocode_window_length < 2 || (size_t)ipow(2, ilog2(vocode_window_length)) != vocode_window_length)
error_display("window-length must be > 1 and a power of two\n"
"(the closest power of two to the number you entered is %d)",
ipow(2, ilog2(vocode_window_length)));
if (vocode_window_overlap < 0 || (size_t)vocode_window_overlap > vocode_window_length / 2)
error_display("window-overlap must be >= 0 and <= window-length/2");
if (vocode_band_count < 1 || (size_t)vocode_band_count > vocode_window_length / 2)
error_display("band-count must be > 0 and <= window-length/2");
if (!quiet)
message_user("%s\nwindow-length: %d window-overlap: %d band-count: %d "
"volume: %.2f", COPYRIGHT,
vocode_window_length, vocode_window_overlap, vocode_band_count, vocode_volume);
}
static VINT _num_frames;
static void start_status(VINT num_frames)
{
_num_frames = num_frames;
}
static int update_status(VINT frame_no) {
int i, count = (frame_no + 1) * 56 / _num_frames;
if (quiet) return 0;
printf("\r%3d%% |", (int)((frame_no + 1) * 100 / _num_frames));
for (i = 0; i < count; ++i)
putchar('*');
for (; i < 56; ++i)
putchar(' ');
printf("| %ld/%ld", frame_no + 1, _num_frames);
fflush(stdout);
return 0; /* Return true to quit */
}
static void finish_status(void) {
if (!quiet)
printf("\n");
}
static void display_error(char *msg)
{
fprintf(stderr, "%s: %s\n", prog_name, msg);
exit(1);
}
int main(int argc, char *argv[])
{
vocode_start_status_cb = start_status;
vocode_update_status_cb = update_status;
vocode_finish_status_cb = finish_status;
error_display_cb = display_error;
parse_args(argc, argv);
vocode_open_files();
check_args();
vocode();
vocode_cleanup();
return 0;
}