Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created July 11, 2014 09:00
Show Gist options
  • Save pklaus/4eafc6cd94533bf92cb0 to your computer and use it in GitHub Desktop.
Save pklaus/4eafc6cd94533bf92cb0 to your computer and use it in GitHub Desktop.
Examples for playing audio files with different Python media libraries
#!/usr/bin/env python
"""
PyAudio Example: Play a wave file.
http://people.csail.mit.edu/hubert/pyaudio/
Mac OS X:
brew install portaudio
pip install http://people.csail.mit.edu/hubert/pyaudio/packages/pyaudio-0.2.8.tar.gz
"""
import pyaudio
import wave
import sys
CHUNK = 1024
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while data != '':
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
#!/usr/bin/env python
import sys
import pyglet
try:
wavefile = sys.argv[1]
except:
print("specify wave file!"); sys.exit(2)
player = pyglet.media.Player()
music = pyglet.media.load(wavefile)
player.queue(music)
player.play()
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment