WAVE文件理解

本文主要对wave文件做个说明

Wav 文件

WAV,即WAVE(Waveform Audio File Format, 波形音频文件格式),是微软资源交换文件格式(RIFF)规范的一个子集,用于存储数字音频文件。是一种无压缩格式文件

The WAVE file format, 由 文件头和文件本身构成. 一个 WAVE 文件是单独的 “WAVE” 块, 其有两部分组成:

  • a “fmt” chunk - 特殊的数据格式
  • a “data” chunk -包含实际数据

Wav 头文件

位置 描述
1 - 4 “RIFF” Marks the file as a riff file. Characters are each 1 byte long.
5 - 8 File size (integer) Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you’d fill this in after creation
9 -12 “WAVE” File Type Header. For our purposes, it always equals “WAVE”.
13-16 “fmt " Format chunk marker. Includes trailing null
17-20 16 Length of format data as listed above
21-22 1 Type of format (1 is PCM) - 2 byte integer
23-24 2 Number of Channels - 2 byte integer
25-28 44100 Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT). Sample Rate = Number of Samples per second, or Hertz.
29-32 176400 (Sample Rate * BitsPerSample * Channels) / 8.
33-34 4 (BitsPerSample * Channels) / 8.1 - 8 bit mono2 - 8 bit stereo/16 bit mono4 - 16 bit stereo
35-36 16 Bits per sample
37-40 “data” “data” chunk header. Marks the beginning of the data section.
41-44 File size (data) Size of the data section.

python 构建 Wav 头文件

点击查看代码
def create_wav_header(audio_size: int, sampleRate:int, bits:int, channel:int):
    header = b''
    header += b"RIFF"
    header += struct.pack('i', int(audio_size + 44 - 8))
    header += b"WAVEfmt "
    header += b'\x10\x00\x00\x00'
    header += b'\x01\x00'
    header += struct.pack('H', channel)
    header += struct.pack('i', sampleRate)
    header += struct.pack('i', int(sampleRate * bits / 8))
    header += struct.pack('H', int(channel * bits / 8))
    header += struct.pack('H', bits)
    header += b'data'
    header += struct.pack('i', audio_size)
    return header
	

*代码见 https://yuan1615.github.io/2022/09/02/WAV-File-Header/

posted on 2022-09-22 14:07  吾知  阅读(318)  评论(0编辑  收藏  举报

导航