无法让 scipy.io.wavfile.read() 工作 [英] Can't get scipy.io.wavfile.read() to work

查看:66
本文介绍了无法让 scipy.io.wavfile.read() 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 .wav 文件读入一个数组,以便我可以绘制该数组并执行 FFT.我用 wave 模块打开了文件,现在我正在挣扎.我被建议使用 scipy.io.wavfile.read(filename, mmap=False) 但我没有任何运气.这个函数应该完全按照我的意愿去做,但事实并非如此.我正在运行 Python 2.7,也许就是这样.请帮我弄清楚如何进行这项工作.我写的代码如下.

导入scipy进口浪潮harp=wave.open('/Users/williamweiss2/Desktop/Test 2/harp.wav','r')frame_harp=harp.getnframes()harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)

这是我尝试运行程序时遇到的错误.

---> harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)

AttributeError: 'module' 对象没有属性 'io'

任何帮助将不胜感激.提前致谢.

解决方案

你弄糊涂了 SciPy 的 WAV 模块,带有 Python的.去掉import wave,使用import scipy.io.wavfile,调用scipy.io.wavfile.read.

示例:

<预><代码>>>>导入 scipy.io.wav 文件>>>FSample, samples = scipy.io.wavfile.read('myfile.wav')

SciPy 的模块为您完成从字节字符串到数字的转换工作,这与 Python 的模块不同.有关更多详细信息,请参阅链接的文档.

I am trying to read a .wav file into an array so that I can then plot the array and do a FFT. I got the file open with the wave module and now I am struggling. I was advised to use scipy.io.wavfile.read(filename, mmap=False) but am not having any luck. This function should do exactly what I want it to do but it isn't. I am running Python 2.7 and maybe that is it. Please help me figure out how to make this work. The code I have written is below.

import scipy
import wave
harp=wave.open('/Users/williamweiss2/Desktop/Test 2/harp.wav','r')
frames_harp=harp.getnframes()
harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)

This is the error I am getting when I try to run the program.

---> harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)

AttributeError: 'module' object has no attribute 'io'

Any help would be greatly appreciated. Thanks in advance.

解决方案

You have confused SciPy's WAV module with Python's. Remove import wave, use import scipy.io.wavfile, and call scipy.io.wavfile.read.

Example:

>>> import scipy.io.wavfile
>>> FSample, samples = scipy.io.wavfile.read('myfile.wav')

SciPy's module does the job of converting from a byte string to numbers for you, unlike Python's module. See the linked docs for more details.

这篇关于无法让 scipy.io.wavfile.read() 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆