1

I'm having trouble writing a short buffer into a wav file on my HD. I followed a few tutorials, but they all give different ways of doing it. Anyway, this is the way I implemented it but for some reason it doesn't work. When I try to print out the result of outfile, I get a 0, and nothing is written to disk. Also, I tried different paths, sometimes with and sometimes without a file name.

UPDATE: When I change the path to only a file name (e.g. hello.wav and not ~/Documents/hello.wav), printing out the outfile returns a memory location such as 0x2194000.

void gilbertAnalysis::writeWAV(float * buffer, int bufferSize){
    SF_INFO sfinfo ;
    sfinfo.channels = 1;
    sfinfo.samplerate = 44100;
    sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;

    const char* path = "~/Documents/hello.wav";

    SNDFILE * outfile = sf_open(path, SFM_WRITE, &sfinfo);
    sf_count_t count = sf_write_float(outfile, &buffer[0], bufferSize) ;
    sf_write_sync(outfile);
    sf_close(outfile);
}
nevos
  • 907
  • 1
  • 10
  • 22

2 Answers2

2

From the libsndfile docs:

On success, the sf_open function returns a non-NULL pointer which should be passed as the first parameter to all subsequent libsndfile calls dealing with that audio file. On fail, the sf_open function returns a NULL pointer. An explanation of the error can obtained by passing NULL to sf_strerror.

If outfile is 0x2194000 and not null, then you probably opened the file correctly.

Try using sf_strerror() to see what error you had when you provided a full file path and not just the file name.

bjornruffians
  • 671
  • 5
  • 16
  • Thanks! It gives me "No such file or directory" error. But, I know that there is no such file, I'm trying to create one. When I only use the filename (nevo.wav), it doesn't give me this error even though I know that the file doesn't exist as well. – nevos Mar 20 '14 at 11:53
  • Perhaps sf_open() doesn't like the use of '~'. Does it work if you provide an absolute path (/home//Documents/hello.wav)? – bjornruffians Mar 20 '14 at 13:12
1

Tilde (~) in file names expands to home directory by shell. For library functions file path must be full, either absolute or relative.

dimich
  • 1,305
  • 1
  • 5
  • 7