#!/usr/bin/python2
# Use Python version 2.
#
# TimeRecord.py
# Record an audio wav file for a specified time duration.
# G. Forrest Cook
# September 28, 2002
# Switched to ecasound's built in time mode: March 25, 2003
#
import sys
import os
import time

aud_recorder = "/usr/local/bin/ecasound"
rm_cmd = "/bin/rm"

#
# Count the command line arguments, without much validity checking.
#
if len(sys.argv) != 4:
    print 'usage: %s wav_file sample_rate record_time' % sys.argv[0]
    sys.exit (1)

#
# Convert the record time from hh:mm:ss to seconds.
#
rectime = sys.argv[3].split (':')
recsec = 3600 * int(rectime[0]) + 60 * int(rectime[1]) + int(rectime[2])
#print "recsec:", recsec

#
# Force remove the old wav file if one exists.
#
os.system (rm_cmd + " -f " + sys.argv[1])

#
# Run the recording command until the time expires.
# -r is realtime mode, -f is the record format, -t is recording time.
#
comd = aud_recorder + " -r -f:16,2," + sys.argv[2] + " -t " + str(recsec) + \
	" -i /dev/dsp -o " + sys.argv[1]
#print "comd:", comd
os.system (comd)

sys.exit (0)
