-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-wav-file-output.c
366 lines (305 loc) · 9.09 KB
/
be-wav-file-output.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "builtin-effect.h"
#include "common.h"
#include "common-string.h"
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "wav.h"
static char *inputNames[] = { "filename" };
static int nInputs = 1;
static int nOutputs = 0;
//static char *sinput_names[] = { "tag" };
static int inputType[] = { INPUT_FILE };
static int iHasMin[] = { FALSE };
static int iHasMax[] = { FALSE };
static float iDefault[] = { 1.0f };
static int iIsLog[] = { TRUE, }; //FIXME ??
static int iUsesSampleRate[] = { FALSE };
static int nInputBuffers = 2;
static int nOutputBuffers = 2;
typedef struct data_s data_t;
struct data_s {
float *input_buffer_left;
float *input_buffer_right;
float *output_buffer_left;
float *output_buffer_right;
char *filename;
char currentFilename[MAX_CHARS];
char parsedFilename[MAX_CHARS];
unsigned long sampleRate;
FILE *f;
};
static void *instantiate (unsigned long sampleRate)
{
data_t *r;
r = calloc(1, sizeof(data_t));
if (!r) {
xerror();
}
r->sampleRate = sampleRate;
return r;
}
static void cleanup (void *instance)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (ins->f) {
fclose(ins->f);
}
free(ins);
}
static void activate (void *instance)
{
data_t *ins = (data_t *)instance;
//char buffer[MAX_CHARS];
unsigned char buf[WAV_HEADER_LEN];
int size = 0x7fffffff; /* Use a bogus size initially */
if (!ins) {
xerror();
}
if (!ins->filename) {
//xerror();
return;
}
if (!*ins->filename) {
return;
}
strncpy(ins->currentFilename, ins->filename, MAX_CHARS);
parse_filename(ins->filename, ins->parsedFilename, MAX_CHARS);
printf("parsed filename: %s\n", ins->parsedFilename);
ins->f = fopen(ins->parsedFilename, "w");
if (!ins->f) {
printf("warning couldn't open file '%s'\n", ins->parsedFilename);
return;
}
//FIXME write wav header
/* Store information */
//internal->wave.common.wChannels = format->channels;
//internal->wave.common.wBitsPerSample = format->bits;
//internal->wave.common.dwSamplesPerSec = format->rate;
memset(buf, 0, WAV_HEADER_LEN);
/* Fill out our wav-header with some information. */
#if 0
strncpy(internal->wave.riff.id, "RIFF",4);
internal->wave.riff.len = size - 8;
strncpy(internal->wave.riff.wave_id, "WAVE",4);
strncpy(internal->wave.format.id, "fmt ",4);
internal->wave.format.len = 16;
internal->wave.common.wFormatTag = WAVE_FORMAT_PCM;
internal->wave.common.dwAvgBytesPerSec =
internal->wave.common.wChannels *
internal->wave.common.dwSamplesPerSec *
(internal->wave.common.wBitsPerSample >> 3);
internal->wave.common.wBlockAlign =
internal->wave.common.wChannels *
(internal->wave.common.wBitsPerSample >> 3);
strncpy(internal->wave.data.id, "data",4);
internal->wave.data.len = size - 44;
#endif
strncpy((char *)buf, "RIFF", 4);
WRITE_U32(buf + 4, (size - 8));
strncpy((char *)(buf + 8), "WAVE", 4);
strncpy((char *)(buf + 12), "fmt ", 4);
WRITE_U32(buf+16, 16); // format len
WRITE_U16(buf+20, WAVE_FORMAT_PCM);
WRITE_U16(buf+22, 2); // wChannels
WRITE_U32(buf+24, ins->sampleRate);
WRITE_U32(buf+28, (2 * ins->sampleRate * (16 >> 3))); //internal->wave.common.dwAvgBytesPerSec);
WRITE_U16(buf+32, (2 * (16 >> 3))); //internal->wave.common.wBlockAlign);
WRITE_U16(buf+34, 16); // bits per sample
strncpy((char *)(buf + 36), "data", 4); //internal->wave.data.id, 4);
WRITE_U32(buf+40, (size - 44)); //internal->wave.data.len);
#if 0
strncpy(buf, internal->wave.riff.id, 4);
WRITE_U32(buf+4, internal->wave.riff.len);
strncpy(buf+8, internal->wave.riff.wave_id, 4);
strncpy(buf+12, internal->wave.format.id,4);
WRITE_U32(buf+16, internal->wave.format.len);
WRITE_U16(buf+20, internal->wave.common.wFormatTag);
WRITE_U16(buf+22, internal->wave.common.wChannels);
WRITE_U32(buf+24, internal->wave.common.dwSamplesPerSec);
WRITE_U32(buf+28, internal->wave.common.dwAvgBytesPerSec);
WRITE_U16(buf+32, internal->wave.common.wBlockAlign);
WRITE_U16(buf+34, internal->wave.common.wBitsPerSample);
strncpy(buf+36, internal->wave.data.id, 4);
WRITE_U32(buf+40, internal->wave.data.len);
#endif
if (fwrite(buf, sizeof(char), WAV_HEADER_LEN, ins->f) != WAV_HEADER_LEN) {
xerror();
}
}
static void deactivate (void *instance)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (ins->f) {
fclose(ins->f);
ins->f = NULL;
}
}
static void connect_input_buffer (void *instance, int num, float *buffer)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!buffer) {
xerror();
}
if (num > 2) {
xerror();
}
if (num == 0) {
ins->input_buffer_left = buffer;
} else {
ins->input_buffer_right = buffer;
}
}
static void connect_output_buffer (void *instance, int num, float *buffer)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!buffer) {
xerror();
}
if (num > 2) {
xerror();
}
if (num == 0) {
ins->output_buffer_left = buffer;
} else {
ins->output_buffer_right = buffer;
}
}
static void connect_input (void *instance, int num, void *input)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!input) {
xerror();
}
if (num >= nInputs) {
xerror();
}
ins->filename = (char *)input;
}
static void run (void *instance, unsigned long sampleCount)
{
data_t *ins = (data_t *)instance;
int i;
//short int buffer[MAX_CHARS * 4]; //FIXME
//unsigned long sz;
//size_t r;
if (!ins) {
xerror();
}
if (!ins->output_buffer_left) {
xerror();
}
if (!ins->output_buffer_right) {
xerror();
}
if (!ins->input_buffer_left) {
xerror();
}
if (!ins->input_buffer_right) {
xerror();
}
if (!str_matches(ins->currentFilename, ins->filename)) {
printf("wavfileout new file: %s\n", ins->filename);
deactivate(ins);
activate(ins);
}
if (!ins->f) {
return;
}
#if 0
r = fread(buffer, 1, sampleCount * 4, ins->f);
if (r < 0) {
xerror();
}
if (r < 4 * (int)sampleCount) {
printf("looping again %d %ld\n", r, sampleCount);
fseek(ins->f, 0, SEEK_SET);
//FIXME use the data we got
return;
}
for (i = 0; i < sampleCount; i++) {
ins->output_buffer_left[i] = ((float)(buffer[i * 2]) * (1.0f / 32767.5f));
ins->output_buffer_right[i] = ((float)(buffer[i * 2 + 1]) * (1.0f / 32767.5f));
}
#if 0
memcpy(ins->output_buffer_left, ins->input_buffer_left, sampleCount * sizeof(float));
for (i = 0; i < sampleCount; i++) {
if (ins->input_buffer_left[i] > *ins->threshold ||
ins->input_buffer_left[i] < -(*ins->threshold)
) {
if (time(NULL) - sec > 1) {
//FIXME don't print too much
//printf("[%s] clip %f\n", tag, data[i]);
printf("[%s] clip %f\n", ins->tag, ins->input_buffer_left[i]);
sec = time(NULL);
}
}
}
if (ins->input_buffer_right) {
if (!ins->output_buffer_right) {
xerror();
}
memcpy(ins->output_buffer_right, ins->input_buffer_right, sampleCount * sizeof(float));
}
#endif
#endif
for (i = 0; i < sampleCount; i++) {
signed short int data;
data = (signed short int)(ins->input_buffer_left[i] * 32767.5f);
fwrite(&data, sizeof(signed short int), 1, ins->f);
data = (signed short int)(ins->input_buffer_left[i] * 32767.5f);
fwrite(&data, sizeof(signed short int), 1, ins->f);
}
if (ins->input_buffer_left) {
if (!ins->output_buffer_left) {
xerror();
}
memcpy(ins->output_buffer_left, ins->input_buffer_left, sampleCount * sizeof(float));
}
if (ins->input_buffer_right) {
if (!ins->output_buffer_right) {
xerror();
}
memcpy(ins->output_buffer_right, ins->input_buffer_right, sampleCount * sizeof(float));
}
}
void be_wav_file_output_init (builtin_effect_t *be)
{
be->label = "wavfileout";
be->description = "write data to wav file";
be->nInputs = nInputs;
be->inputNames = inputNames;
be->inputType = inputType;
be->iHasMin = iHasMin;
be->iHasMax = iHasMax;
be->iDefault = iDefault;
be->iIsLog = iIsLog;
be->iUsesSampleRate = iUsesSampleRate;
be->nOutputs = nOutputs;
be->nInputBuffers = nInputBuffers;
be->nOutputBuffers = nOutputBuffers;
be->instantiate = instantiate;
be->cleanup = cleanup;
be->activate = activate;
be->deactivate = deactivate;
be->connect_input_buffer = connect_input_buffer;
be->connect_output_buffer = connect_output_buffer;
be->connect_input = connect_input;
be->run = run;
}