Skip to content

Instantly share code, notes, and snippets.

@shilan
Forked from mabdrabo/sound_recorder.py
Created June 10, 2020 21:03
Show Gist options
  • Select an option

  • Save shilan/ad5ff907cf12e8e463a30dd1e1c8e376 to your computer and use it in GitHub Desktop.

Select an option

Save shilan/ad5ff907cf12e8e463a30dd1e1c8e376 to your computer and use it in GitHub Desktop.
Simple script to record sound from the microphone, dependencies: easy_install pyaudio
import pyaudio
import wave
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.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 "finished recording"
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
@shilan

shilan commented Jun 10, 2020

Copy link
Copy Markdown
Author

Dataset collection

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