Skip to content

Instantly share code, notes, and snippets.

@TheRealMentor
Created March 23, 2017 07:05
Show Gist options
  • Save TheRealMentor/5eeabfd77116ae61aeb8f25917538c63 to your computer and use it in GitHub Desktop.
Save TheRealMentor/5eeabfd77116ae61aeb8f25917538c63 to your computer and use it in GitHub Desktop.
Reading Wave File
import pyaudio
import wave
#Defining Chunk
chunk = 1024
#Female voice, for male voice file_name = 'brian.wav'
file_name = 'amy.wav'
#Opening file
f = wave.open(file_name,'rb')
#Instantiating PyAudio
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
#Reading data from the file
data = f.readframes(chunk)
#Finding the Number of Channels and Sampling Rate
channels = f.getnchannels()
rate = f.getframerate()
#Printing the File name, Number of Channels and Sampling rate
print 'File:', file_name
print 'Channels:', channels
print 'Rate:', rate
#Playing the voice
while data:
stream.write(data)
data = f.readframes(chunk)
#Stopping the voice
stream.stop_stream()
stream.close()
#Terminating the PyAudio
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment