Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Created January 25, 2016 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbbooher/579095ac89fa2fff58db to your computer and use it in GitHub Desktop.
Save tbbooher/579095ac89fa2fff58db to your computer and use it in GitHub Desktop.
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
p = pyaudio.PyAudio()
def record(file_name):
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK)
frames = []
print "starting recording " + file_name + "."
for i in range(0, int(RATE / CHUNK * 2)):
data = stream.read(CHUNK)
frames.append(data)
print "finished recording"
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(file_name, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
record('first.wav')
'''and again'''
record('second')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment