0

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;
} 
  • Are you checking the return values for `sf_open` and `sf_write_short` to see if they are successful? – Jim Rhodes May 03 '16 at 19:18
  • Thanks for the response Jim Rhodes, I've added the tests I've made that both returned true for sf_open and sf_write_short to an edit of my question. Please tell me if I have done this check correctly. – Dean Cooksey May 03 '16 at 20:57
  • 1
    Is the fact that this is data that originated from an IFFT relevant? – Oliver Charlesworth May 03 '16 at 21:13
  • TBH. .wav is such a simple format (RIFF, 44 byte header) that you can easily implement it yourself. We just have the header pre-filled with all but the file/data length, so when we get the `vector` we just fill in those 2 fields, write out the completed header and then the raw vector contents. – MSalters May 04 '16 at 10:59
  • There is not enough information to reproduce described behaviour. I tried to do it with my own data and it worked... so the error may come from something else. – alpereira7 Jan 08 '19 at 11:21

0 Answers0