Coverting PCM 16bit LE to WAV

北城以北 提交于 2019-12-10 10:06:48

问题


I'm trying to write a program in C that converts a captured Raw 16kHz PCM 16-bit file to a 16-bit WAV.

I've read some posts and people recommended using libsox. Installed it and now i'm really struggling with understanding the man-page.

So far (by reading the example in the source dist) I've figured out that the structs:

  • sox_format_t
  • sox_signalinfo_t

can probably be used to describe the data I'm inputting. I also know how much info i'm processing (time) if that is somehow necessary?

Some guidance is appreciated!


回答1:


I would recommend to write WAV header and data manually, it is really very simple for PCM : https://ccrma.stanford.edu/courses/422/projects/WaveFormat/




回答2:


Well, It worked out as the question is answered. However I'll post the code here so that people can analyze it or just use it in their own projects.

The link provided by both "simonc" and "Nickolay O." was used. Search the web for more info on the individual fields.

struct wavfile
{
    char        id[4];          // should always contain "RIFF"
    int     totallength;    // total file length minus 8
    char        wavefmt[8];     // should be "WAVEfmt "
    int     format;         // 16 for PCM format
    short     pcm;            // 1 for PCM format
    short     channels;       // channels
    int     frequency;      // sampling frequency, 16000 in this case
    int     bytes_per_second;
    short     bytes_by_capture;
    short     bits_per_sample;
    char        data[4];        // should always contain "data"
    int     bytes_in_data;
};

//Writes a header to a file that has been opened (take heed that the correct flags
//must've been used. Binary mode required, then you should choose whether you want to 
//append or overwrite current file
void write_wav_header(char* name, int samples, int channels){
    struct wavfile filler;
    FILE *pFile;
    strcpy(filler.id, "RIFF");
    filler.totallength = (samples * channels) + sizeof(struct wavfile) - 8; //81956
    strcpy(filler.wavefmt, "WAVEfmt ");
    filler.format = 16;
    filler.pcm = 1;
    filler.channels = channels;
    filler.frequency = 16000;
    filler.bits_per_sample = 16;
    filler.bytes_per_second = filler.channels * filler.frequency * filler.bits_per_sample/8;
    filler.bytes_by_capture = filler.channels*filler.bits_per_sample/8;
    filler.bytes_in_data = samples * filler.channels * filler.bits_per_sample/8;    
    strcpy(filler.data, "data");
    pFile = fopen(name, "wb");
    fwrite(&filler, 1, sizeof(filler), pFile);
    fclose(pFile);
}


来源:https://stackoverflow.com/questions/13881574/coverting-pcm-16bit-le-to-wav

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!