Skip to content

Commit

Permalink
Compensate for presence of MetaData.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhshri committed Jun 8, 2017
1 parent aa5c9aa commit e421fb2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/lib_ccaligner/read_wav_file.cpp
Expand Up @@ -6,6 +6,12 @@

#include "read_wav_file.h"

int findIndex(std::vector<unsigned char>& fileData, std::string chunk)
{
auto it = std::search(fileData.begin(), fileData.end(), chunk.begin(), chunk.end());
return it-fileData.begin();
}

WaveFileData::WaveFileData(std::string fileName)
{
_fileName = fileName;
Expand Down Expand Up @@ -116,6 +122,9 @@ bool WaveFileData::parse()
SOURCE : http://soundfile.sapp.org/doc/WaveFormat/
NOTE : I later found out that the subChunks may be located at different offsets
in presence of metadata like LIST INFO etc..
*/

std::string format(_fileData.begin() + 8, _fileData.begin() + 12);
Expand All @@ -126,51 +135,59 @@ bool WaveFileData::parse()
return false;
}

std::string subChunk1ID(_fileData.begin() + 12, _fileData.begin() + 16);
/*
* Apparently, this is just not it. The `fmt ` and `data` chunk may not necessarily be in continuation.
* There may occur inclusion of metadata. So, we'll need to find the location of these chunks.
*/

int fmtIndex = findIndex(_fileData, "fmt ");
int dataIndex = findIndex(_fileData, "data");

std::string subChunk1ID(_fileData.begin() + fmtIndex, _fileData.begin() + fmtIndex + 4);

if(subChunk1ID != "fmt ")
{
std::cout<<"\nInvalid SubChunk1ID : "<<subChunk1ID;
return false;
}

unsigned long subChunk1Size = fourBytesToInt(_fileData, 16);
unsigned long subChunk1Size = fourBytesToInt(_fileData, fmtIndex + 4);

if(subChunk1Size != 16)
{
std::cout<<"\nNot PCM, SubChunk1Size : "<<subChunk1Size;
return false;
}

int audioFormat = twoBytesToInt(_fileData, 20);
int audioFormat = twoBytesToInt(_fileData, fmtIndex + 8);

if(audioFormat != 1)
{
std::cout<<"\nNot PCM, AudioFormat : "<<audioFormat;
return false;
}

int numChannels = twoBytesToInt(_fileData, 22);
int numChannels = twoBytesToInt(_fileData, fmtIndex + 10);

if(numChannels != 1)
{
std::cout<<"\nNot Mono, NumChannels : "<<numChannels;
return false;
}

unsigned long sampleRate = fourBytesToInt(_fileData, 24);
unsigned long sampleRate = fourBytesToInt(_fileData, fmtIndex + 12);

if(sampleRate != 16000)
{
std::cout<<"\nNot 16000Hz SampleRate, SampleRate : "<<sampleRate;
return false;
}

unsigned long byteRate = fourBytesToInt(_fileData, 28);
unsigned long byteRate = fourBytesToInt(_fileData, fmtIndex + 16);

int blockAlign = twoBytesToInt(_fileData, 32);
int blockAlign = twoBytesToInt(_fileData, fmtIndex + 20);

int bitRate = twoBytesToInt(_fileData, 34); //BitsPerSample
int bitRate = twoBytesToInt(_fileData, fmtIndex + 22); //BitsPerSample

if(bitRate != 16)
{
Expand All @@ -184,15 +201,15 @@ bool WaveFileData::parse()
return false;
}

std::string subChunk2ID (_fileData.begin() + 36, _fileData.begin() + 40);
std::string subChunk2ID (_fileData.begin() + dataIndex, _fileData.begin() + dataIndex + 4);

if(subChunk2ID != "data")
{
std::cout<<"\nInvalid SubChunk2ID : "<<subChunk2ID;
return false;
}

unsigned long subChunk2Size = fourBytesToInt(_fileData, 40);
unsigned long subChunk2Size = fourBytesToInt(_fileData, dataIndex + 4);

int numSamples = subChunk2Size * 8 / ( numChannels * bitRate);

Expand All @@ -202,7 +219,7 @@ bool WaveFileData::parse()
{
for (int channel = 0; channel < numChannels; channel++)
{
int sampleIndex = 44 + (blockAlign * i) + channel * bitRate / 8;
int sampleIndex = dataIndex + 8 + (blockAlign * i) + channel * bitRate / 8;

int sampleValue = twoBytesToInt(_fileData, sampleIndex);
double sample = twoBytesToDouble(sampleValue);
Expand Down
3 changes: 3 additions & 0 deletions src/lib_ccaligner/read_wav_file.h
Expand Up @@ -12,6 +12,9 @@
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>

int findIndex(std::vector<char>& fileData, std::vector<char>& searchString);

class WaveFileData
{
Expand Down

0 comments on commit e421fb2

Please sign in to comment.