-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-ogg-file-output.c
398 lines (334 loc) · 10.7 KB
/
be-ogg-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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "builtin-effect.h"
#include "common.h"
#include "common-string.h"
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <vorbis/vorbisenc.h>
//FIXME only stereo in/out right now
// from encoder_example.c from oggenc package
static char *inputNames[] = { "quality", "filename" };
static int nInputs = 2;
static int nOutputs = 0;
static int inputType[] = { INPUT_FLOAT, INPUT_FILE };
static int iHasMin[] = { TRUE, FALSE };
static int iHasMax[] = { TRUE, FALSE };
static float iMin[] = { 0.0f, 0.0f };
static float iMax[] = { 1.0f, 0.0f };
static float iDefault[] = { 0.5f, 0.0f };
static int iIsLog[] = { FALSE, FALSE }; //FIXME ??
static int iUsesSampleRate[] = { FALSE, 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;
float *quality;
char currentFilename[MAX_CHARS];
char parsedFilename[MAX_CHARS];
unsigned long sampleRate;
ogg_stream_state os; /* take physical pages, weld into a logical
stream of packets */
ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
ogg_packet op; /* one raw packet of data for decode */
vorbis_info vi; /* struct that stores all the static vorbis bitstream
settings */
vorbis_comment vc; /* struct that stores all the user comments */
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
vorbis_block vb; /* local working space for packet->PCM decode */
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) {
//FIXME all the other shit
//ogg_write(NULL, 0);
fclose(ins->f);
}
free(ins);
}
static void activate (void *instance)
{
data_t *ins = (data_t *)instance;
int ret;
int eos = 0;
char dateString[MAX_CHARS];
char titleString[MAX_CHARS];
struct timeval tv;
struct tm *tp;
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("%s:%s warning couldn't open file '%s'\n", __FILE__, __func__, ins->parsedFilename);
return;
}
// here we go
vorbis_info_init(&ins->vi);
ret = vorbis_encode_init_vbr(&ins->vi, 2, ins->sampleRate, *ins->quality);
if (ret) {
//FIXME
xerror();
}
vorbis_comment_init(&ins->vc);
vorbis_comment_add_tag(&ins->vc, "ENCODER", "gtrfx");
//FIXME way to pass in real name
vorbis_comment_add_tag(&ins->vc, "ARTIST", getenv("USER"));
vorbis_comment_add_tag(&ins->vc, "COMMENT", ins->parsedFilename);
gettimeofday(&tv, NULL);
tp = localtime(&tv.tv_sec);
snprintf(dateString, MAX_CHARS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
vorbis_comment_add_tag(&ins->vc, "DATE", dateString);
snprintf(titleString, MAX_CHARS, "%s gtrfx %04d-%02d-%02d-%02d:%02d:%02d", getenv("USER"), 1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
vorbis_comment_add_tag(&ins->vc, "TITLE", titleString);
/* set up the analysis state and auxiliary encoding storage */
vorbis_analysis_init(&ins->vd, &ins->vi);
vorbis_block_init(&ins->vd, &ins->vb);
/* set up our packet->stream encoder */
/* pick a random serial number; that way we can more likely build
chained streams just by concatenation */
srand(time(NULL));
ogg_stream_init(&ins->os, rand());
/* Vorbis streams begin with three headers; the initial header (with
most of the codec setup parameters) which is mandated by the Ogg
bitstream spec. The second header holds any comment fields. The
third header holds the bitstream codebook. We merely need to
make the headers, then pass them to libvorbis one at a time;
libvorbis handles the additional Ogg bitstream constraints */
{
ogg_packet header;
ogg_packet header_comm;
ogg_packet header_code;
vorbis_analysis_headerout(&ins->vd, &ins->vc, &header, &header_comm, &header_code);
ogg_stream_packetin(&ins->os, &header); /* automatically placed in its own page */
ogg_stream_packetin(&ins->os, &header_comm);
ogg_stream_packetin(&ins->os, &header_code);
/* This ensures the actual
* audio data will start on a new page, as per spec
*/
while (!eos) {
int result = ogg_stream_flush(&ins->os,&ins->og);
if (result == 0)
break;
//fwrite(ins->og.header, 1, ins->og.header_len, stdout);
//fwrite(ins->og.body, 1, ins->og.body_len, stdout);
fwrite(ins->og.header, 1, ins->og.header_len, ins->f);
fwrite(ins->og.body, 1, ins->og.body_len, ins->f);
}
}
}
static void deactivate (void *instance)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (ins->f) {
//FIXME bunch of shit
//ogg_write(NULL, 0);
//vorbis_analysis_wrote(&vd,0); // signaling all done
//ogg_stream_clear(&vb);
//vorbis_clock_clear(&vb);
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();
}
switch(num) {
case 0:
ins->quality = (float *)input;
break;
case 1:
ins->filename = (char *)input;
break;
default:
// shouldn't get here
break;
}
}
static void run (void *instance, unsigned long sampleCount)
{
data_t *ins = (data_t *)instance;
int i;
float **buffer;
int eos = 0;
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 (sampleCount == 0) {
printf("%s:%s FIXME sampleCount is zero, decoder will shut down\n", __FILE__, __func__);
xerror();
}
if (!str_matches(ins->currentFilename, ins->filename)) {
printf("oggfileout new file: %s\n", ins->filename);
printf("current: %s\n", ins->currentFilename);
deactivate(ins);
activate(ins);
}
if (!ins->f) {
return;
}
//FIXME zero write indicating no more data
//buffer = vorbis_analysis_buffer(&ins->vd, READ);
buffer = vorbis_analysis_buffer(&ins->vd, sampleCount);
for (i = 0; i < sampleCount; i++) {
buffer[0][i] = ins->input_buffer_left[i];
buffer[1][i] = ins->input_buffer_right[i];
}
/* tell the library how much we actually submitted */
vorbis_analysis_wrote(&ins->vd, sampleCount);
/* vorbis does some data preanalysis, then divvies up blocks for
more involved (potentially parallel) processing. Get a single
block for encoding now */
while (vorbis_analysis_blockout(&ins->vd, &ins->vb) == 1) {
/* analysis, assume we want to use bitrate management */
vorbis_analysis(&ins->vb, NULL);
vorbis_bitrate_addblock(&ins->vb);
while (vorbis_bitrate_flushpacket(&ins->vd, &ins->op)) {
/* weld the packet into the bitstream */
ogg_stream_packetin(&ins->os, &ins->op);
/* write out pages (if any) */
while (!eos) {
int result = ogg_stream_pageout(&ins->os, &ins->og);
if (result == 0)
break;
fwrite(ins->og.header, 1, ins->og.header_len, ins->f);
fwrite(ins->og.body, 1, ins->og.body_len, ins->f);
/* this could be set above, but for illustrative purposes, I do
it here (to show that vorbis does know where the stream ends) */
if (ogg_page_eos(&ins->og))
eos = 1;
}
}
}
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_ogg_file_output_init (builtin_effect_t *be)
{
be->label = "oggfileout";
be->description = "write data to ogg file";
be->nInputs = nInputs;
be->inputNames = inputNames;
be->inputType = inputType;
be->iHasMin = iHasMin;
be->iHasMax = iHasMax;
be->iMin = iMin;
be->iMax = iMax;
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;
}