-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwav.cpp
503 lines (414 loc) · 11 KB
/
wav.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
//-----------------------------------------------------------------------------
// This file is part of AEO-Light
//
// Copyright (c) 2016 University of South Carolina
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// AEO-Light is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Funding for AEO-Light development was provided through a grant from the
// National Endowment for the Humanities
//-----------------------------------------------------------------------------
#include "wav.h"
#include <vector>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <assert.h>
#ifndef UINT16_MAX
#define UINT16_MAX (0xFFFF)
#endif
#ifndef INT16_MAX
#define INT16_MAX (0x7FFF)
#endif
#define UMAX(b) ((1ull<<(b))-1)
#define SMAX(b) (UMAX((b)-1))
wav::wav(unsigned int rate)
{
strncpy (chunkID,"RIFF",4);
riffSize = 0; // filesize minus 8
strncpy(typeID,"WAVE",4);
strncpy(formatChunkID,"fmt ",4);
formatChunkSize = 16;
formatTag = 1; // 1 = PCM linear quantization (uncompressed)
nChannels = 1; // mono
samplesPerSec = rate; // e.g., 48000
bwfckSize=702; /* size of extension chunk ==702 */
strncpy (bwfchunkID,"bext",4);
strncpy (Description,"Film Sound Extraction",256);
strncpy (Originator,"AEO-Light",32);
strncpy (OriginationTime,"10:50:45",8); /* ASCII : hh:mm:ss */
strncpy (OriginationDate,"2015:05:05",10);/* ASCII : yyyy:mm:dd */
strncpy(bwfchunkID,"bext",4);
Version=2;
bitsPerSample = 16;
strncpy(dataChunkID,"data",4);
numframes=0;
avg_inc=0;
//timecon = 100;
//dcrestore = new int16_t[timecon];
val_avg = 0;
buffer = NULL;
bufSize = 0;
samplesPerFrame = 2000;
TimeReferenceHigh=0x000;
TimeReferenceLow=0x0FF;
yml1 = 0.5;
xml1 = 0.5;
ymr1 = 0.5;
xr=0.5;
yr=0.50;
xl=0.5;
yl=0.5;
xmr1 = 0.5;
}
wav::~wav()
{
// a wav file opened for reading (sythetic image) will have the
// sound signal in this->buffer.
if(buffer) delete [] buffer;
}
FILE *wav::open(const char *fn)
{
// update the header regarding the size of the data:
//audiofilename =new std::string(fn);
//temp_audiofilename =new std::string( );
//*temp_audiofilename = *audiofilename + ".tmp";
xl = 0.5;
xr = 0.5;
bytesPerSec = samplesPerSec * bitsPerSample * nChannels;
blockAlign = bitsPerSample * nChannels / 8;
dataChunkSize = 0;
numframes = 0;
audio_file = fopen(fn, "wb+");
if(audio_file==NULL)
{
//std::cerr << "Cannot open " << fn << " for writing\n";
return NULL;
}
fwrite(this, 44, 1, audio_file);
return audio_file;
}
void wav::writebuffer(float ** audioframe,int samples)
{
double vl,vr;
int32_t vall;
int32_t valr;
int b;
numframes = samples/samplesPerFrame;
for(int i =0; i<samples; i++)
{
xl = (audioframe[0][i]) ;
if (nChannels ==2)
xr = (audioframe[1][i]) ;
else
xr = xl;
// rescale to unsigned int
vall=int32_t(xl*(UMAX(bitsPerSample)));
valr=int32_t(xr*(UMAX(bitsPerSample)));
for(b=bitsPerSample; b>0; vall >>= 8, b-=8)
fputc(vall & 0xFF, audio_file);
if (nChannels ==2)
for(b=bitsPerSample; b>0; valr >>= 8, b-=8)
fputc(valr & 0xFF, audio_file);
}
}
void wav::set_timecode(unsigned int seconds,unsigned int frames)
{
unsigned long timesamples = seconds * (samplesPerSec);
timesamples+= frames*samplesPerFrame;
TimeReferenceLow=timesamples;
TimeReferenceHigh=timesamples>>32;
}
void wav::writeframe(float * audioframe,bool dcbias)
{
#define S(x) ((x)*2.0 - 1.0)
#define U(x) (((x)+1.0) / 2.0)
double vl,vr;
int32_t vall;
int32_t valr;
numframes++;
int16_t oval;
long avg=0;
int b;
LPF_Beta = 0.8;
alpha = 0.98;
float arrayloc=0;
for(int i =0; i<samplesPerFrame; i++)
{
xl = (audioframe[(i*nChannels)]) ;
if (nChannels ==2)
xr = (audioframe[(i*nChannels)+1]) ;
else
xr=xl;
yr = U(alpha*S(ymr1) + S(xr) - S(xmr1));
yl = U(alpha*S(yml1) + S(xl) - S(xml1));
ymr1 = yr;
yml1 = yl;
xml1 = xl;
xmr1 = xr;
hpol =hpol - (LPF_Beta * (hpol - yl));
hpor =hpor - (LPF_Beta * (hpor - yr));
// rescale to Signed int
vall=int32_t((xl*UMAX(bitsPerSample))-(UMAX(bitsPerSample)/2));
valr=int32_t((xr*UMAX(bitsPerSample))-(UMAX(bitsPerSample)/2));
//fwrite(&vall,2,1,audio_file);
for(b=bitsPerSample; b>0; vall >>= 8, b-=8)
fputc(vall & 0xFF, audio_file);
if (nChannels ==2)
//fwrite(&valr,2,1,audio_file);
for(b=bitsPerSample; b>0; valr >>= 8, b-=8)
fputc(valr & 0xFF, audio_file);
}
}
void wav::close()
{
bytesPerSec = samplesPerSec * (bitsPerSample/8) * nChannels;
blockAlign = bitsPerSample * nChannels / 8;
dataChunkSize = numframes*samplesPerFrame* blockAlign;
// riffSize = dataChunkSize + bwfckSize + 134
// (134 = fmt+list+4*8+4, 4*8 = chunk headers, +4 for the "WAVE")
// but we'll just calculate that at file close, so as to handle
// any other chunks that developers may include later.
if(audio_file==NULL)
{
std::cerr << "Error writing " << numframes*samplesPerFrame <<
" samples\n";
return;
}
// write the bext chunk at the end of the file
fwrite(&(this->bwfchunkID[0]), 346, 1, audio_file);
//skip two bytes of padding in the middle of the struct
fwrite(&(this->TimeReferenceLow), 364, 1, audio_file);
// update the header regarding the size of the data:
riffSize = ftell(audio_file)-8;
fseek ( audio_file , 0 , SEEK_SET );
fwrite(this, 44, 1, audio_file);
fclose(audio_file);
}
void wav::write(const char *fn, const std::vector<double> &signal)
{
FILE *fp;
int b;
// update the header regarding the size of the data:
bytesPerSec = samplesPerSec * (bitsPerSample/8) * nChannels;
blockAlign = bitsPerSample * nChannels / 8;
dataChunkSize = signal.size() * blockAlign;
riffSize = dataChunkSize + 36;
fp = fopen(fn, "wb");
if(fp==NULL)
{
std::cerr << "Cannot open " << fn << " for writing\n";
return;
}
fwrite(this, sizeof(*this), 1, fp);
double v;
uint32_t val;
std::vector<double>::const_iterator i;
for(i = signal.begin(); i != signal.end(); ++i)
{
// rescale to U16 = [0 , 2^16-1] = [0, 65535]
v = (*i) * UMAX(bitsPerSample);
if(v < 0) val = 0;
else if(v > UMAX(bitsPerSample)) val = UMAX(bitsPerSample);
else val = uint32_t(v);
// flip the leading bit to convert from U16 to S16
val -= UMAX(bitsPerSample)/2;
//fwrite(&val,2,1,fp);
for(b=bitsPerSample; b; val >>= 8, b-=8)
fputc(val & 0xFF, fp);
}
fclose(fp);
}
void wav::BeginInfoChunk()
{
infochunkPosition = ftell(audio_file);
fwrite("LIST",1,4,audio_file);
fwrite("\0\0\0\0",1,4,audio_file);
fwrite("INFO",1,4,audio_file);
}
void wav::AddInfo(const char *id, const char *data)
{
uint32_t dataSize = strlen(data)+1; // include null
fwrite(id,1,4,audio_file);
fwrite(&dataSize,4,1,audio_file);
fwrite(data,1,dataSize,audio_file);
if(dataSize%2) fputc(0, audio_file); // pad to even
}
void wav::EndInfoChunk()
{
long endPos = ftell(audio_file);
uint32_t infoSize = (endPos - infochunkPosition) - 8;
fseek(audio_file, infochunkPosition+4, SEEK_SET);
fwrite(&infoSize,4,1,audio_file);
fseek(audio_file, 0, SEEK_END);
}
// VERY dumb WAV reader: assumes signed 16 bit samples (WAV standard)
// returns unsigned 16 bit samples
bool wav::read(const char *fn)
{
audio_file = fopen(fn, "rb");
if(audio_file == NULL)
{
std::cerr << "Cannot open " << fn << " for reading\n";
perror(NULL);
return false;
}
fread(this,44,1,audio_file);
if(std::strncmp(this->chunkID,"RIFF",4)!=0)
{
std::cerr << "Not a WAV file.\n";
return false;
}
if(this->formatTag != 1)
{
std::cerr << "WAV file codec format must be PCM\n";
return false;
}
if(this->nChannels != 1)
{
std::cerr << "WAV file must be mono\n";
return false;
}
// skip chunks until we get to the "data" chunk
while(std::strncmp(this->dataChunkID, "data", 4) != 0)
{
// skip over this chunk to the header of next chunk
fseek(audio_file, this->dataChunkSize, SEEK_CUR);
//read the next chunk's ID and size
fread(&(this->dataChunkID), 1, 4, audio_file);
fread(&(this->dataChunkSize), 4, 1, audio_file);
}
bufSize = this->dataChunkSize/(this->bitsPerSample/8);
int16_t *sbuf = new int16_t[bufSize];
buffer = new uint16_t[bufSize]; // freed when the wav object is destroyed
fread(sbuf, this->bitsPerSample/8, bufSize, audio_file);
fclose(audio_file);
for(uint32_t i=0; i<bufSize; ++i)
{
// lift the bits from signed to unsigned
buffer[i] = uint16_t(int32_t(sbuf[i])+INT16_MAX+1);
}
delete [] sbuf;
return true;
}
int wav::GetHeight(void) const
{
return samplesPerSec / 24;
}
int wav::GetScanHeight(void) const
{
return GetHeight() * 1.1;
}
int wav::GetOverlap(void) const
{
return GetScanHeight() - GetHeight();
}
double *wav::GetFrame(long frameNum, double *buf) const
{
int h,w;
h = w = GetScanHeight();
if(buf==NULL)
{
size_t sz = h*w;
buf = new double[sz];
if(buf==NULL)
{
std::cerr << "Out of Memory: synth Wav buf\n";
exit(1);
}
}
uint16_t *bp;
bp = this->buffer + (frameNum-1) * GetHeight();
double *p = buf;
for(int r=0; r<h; r++)
{
double val = (double(bp[r]) / double(0x00FFFF));
for(int c=0; c<w; c++)
{
*(p++) = val;
}
}
return buf;
}
// 20% variable density; 10% black 50% variable area 10% black 10% frame marker
unsigned char *wav::GetFrameImage(size_t frameNum, unsigned char *buf,
int &width, int &height, bool &endian) const
{
height = GetScanHeight();
width = height;
if(buf==NULL)
{
size_t sz = height*width*2*4;
buf = new unsigned char[sz];
if(buf==NULL)
{
std::cerr << "Out of Memory: synth Wav buf\n";
exit(1);
}
}
uint16_t *bp;
bp = this->buffer + (frameNum-1) * GetHeight();
unsigned char *p = buf;
int c;
const uint16_t white[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF };
const uint16_t black[] = { 0x0000, 0x0000, 0x0000, 0x0000 };
for(int r=0; r<height; r++)
{
uint16_t val = bp[r];
// variable density
for(c=0; c<4*width*.2; c++)
{
memcpy(p,&val,2);
p+=2;
}
// black
for( ; c<4*width*.3; c++)
{
memcpy(p,black,2);
p+=2;
}
// variable area: white then black
double area = double(val)/double(UINT16_MAX);
for( ; c<4*width*(.3 + area*.5); c++)
{
memcpy(p, white, 2);
p+=2;
}
for( ; c<4*width; c++)
{
memcpy(p, black, 2);
p+=2;
}
}
// mark the frame start and end
const uint16_t yellow[] = { 0xFFFF, 0xFFFF, 0x0000, 0x0000};
int overlap = (height - GetHeight())/2;
int i;
// top of frame
for(i=0; i<6;i++)
{
p = buf + 2*4*width*(overlap+i) + 2*4*(width/10)*9;
for(c=0; c<width/10; c++, p+=8) memcpy(p,yellow,8);
}
// bottom of frame
for(i=0; i<6;i++)
{
p= buf + 2*4*width*(overlap + GetHeight() + i)+ 2*4*(width/10)*9;
for(c=0; c<width/10; c++, p+=8) memcpy(p,yellow,8);
}
endian = false;
return buf;
}