Advertisement
smathot

PyAudio example for OpenSesame

Jan 11th, 2013
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. # Example modified from
  2. # <http://people.csail.mit.edu/hubert/pyaudio/#docs>
  3.  
  4. """PyAudio Example: Play a WAVE file."""
  5.  
  6. import pyaudio
  7. import wave
  8. import sys
  9.  
  10. # Get from the filepool
  11. src = exp.get_file('example.wav')
  12.  
  13. # Index of the output device or None for default
  14. output_device_index = None
  15.  
  16. # The chunk size for playing back the stream
  17. chunk_size = 1024
  18.  
  19. # Initialize PyAudio and open an output stream
  20. p = pyaudio.PyAudio()
  21. wf = wave.open(src, 'rb')
  22. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
  23.     channels=wf.getnchannels(),
  24.     rate=wf.getframerate(),
  25.     output=True,
  26.     output_device_index=output_device_index)
  27.  
  28. # Read and play stream data until the stream is empty
  29. data = wf.readframes(chunk_size)
  30. while data != '':
  31.     stream.write(data)
  32.     data = wf.readframes(chunk_size)
  33.  
  34. # Clean up
  35. stream.stop_stream()
  36. stream.close()
  37. p.terminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement