使用 wavfile.read 从 wav 文件中提取位深度

Extract bit depth from wav file using wavfile.read

我试图使用 wavfile.read 从 wav 文件中读取位深度,但是,我没有成功。

这是我目前的代码:

import numpy as np
from numpy.fft import fft, rfft
import matplotlib.pyplot as plt
from scipy.io import wavfile
from scipy import signal
import librosa
import librosa.display

input_file = (r'G:/pt5GAL_TCL_mono_Mono.wav')
sample_rate1, samples = wavfile.read(input_file)

# File Info
signal, sample_rate2 = librosa.load(input_file)
print('Total number of samples: ', signal.shape[0])
print('Sample rate: ', sample_rate1)
print('Length of file in seconds: ', librosa.get_duration(signal))
# print('Bit Depth: ', bits_per_sample)

如何return wav文件的位深度?

您不能从 scipy.io.wavfile.read 的 return 值直接访问每个样本的位数,但您可以从 return 的 dtype 间接(并且可能含糊不清)确定它] 根据 https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html.

中显示的 table 编辑 NumPy 数组

当然,您可以只使用 Python 标准库中的 wave 模块,它有一个方法 getsampwidth.