I am new to Audio Programming and have been having quite some trouble using Libsndfile to write a 16bit wav file. So the part where I am showing the code is from after the IFFT data has been retrieved. At the moment I'm managing to create "test.wav" But is of size 0, containing no information; a junk file. Any help would be astronomically appreciated, thank you!
The extracted relevant code:
short audioStream[65536];
float ifftData[65536];
//Represent float data as short
for(int i = 0; i < 65536; i++) {
audioStream[i] = ifftData[i] * 32767;
}
const char *fname = "test.wav";
//SNDFILE
create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
void create_file (const char * fname, int format) {
SNDFILE *file2;
SF_INFO x;
x.samplerate = 44100;
x.frames = 65536;
x.format = format;
x.channels = 1;
file2 = sf_open(fname,SFM_WRITE,&x);
sf_write_short(file2, audioStream, 65536);
sf_close(file2);
}
EDIT: Added additional information on tests made on sf_open() and sf_write_short(). Both == True
if(!file2) {
std::cout << "WAV FILE IS NOT ABLE TO OPEN" << std::endl;
}else if(file2){
std::cout << "WAV FILE OPENED" << std::endl;
}
if(!sf_write_short(file2, audioStream, 65536)){
std::cout << "ERROR WRITING TO WAVE" << std::endl;
}else if(sf_write_short(file2, audioStream, 65536)){
std::cout << "WROTE AUDIOSTREAM TO WAVE" << std::endl;
}