在 Python 中读取 *.wav 文件 [英] Reading *.wav files in Python

查看:33
本文介绍了在 Python 中读取 *.wav 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要分析用 .wav 文件编写的声音.为此,我需要将此文件转换为一组数字(例如,数组).我想我需要使用wave包.但是,我不知道它究竟是如何工作的.例如我做了以下事情:

I need to analyze sound written in a .wav file. For that I need to transform this file into set of numbers (arrays, for example). I think I need to use the wave package. However, I do not know how exactly it works. For example I did the following:

import wave
w = wave.open('/usr/share/sounds/ekiga/voicemail.wav', 'r')
for i in range(w.getnframes()):
    frame = w.readframes(i)
    print frame

由于此代码,我希望将声压视为时间的函数.相比之下,我看到了很多奇怪、神秘的符号(不是十六进制数字).任何人都可以请帮我解决这个问题吗?

As a result of this code I expected to see sound pressure as function of time. In contrast I see a lot of strange, mysterious symbols (which are not hexadecimal numbers). Can anybody, pleas, help me with that?

推荐答案

Per 文档scipy.io.wavfile.read(somefile) 返回一个包含两项的元组:第一个是 采样率 以每秒样本数为单位,第二个是一个 numpy 数组,其中包含所有读取的数据文件:

Per the documentation, scipy.io.wavfile.read(somefile) returns a tuple of two items: the first is the sampling rate in samples per second, the second is a numpy array with all the data read from the file:

from scipy.io import wavfile
samplerate, data = wavfile.read('./output/audio.wav')

这篇关于在 Python 中读取 *.wav 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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