import time, sys
import pymedia.audio.sound as sound
import pymedia.audio.acodec as acodec
def voiceRecorder( secs, name ):
f= open( name, 'wb' )
cparams= { 'id': acodec.getCodecId( 'mp3' ),
'bitrate': 128000,
'sample_rate': 44100,
'channels': 2 }
ac= acodec.Encoder( cparams )
snd= sound.Input( 44100, 2, sound.AFMT_S16_LE )
snd.start()
while snd.getPosition()<= secs:
s= snd.getData()
if s and len( s ):
for fr in ac.encode( s ):
f.write( fr )
else:
time.sleep( .003 )
snd.stop()
if __name__ == "__main__":
if len( sys.argv )!= 3:
print 'Usage: voice_recorder <seconds> <file_name>'
else:
voiceRecorder( int( sys.argv[ 1 ] ), sys.argv[ 2 ] )