【发布时间】:2016-11-03 11:06:11
【问题描述】:
scipy.io.wavfile.read 的文档说它返回采样率和数据。但是对于 .wav 文件,这里的数据实际上意味着什么?
谁能通俗地告诉我这些数据是如何准备的?
PS。我在某处读到这意味着幅度?我读的正确吗?如果是,scipy.io.wavfile.readscipy.io.wavfile.read 是如何计算和返回的幅度?
【问题讨论】:
scipy.io.wavfile.read 的文档说它返回采样率和数据。但是对于 .wav 文件,这里的数据实际上意味着什么?
谁能通俗地告诉我这些数据是如何准备的?
PS。我在某处读到这意味着幅度?我读的正确吗?如果是,scipy.io.wavfile.readscipy.io.wavfile.read 是如何计算和返回的幅度?
【问题讨论】:
scipy.io.wavfile.read 是一个方便的包装器,用于将.wav 文件分解为文件头和文件中包含的数据。
Returns
-------
rate : int
Sample rate of wav file.
data : numpy array
Data read from wav file. Data-type is determined from the file;
see Notes.
来自源代码的简化代码:
fid = open(filename, 'rb')
try:
file_size, is_big_endian = _read_riff_chunk(fid) # find out how to read the file
channels = 1 # assume 1 channel and 8 bit depth if there is no format chunk
bit_depth = 8
while fid.tell() < file_size: #read the file a couple of bytes at a time
# read the next chunk
chunk_id = fid.read(4)
if chunk_id == b'fmt ': # retrieve formatting information
fmt_chunk = _read_fmt_chunk(fid, is_big_endian)
format_tag, channels, fs = fmt_chunk[1:4]
bit_depth = fmt_chunk[6]
if bit_depth not in (8, 16, 32, 64, 96, 128):
raise ValueError("Unsupported bit depth: the wav file "
"has {}-bit data.".format(bit_depth))
elif chunk_id == b'data':
data = _read_data_chunk(fid, format_tag, channels, bit_depth,is_big_endian, mmap)
finally:
if not hasattr(filename, 'read'):
fid.close()
else:
fid.seek(0)
return fs, data
数据本身通常是 PCM 表示的不同通道的连续帧中的声压级。 scipy.io.wavfile.read 返回的采样率是确定一秒钟代表多少帧所必需的。
question 提供了对 .wav 格式的一个很好的解释。
scipy 本身不会计算太多。
【讨论】: