-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWriter.cpp
643 lines (544 loc) · 19.1 KB
/
FileWriter.cpp
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#include <cstdlib>
#include <cstring>
#include <chrono>
#include <math.h>
#include "FileWriter.h"
std::thread FileWriter::fileWriteThread ;
int FileWriter::buffer_write_index = 0 ;
LockFreeQueue<buffer_t*, LOCK_FREE_SIZE> FileWriter::lockFreeQueue;
void * FileWriter::mp3_buffer = NULL;
bool FileWriter:: useStaticBuffer = false ;
staticBuffer_t FileWriter::buffers [1025] ;
/* the reason this has to be less than buffer size is that arrays start at zero
* why this worked at all on some devices amazes me
*/
int FileWriter::MAX_STATIC_BUFFER = 1024;
long FileWriter::disk_writes = 0 ;
long FileWriter::processed = 0 ;
int FileWriter::bufferUsed = 0;
int FileWriter::unreported_overruns = 0 ;
int FileWriter::total_overruns = 0;
# ifdef __linux__
SNDFILE * FileWriter::soundfile = NULL;
# endif
buffer_t *FileWriter::current_buffer;
buffer_t *FileWriter::bg_buffer;
int FileWriter::num_channels = 1 ;
int FileWriter::block_size = 384 ;
float FileWriter::min_buffer_time = 0.01f,
FileWriter::max_buffer_time = 40.0f ;
int FileWriter::jack_samplerate ;
int FileWriter::buffer_size_in_bytes;
bool FileWriter::ready = false ;
vringbuffer_t * FileWriter::vringbuffer = NULL;
FileType FileWriter::fileType = MP3 ;
OpusEncoder *FileWriter::encoder ;
opus_int16 FileWriter::opusIn[960 * 2];
int FileWriter::opusRead = 0;
unsigned char FileWriter::opusOut[MAX_PACKET_SIZE];
FILE * FileWriter::outputFile = NULL;
OggOpusEnc * FileWriter:: oggOpusEnc = NULL;
OggOpusComments *FileWriter::comments = NULL;
lame_t FileWriter::lame = NULL ;
static char * extensions [] = {
".wav",
".ogg",
".mp3"
} ;
int FileWriter::autoincrease_callback(vringbuffer_t *vrb, bool first_call, int reading_size, int writing_size) {
// HERE
// LOGD("first call: %d, reading size: %d, writing size: %d", first_call, reading_size, writing_size);
if(buffers_to_seconds(writing_size) < min_buffer_time) {
// LOGD("return 2");
return 2; // autoincrease_callback is called approx. at every block. So it should not be necessary to return a value higher than 2. Returning a very low number might also theoretically put a lower constant strain on the memory bus, thus theoretically lower the chance of xruns.
}
return 0 ;
}
int64_t FileWriter::seconds_to_frames(float seconds){
return (int64_t) (((long double)seconds)*((long double)jack_samplerate));
}
float FileWriter::frames_to_seconds(int frames){
return ((float)frames)/jack_samplerate;
}
// round up.
int FileWriter::seconds_to_blocks(float seconds){
return (int)ceilf((seconds*jack_samplerate/(float)block_size));
}
void* FileWriter:: my_calloc(size_t size1,size_t size2){
size_t size = size1*size2;
void* ret = malloc(size);
if(ret==NULL){
fprintf(stderr,"\nOut of memory. Try a smaller buffer.\n");
return NULL; }
memset(ret,0,size);
return ret;
}
// same return value as seconds_to_blocks
int FileWriter::seconds_to_buffers(float seconds){
return seconds_to_blocks(seconds);
}
FileWriter::~FileWriter () {
free (empty_buffer);
// vringbuffer_delete(vringbuffer);
}
FileWriter::FileWriter () {
IN
ready = false ;
OUT
}
void FileWriter::openFile () {
IN
LOGD("opening file [%s] using sample rate: [%d]\tchannels: [%d]", filename.c_str(), jack_samplerate, num_channels);
memset (&opusIn, 0, 960*2);
memset (&opusOut, 0, 3*1276);
# ifdef __linux__
memset(&sf_info,0,sizeof(SF_INFO));
sf_info.channels = num_channels ;
// why the below?
// sf_info.samplerate = jack_samplerate * num_channels ;
sf_info.samplerate = jack_samplerate ;
sf_info.format = SF_FORMAT_WAV ;
sf_info.format |= SF_FORMAT_FLOAT ;
if(sf_format_check(&sf_info)==0){
LOGE ("\nFileformat not supported by libsndfile. Try other options.\n");
return ;
}
# endif
if (fileType == WAV) {
# ifdef __linux__
LOGD("[%s] %d %d %d", __PRETTY_FUNCTION__, sf_info.samplerate, sf_info.channels,
sf_info.format);
FileWriter::soundfile = sf_open(filename.c_str(), SFM_WRITE, &sf_info);
if (soundfile == NULL) { // || ai==10){
HERE
LOGF ("\nCan not open sndfile \"%s\" for output (%s)\n", filename.c_str(),
sf_strerror(NULL));
} else {
LOGD("[%s] Opened file %s", __PRETTY_FUNCTION__, filename.c_str());
// sf_command (soundfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
}
# endif
}
else if (fileType == OPUS) {
int err;
encoder = opus_encoder_create(jack_samplerate, num_channels, OPUS_APPLICATION_AUDIO, &err);
if (err<0) {
HERE LOGF("failed to create an encoder: %s\n", opus_strerror(err));
}
err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(bitRate));
if (err<0) {
LOGF("failed to set bitrate: %s\n", opus_strerror(err));
}
// outputFile = fopen(filename.c_str(), "wb");
outputFile = NULL ;
int error = 0 ;
LOGD("going to create opus encoder with filename: %s", filename.c_str());
# ifdef __linux__
comments = ope_comments_create() ;
ope_comments_add(comments, "TITLE", "AmpRack Demo");
oggOpusEnc = ope_encoder_create_file(filename.c_str(), comments, jack_samplerate, num_channels, 0, &error) ;
if (error != NULL) {
HERE LOGF("cannot create encoder: %s", ope_strerror(error));
}
err = ope_encoder_ctl(oggOpusEnc, OPUS_SET_BITRATE(bitRate));
# endif
} else if (fileType == MP3) {
LOGD("init lame encoder");
lame = lame_init();
lame_set_in_samplerate(lame, jack_samplerate);
lame_set_VBR(lame, vbr_default);
lame_set_out_samplerate(lame, jack_samplerate);
lame_set_num_channels(lame, num_channels);
/*
lame_set_brate(lame, 64);
*/
lame_set_preset(lame, MEDIUM);
int retval = lame_init_params(lame);
if (retval < 0) {
HERE LOGF("Unable to intialize lame parameters: %d", retval);
}
outputFile = fopen(filename.c_str(), "wb");
mp3_buffer = malloc ((block_size * 1.25) + 7200);
}
OUT
}
void FileWriter::setChannels (int channels) {
num_channels = channels ;
}
void FileWriter::closeFile () {
IN
if (fileType == MP3) {
LOGD ("[mp3] free buffer\n");
unsigned char *mp3buf ;
mp3buf = (unsigned char *) malloc (8192*3);
lame_encode_flush(lame, mp3buf, 8192*3);
fwrite (mp3buf, 8192*3, sizeof (unsigned char), outputFile);
free (mp3buf);
lame_close(lame) ;
fclose(outputFile);
outputFile = NULL;
lame = NULL ;
free(mp3_buffer);
}
else if (fileType == WAV && outputFile) {
LOGD ("[wav] free buffer\n");
fclose(outputFile);
outputFile = NULL;
}
else if (fileType == OPUS ) {
LOGD ("[opus] free buffer\n");
if (oggOpusEnc) {
# ifdef __linux__
ope_encoder_drain(oggOpusEnc);
ope_encoder_destroy(oggOpusEnc);
ope_comments_destroy(comments);
oggOpusEnc = NULL ;
comments = NULL ;
# endif
}
}
# ifdef __linux__
if (soundfile) {
LOGD ("[sndfile] free buffer\n");
sf_close(soundfile);
soundfile = NULL;
}
# endif
OUT
}
int FileWriter::disk_write_callback(float *arg,size_t nframes) {
if (bufferUsed < MAX_STATIC_BUFFER) {
for (int i = 0; i < nframes; i++) {
buffers[bufferUsed].data [i] = arg[i];
}
buffers[bufferUsed].pos = nframes;
bufferUsed ++ ;
return 0;
} else {
// disk_write(arg, nframes);
bufferUsed = 0;
}
return 0 ;
}
int FileWriter::disk_write(AudioBuffer * buffer) {
float * data = buffer -> data ;
int frames = buffer -> pos ;
/*
LOGD("----------| %d |-----------", std::chrono::system_clock::now()) ;
for (int i = 0 ; i < frames ; i ++) {
LOGD("%f\t", data [i]) ;
}
return 0 ;
*/
// LOGD("[ringbuffer id] %d", getpid ());
// IN
// LOGD("ready: %d", ready);
if (! ready || frames == 0 ) {
return 0;
}
// LOGD("disk write [%d] %d frames", disk_writes, frames);
disk_writes ++ ;
if (fileType == MP3) {
int write = lame_encode_buffer_ieee_float(lame, data, NULL, frames, (unsigned char *) mp3_buffer, (block_size * 1.25) + 7200);
if (write < 0) {
LOGF("unable to encode mp3 stream: %d", write);
} else {
fwrite(mp3_buffer, write, 1, outputFile);
}
// OUT
return 0 ;
}
if (fileType== OPUS) {
# ifdef __linux__
ope_encoder_write_float(oggOpusEnc, data, frames);
#endif
// OUT
return 1;
}
if (fileType == 987) {
if (opusRead < 960) {
float * f = static_cast<float *>(data);
for (int x = 0 ; x < frames ; x ++) {
opusIn[opusRead] = (opus_int )(size_t) f ;
opusRead ++ ;
*f ++ ;
}
// OUT
return 1 ;
} else {
int nbBytes = opus_encode(encoder, opusIn, 960, opusOut, MAX_PACKET_SIZE);
if (nbBytes<0) {
LOGF("encode failed: %s\n", opus_strerror(nbBytes));
} else {
fwrite(opusOut, sizeof (unsigned char ), opusRead, outputFile) ;
opusRead = 0 ;
// OUT
return 1 ;
}
}
}
# ifdef __linux__
if((size_t)sf_writef_float(FileWriter::soundfile, data,frames) != frames){
LOGF("Error. Can not write sndfile (%s)\n",
sf_strerror(FileWriter::soundfile)
);
// OUT
return 0;
}
#endif
// OUT
return 1;
}
void FileWriter::startRecording () {
IN
openFile();
ready = true ;
// fileWriteThread = std::thread (&FileWriter::writeLoop, this);
OUT
}
void FileWriter::writeLoop () {
buffer_t *buffer ;
while (ready) {
if (lockFreeQueue.pop(buffer)) {
// disk_write(buffer);
}
// std::this_thread::sleep_for(std::chrono::microseconds (10));
}
}
void FileWriter::stopRecording () {
IN
// because we use a large buffer, there can be samples in the buffer which have not been written yet
// if (useStaticBuffer)
// vringbuffer_return_writing(vringbuffer,buffers);
// vringbuffer_stop_callbacks(vringbuffer);
ready = false ;
closeFile();
HERE
// fileWriteThread.join();
LOGD("recording stopped: %d buffer underruns", total_overruns);
//~ bg_buffer->pos = 0 ;
OUT
}
void FileWriter::setBufferSize (int bufferSize) {
IN
block_size = bufferSize ;
buffer_size_in_bytes = ALIGN_UP_DOUBLE(sizeof(buffer_t) + block_size*num_channels*sizeof(float ));
// buffer_size_in_bytes = buffer_size_in_bytes * 4 ;
LOGD("setting buffer size: %d from block size: %d", buffer_size_in_bytes, block_size);
disk_writes = 0 ;
processed = 0 ;
if (vringbuffer != NULL) {
///| @attention: this *must* be freed
free (bg_buffer->data);
free (bg_buffer) ;
free (empty_buffer);
vringbuffer_delete(vringbuffer);
vringbuffer = NULL ;
}
if (bg_buffer != nullptr) {
free (bg_buffer->data);
free (bg_buffer) ;
}
bg_buffer = static_cast<buffer_t *>(calloc(1, sizeof(buffer_t)));
// HERE LOGD("using buffer size %d", bufferSize);
bg_buffer->data = static_cast<float *>(malloc(buffer_size_in_bytes));
bg_buffer->pos = 0 ;
if (false &&vringbuffer == NULL) {
// vringbuffer = vringbuffer_create(JC_MAX(4, seconds_to_buffers(min_buffer_time)),
// JC_MAX(4, seconds_to_buffers(max_buffer_time)),
// (size_t) buffer_size_in_bytes);
// vringbuffer_set_receiver_callback(vringbuffer, disk_callback);
// vringbuffer_set_autoincrease_callback(vringbuffer,autoincrease_callback,0);
/// TODO: Free this memory!
// current_buffer = static_cast<buffer_t *>(vringbuffer_get_writing(vringbuffer));
bg_buffer = static_cast<buffer_t *>(calloc(1, sizeof(buffer_t)));
// HERE LOGD("using buffer size %d", bufferSize);
bg_buffer->data = static_cast<float *>(malloc(buffer_size_in_bytes));
bg_buffer->pos = 0 ;
// empty_buffer = static_cast<float *>(my_calloc(sizeof(float), block_size * num_channels));
}
// if(vringbuffer == NULL){
// HERE LOGF ("Unable to create ringbuffer!") ;
// OUT
// return ;
// }
// for (int i = 0 ; i < 32 ; i ++) {
// buffers[i] = static_cast<buffer_t *>(malloc(sizeof(buffer_t)));
// buffers [i] -> data = static_cast<float *>(malloc(block_size));
// }
OUT
}
void FileWriter::setSampleRate (int sampleRate) {
IN
if (sampleRate == 0) {
HERE LOGE("sample rate passed as 0!") ;
jack_samplerate = 48000 ;
} else
jack_samplerate = sampleRate ;
}
void FileWriter::setFileName (std::string name) {
IN
filename = std::string (name) + std::string (extensions [fileType]) ;
LOGD("[%s] filename set to %s", __PRETTY_FUNCTION__ , name.c_str());
OUT
}
void FileWriter::process_fill_buffer(float *in[], buffer_t *buffer, int i, int end){
float *data=buffer->data;
int pos=buffer->pos*num_channels;
buffer->pos=pos/num_channels;
}
bool FileWriter::process_new_current_buffer(int frames_left){
IN
current_buffer=(buffer_t*)vringbuffer_get_writing(vringbuffer);
// current_buffer = vringbuffer->for_writer1 ;
if(current_buffer==NULL){
total_overruns++;
unreported_overruns += frames_left;
OUT
return false;
}
current_buffer->pos=0;
OUT
return true;
}
void FileWriter::send_buffer_to_disk_thread(buffer_t *buffer){
buffer->overruns = unreported_overruns;
vringbuffer_return_writing(vringbuffer,buffer);
unreported_overruns = 0;
}
void FileWriter::process_fill_buffers(void *data, int samples){
if(current_buffer==NULL && process_new_current_buffer(samples)==false) {
LOGD("no buffer and no samples!") ;
return;
}
// fixme: why do I always take void* as argument type and cast it to float?
current_buffer->data = (float*) data ;
current_buffer->pos = samples ;
send_buffer_to_disk_thread(current_buffer);
current_buffer = NULL;
return ;
float *in[num_channels];
int i = 0 ;
while(i<samples){
int size=JC_MIN(samples - i,
block_size - current_buffer->pos
);
process_fill_buffer(in,current_buffer,i,i+size);
LOGD("%d %d %d %d", i, samples, size, current_buffer->pos) ;
i+=size;
if(current_buffer->pos == block_size){
send_buffer_to_disk_thread(current_buffer);
if(process_new_current_buffer(samples-i)==false)
return;
} else {
LOGD("pos != block size") ;
}
}
}
int FileWriter::process(int nframes, const float *arg) {
// IN
// LOGD("file writer process");
if (!ready) {
HERE LOGW("not ready, return");
// OUT
return 0 ;
}
// process_fill_buffers(arg, nframes);
// simplified the following 26-06-22
// don't know the consequences yet
// if ((buffer_t*)vringbuffer_get_writing(vringbuffer) == NULL)
// return 0;
if (useStaticBuffer) {
// LOGD("use static buffer");
if (bufferUsed < MAX_STATIC_BUFFER) {
// LOGD("buffer used: %d", bufferUsed);
// LOGD("bufferUsed: %d, nframes: %d", bufferUsed, nframes) ;
for (int i = 0; i < nframes; i++) {
buffers[bufferUsed].data [i] = arg[i];
// LOGD("buffers[bufferUsed].data [%d] = arg[%d];", i, i);
}
buffers[bufferUsed].pos = nframes;
bufferUsed ++ ;
// OUT
return 0;
} else {
// LOGD("bufferUsed = MAX_STATIC_BUFFER, vringbuffer_return_writing") ;
// vringbuffer_return_writing(vringbuffer,buffers);
bufferUsed = 0;
}
} else {
// bg_buffer->data = (float *) arg;
// LOGD("frames: %d", nframes);
// if (bg_buffer->pos >= buffer_size_in_bytes - 1)
// bg_buffer->pos = 0 ;
// if (false && buffer_write_index > 19 ) {
buffer_write_index = 0 ;
// LOGD("[realtime id] %d", gettid ());
// process_new_current_buffer(nframes);
// vringbuffer_return_writing(vringbuffer,bg_buffer);
// if (current_buffer != nullptr)
// LOGD("current buffer %d", current_buffer->pos);
bg_buffer->pos = 0 ;
// }
// process_new_current_buffer(nframes) ;
// if (current_buffer == NULL) {
// LOGE("no new buffer");
// return 0 ;
// }
// LOGD("testmest %d", current_buffer->data [0]);
// vringbuffer_return_writing(vringbuffer,current_buffer);
// while (vringbuffer_writing_size(vringbuffer)==0)
// usleep(2);
// while(vringbuffer_reading_size(vringbuffer) > 0)
// usleep(2);
// jack_ringbuffer_read(vringbuffer->for_writer2,(char*)¤t_buffer,sizeof(void*)); // Checking writer2 first since that memory is more likely to already be in the cache.
// jack_ringbuffer_read(vringbuffer->for_writer1,(char*)¤t_buffer,sizeof(void*));
for (int i = 0 ; i < nframes ; i ++) {
if (i >= block_size) {
HERE LOGF("more samples than we can handle! [%d]", nframes) ;
break ;
}
// if (arg [i] < -1.0)
// bg_buffer->data[bg_buffer->pos] = -.95 ;
// else if (arg [i] > 1.0)
// bg_buffer->data[bg_buffer->pos] = .95 ;
// else
// LOGD("%f", arg [i]);
bg_buffer->data[i] = arg [i] ;
// buffer_write_index ++ ;
// bg_buffer->pos ++ ;
// HERE
// current_buffer -> data [i] = arg [i];
// HERE
}
// current_buffer->pos = nframes;
// vringbuffer_return_writing(vringbuffer,current_buffer);
// bg_buffer->pos += nframes;
bg_buffer->pos = nframes;
// lockFreeQueue.push(bg_buffer);
// LOGD("lock free queue push [%d] %d", processed, nframes);
processed++;
// vringbuffer_return_writing(vringbuffer,bg_buffer);
// current_buffer->data = (float *) arg;
// current_buffer->pos = nframes;
// disk_write(bg_buffer->data, nframes);
}
/// does the following do ANYTHING?
// vringbuffer_trigger_autoincrease_callback(vringbuffer);
// OUT
return 0 ;
}
float FileWriter::buffers_to_seconds(int buffers){
return blocks_to_seconds(buffers);
}
float FileWriter::blocks_to_seconds(int blocks){
return (float)blocks*(float)block_size/jack_samplerate;
}
void FileWriter::setFileType (int fType) {
fileType = static_cast<FileType>(fType);
}
void FileWriter::setLamePreset (int preset) {
IN
lame_set_preset(lame, preset);
OUT
}