Search code examples
caudiolibvlclibsndfile

Writing a raw audio to a file?


I'm writing a piece of code in C for pocketsphinx module of freeswitch to save the utterance (waveform or audio) to a file. I receive the audio as a void *data and its unsigned int len and I have to save it as a RAW (or PCM) file (no headers). How do I do this?

I've tried this:

FILE *_file;
int16_t *_data;
_data = (int16_t *) data;
_file=fopen("utterance","ab");
fwrite(data, sizeof(_data[0]), sizeof(_data)/sizeof(_data[0]), _file);
fclose(_file);
_file=NULL;

It doesn't work (Maybe I'm not doing it right?). I've also found libvlc and libsndfile but haven't found any function that'd serve me. Anyone here have a simple example/tutorial on this?

I'm working on C, VS2010, Win8.1 (x64)


Solution

  • you shouldn't use sizeof(_data)/sizeof(_data[0]) when _data is a pointer; sizeof(_data) is how many bytes the pointer _data takes

    size_t valueCount = sampleCount * channelCount;
    FILE *_file;
    int16_t *_data;
    _data = (int16_t *) data;
    _file=fopen("utterance","ab");
    fwrite(data, sizeof(_data[0]), valueCount, _file);
    fclose(_file);
    _file=NULL;
    

    you could also use

    fwrite(data, 1, len, _file);