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:cry:
Sorry for my English but i'm not native speaker.
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
juss
Please help me, i cannot fix it by myself
Did you write all of this code? If you did, then why can't you fix your own code? If you wrote it, you must know how to fix it if something goes wrong.
Did you use your debugger that comes with the compiler? That's how you see where things are going wrong.
Regards,
Paul McKenzie
Re: c++ audio programming (wave format)
the program compile, and gives wave file as output, so debbuger will not help me. Only boundaries (end and beggining of file) are wrong. I wrote it by myself at shool, but it was long time ago and now i need it to practical purposes. If everyone would know how to fix they own programms the forum would not exist.
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
juss
the program compile, and gives wave file as output, so debbuger will not help me. Only boundaries (end and beggining of file) are wrong. I wrote it by myself at shool, but it was long time ago and now i need it to practical purposes. If everyone would know how to fix they own programms the forum would not exist.
Beleive it or not, there are programmers in the world who graduated college, and worked for years professionally as a programmer and never needed strangers on a forum to debug their programs for them. :eek::rolleyes:
Of course the debugger will help. Debugging is stepping through your code and finding out where what you expected to happen differs from what actually is happening.
Re: c++ audio programming (wave format)
TheCPUWizard phrased it nicely (whatever happened to him?):
http://forums.codeguru.com/showpost....12&postcount=5
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
Martin O
Beleive it or not, there are programmers in the world who graduated college, and worked for years professionally as a programmer and never needed strangers on a forum to debug their programs for them. :eek::rolleyes:
.
I believe, but forum is for that who have problems and want ask somebody for help.
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
juss
I believe, but forum is for that who have problems and want ask somebody for help.
Then when you get another problem where you need to debug, are you going to ask for help again?
The point is that you must learn how to debug your own code, or else you will forever ask others to debug code for you, which will annoy them. It's as if you signed up for a swimming course, and instead of going in the water, you want someone else to do the swimming for you.
Regards,
Paul McKenzie
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
juss
so debbuger will not help me.
Do you know what the debugger is? The debugger allows you to run your program a single step at a time. You then can see where the program breaks down. Learn to use the debugger that comes with your compiler.
Learning how to debug your own code is mandatory in learning how to write programs. If you can't debug your own code, I suggest you get someone who knows how to write and debug programs to write the programs for you.
Otherwise, asking members on a forum to debug code you wrote without you doing any debugging yourself will get annoying very quickly.
Regards,
Paul McKenzie
Re: c++ audio programming (wave format)
I think that CodeGuru experts are being mean this time around. I was never taught how to use the debugger in school, I had to learn it myself. You all seem very condescending. Inexperience is not the same as ignorance. I feel you all owe an inexperienced programmer an apology :( I don't see him asking you to debug it for him, I see him asking you for help without knowledge of how to properly use the debugger.
@juss, what IDE are you using? (Xcode, Code::Blocks, eclipse, Visual Studios...) I'm sure we will gladly help you understand the debugger, it is certainly a tool that you will need at a professional level.
That said, wav is an ancient format that no one uses anymore. mp3 and mp4 have come to dominate the media landscape, and there are prebuilt libraries that allow you to handle files without the low level manipulation (ffmpeg, fmod, and openAL are the most popular, and fmod and openAL support wav natively)
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
ninja9578
I think that CodeGuru experts are being mean this time around. I was never taught how to use the debugger in school, I had to learn it myself.
When I went to school three decades ago, there were no interactive debuggers, and very few interactive terminals available for school use (in other words, punch cards). But the rule still stood that the student had to debug the code themselves, and only after debugging could they ask for help. Debugging in those days consisted of sitting down and carefully doing a run-through of your program by hand, writing down how variables were behaving at each step, and the best tool at the time for students were good old "print" statements to print diagnostic information. This just came naturally to the student when programs were not working correctly.
The beginner programmers of today and of the past few years have the luxury of not having to go through their code by hand, or writing print statements, or use "divide and conquer" methods to see how their programs are working due to the advancement of interactive debuggers. But the frustrating thing is that even if the programmer knew nothing of these tools, a few print statements here and there would indicate they've tried something to debug their code.
Just to post code that they've written themselves and not even take that rudimentary step of debugging their own code, even if they have to use print statements, is what I've been seeing on this site more often than not.
Regards,
Paul McKenzie
Re: c++ audio programming (wave format)
Quote:
Originally Posted by
ninja9578
I think that CodeGuru experts are being mean this time around. I was never taught how to use the debugger in school, I had to learn it myself. You all seem very condescending. Inexperience is not the same as ignorance. I feel you all owe an inexperienced programmer an apology :( I don't see him asking you to debug it for him, I see him asking you for help without knowledge of how to properly use the debugger.
Please. I should apologize for pointing out that he was wrong about the debugger not helping him? Preceding that sentence with 'Of course it will..' was offensive?? Pointing out that, yes, in the real world, programmers (good ones anyway) debug their own programs instead of dumping it on a forum & saying debug it for me? He was told to use his debugger & dismissively replied 'that won't help, if everyone could fix their own programs there wouldn't be a site like code guru'. I consider that rude--someone who's never posted on this site telling a veteran how the forum works & dismissing his advice.
Quote:
Originally Posted by
ninja9578
That said, wav is an ancient format that no one uses anymore.
I do, with multitracking audio software. You say 'everyone does this', 'no one does that' too much.