Advertisement
sowamaciej

Untitled

Nov 14th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import wave
  2.  
  3. filename = 'myfile.wav'
  4.  
  5. # Set chunk size of 1024 samples per data frame
  6. chunk = 1024
  7.  
  8. # Open the sound file
  9. wf = wave.open(filename, 'rb')
  10.  
  11. # Create an interface to PortAudio
  12. p = pyaudio.PyAudio()
  13.  
  14. # Open a .Stream object to write the WAV file to
  15. # 'output = True' indicates that the sound will be played rather than recorded
  16. stream = p.open(format = p.get_format_from_width(wf.getsampwidth()),
  17. channels = wf.getnchannels(),
  18. rate = wf.getframerate(),
  19. output = True)
  20.  
  21. # Read data in chunks
  22. data = wf.readframes(chunk)
  23.  
  24. # Play the sound by writing the audio data to the stream
  25. while data != '':
  26. stream.write(data)
  27. data = wf.readframes(chunk)
  28.  
  29. # Close and terminate the stream
  30. stream.close()
  31. p.terminate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement