Last active
July 7, 2019 06:34
-
-
Save Bill-Park/6f89d1d225d05893ee6662bb2388d754 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import pyaudio | |
import wave | |
import audioop | |
import math | |
import os | |
CHUNK = 1024 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 2 | |
RATE = 44100 | |
RECORD_SECONDS = 5 | |
WAVE_OUTPUT_FILENAME = "output.mp3" | |
def main(file_name = "output.mp3") : | |
p = pyaudio.PyAudio() | |
stream = p.open(format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK) | |
print("* recording") | |
frames = [] | |
volume_count = 0 | |
while volume_count < 20 : | |
data = stream.read(CHUNK) | |
frames.append(data) | |
rms = audioop.rms(data, 2) | |
volume = 20 * math.log10(rms) | |
print(volume) | |
if volume < 51 : | |
volume_count += 1 | |
else : | |
volume_count = 0 | |
#todo check average volume counter | |
# for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)) : | |
print("* done recording") | |
stream.stop_stream() | |
stream.close() | |
p.terminate() | |
wf = wave.open(file_name, 'wb') | |
wf.setnchannels(CHANNELS) | |
wf.setsampwidth(p.get_sample_size(FORMAT)) | |
wf.setframerate(RATE) | |
wf.writeframes(b''.join(frames)) | |
wf.close() | |
return file_name | |
if __name__ == "__main__" : | |
file_name = main() | |
os.system("aplay {}".format(file_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment