|
-
May 31st, 2012, 09:30 PM
#1
c++ audio programming (wave format)
Hello, it's my first post and I have problem with this code:
Code:
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
// multiplier
const double MULTIPLIER = 0.0000001;
bool fexists(const char *filename) {
ifstream ifile(filename);
return ifile;
}
int main(int argc, char *argv[]) {
string wavFile;
if (argc == 2) {
wavFile = argv[1]; // path to wave
} else if (argc != 2) {
cout << "Select wave file: ";
getline(cin, wavFile);
} else {
cout << "Usage: wavecutter pathToWave" << endl;
system("PAUSE");
return 0;
}
int index = wavFile.find_last_of("\\");
string path = wavFile.substr(0, index+1); // path to file
string txt = string(wavFile);
int begin = txt.length()-3;
txt.replace(begin, txt.length()-1, "lab"); // txt file with devision parameters
// buffers for WAVE'a
char ChunkID[4], Format[4], Subchunk1ID[4], Subchunk2ID[4];
long ChunkSize,Subchunk1Size, SampleRate, ByteRate,Subchunk2Size;
short AudioFormat, NumChannels, BlockAlign, BitsPerSample;
short *data;
if (!fexists(wavFile.c_str())) {
cout << "Cannot find wave file at specified location: " << wavFile << endl;
system("PAUSE");
return 0;
}
// reading source wave file
FILE *fhandle=fopen(wavFile.c_str(),"rb");
cout << "\nData file: " << txt << endl;
cout << "Audio file: " << wavFile << endl;
fread(ChunkID, 1, 4, fhandle);
fread(&ChunkSize, 4, 1, fhandle); cout << "ChunkSize = " << ChunkSize << endl;
fread(Format, 1, 4, fhandle);
fread(Subchunk1ID, 1, 4, fhandle);
fread(&Subchunk1Size,4,1,fhandle); cout << "SubChunk1Size = " << Subchunk1Size << endl;
fread(&AudioFormat,2,1,fhandle); cout << "AudioFormat = " << AudioFormat << endl;
fread(&NumChannels,2,1,fhandle); cout << "NumChannels = " << NumChannels << endl;
fread(&SampleRate,4,1,fhandle); cout << "SampleRate = " << SampleRate << endl;
fread(&ByteRate,4,1,fhandle); cout << "ByteRate = " << ByteRate << endl;
fread(&BlockAlign,2,1,fhandle); cout << "BlockAlign = " << BlockAlign << endl;
fread(&BitsPerSample,2,1,fhandle); cout << "BitsPerSample = " << BitsPerSample << endl;
fread(&Subchunk2ID,1,4,fhandle);
fread(&Subchunk2Size,4,1,fhandle); cout << "Subchunk2Size = " << Subchunk2Size << endl;
long bits = BitsPerSample/8; cout << "Bits = " << bits << endl;
long size = Subchunk2Size/bits; cout << "Size = " << size << endl;
long seconds = Subchunk2Size/ByteRate; // length in seconds
long milisecs = 0;
if (seconds > 0) {
milisecs = seconds*1000;
} else {
milisecs = Subchunk2Size*1000/ByteRate;
}
cout << "Duration = " << seconds << "s" << ", " << milisecs << "ms" << endl;
data = new short[size]; // create an element for every sample
fread(data, bits, size, fhandle); // reading raw audio data
fclose(fhandle);
cout << "Path = "<<path<<endl;
if (!fexists(txt.c_str())) {
cout << "Cannot find LAB file at specified location: " << txt << endl;
system("PAUSE");
return 0;
}
// data processing
ifstream ifs(txt.c_str()); // c_str -> string to char*
string line;
// .lab line by line
while (getline(ifs, line)) {
if (line.length() < 3) continue; // ignorowanie blednych linii
double start=0, end=0;
// parsing data
int index1 = line.find_first_of(" ");
string part1 = line.substr(0, index1);
int index2 = line.find_last_of(" ");
int indexDiff = index2 - index1;
string part2 = line.substr(index1+1, indexDiff);
indexDiff = line.length() - index2;
string partName = line.substr(index2+1, indexDiff);
start = atol(part1.c_str()) * MULTIPLIER;
end = atol(part2.c_str()) * MULTIPLIER;
partName.insert(partName.length(), ".wav");
string partPath = string(path);
partPath.insert(partPath.length(), partName);
cout << endl;
// writing syllable wave file
cout << "Writing audio part [" << start << "s -> " << end << "s] to file: " << partPath << endl;
fhandle = fopen(partPath.c_str(), "wb");
fwrite(ChunkID, 1, 4, fhandle);
fwrite(&ChunkSize, 4, 1, fhandle);
fwrite(Format, 1, 4, fhandle);
fwrite(Subchunk1ID, 1, 4, fhandle);
fwrite(&Subchunk1Size, 4, 1, fhandle);
fwrite(&AudioFormat, 2, 1, fhandle);
fwrite(&NumChannels, 2, 1, fhandle);
fwrite(&SampleRate, 4, 1, fhandle);
fwrite(&ByteRate, 4, 1, fhandle);
fwrite(&BlockAlign, 2, 1, fhandle);
fwrite(&BitsPerSample, 2, 1, fhandle);
fwrite(&Subchunk2ID, 1, 4, fhandle);
// calculating start
double additionDBL = (start*size/milisecs)*1000;
int addition = (int) additionDBL;
if (start == 0) addition = 0;
// calculating size of
double diff = end - start; // part length
// change size
double newsizeDBL = (diff*size/milisecs)*1000;
int newsize = (int) newsizeDBL;
cout << newsize << endl;
// proportional change of subchunk2 segment
double newSubchunkSizeDBL = (diff*Subchunk2Size/milisecs)*1000;
int Subchunk2Size_new = (int) newSubchunkSizeDBL;
fwrite(&Subchunk2Size_new, 4, 1, fhandle);
// write data
fwrite(data+addition, bits, newsize, fhandle);
fclose(fhandle);
}
fhandle = NULL;
delete fhandle;
data = NULL;
delete data;
cout << endl;
cout << "\nDone!\n";
system("PAUSE");
return 0;
}
It should cut wav files into smaller pieces using text file .lab (the walues multipield by * 0.0000001 gives time in second. For example, i have such file with recorded yes and no: http://www.juss.cba.pl/yesno.wav and such bondaries http://www.juss.cba.pl/yesno.lab It all should be in the same folder, then, after puting "yesno.wav" into command line and press enter it should gives 2 files with yes and no words. But it doesnt - i obtained one file with silent and one short sound. Can anyone tell me whats wrong? The record is recorded by Audacity, default sample rate 44100, default sample format 16-bit.
Please help me, i cannot fix it by myself
Sorry for my English but i'm not native speaker.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|