Skip to content

Instantly share code, notes, and snippets.

@noahcse
Forked from mabdrabo/sound_recorder.py
Created July 22, 2017 21:06
Show Gist options
  • Save noahcse/d698b007d2da07a8221bcb982a7ad8b5 to your computer and use it in GitHub Desktop.
Save noahcse/d698b007d2da07a8221bcb982a7ad8b5 to your computer and use it in GitHub Desktop.

Revisions

  1. @mabdrabo mabdrabo created this gist Jan 28, 2014.
    36 changes: 36 additions & 0 deletions sound_recorder.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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()