5

我正在寻找一种快速、最好是标准库机制来确定 wav 文件的位深度,例如“16 位”或“24 位”。

我正在使用对 Sox 的子进程调用来获取过多的音频元数据,但是子进程调用非常慢,我目前只能从 Sox 可靠地获取的唯一信息是位深度。

内置的 wave 模块没有“getbitdepth()”之类的功能,并且也不兼容 24 位 wav 文件 - 我可以使用“尝试除外”来使用 wave 模块访问文件元数据(如果有效,请手动记录它是 16 位的)然后除了调用 sox 之外(其中 sox 将执行分析以准确记录其位深度)。我担心的是,这种方法感觉像是猜测工作。如果读取一个 8 位文件怎么办?如果不是,我会手动分配 16 位。

SciPy.io.wavefile 也与 24 位音频不兼容,因此会产生类似的问题。

教程非常有趣,甚至包括一些非常低级(至少 Python 是低级)的脚本示例,用于从 wav 文件头中提取信息 - 不幸的是,这些脚本不适用于 16 位音频。

有什么方法可以简单地(并且不调用 sox)确定我正在检查的 wav 文件的位深度是多少?

我正在使用的波头解析器脚本如下:

import struct
import os

def print_wave_header(f):
    '''
    Function takes an audio file path as a parameter and 
    returns a dictionary of metadata parsed from the header
    '''
    r = {} #the results of the header parse
    r['path'] = f
    fin = open(f,"rb") # Read wav file, "r flag" - read, "b flag" - binary 
    ChunkID=fin.read(4) # First four bytes are ChunkID which must be "RIFF" in ASCII
    r["ChunkID"]=ChunkID
    ChunkSizeString=fin.read(4) # Total Size of File in Bytes - 8 Bytes
    ChunkSize=struct.unpack('I',ChunkSizeString) # 'I' Format is to to treat the 4 bytes as unsigned 32-bit inter
    TotalSize=ChunkSize[0]+8 # The subscript is used because struct unpack returns everything as tuple
    r["TotalSize"]=TotalSize
    DataSize=TotalSize-44 # This is the number of bytes of data
    r["DataSize"]=DataSize
    Format=fin.read(4) # "WAVE" in ASCII
    r["Format"]=Format
    SubChunk1ID=fin.read(4) # "fmt " in ASCII
    r["SubChunk1ID"]=SubChunk1ID
    SubChunk1SizeString=fin.read(4) # Should be 16 (PCM, Pulse Code Modulation)
    SubChunk1Size=struct.unpack("I",SubChunk1SizeString) # 'I' format to treat as unsigned 32-bit integer
    r["SubChunk1Size"]=SubChunk1Size
    AudioFormatString=fin.read(2) # Should be 1 (PCM)
    AudioFormat=struct.unpack("H",AudioFormatString) ## 'H' format to treat as unsigned 16-bit integer
    r["AudioFormat"]=AudioFormat[0]
    NumChannelsString=fin.read(2) # Should be 1 for mono, 2 for stereo
    NumChannels=struct.unpack("H",NumChannelsString) # 'H' unsigned 16-bit integer
    r["NumChannels"]=NumChannels[0]
    SampleRateString=fin.read(4) # Should be 44100 (CD sampling rate)
    SampleRate=struct.unpack("I",SampleRateString)
    r["SampleRate"]=SampleRate[0]
    ByteRateString=fin.read(4) # 44100*NumChan*2 (88200 - Mono, 176400 - Stereo)
    ByteRate=struct.unpack("I",ByteRateString) # 'I' unsigned 32 bit integer
    r["ByteRate"]=ByteRate[0]
    BlockAlignString=fin.read(2) # NumChan*2 (2 - Mono, 4 - Stereo)
    BlockAlign=struct.unpack("H",BlockAlignString) # 'H' unsigned 16-bit integer
    r["BlockAlign"]=BlockAlign[0]
    BitsPerSampleString=fin.read(2) # 16 (CD has 16-bits per sample for each channel)
    BitsPerSample=struct.unpack("H",BitsPerSampleString) # 'H' unsigned 16-bit integer
    r["BitsPerSample"]=BitsPerSample[0]
    SubChunk2ID=fin.read(4) # "data" in ASCII
    r["SubChunk2ID"]=SubChunk2ID
    SubChunk2SizeString=fin.read(4) # Number of Data Bytes, Same as DataSize
    SubChunk2Size=struct.unpack("I",SubChunk2SizeString)
    r["SubChunk2Size"]=SubChunk2Size[0]
    S1String=fin.read(2) # Read first data, number between -32768 and 32767
    S1=struct.unpack("h",S1String)
    r["S1"]=S1[0]
    S2String=fin.read(2) # Read second data, number between -32768 and 32767
    S2=struct.unpack("h",S2String)
    r["S2"]=S2[0]
    S3String=fin.read(2) # Read second data, number between -32768 and 32767
    S3=struct.unpack("h",S3String)
    r["S3"]=S3[0]
    S4String=fin.read(2) # Read second data, number between -32768 and 32767
    S4=struct.unpack("h",S4String)
    r["S4"]=S4[0]
    S5String=fin.read(2) # Read second data, number between -32768 and 32767
    S5=struct.unpack("h",S5String)
    r["S5"]=S5[0]
    fin.close()
    return r
4

2 回答 2

8

基本上与 Matthias 的答案相同,但使用可复制粘贴的代码。

要求

pip install soundfile

代码

import soundfile as sf

ob = sf.SoundFile('example.wav')
print('Sample rate: {}'.format(ob.samplerate))
print('Channels: {}'.format(ob.channels))
print('Subtype: {}'.format(ob.subtype))

解释

  • 声道:通常为 2,表示您有一个左扬声器和一个右扬声器。
  • 采样率:音频信号是模拟的,但我们希望以数字方式表示它们。这意味着我们希望在价值和时间上离散化它们。采样率给出了我们每秒获得值的次数。单位是赫兹。采样率需要至少是原始声音中最高频率的两倍,否则会出现混叠。人类的听觉范围从 ~20Hz 到 ~20kHz,因此您可以切断任何高于 20kHZ 的声音。这意味着超过 40kHz 的采样率没有多大意义。
  • 位深度:位深度越高,可以捕获的动态范围越大。动态范围是乐器、部分或音乐的最安静和最大音量之间的差异。典型值似乎是 16 位或 24 位。16 位的位深度的理论动态范围为 96 dB,而 24 位的动态范围为 144 dB()。
  • SubtypePCM_16表示 16 位深度,其中 PCM 代表Pulse-Code Modulation

选择

如果您只寻找命令行工具,那么我可以推荐MediaInfo

$ mediainfo example.wav
General
Complete name                            : example.wav
Format                                   : Wave
File size                                : 83.2 MiB
Duration                                 : 8 min 14 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 1 411 kb/s

Audio
Format                                   : PCM
Format settings                          : Little / Signed
Codec ID                                 : 1
Duration                                 : 8 min 14 s
Bit rate mode                            : Constant
Bit rate                                 : 1 411.2 kb/s
Channel(s)                               : 2 channels
Sampling rate                            : 44.1 kHz
Bit depth                                : 16 bits
Stream size                              : 83.2 MiB (100%)
于 2019-01-13T08:57:41.413 回答
7

我强烈推荐soundfile模块(但请注意,我很有偏见,因为我写了很大一部分)。

在那里,您可以将文件作为soundfile.SoundFile对象打开,该对象具有包含您要查找的信息的子类型属性。

在您的情况下,这可能是'PCM_16'or 'PCM_24'

于 2017-09-15T13:54:38.657 回答