如何在Python 2.6.6中播放音频同时显示文本?
我正在尝试用Python 2.6.6写一个程序,想要在后台播放音频的同时显示文字。目前我只完成了一部分文字。
print "Well here we are again"
print "It’s always such a pleasure"
print "Remember when you tried to kill me twice?"
print "Oh, how we laughed and laughed"
print "Except I wasn’t laughing"
print "Under the circumstances I’ve been shockingly nice."
我有一个.wav格式的音频文件,想在程序开始时播放它。我还希望文字能和音乐同步播放(我可以指定文字在歌曲中的显示时间,比如00:00:02)。我想这应该可以通过某种音频模块来实现。
谢谢!
3 个回答
1
你可能会觉得pygame很有用。
4
我最近做了类似的事情,使用了audiere模块。
import audiere
ds = audiere.open_device()
os = ds.open_array(input, fs)
os.play()
这个模块会打开第一个可用的音频设备,因为你在用Windows,所以很可能是DirectSound。input
只是一个numpy数组,fs
是采样频率(因为输入是一个原始数组,你需要指定这个频率)。os.play()
是一个非阻塞的调用,这意味着你可以同时打印文本或者做其他事情,还有其他方法可以暂停或停止播放等。为了播放其他类型的文件,我只是先把它们转换成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支持立体声播放,只需要提供一个二维数组。