Skip to content

Instantly share code, notes, and snippets.

@kotobuki
Created June 12, 2016 07:44
Show Gist options
  • Save kotobuki/f8e7ca76186a6b575c9bf1e86bed5eb1 to your computer and use it in GitHub Desktop.
Save kotobuki/f8e7ca76186a6b575c9bf1e86bed5eb1 to your computer and use it in GitHub Desktop.
# Original: https://people.csail.mit.edu/hubert/pyaudio/
# Modified to be tested on a Raspberry Pi
"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
@Nishanksingla
Copy link

Hey,

Is this working for you ?
I tried the same settings in Macbook pro and I am getting this error "IOError: [Errno Input overflowed] -9981".

Can you help me with this error ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment