Python:将 wav 文件写入 numpy 浮点数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16778878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:39:26  来源:igfitidea点击:

Python: write a wav file into numpy float array

pythonnumpywav

提问by IAM

ifile = wave.open("input.wav")

how can I write this file into a numpy float array now?

我现在如何将此文件写入一个 numpy 浮点数组?

采纳答案by Joran Beasley

>>> from scipy.io.wavfile import read
>>> a = read("adios.wav")
>>> numpy.array(a[1],dtype=float)
array([ 128.,  128.,  128., ...,  128.,  128.,  128.])

typically it would be bytes which are then ints... here we just convert it to float type

通常它是字节,然后是整数......在这里我们只是将它转换为浮点类型

you can read about read here https://docs.scipy.org/doc/scipy/reference/tutorial/io.html#module-scipy.io.wavfile

你可以在这里阅读https://docs.scipy.org/doc/scipy/reference/tutorial/io.html#module-scipy.io.wavfile

回答by Esterlinkof

Use librosapackage and simply load wav file to numpy array with:

使用librosa包并简单地将 wav 文件加载到 numpy 数组:

y, sr = librosa.load(filename)

loads and decodes the audio as a time series y, represented as a one-dimensional NumPy floating point array. The variable sr contains the sampling rate of y, that is, the number of samples per second of audio. By default, all audio is mixed to mono and resampled to 22050 Hz at load time. This behavior can be overridden by supplying additional arguments to librosa.load().

将音频加载并解码为时间序列 y,表示为一维 NumPy 浮点数组。变量 sr 包含 y 的采样率,即音频每秒的采样数。默认情况下,所有音频都混合为单声道,并在加载时重新采样为 22050 Hz。可以通过向 librosa.load() 提供附加参数来覆盖此行为。

More information at Librosa library documentation

Librosa 库文档中的更多信息