2011-05-23 36 views
2

我想用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)。我認爲這可以通過某種音頻模塊來實現。

謝謝!

+0

你想要你的答案, 這就是我要指望的, 我曾經想讓你死,但是, 現在我只想要更多的聲望。 – wim 2011-05-24 08:18:13

+0

Portal 2是一款非常棒的遊戲。 :3 – 2011-05-25 00:05:12

回答

3

我最近做了類似的事情和所使用的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 

因此,例如,使inputfs你可以去:

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陣列。

1

您需要的是適用於Python的GStreamer。

查看this教程,這是一個很好的開始。

編輯: 在Windows中,你可以使用來自標準庫(哇Python有那個!)的廣州市運生模塊見winsound文檔。

+0

我正在運行Windows,並從我收集的GStreamer不兼容。我將如何使用內置於Python的工具來完成這項任務,或者這是不可能的? – 2011-05-23 22:41:12

+0

是的,你可以在Windows中做到這一點。檢查編輯的答案。 – 2011-05-24 07:28:10

相關問題