You can do this in Python. Here is an example using
Below example records audio from microphone for
#seconds as per variable
duration which you can modify as per your requirements.
Same contents are played back using standard audio output (speakers).
This code can be scripted and played in a loop.
More on this over here
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
duration = 10 # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2, dtype='float64')
print "Recording Audio for %s seconds" %(duration)
print "Audio recording complete , Playing recorded Audio"
print "Play Audio Complete"
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
Recording Audio for 10 seconds
Audio recording complete , Playing recorded Audio
Play Audio Complete