3

我正在尝试用 C 编写一个程序,将捕获的Raw 16kHz PCM 16 位文件转换为16 位 WAV

我读过一些帖子,有人推荐使用libsox. 安装它,现在我真的很难理解手册页

到目前为止(通过阅读源 dist 中的示例)我发现structs

  • sox_format_t
  • sox_signalinfo_t

大概可以用来描述我输入的数据。如果有必要,我也知道我正在处理多少信息(时间)?

一些指导表示赞赏!

4

2 回答 2

5

好吧,问题得到了解答。但是,我将在此处发布代码,以便人们可以对其进行分析或仅在自己的项目中使用它。

“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);
}
于 2013-01-03T11:20:25.837 回答
2

我建议手动编写 WAV 标头和数据,PCM 非常简单: https ://web.archive.org/web/20080706175634/https://ccrma.stanford.edu/courses/422/projects/WaveFormat /

更新:原始链接https://ccrma.stanford.edu/courses/422/projects/WaveFormat/失效。

于 2012-12-14T16:34:56.883 回答