繁体   English   中英

使用Wave Python模块获取和编写音频

[英]Using Wave Python Module to Get and Write Audio

因此,我正在尝试使用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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM