How would I play audio in Python 2.6.6 while displaying text?
I am trying to make a program with Python开发者_高级运维 2.6.6 that displays text while having audio playing in the background. I have only completed some of the text.
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."
I have a .wav file that I would like to play at the beginning of the program. I would also like the text to play in cue with the music (I would be able to specify when the text displays during the song such as 00:00:02). I assume this would be possible with an audio module of some sort.
Thanks!
I did a similar thing recently and used the audiere
module
import audiere
ds = audiere.open_device()
os = ds.open_array(input, fs)
os.play()
This will open the first available audio device, since you're on windows it's probably DirectSound. input
is just a numpy array, fs
is the sampling frequency (since the input is a raw array you'll need to specify that). os.play()
is a non-blocking call so you can print your txt or whatever you need to do at the same time, there are other methods to pause/stop etc. To play other file types I simply converted them to wav first.
Here's how I unpacked the wav file
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
So for example to make input
and fs
you could go:
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)
This suited me but my requirements were for FFT input, not karaoke :)
I'm sure audiere
has stereo playback just by giving a 2-dim array.
You may find pygame helpful.
http://www.pygame.org/docs/ref/mixer.html
What you need is the GStreamer for Python.
Check this tutorial, it's a good place to start.
EDIT: In Windows you can use the winsound module from standard library (Whoa! Python has that!) See winsound docs.
精彩评论