Question

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);
}
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top