-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwave-processor.cpp
529 lines (470 loc) · 17.2 KB
/
wave-processor.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
#include <io.h>
// for _O_RDONLY etc.
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <FLOAT.H>
#include <vector>
#include "wave-processor.h"
typedef struct {
char data_chunk_id[4]; /* 'data' */
unsigned long data_chunk_size; /* length of data */
} waveheader_ext_t;
WaveProcessor::WaveProcessor(int unifiedSamplingRate)
{
m_unifiedSamplingRate = unifiedSamplingRate;
m_pfWaveR = NULL;
memset(m_newlyMadeSamples, 0, (sizeof(short)+4) * BUFFER_SIZE_IN_SAMPLES_DS);
coefficient[0] = 0.003638091;
coefficient[1] = 0.008158223;
coefficient[2] = 0.020889028;
coefficient[3] = 0.043770138;
coefficient[4] = 0.074889944;
coefficient[5] = 0.108669917;
coefficient[6] = 0.137467074;
coefficient[7] = 0.154050524;
coefficient[8] = 0.154050524;
coefficient[9] = 0.137467074;
coefficient[10] = 0.108669917;
coefficient[11] = 0.074889944;
coefficient[12] = 0.043770138;
coefficient[13] = 0.020889028;
coefficient[14] = 0.008158223;
coefficient[15] = 0.003638091;
}
WaveProcessor::~WaveProcessor()
{
if (m_pfWaveR)
fclose(m_pfWaveR);
if (m_newlyMadeSamples);
//free(m_newlyMadeSamples);
}
void WaveProcessor::CloseWaveFile()
{
if (m_pfWaveR) {
fclose(m_pfWaveR);
m_pfWaveR = NULL;
}
if (m_newlyMadeSamples);
{
//free(m_newlyMadeSamples);
//m_newlyMadeSamples = NULL;
}
}
void WaveProcessor::Clear()
{
m_pfWaveR = NULL;
memset(m_newlyMadeSamples, 0, (sizeof(short)+4) * BUFFER_SIZE_IN_SAMPLES_DS);
memset(samplesArray, 0, sizeof(short)* SamplesVectorSize);
}
// 读文件头,并检查文件是否合法
// 最后两个参数:消息缓存指针和缓存大小(字节数)
//
int WaveProcessor::OpenWaveFile(const char *psfn_waveR)
{
m_pfWaveR = NULL;
FILE *pfWave = fopen(psfn_waveR, "rb"); // read from a binary file
if (pfWave == NULL) {
printf("Can't open the wf file %s!\n", psfn_waveR);
return -10;
}
// 得到文件长度,。。。
fseek(pfWave, 0, SEEK_END); // 指针移到文件尾
long llen_data;
llen_data = ftell(pfWave);
fseek(pfWave, 0, SEEK_SET); // 指针重回文件头
//
fread(&m_header, sizeof(waveheader_t), 1, pfWave);
if (strncmp(m_header.root_chunk_id, "RIFF", 4) != 0 || strncmp(m_header.riff_type_id, "WAVE", 4) != 0) {
// not a wave file
fclose(pfWave);
printf("Not a wave file, abort !\n");
return -20;
}
if (m_header.fmt_chunk_data_size > 16) {
fseek(pfWave, m_header.fmt_chunk_data_size - 16, SEEK_CUR);
}
waveheader_ext_t header_ext; // 扩展文件头
fread(&header_ext, sizeof(waveheader_ext_t), 1, pfWave);
long lLenWaveHeader = sizeof(waveheader_t)+m_header.fmt_chunk_data_size - 16 + sizeof(waveheader_ext_t);
////////////////////////////////////////////////////////////////////////////////////////////////////
llen_data -= lLenWaveHeader;
// printf("\nwave header length : %u\n", lLenWaveHeader);
// printf("wave data length : %u\n\n", llen_data);
// char root_chunk_id[4]; // 'RIFF'
// unsigned long root_chunk_data_size; // length of root chunk
// printf("root_chunk_data_size : %u\n", m_header.root_chunk_data_size);
// char riff_type_id[4]; // 'WAVE'
// char fmt_chunk_id[4]; // 'fmt '
// unsigned long fmt_chunk_data_size; // length of sub_chunk, always 16 bytes
// printf("fmt_chunk_data_size(16) : %u\n", m_header.fmt_chunk_data_size);
// unsigned short compression_code; // always 1 = PCM-Code
// printf("compression_code(1) : %d\n", m_header.compression_code);
// unsigned short num_of_channels; // 1 = Mono, 2 = Stereo
// printf("num_of_channels(1 = Mono, 2 = Stereo) : %d\n", m_header.num_of_channels);
// unsigned long sample_rate; // Sample rate
// printf("sample_rate : %u\n", m_header.sample_rate);
// unsigned long byte_p_sec; // average bytes per sec
// printf("byte_p_sec(average bytes per sec) : %u\n", m_header.byte_p_sec);
// unsigned short byte_p_sample; // Bytes per sample, including the sample's data for all channels!
// printf("byte_p_sample : %d\n", m_header.byte_p_sample);
// unsigned short bit_p_sample; // bits per sample, 8, 12, 16
// printf("bit_p_sample(8, 12, or 16) : %d\n", m_header.bit_p_sample);
// char data_chunk_id[4]; // 'data'
// unsigned long data_chunk_size; // length of data
// printf("data_chunk_size : %u\n", header_ext.data_chunk_size);
if ((unsigned long)llen_data != header_ext.data_chunk_size) {
//printf("llen_data = %u, not equal to data_chunk_size !\n", (unsigned long)llen_data);
// assert(0);
}
if (m_header.compression_code != 1) {
fclose(pfWave);
printf("Not in the reqired compression code, abort !\n");
return -30;
}
if (m_header.bit_p_sample != 16) {
fclose(pfWave);
printf("Bits per sample is not 16, abort !\n");
return -40;
}
if (m_header.sample_rate < m_unifiedSamplingRate) {
fclose(pfWave);
printf("Sampling rate is less than required, abort !\n");
return -50;
}
if (m_header.num_of_channels > 2) {
fclose(pfWave);
printf("More than 2 channels, abort !\n");
return -60;
}
// number of original samples in the audio file !!!
// m_lNumSamplesInFile = m_header_ext.data_chunk_size/m_header.byte_p_sample;
// number of selected samples
// m_lNumSamplesInFile /= DOWN_SAMPLING_RATE;
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
m_pfWaveR = pfWave;
fseek(m_pfWaveR, lLenWaveHeader, SEEK_SET); // 将文件指针移到 Waveform 数据开始处
return 0; // OK
}
// 按统一采样率做(欠)采样,并将多声道(多个采样值)合并成一个声道(一个采样值)
int WaveProcessor::MakeTargetSamplesData()
{
assert(m_header.bit_p_sample == 16 || m_header.bit_p_sample == 8);
// "m_header.sample_rate" : 原始采样频率
// 原始采样率必须高于或等于 "m_unifiedSamplingRate"(此即目标采样率!!!)
if (m_header.sample_rate < m_unifiedSamplingRate) {
printf("m_header.sample_rate(%u) < %d !\n", m_header.sample_rate, m_unifiedSamplingRate);
return -1;
}
//short *m_newlyMadeSamples = NULL;
unsigned char *szOrginalSampsBuffer = NULL;
// buffer to hold newly made down-sampling samples, 16 bits per sample!
/* in order to accelerate
m_newlyMadeSamples = (short *)malloc(sizeof(short)*BUFFER_SIZE_IN_SAMPLES_DS+
(m_header.byte_p_sample)*BUFFER_SIZE_IN_SAMPLES_DS);
*/
if (m_newlyMadeSamples == NULL) {
printf("No memory !\n");
return -2;
}
szOrginalSampsBuffer = (unsigned char *)(m_newlyMadeSamples + BUFFER_SIZE_IN_SAMPLES_DS);
// ////////////////////////////////////////////////////////////////////////////////////////
#ifdef __NORMALIZE_AUDIO_SAMPLES
maxAbs = 0;
#endif
int xxx; // OK
// 注意:欠采样信号和原采样信号对应的时长是一样的!!!
// “原采样缓存”中第一个位置所存的采样的全局下标
long iOriginalSampStartG = 0;
// 本批生成的第一个欠采样点(即“欠采样缓存”中第一个点)的全局下标
long iTargetSampStartG = 0;
// total number of down-sampling samples made
unsigned long nNumNewlyMadeSampsTotal = 0;
// 用完整信号,。。。
int& sr = xxx;
sr = m_unifiedSamplingRate;
//_write(fhw, &sr, sizeof(int)); // 采样率
//_write(fhw, &iTargetSampStartG, sizeof(unsigned long)); // 起始采样下标
//_write(fhw, &nNumNewlyMadeSampsTotal, sizeof(unsigned long)); // 目标采样个数
xxx = 0;
//samplesVector.clear();
memset(samplesArray, 0, sizeof(short)* SamplesVectorSize);
//如果是多声道,取各个省道的平均
if (m_header.num_of_channels > 1)
{
if (m_header.bit_p_sample == 16)
{
// 每从原始采样数据文件读“一批”原采样数据,就生成一批欠采样。
while (!feof(m_pfWaveR)) {
int nNumNewlyMadeSamps = 0; // 本批(即从本次读取的原始信号数据)生成的欠采样点个数
//(一)从当前歌曲音频数据文件读一块数据,。。。
size_t uNumOrigSampsRead;
uNumOrigSampsRead = fread(szOrginalSampsBuffer,
m_header.byte_p_sample, // 一个“采样”包含了一个采样时间点所有声道的数据
BUFFER_SIZE_IN_SAMPLES_DS, // 要读取的采样个数
m_pfWaveR);
if (uNumOrigSampsRead == 0) {// no content read
break;
}
if (iTargetSampStartG == 0) {
// 无论如何做欠采样,第一个原始采样总是要直接拷贝的。也就是说,欠采样信号的第一个样本点就是原信号的第一个
// 样本点!
assert(nNumNewlyMadeSamps == 0);
// 第一个采样值照搬
// 取各个声道的平均
long lval; // 用 "long" 保证计算过程不溢出
lval = *((short *)szOrginalSampsBuffer);
lval += *(((short *)szOrginalSampsBuffer) + 1);
lval /= 2;
m_newlyMadeSamples[nNumNewlyMadeSamps] = lval;
nNumNewlyMadeSamps++;
}
//(二)生成对应的新采样,。。。
while (1) {
// 核心是将欠采样点对应到某个原采样点(只有左采样点),或两个相邻的原采样点(即左右两个采样点)之间!!!
// 与待求的欠采样点对应的原始采样点的全局下标(理论上不一定是整数)
long iOriginalSampG;
long iTargetSampG;
// 根据欠采样点下标,。。。
iTargetSampG = iTargetSampStartG + nNumNewlyMadeSamps;
// 计算其对应的原采样点下标
iOriginalSampG = (double)iTargetSampG*m_header.sample_rate / m_unifiedSamplingRate + 0.5;
if (iOriginalSampG > iOriginalSampStartG + uNumOrigSampsRead - 1) {
// 还未读到“目标原采样点”,。。。
iOriginalSampStartG += uNumOrigSampsRead;
break;
}
// 目标原采样点在缓存中的位置
const unsigned char *puchar = szOrginalSampsBuffer +
(m_header.byte_p_sample)*(iOriginalSampG - iOriginalSampStartG);
// clrc_002
//#ifdef __COMBINE_L_R_CHANNELS
// 取各个声道的平均
long lval = 0;
long total = 0;
//如果不够15个采样点,就取之前采样点的平均
if (iOriginalSampG - iOriginalSampStartG < 15)
{
int sampleNum = iOriginalSampG - iOriginalSampStartG;
for (int i = sampleNum; i >= 0; i--)
{
lval = *(((short *)puchar) - i); // 本采样的第一个声道
lval += *(((short *)puchar) - i + 1); // 本采样的第二个声道
lval /= 2;
total += lval;
}
m_newlyMadeSamples[nNumNewlyMadeSamps] = total / (sampleNum + 1);
}
//否则,利用FIR滤波器使用16个采样点
else
{
for (int i = 15; i >= 0; i--)
{
lval = *(((short *)puchar) - i); // 本采样的第一个声道
lval += *(((short *)puchar) - i + 1); // 本采样的第二个声道
lval /= 2;
total += lval * coefficient[i];
}
m_newlyMadeSamples[nNumNewlyMadeSamps] = total;
}
nNumNewlyMadeSamps++; // 本批欠采样个数计数
} // end of "while (1) {"
for (int i = 0; i < nNumNewlyMadeSamps; i++)
samplesArray[nNumNewlyMadeSampsTotal + i] = m_newlyMadeSamples[i];
//(四)准备从音乐文件读入下一批原采样数据,。。。
iTargetSampStartG += nNumNewlyMadeSamps;
nNumNewlyMadeSampsTotal += nNumNewlyMadeSamps;
} // end of "while ( !feof(m_pfWaveR) ) {"
}
else if (m_header.bit_p_sample == 8)
{
// 每从原始采样数据文件读“一批”原采样数据,就生成一批欠采样。
while (!feof(m_pfWaveR)) {
int nNumNewlyMadeSamps = 0; // 本批(即从本次读取的原始信号数据)生成的欠采样点个数
//(一)从当前歌曲音频数据文件读一块数据,。。。
size_t uNumOrigSampsRead;
uNumOrigSampsRead = fread(szOrginalSampsBuffer,
m_header.byte_p_sample, // 一个“采样”包含了一个采样时间点所有声道的数据
BUFFER_SIZE_IN_SAMPLES_DS, // 要读取的采样个数
m_pfWaveR);
if (uNumOrigSampsRead == 0) {// no content read
break;
}
if (iTargetSampStartG == 0) {
// 无论如何做欠采样,第一个原始采样总是要直接拷贝的。也就是说,欠采样信号的第一个样本点就是原信号的第一个
// 样本点!
assert(nNumNewlyMadeSamps == 0);
// 第一个采样值照搬
// 取各个声道的平均
long lval; // 用 "long" 保证计算过程不溢出
lval = *((char *)szOrginalSampsBuffer);
lval += *(((char *)szOrginalSampsBuffer) + 1);
lval *= 256;
lval /= 2;
m_newlyMadeSamples[nNumNewlyMadeSamps] = lval;
nNumNewlyMadeSamps++;
}
//(二)生成对应的新采样,。。。
while (1) {
// 核心是将欠采样点对应到某个原采样点(只有左采样点),或两个相邻的原采样点(即左右两个采样点)之间!!!
// 与待求的欠采样点对应的原始采样点的全局下标(理论上不一定是整数)
long iOriginalSampG;
long iTargetSampG;
// 根据欠采样点下标,。。。
iTargetSampG = iTargetSampStartG + nNumNewlyMadeSamps;
// 计算其对应的原采样点下标
iOriginalSampG = (double)iTargetSampG*m_header.sample_rate / m_unifiedSamplingRate + 0.5;
if (iOriginalSampG > iOriginalSampStartG + uNumOrigSampsRead - 1) {
// 还未读到“目标原采样点”,。。。
iOriginalSampStartG += uNumOrigSampsRead;
break;
}
// 目标原采样点在缓存中的位置
const unsigned char *puchar = szOrginalSampsBuffer +
(m_header.byte_p_sample)*(iOriginalSampG - iOriginalSampStartG);
// clrc_002
//#ifdef __COMBINE_L_R_CHANNELS
// 取各个声道的平均
long lval = 0;
long total = 0;
//如果不够15个采样点,就取之前采样点的平均
if (iOriginalSampG - iOriginalSampStartG < 15)
{
int sampleNum = iOriginalSampG - iOriginalSampStartG;
for (int i = sampleNum; i >= 0; i--)
{
lval = *(((char *)puchar) - i); // 本采样的第一个声道
lval += *(((char *)puchar) - i + 1); // 本采样的第二个声道
lval *= 256;
lval /= 2;
total += lval;
}
m_newlyMadeSamples[nNumNewlyMadeSamps] = total / (sampleNum + 1);
}
//否则,利用FIR滤波器使用16个采样点
else
{
for (int i = 15; i >= 0; i--)
{
lval = *(((char *)puchar) - i); // 本采样的第一个声道
lval += *(((char *)puchar) - i + 1); // 本采样的第二个声道
lval *= 256;
lval /= 2;
total += lval * coefficient[i];
}
m_newlyMadeSamples[nNumNewlyMadeSamps] = total;
}
nNumNewlyMadeSamps++; // 本批欠采样个数计数
} // end of "while (1) {"
for (int i = 0; i < nNumNewlyMadeSamps; i++)
samplesArray[nNumNewlyMadeSampsTotal + i] = m_newlyMadeSamples[i];
//(四)准备从音乐文件读入下一批原采样数据,。。。
iTargetSampStartG += nNumNewlyMadeSamps;
nNumNewlyMadeSampsTotal += nNumNewlyMadeSamps;
} // end of "while ( !feof(m_pfWaveR) ) {"
}
}
//单声道的话,直接就是
else
{
// 每从原始采样数据文件读“一批”原采样数据,就生成一批欠采样。
while (!feof(m_pfWaveR)) {
int nNumNewlyMadeSamps = 0; // 本批(即从本次读取的原始信号数据)生成的欠采样点个数
//(一)从当前歌曲音频数据文件读一块数据,。。。
size_t uNumOrigSampsRead;
uNumOrigSampsRead = fread(szOrginalSampsBuffer,
m_header.byte_p_sample, // 一个“采样”包含了一个采样时间点所有声道的数据
BUFFER_SIZE_IN_SAMPLES_DS, // 要读取的采样个数
m_pfWaveR);
if (uNumOrigSampsRead == 0) {// no content read
break;
}
if (iTargetSampStartG == 0) {
// 无论如何做欠采样,第一个原始采样总是要直接拷贝的。也就是说,欠采样信号的第一个样本点就是原信号的第一个
// 样本点!
assert(nNumNewlyMadeSamps == 0);
// 第一个采样值照搬
if (m_header.bit_p_sample == 16) {
m_newlyMadeSamples[nNumNewlyMadeSamps] = *((short *)szOrginalSampsBuffer);
}
else if (m_header.bit_p_sample == 8) {
m_newlyMadeSamples[nNumNewlyMadeSamps] = *((char *)szOrginalSampsBuffer);
m_newlyMadeSamples[nNumNewlyMadeSamps] *= 256;
}
nNumNewlyMadeSamps++;
}
//(二)生成对应的新采样,。。。
while (1) {
// 核心是将欠采样点对应到某个原采样点(只有左采样点),或两个相邻的原采样点(即左右两个采样点)之间!!!
// 与待求的欠采样点对应的原始采样点的全局下标(理论上不一定是整数)
long iOriginalSampG;
long iTargetSampG;
// 根据欠采样点下标,。。。
iTargetSampG = iTargetSampStartG + nNumNewlyMadeSamps;
// 计算其对应的原采样点下标
iOriginalSampG = (double)iTargetSampG*m_header.sample_rate / m_unifiedSamplingRate + 0.5;
if (iOriginalSampG > iOriginalSampStartG + uNumOrigSampsRead - 1) {
// 还未读到“目标原采样点”,。。。
iOriginalSampStartG += uNumOrigSampsRead;
break;
}
// 目标原采样点在缓存中的位置
const unsigned char *puchar = szOrginalSampsBuffer +
(m_header.byte_p_sample)*(iOriginalSampG - iOriginalSampStartG);
long lval = 0;
long total = 0;
//如果不够15个采样点,就取之前采样点的平均
if (iOriginalSampG - iOriginalSampStartG < 15)
{
int sampleNum = iOriginalSampG - iOriginalSampStartG;
for (int i = sampleNum; i >= 0; i--)
{
if (m_header.bit_p_sample == 16) {
lval = *(((short *)puchar) - i);
}
else if (m_header.bit_p_sample == 8) {
lval = *(((char *)puchar) - i);
lval *= 256;
}
total += lval;
}
m_newlyMadeSamples[nNumNewlyMadeSamps] = total / (sampleNum + 1);
}
//否则,利用FIR滤波器使用16个采样点
else
{
for (int i = 15; i >= 0; i--)
{
if (m_header.bit_p_sample == 16) {
lval = *(((short *)puchar) - i);
}
else if (m_header.bit_p_sample == 8) {
lval = *(((char *)puchar) - i);
lval *= 256;
}
total += lval * coefficient[i];
}
m_newlyMadeSamples[nNumNewlyMadeSamps] = total;
}
nNumNewlyMadeSamps++; // 本批欠采样个数计数
} // end of "while (1) {"
for (int i = 0; i < nNumNewlyMadeSamps; i++)
samplesArray[nNumNewlyMadeSampsTotal + i] = m_newlyMadeSamples[i];
//(四)准备从音乐文件读入下一批原采样数据,。。。
iTargetSampStartG += nNumNewlyMadeSamps;
nNumNewlyMadeSampsTotal += nNumNewlyMadeSamps;
} // end of "while ( !feof(m_pfWaveR) ) {"
}
m_newlyMadeSamplesNumber = nNumNewlyMadeSampsTotal;
return xxx; // OK
}
void WaveProcessor::GetSamplesVector(short* all_time_data, unsigned long& all_time_data_size)
{
for (unsigned int i = 0; i < m_newlyMadeSamplesNumber; i++)
all_time_data[i] = samplesArray[i];
//all_time_data = samplesVector;
all_time_data_size = m_newlyMadeSamplesNumber;
}