Skip to content

Commit

Permalink
Fix incorrect sampleRate and other values.
Browse files Browse the repository at this point in the history
Converted char to unsigned char to erradicate error due to neative
values.
  • Loading branch information
saurabhshri committed Jun 8, 2017
1 parent 66f2490 commit aa5c9aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/lib_ccaligner/read_wav_file.cpp
Expand Up @@ -12,7 +12,7 @@ WaveFileData::WaveFileData(std::string fileName)
_samples.resize(0);
}

bool WaveFileData::checkValidWave (std::vector<char>& fileData)
bool WaveFileData::checkValidWave (std::vector<unsigned char>& fileData)
{
/*Offset Size Name Description
* 0 4 ChunkID Contains the letters "RIFF" in ASCII form
Expand Down Expand Up @@ -44,8 +44,8 @@ bool WaveFileData::openFile ()
*/

std::noskipws(infile);
std::istream_iterator<char> begin (infile), end;
std::vector<char> fileData (begin, end);
std::istream_iterator<unsigned char> begin (infile), end;
std::vector<unsigned char> fileData (begin, end);

if(checkValidWave(fileData))
{
Expand Down Expand Up @@ -210,21 +210,19 @@ bool WaveFileData::parse()
}
}

std::cout<<subChunk1ID<<" "<<subChunk2ID<<" "<<sampleRate<<" "<<bitRate<<std::endl;

return true;
}

/* Convert 4 bytes to int
* https://stackoverflow.com/a/2386134/6487831
*/

unsigned long WaveFileData::fourBytesToInt (std::vector<char>& fileData, int index)
unsigned long WaveFileData::fourBytesToInt (std::vector<unsigned char>& fileData, int index)
{
return ((fileData[index + 3] << 24) | (fileData[index + 2] << 16) | (fileData[index + 1] << 8) | fileData[index]);
}

int WaveFileData::twoBytesToInt (std::vector<char>& fileData, int index)
int WaveFileData::twoBytesToInt (std::vector<unsigned char>& fileData, int index)
{
return ((fileData[index + 1] << 8) | fileData[index]);
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib_ccaligner/read_wav_file.h
Expand Up @@ -16,14 +16,14 @@
class WaveFileData
{
std::string _fileName;
std::vector<char> _fileData;
std::vector<unsigned char> _fileData;
std::vector<std::vector<double>> _samples;

bool checkValidWave (std::vector<char>& fileData);
bool checkValidWave (std::vector<unsigned char>& fileData);
bool parse();

unsigned long fourBytesToInt (std::vector<char>& fileData, int index);
int twoBytesToInt (std::vector<char>& fileData, int index);
unsigned long fourBytesToInt (std::vector<unsigned char>& fileData, int index);
int twoBytesToInt (std::vector<unsigned char>& fileData, int index);
double twoBytesToDouble (int sample);

public:
Expand Down

0 comments on commit aa5c9aa

Please sign in to comment.