2012-12-14 115 views
3

我想用C被捕獲的原始16kHz的PCM 16bit的文件轉換爲16位WAV寫一個程序。Coverting PCM 16bit的LE爲WAV

我讀過一些帖子和人們推薦使用libsox。安裝它,現在我真的很難理解man-page

到目前爲止(通過讀取源DIST的例子),我已經想通了,該structs

  • sox_format_t
  • sox_signalinfo_t

大概可以用來描述數據我在輸入。我也知道我在處理多少信息(時間),如果這是某種必要的話?

一些指導表示讚賞!

+1

如果你想要的唯一輸出格式是WAV,我會跳過學習任何第三方API並自己寫。 WAV的[文件格式](https://ccrma.stanford.edu/courses/422/projects/WaveFormat/)非常簡單。 – simonc

+0

似乎值得一試。我會試一試! – Mazze

回答

4

好,它的工作的問題的答案。不過,我會在這裏發佈代碼,以便人們可以分析它,或者只是在自己的項目中使用它。

由「simonc」和「Nickolay O」提供的鏈接。被使用。在網上搜索有關各個字段的更多信息。

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); 
}