Advertisement
smathot

Panning with PyAudio

Jan 18th, 2013
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. # Example modified from
  2. # <http://people.csail.mit.edu/hubert/pyaudio/#docs>
  3. # and
  4. # <http://stackoverflow.com/questions/12076161/playing-a-single-channel-of-audio-using-pyaudio-or-similar>
  5.  
  6. """PyAudio Example: Play a WAVE file."""
  7.  
  8. import pyaudio
  9. import wave
  10. import sys
  11.  
  12. # Get from the filepool
  13. src = exp.get_file('example.wav')
  14.  
  15. # Index of the output device or None for default
  16. output_device_index = None
  17.  
  18. # The chunk size for playing back the stream
  19. chunk_size = 1024
  20.  
  21. # The channel to play back (0 or 1)
  22. channel = 0
  23.  
  24. def convert(data, samplesize=2, channel=0):
  25.     for i in range(0, len(data), 2*samplesize):
  26.         for j in range(0, samplesize):
  27.            data[i + j + samplesize * channel] = 0
  28.  
  29. # Initialize PyAudio and open an output stream
  30. p = pyaudio.PyAudio()
  31. wf = wave.open(src, 'rb')
  32. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
  33.     channels=wf.getnchannels(),
  34.     rate=wf.getframerate(),
  35.     output=True,
  36.     output_device_index=output_device_index)
  37.  
  38. # Read and play stream data until the stream is empty
  39. data = bytearray(wf.readframes(chunk_size))
  40. samplesize = wf.getsampwidth()
  41. while data != '':
  42.     convert(data, samplesize=samplesize, channel=channel)
  43.     stream.write(str(data))
  44.     data = bytearray(wf.readframes(chunk_size))
  45.  
  46. # Clean up
  47. stream.stop_stream()
  48. stream.close()
  49. p.terminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement