Skip to content

Instantly share code, notes, and snippets.

@korjaa
Last active May 18, 2022 06:15
Show Gist options
  • Save korjaa/b17a20984ff5211c3a6157db6819d17e to your computer and use it in GitHub Desktop.
Save korjaa/b17a20984ff5211c3a6157db6819d17e to your computer and use it in GitHub Desktop.
import wave
import pyaudio # pip install pyaudio
# Initialize
audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024)
# Record
frames = []
print "recording"
for i in range(0, int(44100 / 1024 * 3.0)): # records 3 seconds
data = stream.read(1024)
frames.append(data)
print "done"
# Close recording
stream.stop_stream()
stream.close()
audio.terminate()
# Save to file
print "saving to file"
wave_file = wave.open("output.wav", "wb")
wave_file.setnchannels(1)
wave_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
wave_file.setframerate(44100)
wave_file.writeframes(b''.join(frames))
wave_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment