我最近做了類似的事情和所使用的audiere
模塊
import audiere
ds = audiere.open_device()
os = ds.open_array(input, fs)
os.play()
這將打開第一個可用的音頻設備,因爲你使用的是Windows,它可能DirectSound的。 input
只是一個numpy數組,fs
是採樣頻率(因爲輸入是一個原始數組,您需要指定)。 os.play()
是一個非阻塞呼叫,所以你可以打印你的TXT或任何你需要做的同時,還有其他方法來暫停/停止等。要播放其他文件類型,我簡單地將它們轉換爲WAV第一。
以下是我解壓縮WAV文件
def wave_unpack(fname):
"""
input: wave filename as string
output: left, right, params
unpacks a wave file and return left and right channels as arrays
(in case of a mono file, left and right channels will be copies)
params returns a tuple containing:
-number of audio channels (1 for mono, 2 for stereo)
-sample width in bytes
-sampling frequency in Hz
-number of audio frames
-compression type
-compression name
"""
import sndhdr, os, wave, struct
from scipy import array
assert os.path.isfile(fname), "file location must be valid"
assert sndhdr.what(fname)[0] == 'wav', "file must have valid header"
try:
wav = wave.open(fname)
params = (nchannels,sampwidth,rate,nframes,comp,compname) = wav.getparams()
frames = wav.readframes(nframes*nchannels)
finally:
wav.close()
out = struct.unpack_from ("%dh" % nframes*nchannels, frames)
if nchannels == 2:
left = array(out[0::2])
right = array(out[1::2])
elif nchannels == 1:
right = left = array(out)
else:
assert 0, "number of channels must be 1 or 2"
return left, right, params
因此,例如,使input
和fs
你可以去:
from scipy import c_
left, right, params = wave_unpack(fn)
fs = params[2]
left_float = left.astype('f')/2**15
right_float = right.astype('f')/2**15
stereo = c_[left_float, right_float]
input = mono = stereo.mean(1)
這適合我,但我的要求是針對FFT的輸入,而不是卡拉OK :)
我敢肯定audiere
有立體聲播放,只是通過給2-dim陣列。
來源
2011-05-24 08:14:39
wim
你想要你的答案, 這就是我要指望的, 我曾經想讓你死,但是, 現在我只想要更多的聲望。 – wim 2011-05-24 08:18:13
Portal 2是一款非常棒的遊戲。 :3 – 2011-05-25 00:05:12