如何获取.wav文件格式的numpy数组输出

3

我是Python的新手,我正在尝试训练我的音频语音识别模型。我想读取一个.wav文件,并将该文件的输出转换为Numpy数组。我该如何做?


1
类似这样的:scipy.io.wavfile.read - undefined
2个回答

6

根据 @Marco 的评论,您可以查看 Scipy 库,特别是 scipy.io

from scipy.io import wavfile

要读取您的文件('filename.wav'),只需执行以下操作:

output = wavfile.read('filename.wav')

这将输出一个元组 (我命名为 'output'):

  • output[0], 采样率
  • output[1], 你想要分析的样本数组

从scipy.io中导入wavfile output = wavfile.read('filename.wav'),这样我能得到numpy数组吗? - undefined
是的,output[1] 是一个包含声音振幅的 np.array(每秒钟的读数由你的采样率 output[0] 指示)。 - undefined

5
这可以通过几行代码使用内置的wave和显然的numpy实现。您不需要使用librosascipysoundfile。最新版本给我在读取wav文件时带来了问题,这也是我现在写这篇文章的原因。
import numpy as np
import wave

# Start opening the file with wave
with wave.open('filename.wav') as f:
    # Read the whole file into a buffer. If you are dealing with a large file
    # then you should read it in blocks and process them separately.
    buffer = f.readframes(f.getnframes())
    # Convert the buffer to a numpy array by checking the size of the sample
    # with in bytes. The output will be a 1D array with interleaved channels.
    interleaved = np.frombuffer(buffer, dtype=f'int{f.getsampwidth()*8}')
    # Reshape it into a 2D array separating the channels in columns.
    data = np.reshape(interleaved, (-1, f.getnchannels()))

我喜欢将其封装为一个函数,该函数返回采样频率并使用 pathlib.Path 对象。这样可以使用 sounddevice 播放。

# play_wav.py
import sounddevice as sd
import numpy as np
import wave

from typing import Tuple
from pathlib import Path


# Utility function that reads the whole `wav` file content into a numpy array
def wave_read(filename: Path) -> Tuple[np.ndarray, int]:
    with wave.open(str(filename), 'rb') as f:
        buffer = f.readframes(f.getnframes())
        inter = np.frombuffer(buffer, dtype=f'int{f.getsampwidth()*8}')
        return np.reshape(inter, (-1, f.getnchannels())), f.getframerate()


if __name__ == '__main__':
    # Play all files in the current directory
    for wav_file in Path().glob('*.wav'):
        print(f"Playing {wav_file}")
        data, fs = wave_read(wav_file)
        sd.play(data, samplerate=fs, blocking=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接