c - 将 2 channel WAV 文件解交织为两个包含原始数据的文本文件

标签 c

我有一个基于上述问题的作业。问题中已知采样频率和每个样本的大小。我只需要了解所需的编码类型。

最佳答案

使用文件格式规范,例如 this one了解如何读取文件头、确定采样率、比特率等。

The canonical WAVE format starts with the RIFF header:

0         4   ChunkID          Contains the letters "RIFF" in ASCII form
                               (0x52494646 big-endian form).
4         4   ChunkSize        36 + SubChunk2Size, or more precisely:
                               4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
                               This is the size of the rest of the chunk 
                               following this number.  This is the size of the 
                               entire file in bytes minus 8 bytes for the
                               two fields not included in this count:
                               ChunkID and ChunkSize.
8         4   Format           Contains the letters "WAVE"
                               (0x57415645 big-endian form).

The "WAVE" format consists of two subchunks: "fmt " and "data":
The "fmt " subchunk describes the sound data's format:

12        4   Subchunk1ID      Contains the letters "fmt "
                               (0x666d7420 big-endian form).
16        4   Subchunk1Size    16 for PCM.  This is the size of the
                               rest of the Subchunk which follows this number.
20        2   AudioFormat      PCM = 1 (i.e. Linear quantization)
                               Values other than 1 indicate some 
                               form of compression.
22        2   NumChannels      Mono = 1, Stereo = 2, etc.
24        4   SampleRate       8000, 44100, etc.
28        4   ByteRate         == SampleRate * NumChannels * BitsPerSample/8
32        2   BlockAlign       == NumChannels * BitsPerSample/8
                               The number of bytes for one sample including
                               all channels. I wonder what happens when
                               this number isn't an integer?
34        2   BitsPerSample    8 bits = 8, 16 bits = 16, etc.
          2   ExtraParamSize   if PCM, then doesn't exist
          X   ExtraParams      space for extra parameters

The "data" subchunk contains the size of the data and the actual sound:

36        4   Subchunk2ID      Contains the letters "data"
                               (0x64617461 big-endian form).
40        4   Subchunk2Size    == NumSamples * NumChannels * BitsPerSample/8
                               This is the number of bytes in the data.
                               You can also think of this as the size
                               of the read of the subchunk following this 
                               number.
44        *   Data             The actual sound data.

之后,您将找到原始 pcm 数据,如交错

[sample 1      ][sample 2      ]
[s1,ch1][s1,ch2][s2,ch1][s2,ch2]

您可以以写入、二进制模式打开每个样本的文本文件,然后循环音频数据,读取单个样本/ channel 的字节,然后使用 fprintffwrite 将它们写入正确的文件。

关于c - 将 2 channel WAV 文件解交织为两个包含原始数据的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748191/

相关文章:

c - 如何在 C 中将十六进制转换为字节数组

c - 正确播种 rand_r(int *val) 方法

c - 加密数据作为字符串

python - 使用 Pycparser 解析变量依赖关系

c - 是否可以将 linux 计时器通知信号定向到特定线程?

c - 一台服务器绑定(bind)不同的端口

c - 如何释放指向保存地址的指针的指针

c - 检测 C 头文件中的 undefined symbol

c++ - 在发送数据之前压缩数据 - 控制字符?

java - 我正在尝试 JNI 如何在 Linux 中设置 java.library.path