Skip to content

Instantly share code, notes, and snippets.

@atran
Created August 20, 2014 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atran/afb94ea1e3550e104598 to your computer and use it in GitHub Desktop.
Save atran/afb94ea1e3550e104598 to your computer and use it in GitHub Desktop.
Play MP3s with pyaudio
import pyaudio
import mad
import sys
if len(sys.argv) < 2:
print "Plays a wave file.\n\n" +\
"Usage: %s filename.wav" % sys.argv[0]
sys.exit(-1)
mf = mad.MadFile(sys.argv[1])
p = pyaudio.PyAudio()
# open stream
stream = p.open(format =
p.get_format_from_width(pyaudio.paInt32),
channels = 2,
rate = mf.samplerate(),
output = True)
# read data
data = mf.read()
# play stream
while data != None:
stream.write(data)
data = mf.read()
stream.close()
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment