import pyaudio
import wave
import random
import os
import time
import sys

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 16000
CHUNK = 1024
file_name = sys.argv[1]
RECORD_SECONDS = int(sys.argv[2])

# start Recording
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
	            rate=RATE, input=True,
	            frames_per_buffer=CHUNK)
print("recording")
frames = []
 
for j in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
	data = stream.read(CHUNK)
	frames.append(data)
print("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
 
waveFile = wave.open(file_name, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()

 

 

 
 


