【问题标题】:Using Wave Python Module to Get and Write Audio使用 Wave Python 模块获取和编写音频
【发布时间】:2012-04-09 16:52:41
【问题描述】:

所以,我正在尝试使用 Python Wave 模块来获取音频文件,并基本上从中获取所有帧,检查它们,然后将它们写回另一个文件。我试图将我正在读取的声音输出到另一个文件,但它要么是噪音,要么根本没有声音。所以,我很确定我没有分析文件并获得正确的帧......?我正在处理一个立体声 16 位声音文件。虽然我可以使用更简单的文件来理解这个过程,但我最终希望能够接受任何类型的声音文件来使用,所以我需要了解问题所在。

我还注意到 Wave 模块无法读取 32 位声音文件 - 它给了我“未知格式”的错误。有什么想法吗?是否可以绕过它,以便我至少可以读取 32 位音频文件,即使我只能“渲染”16 位文件?

我有点意识到左右声道之间的波形文件是交错的(左声道的第一个样本,右声道的第二个样本,等等)。但是我如何分离通道?这是我的代码。我剪掉了输出代码,看看我是否正确地读取了文件。我正在使用 Python 2.7.2:

import scipy
import wave
import struct
import numpy
import pylab

fp = wave.open('./sinewave16.wav', 'rb') # Problem loading certain kinds of wave files in binary?

samplerate = fp.getframerate()
totalsamples = fp.getnframes()
fft_length = 2048 # Guess
num_fft = (totalsamples / fft_length) - 2

temp = numpy.zeros((num_fft, fft_length), float)

leftchannel = numpy.zeros((num_fft, fft_length), float)
rightchannel = numpy.zeros((num_fft, fft_length), float)

for i in range(num_fft):

    tempb = fp.readframes(fft_length / fp.getnchannels() / fp.getsampwidth());

    #tempb = fp.readframes(fft_length)

    up = (struct.unpack("%dB"%(fft_length), tempb))

    #up = (struct.unpack("%dB"%(fft_length * fp.getnchannels() * fp.getsampwidth()), tempb))
    #print (len(up))
    temp[i,:] = numpy.array(up, float) - 128.0


temp = temp * numpy.hamming(fft_length)

temp.shape = (-1, fp.getnchannels())

fftd = numpy.fft.rfft(temp)

pylab.plot(abs(fftd[:,1]))

pylab.show()

#Frequency of an FFT should be as follows:

#The first bin in the FFT is DC (0 Hz), the second bin is Fs / N, where Fs is the sample rate and N is the size of the FFT. The next bin is 2 * Fs / N. To express this in general terms, the nth bin is n * Fs / N.
# (It would appear to me that n * Fs / N gives you the hertz, and you can use sqrt(real portion of number*r + imaginary portion*i) to find the magnitude of the signal

目前,这将加载声音文件,将其解压缩到一个结构中,并绘制声音文件以便我查看它,但我认为它没有获取所有音频文件,或者它没有得到它正确。我是否将波形文件正确读入结构?是否有任何关于使用 Python 读取和分析波形/音频文件的最新资源?任何帮助将不胜感激。

【问题讨论】:

标签: python audio


【解决方案1】:

也许你应该试试 SciPy io.wavefile 模块:

http://docs.scipy.org/doc/scipy/reference/io.html

【讨论】:

  • 感谢您的建议。我去看看。
  • 我刚查了一下,音频读的很清楚,很好。感谢您的建议。
  • 链接失效了,这是新链接:docs.scipy.org/doc/scipy/reference/io.html
  • @Kaifei,感谢您的更新。我编辑了答案并更新了链接。
猜你喜欢
  • 2017-04-08
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多