I am trying to read a text file with paths of wave files, and then performing FFT on them.
However sometimes either i met with std::bad_alloc
error, or it will just hang there and not run, and the memory will just build up to 100+Gb (i open a separate terminal and monitor htop
), im guesing this is a memory leak issue?
And sometimes (1% of the time), it will just run smoothly.
The text file:
name1 /path/to/this/wavpath/name1.wav
name2 /path/to/this/wavpath/name2.wav
name100 /path/to/this/wavpath/name100.wav
main script:
int main (int argc, char ** argv) {
// Init (Time) : These are variables for me to count the time taken for ReadingAudio and PerformingFFT
struct timeval start, end;
double time_taken;
double SUM_readaudio, SUM_computeFFT;
double AVG_readaudio, AVG_computeFFT;
vector<double> Dur_readaudio(100,0), Dur_computeFFT(100,0);
gettimeofday(&start, NULL); ios_base::sync_with_stdio(false);
// Init (Read Audio) : These are variables for me to count the time taken for Reading Audio
int AUDIO_DURATION = 5*8000;
vector<double> buffer;
// Init (FFT) : These are variables for me to count the time taken for PerformingFFT
int nfft = 372;
int hop_length = nfft/2;
int numFrames;
vector<vector<double> > Spectrogram(numFrames, vector<double>(nfft/2));
cout << "\n==== Params =====" << endl;
cout << " This exec name = " << argv[0] << endl;
const char* wav_scp = argv[1];
printf("wav_scp = %s\n", wav_scp);
// Create Window
vector<double> windowFunction = computeWindow(nfft);
// Reading wav.scp
unsigned space_idx;
ifstream file(wav_scp);
string uttid, wavpath, curr_line;
int npt=0;
string line;
if (file.is_open()) {
while (getline(file, line)) {
// Get uttid and string path
curr_line = line.c_str();
space_idx = curr_line.find(" ");
uttid = curr_line.substr(0, space_idx);
wavpath = curr_line.substr(space_idx+1, curr_line.length());
gettimeofday(&start, NULL); ios_base::sync_with_stdio(false);
/* Read Audio */
gettimeofday(&end, NULL);
time_taken = get_duration(start, end);
cout << "time_taken=" << time_taken << endl;
gettimeofday(&start, NULL); ios_base::sync_with_stdio(false);
/* Compute FFT */
gettimeofday(&end, NULL);
time_taken = get_duration(start, end);
cout << "time_taken=" << time_taken << endl;
npt++;
}
file.close();
}
cout << "Done" << endl;
return 0 ;
}
I omitted the Read audio
and Compute FFT
because i dont think they induce the problem,
as even when i omit them, i face the same problem.
I sometimes have the following error message:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
Some observations:
\\ Inits (*)
, as well as all \\ Read Audio
& \\ Compute FFT
codes, it can run successfullyInits
, without the \\ Read Audio
& \\ Compute FFT
codes, the bad_alloc sometimes happens.\\ Get uttid and string path
, and assign a fixed wavpath
, it can run the \\ Read Audio
& \\ Compute FFT
smoothly. (although it is still iterating the text file, except the lines are not being used)The variable numFrames isn't initialised. This is undefined behaviour.
Typically this should result in Spectrogram having an arbitrary and most likely very high size, which should explain the memory problem.