But I found two typo's in it.One: input=TRUE must be input=True: Second:while data != ‘’: Must be while data != '':
Third: what is rb meaning: wf = wave.open(“filename.wav”, “rb”)
Can somone help me out with this script.
Code: Select all
# You need to import the pyaudio module import pyaudio # First, we will listen # We need to set some parameters # Buffer chunk size in bytes CHUNK = 1024 # The audio format FORMAT = pyaudio.paInt16 # The number of channels to record on CHANNELS = 2 # The sample rate, 44.1KHz RATE = 44100 # The number of seconds to record for RECORD_SECS = 5 # Next, we create a PyAudio object p = pyaudio.PyAudio() # We need a stream to record from stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=TRUE, frames_per_buffer=CHUNK) # We can now record into a temporary buffer frames =  for i in range(0, int(RATE / CHUNK * RECORD_SECS)): data = stream.read(CHUNK) frames.append(data) # We can now shut everything down stream.stop_stream() stream.close() p.terminate() # If we want to play a wave file, we will need the wave module import wave # We can open it, give a filename wf = wave.open(“filename.wav”, “rb”) # We need a new PyAudio object p = pyaudio.PyAudio() # We will open a stream, using the settings from the wave file stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) # We can now read from the file and play it out data = wf.readframes(CHUNK) while data != ‘’: stream.write(data) data = wf.readframes(CHUNK) # Don’t forget to shut everything down again stream.stop_stream() stream.close() p.terminate()