Skip to content

Instantly share code, notes, and snippets.

@yogiblue
Last active August 29, 2015 14:14
Show Gist options
  • Save yogiblue/a34d738fcc5726bc0088 to your computer and use it in GitHub Desktop.
Save yogiblue/a34d738fcc5726bc0088 to your computer and use it in GitHub Desktop.
Use PyAudio to record two channels of audio to WAV file
"""Use PyAudio to record two channels of audio to timestamped one hour long WAV files."""
import pyaudio
import wave
import datetime
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
# record 3606 seconds to get a full 60 minutes
RECORD_SECONDS = 3606
# number of hours to record
RECORD_HOURS = 168
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
print datetime.datetime.now()
for j in range(RECORD_HOURS):
stringdate = "{:%Y-%m-%d_%H-%M-%S}".format(datetime.datetime.now())
print "Recording " + stringdate + "__" + str(j) + ".wav"
WAVE_OUTPUT_FILENAME = stringdate + "__" + str(j) + ".wav"
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
frames = []
data = stream.read(CHUNK)
frames.append(data)
wf.writeframes(b''.join(frames))
if i % 2000 == 0:
print "Recorded " + str(100 * i / int(RATE / CHUNK * RECORD_SECONDS)) + "%"
wf.close()
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
print "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment