1

我找到了这个站点:WAVE PCM soundfile format并编写了一个结构(代码如下)。但我不知道使用 .wav 文件需要执行哪些步骤。

我找不到任何描述使用 .wav (RIFF) 文件的步骤的书籍或指南。并描述如何读写(如何读取 RIFF 标头)。

我已经编写了以下代码,但现在我处于死胡同:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char        Subchunk1ID[4];
    int         Subchunk1Size;
    short int   AudioFormat;
    short int   NumChannels;
    int         SampleRate;
    int         ByteRate;
    short int   BlockAlign;
    short int   BitsPerSample;

} fmt;

typedef struct
{
    char        Subchunk2ID[4];
    int         Subchunk2Size;
    int         Data[441000]; // 10 secs of garbage. he-he)
} data;

typedef struct
{
    char        ChunkId[4];
    int         ChunkSize;
    char        Format[4];
    fmt         SubChunk_fmt;
    data        SubChunk_data;
} header;



int main()
{
    FILE *input; 
    fopen("input.wav", "r");

    /* How to read the RIFF header, how to manipulate it
       and how to access and manipulate 'data' sub chunk? */

    return 0;
}

PS 我不要求你代替我做一些工作,只是给我一些文献或链接,我将能够提高我在这个主题上的知识。

4

2 回答 2

1

Your question seems to be "I have a file format, how can I write a dissector for it in C". Given that you've stated that you don't know how to use fread(), my advice would be to look for a library that does it for you, such as libavcodec as mentioned by Filippo Savi. Otherwise, the question is too broad to be a fit for this site.

Writing a function to read in a file of known format is basically a lot of slog work. You have the file, which is a stream of bytes, you have the documentation, indicating, from the first byte on, exactly what you'll find, and how many bytes each thing holds. The work, then, involves reading everything in, one chunk at a time, and putting it into the appropriate place in your in-memory structure. (Or, if you really know what you're doing and are only interested in subsections, you can often use the knowledge of structure to read sizes and indexes, and then jump straight to some item in the middle that you want.)

The fread() function can be used to read things in one object at a time, or to read in a large chunk of data and process that in memory. The fread() function call looks like this: fread(ptr, size, count, in), so if you had a file handle named input, obtained by calling, for example, FILE *input = fopen("input.wav", "rb");, and you have a buffer called ptr, obtained, for example, by char *ptr = malloc(4096);, you could read the first three 4-byte elements of the file (which, according to the document you linked, are a signature, a size calculation, and another signature,) by calling fread(ptr, 4, 3, input). (You could also achieve the same goal by calling fread(ptr, 12, 1, input);, or fread(ptr, 1, 12, input);.)

This example might be enough to get you started, but if it isn't, then your question is "how do I do general file I/O and/or data manipulation in C", which I don't intend to try to answer. Sorry.

于 2013-05-05T20:17:08.320 回答
1

众所周知的格式和偏移量应该很容易获取数据并使用 fread() 将它们放入结构中,以便从文件中获取二进制数据

如果你想播放你得到的东西,那么你需要某种库,但如果你想播放声音,我建议你使用经过良好测试的编解码器库,如 libavcodec

PS你当然需要先用 fopen() 打开文件

于 2013-05-05T19:12:16.570 回答