-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataPrep.cpp
605 lines (470 loc) · 17.9 KB
/
DataPrep.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
#ifdef _MSC_VER
#define NOMINMAX
#include <algorithm>
#endif
#include "DataPrep.h"
#include "ServiceFunc.h"
using namespace std;
/**
* Read Wav data from a file
*/
WavData* WavData::readFromFile(const std::string& file) {
WavHeader wavHeader;
// Open file
std::fstream fs;
fs.open(file.c_str(), std::ios::in | std::ios::binary);
if (!fs.good()) {
fprintf(stderr, "Input file not found: %s\n", file.c_str());
return NULL;
}
// Read header
fs.read((char*)(&wavHeader), sizeof(WavHeader));
if (!checkHeader(wavHeader)) {
return NULL;
}
// Read raw data
WavData* wavData = new WavData(wavHeader);
readData(fs, wavHeader, *wavData);
fs.close();
return wavData;
}
/**
* Checks a set of restrictions
*/
bool WavData::checkHeader(const WavHeader& wavHeader) {
if ((0 != strncmp(wavHeader.riff, "RIFF", sizeof(wavHeader.riff))) || (0 != strncmp(wavHeader.wave, "WAVE", sizeof(wavHeader.wave)))) {
fprintf(stderr, "Invalid RIFF/WAVE format\n");
return false;
}
if (1 != wavHeader.audioFormat) {
fprintf(stderr, "Invalid WAV format: only PCM audio format is supported\n");
return false;
}
if (wavHeader.numOfChan > 2) {
fprintf(stderr, "Invalid WAV format: only 1 or 2 channels audio is supported\n");
return false;
}
uint32_t bitsPerChannel = wavHeader.bitsPerSample / wavHeader.numOfChan;
if (16 != bitsPerChannel) {
fprintf(stderr, "Invalid WAV format: only 16-bit per channel is supported\n");
return false;
}
assert(wavHeader.subchunk2Size > 0);
if (wavHeader.subchunk2Size > LONG_MAX) {
fprintf(stderr, "File too big\n");
return false;
}
return true;
}
void WavData::readData(std::fstream& fs, const WavHeader& wavHeader, WavData& wavFile) {
raw_t value, minValue = 0, maxValue = 0; // in dB
int16_t value16, valueLeft16, valueRight16;
uint32_t bytesPerSample = static_cast<uint32_t>(wavHeader.bitsPerSample / 8);
uint32_t numberOfSamplesXChannels = wavHeader.subchunk2Size /
(wavHeader.numOfChan * bytesPerSample);
wavFile.rawData = new raw_t[numberOfSamplesXChannels];
uint32_t sampleNumber;
for (sampleNumber=0; sampleNumber < numberOfSamplesXChannels && !fs.eof(); sampleNumber++) {
if (1 == wavHeader.numOfChan) {
fs.read((char*)(&value16), sizeof(int16_t));
value = static_cast<raw_t>(value16);
} else {
fs.read((char*)(&valueLeft16), sizeof(int16_t));
fs.read((char*)(&valueRight16), sizeof(int16_t));
value = static_cast<raw_t>((abs(valueLeft16) + abs(valueRight16)) / 2);
}
if (maxValue < value) {
maxValue = value;
}
if (minValue > value) {
minValue = value;
}
wavFile.rawData[sampleNumber] = value;
}
assert(sampleNumber > 0);
// Normalization
wavFile.normalizedData = new double[numberOfSamplesXChannels];
double maxAbs = max(fabs(minValue), fabs(maxValue));
for (sampleNumber = 0; sampleNumber < numberOfSamplesXChannels; sampleNumber++) {
wavFile.normalizedData[sampleNumber] = static_cast<double>(wavFile.rawData[sampleNumber]) / maxAbs;
}
// Update values
wavFile.setMinVal(minValue);
wavFile.setMaxVal(maxValue);
wavFile.setNumberOfSamples(numberOfSamplesXChannels);
}
bool separateSamplesToFrames(WavData* data, std::vector<sFrame> *frames)
{
extern bool option_SilentMode;
sFrame sfr;
uint32_t bytesPerFrame = static_cast<uint32_t>((data->getHeader()).bytesPerSec * FRAME_LENGTH / 1000.0);
uint32_t bytesPerSample = static_cast<uint32_t>((data->getHeader()).bitsPerSample / 8);
uint32_t samplesPerFrame = static_cast<uint32_t>(bytesPerFrame / bytesPerSample);
if (samplesPerFrame <= 0)
{
if (option_SilentMode)
cout << "wrong header, file will be skipped. ";
else
cout << "wrong header, file will be skipped (number of samples per frame = " << samplesPerFrame << ", but cannot be less or equal than 0. ";
return false;
}
uint32_t samplesPerNonOverlap = static_cast<uint32_t>(samplesPerFrame * (1 - FRAME_OVERLAP));
uint32_t framesCount = ((data->getHeader()).subchunk2Size / bytesPerSample) / samplesPerNonOverlap;
if ((data->getHeader()).subchunk2Size < 10000)
{
if (option_SilentMode)
cout << "Wrong header, file will be skipped.";
else
cout << "Wrong header, file will be skipped (subchunk 2 size = " << (data->getHeader()).subchunk2Size << ", but expected not less than 22040). ";
return false;
}
uint32_t indexBegin = 0, indexEnd = 0;
uint32_t size = data->getNumberOfSamples();
uint32_t frameId = 0;
for (frameId = 0; frameId < framesCount; ++frameId) {
indexBegin = frameId * samplesPerNonOverlap;
indexEnd = indexBegin + samplesPerFrame;
if (indexEnd <= size) {
sfr.id = frameId;
sfr.firstSample = frameId * samplesPerNonOverlap;
sfr.lastSample = sfr.firstSample + samplesPerFrame;
sfr.rms = RMS(data->getRawData(), sfr.firstSample, sfr.lastSample);
sfr.entropy = Entropy(data->getNormalizedData(), sfr.firstSample, sfr.lastSample, ENTROPY_BINS, -1, 1);
sfr.mfcc = calcMFCC (data->getNormalizedData(), sfr.firstSample, sfr.lastSample, MFCC_SIZE, data->getHeader().samplesPerSec, MFCC_FREQ_MIN, MFCC_FREQ_MAX);
frames->insert(frames->begin() + frameId, sfr);
} else {
break;
}
}
return true;
}
bool findSilenceThreshold(std::vector<sFrame> frames, bool *hasSilence, double *rmsMax, double *soundThreshold) {
*rmsMax = 0;
double rms, rmsSilence = 0., entropyMax=-100., entropyMin=100., entropyAvg = 0.;
rms = *rmsMax = frames.at(0).rms;
*hasSilence = false;
uint32_t cnt = 0;
for (vector<sFrame>::const_iterator frmIt = frames.begin(); frmIt != frames.end(); ++frmIt) {
entropyMax = std::max(entropyMax, frmIt->entropy);
entropyMin = std::min(entropyMin, frmIt->entropy);
entropyAvg = (entropyMax + entropyMin) / 2;
}
for (vector<sFrame>::const_iterator frmIt = frames.begin(); frmIt != frames.end(); ++frmIt) {
rms = frmIt->rms;
*rmsMax = std::max(*rmsMax, rms);
if (frmIt->entropy < entropyAvg*0.9) {
*hasSilence = true;
rmsSilence += frmIt->rms;
cnt++;
}
}
rmsSilence /= cnt;
*soundThreshold = rmsSilence * SOUND_THRESHOLD_COEF;
return true;
}
bool separateFramesToSounds(std::vector<sFrame> frames, std::vector<sSound> * sounds) {
std::vector<sSound> ressounds;
bool hasSilence;
double rmsMax = 0., soundThreshold = 0.;
findSilenceThreshold(frames, &hasSilence, &rmsMax, &soundThreshold);
int32_t SoundId = -1;
int32_t firstFrameInCurrentSoundNumber = -1;
uint32_t lastFrameInCurrentSoundNumber = 0;
int32_t silenceCnt = 0;
sSound tmpsSound;
if (hasSilence) {
for (vector<sFrame>::const_iterator frmIt = frames.begin(); frmIt != frames.end(); ++frmIt) {
// Got a sound at 1st time or after a silence
if ((frmIt->rms > soundThreshold) && (firstFrameInCurrentSoundNumber == -1)) {
firstFrameInCurrentSoundNumber = frmIt->id;
silenceCnt = 0;
}
// Sound continues
if ((frmIt->rms > soundThreshold) && (firstFrameInCurrentSoundNumber != -1)) {
lastFrameInCurrentSoundNumber = frmIt->id;
silenceCnt = 0;
}
// Silence started after a sound
if ((frmIt->rms <= soundThreshold) && (firstFrameInCurrentSoundNumber >= 0)) {
++silenceCnt;
if (silenceCnt > SOUNDS_MIN_DISTANCE) {
if ((lastFrameInCurrentSoundNumber - firstFrameInCurrentSoundNumber) >= SOUND_MIN_SIZE) { ///////////////////////// Вот тут проверку на слишком длинные звуки!?
++SoundId;
tmpsSound.reset();
tmpsSound.id = SoundId;
tmpsSound.firstFrame = firstFrameInCurrentSoundNumber;
tmpsSound.lastFrame = lastFrameInCurrentSoundNumber;
for (uint32_t i = firstFrameInCurrentSoundNumber; i <= lastFrameInCurrentSoundNumber; i++)
{
tmpsSound.vMFCC.push_back(frames.at(i).mfcc);
}
ressounds.push_back(tmpsSound);
sounds->push_back(tmpsSound);
}
firstFrameInCurrentSoundNumber = -1;
}
}
// If there's not enough silence after the last sound and before the end of file
if ((lastFrameInCurrentSoundNumber >= (frames.back().id - SOUNDS_MIN_DISTANCE)) && ((lastFrameInCurrentSoundNumber - firstFrameInCurrentSoundNumber) >= SOUND_MIN_SIZE)) {
++SoundId;
tmpsSound.reset();
tmpsSound.id = SoundId;
tmpsSound.firstFrame = firstFrameInCurrentSoundNumber;
tmpsSound.lastFrame = lastFrameInCurrentSoundNumber;
for (uint32_t i = firstFrameInCurrentSoundNumber; i <= lastFrameInCurrentSoundNumber; i++)
{
tmpsSound.vMFCC.push_back(frames.at(i).mfcc);
}
ressounds.push_back(tmpsSound);
sounds->push_back(tmpsSound);
break;
}
}
}
if (!hasSilence || (SoundId==-1))// There's no silence, whole file is one sound
{
double soundRMS1 = 0., soundRMS = 0.;
tmpsSound.reset();
firstFrameInCurrentSoundNumber = 0;
lastFrameInCurrentSoundNumber = frames.back().id;
for (auto frmIt = frames.begin(); frmIt != frames.end(); frmIt++)
{
soundRMS1 += frmIt->rms;
}
for (sFrame &frmIt : frames)
{
soundRMS += frmIt.rms;
}
soundRMS /= lastFrameInCurrentSoundNumber - firstFrameInCurrentSoundNumber;
if (soundRMS < soundThreshold / SOUND_THRESHOLD_COEF) return false;
tmpsSound.id = 0;
tmpsSound.firstFrame = firstFrameInCurrentSoundNumber;
tmpsSound.lastFrame = lastFrameInCurrentSoundNumber;
for (uint32_t i = firstFrameInCurrentSoundNumber; i <= lastFrameInCurrentSoundNumber; i++)
{
tmpsSound.vMFCC.push_back(frames.at(i).mfcc);
}
ressounds.push_back(tmpsSound);
sounds->push_back(tmpsSound);
SoundId++;
}
if (SoundId==-1) return false;
else return true;
}
bool saveSoundAsAudio(const std::string& file, const std::vector<sFrame> frames, const sSound sound, WavData *wavdata) {
uint32_t sampleStart = frames.at(sound.firstFrame).firstSample;
uint32_t sampleFinish = frames.at(sound.lastFrame).lastSample;
uint32_t waveSize = (sampleFinish - sampleStart) * sizeof(raw_t);
// prepare a new header and write it to file stream
WavHeader headerNew;
strncpy(headerNew.riff, wavdata->getHeader().riff, 4);
headerNew.chunkSize = waveSize + sizeof(WavHeader);
strncpy(headerNew.wave, wavdata->getHeader().wave, 4);
strncpy(headerNew.fmt, wavdata->getHeader().fmt, 4);
headerNew.subchunk1Size = wavdata->getHeader().subchunk1Size;
headerNew.audioFormat = wavdata->getHeader().audioFormat;
headerNew.numOfChan = 1;
headerNew.samplesPerSec = wavdata->getHeader().samplesPerSec;
headerNew.bytesPerSec = wavdata->getHeader().samplesPerSec * sizeof(raw_t);
headerNew.blockAlign = sizeof(raw_t);
headerNew.bitsPerSample = sizeof(raw_t) * 8;
strncpy(headerNew.data, wavdata->getHeader().data, 4);
headerNew.subchunk2Size = waveSize;
std::ofstream fs;
fs.open(file.c_str(), std::ios::out | std::ios::binary);
if (!fs.good()) {
fprintf(stderr, "Output file is not created: %s\n", file.c_str());
return false;
}
fs.write((char*)&headerNew, sizeof(WavHeader));
raw_t* data = new raw_t[waveSize / sizeof(raw_t)];
uint32_t i = 0;
for (uint32_t currentSample = sampleStart; currentSample <= sampleFinish; currentSample++) {
data[i] = wavdata->getRawData()[currentSample];
++i;
}
fs.write((char*)data, waveSize);
fs.close();
delete [] data;
return true;
}
bool makeTrainDataFile(const std::string& trainInputFolder, const std::string& trainDataFilePath)
{
const char * trainWaveFilesFolder = trainInputFolder.c_str();
WavData* trainWave;
std::string filename, path ,fullpath;
std::vector<sFrame> trainFrames;
std::fstream trainfile;
trainfile.open(trainDataFilePath.c_str(), std::ios::out | std::ios::binary);
cout<< "Writing of MFCC from wave files to a train data file " << trainDataFilePath << " is started..."<< endl;
DIR *dir;
struct dirent *ent;
if ((dir = opendir (trainWaveFilesFolder)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
if (ent->d_name[0]!='.')
{
cout<< ent->d_name << " - " << flush;
fullpath = trainInputFolder + filename.assign(ent->d_name);
trainFrames.clear();
trainWave = WavData::readFromFile(fullpath);
if (!trainWave)
{
return false;
}
separateSamplesToFrames(trainWave, &trainFrames);
trainfile << ent->d_name << std::endl;
trainfile << trainFrames.size() << std::endl;
cout << trainFrames.size() << " frames" << endl;
for (std::vector<sFrame>::const_iterator item = trainFrames.begin(); item!=trainFrames.end(); ++item)
{
for (int i = 0; i < MFCC_SIZE; ++i)
{
trainfile << std::setprecision(12) << item->mfcc[i] << std::endl;
}
}
}
}
closedir (dir);
trainfile.close();
}
else
{
cerr << "Cannot open directory " << trainDataFilePath << endl;
return false;
}
return true;
}
bool readTrainDataFile(const std::string& trainDataFilePath, std::vector<svMFCC> *trainMatrices)
{
extern bool option_SilentMode;
svMFCC item;
std::fstream trainfile;
trainfile.open(trainDataFilePath, std::ios::in | std::ios::binary);
if (!trainfile.good())
{
cerr << "Train data file not found: " << trainDataFilePath << endl;
return false;
}
if (!option_SilentMode) {cout<< "Reading of MFCC from a train data file " << trainDataFilePath << " is started..."<< flush;}
std::string waveFileName, strFramesNumber, strMFCC;
uint32_t FramesNumber = 0;
double * MFCC1 = new double[100000];
while (!trainfile.eof())
{
item.reset();
getline(trainfile, waveFileName);
if (waveFileName == "")
{
break;
}
getline(trainfile, strFramesNumber);
FramesNumber = atoi(strFramesNumber.c_str());
item.waveFilename = waveFileName;
item.numberOfFrames = FramesNumber;
for (uint32_t i = 0; i < FramesNumber; i++)
{
item.vMFCC.push_back(MFCC1);
for (uint32_t j = 0; j < MFCC_SIZE; j++, MFCC1++)
{
getline(trainfile, strMFCC);
*MFCC1 = stod(strMFCC);
}
}
trainMatrices->push_back(item);
}
trainfile.close();
if (!option_SilentMode) {cout << "Completed. " << endl << endl ;}
return true;
}
double* matrixSlicer(std::vector<double*> matrix, uint16_t sliceNo)
{
double* arr = new double[matrix.size()];
uint16_t i = 0;
for (auto it = matrix.begin(); it!=matrix.end(); ++it)
{
arr[i] = (*it)[sliceNo];
++i;
}
return arr;
}
int makeDecision (std::string inputFile, std::vector<svMFCC> etalonframes)
{
extern bool option_WriteLog;
extern bool option_SilentMode;
extern bool option_WriteSplittedSounds;
extern bool option_Recognize;
extern std::string setting_InputDataFolder;
extern std::string setting_ResultsFileName;
extern ofstream log_file;
bool isOk;
std::vector<sFrame> frames;
std::vector<sSound> sounds;
WavData* wavData = WavData::readFromFile(setting_InputDataFolder + inputFile);
isOk = separateSamplesToFrames(wavData, &frames);
if (!isOk) {cout << "Something went wrong during separation of samples to frames. " << flush; return -1;}
isOk = separateFramesToSounds(frames, &sounds);
if (!isOk) {cout << "Something went wrong during separation of frames to sounds. " << endl; return -1;}
double distance = 0.;
double absoluteMinimum = 3000.;
if (option_WriteLog)
{
log_file << "================================================================================================" << endl;
log_file << " file: " << inputFile << endl;
log_file << "================================================================================================" << endl;
}
if (option_Recognize)
{
for (std::vector<sSound>::const_iterator sitem = sounds.begin(); sitem != sounds.end(); ++sitem)
{
double distMin = 3000.;
if (!option_SilentMode) {cout << "Sound #" << sitem->id << " " << flush;}
for (std::vector<svMFCC>::const_iterator item = etalonframes.begin(); item != etalonframes.end(); ++item)
{
if (option_WriteLog)
{
log_file << "Sound #" << sitem->id << " and etalon file " << setw (8) << item->waveFilename << " ... " << flush;
}
distance = calcDistanceFor2Matrices(sitem->vMFCC, item->vMFCC, MFCC_SIZE);
if (option_WriteLog)
{
log_file << "Distance is " << distance << endl;
}
distMin = std::min(distMin, distance);
}
if (!option_SilentMode) {cout << "min. distance = " << fixed << setprecision(2) << distMin << "; " << flush; }
if (option_WriteLog)
{
log_file << "=================================" << endl;
log_file << " Minimal distance = " << distMin << "; " << endl;
log_file << "=================================" << endl << endl;}
absoluteMinimum = std::min(absoluteMinimum, distMin);
}
if (!option_SilentMode) {cout << " WAVE min. distance = " << fixed << setprecision(2) << absoluteMinimum << " ==> " << flush; }
//cout << ":" << flush;
}
/** (Optional) Writing of Sounds extracted from input files *************************************/
if (option_WriteSplittedSounds)
{
if (!option_SilentMode) { cout << "{Writing Sounds extracted from input files is started..." << flush;}
#ifndef _MSC_VER
std::string mkdir_command("mkdir -p " + setting_InputDataFolder + "splitted" + separator());
#else
std::string mkdir_command("IF NOT EXIST " + setting_InputDataFolder + "splitted" + separator() + " mkdir " + setting_InputDataFolder + "splitted" + separator());
#endif
system(mkdir_command.c_str());
int i = 0;
for (std::vector<sSound>::const_iterator it = sounds.begin(); it != sounds.end(); ++it)
{
std::string path(setting_InputDataFolder + "splitted" + separator() + inputFile + "_Sound_" + to_string(i++) + ".wav");
isOk = saveSoundAsAudio(path, frames, *it, wavData);
if (!isOk) {cout << "Something went wrong during writing of Sounds extracted from input files" << endl; return -2;}
}
if (!option_SilentMode) {cout << "Done.} " << flush;}
}
if (absoluteMinimum <= DECISION_DISTANCE_THRESHOLD)
return 1;
else
return 0;
}