Skip to content

Instantly share code, notes, and snippets.

@vubon
Last active August 23, 2022 02:16
Show Gist options
  • Save vubon/6f0697810c9ce5f4c76e69eb2d469bb7 to your computer and use it in GitHub Desktop.
Save vubon/6f0697810c9ce5f4c76e69eb2d469bb7 to your computer and use it in GitHub Desktop.
PyAudio Voice Record
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 20 # You can change the time . it is calculationg second
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.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("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment