Created
June 29, 2018 21:44
-
-
Save AdySan/25dce7b3ecfc65e88e64658758d8c74a to your computer and use it in GitHub Desktop.
this works on Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import pyaudio | |
import wave | |
CHUNK = 512 | |
FORMAT = pyaudio.paInt16 #paInt8 | |
CHANNELS = 1 | |
RATE = 44100 #sample rate | |
RECORD_SECONDS = 5 | |
WAVE_OUTPUT_FILENAME = "pyaudio-output.wav" | |
p = pyaudio.PyAudio() | |
stream = p.open(format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK) #buffer | |
print("* recording") | |
frames = [] | |
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): | |
data = stream.read(CHUNK) | |
frames.append(data) # 2 bytes(16 bits) per channel | |
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