Skip to content

Instantly share code, notes, and snippets.

@grohith327
Last active July 27, 2020 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grohith327/deedd75d73070e7667eef1977162a390 to your computer and use it in GitHub Desktop.
Save grohith327/deedd75d73070e7667eef1977162a390 to your computer and use it in GitHub Desktop.
import pyaudio
import wave
import warnings
warnings.filterwarnings(action='ignore',category=FutureWarning)
CHUNK = 256
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 22050
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "test.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, exception_on_overflow = False)
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