amodem: refactor calibration and audio I/O into main script

This commit is contained in:
Roman Zeyde
2014-09-05 18:04:42 +03:00
parent d5505193b1
commit 601b2f650d

View File

@@ -7,9 +7,8 @@ else:
_stdin = sys.stdin.buffer
_stdout = sys.stdout.buffer
import argparse
import logging
format = '%(asctime)s %(levelname)-10s %(message)-100s %(filename)s:%(lineno)d'
logging.basicConfig(level=logging.DEBUG, format=format)
log = logging.getLogger('__name__')
@@ -17,9 +16,8 @@ from amodem import config
from amodem import recv
from amodem import send
from amodem import wave
from amodem import calib
import argparse
from functools import partial
null = open('/dev/null', 'wb')
@@ -47,17 +45,6 @@ def FileType(mode, process=None):
return opener
def _run(args, process):
p = process(fname=args.filename)
exitcode = 0
try:
exitcode = p.wait()
except KeyboardInterrupt:
p.kill()
exitcode = p.wait()
sys.exit(exitcode)
def main():
p = argparse.ArgumentParser()
subparsers = p.add_subparsers()
@@ -70,7 +57,10 @@ def main():
sender.add_argument(
'-o', '--output', help='output file (use "-" for stdout).'
' if not specified, `aplay` tool will be used.')
sender.add_argument(
'-c', '--calibrate', default=False, action='store_true')
sender.add_argument(
'-w', '--wave', default=False, action='store_true')
sender.add_argument(
'--silence-start', type=float, default=1.0,
help='seconds of silence before transmission starts')
@@ -79,7 +69,7 @@ def main():
help='seconds of silence after transmission stops')
sender.set_defaults(
main=send.main,
main=run_send,
input_type=FileType('rb'),
output_type=FileType('wb', wave.play)
)
@@ -92,6 +82,10 @@ def main():
' if not specified, `arecord` tool will be used.')
receiver.add_argument(
'-o', '--output', help='output file (use "-" for stdout).')
receiver.add_argument(
'-c', '--calibrate', default=False, action='store_true')
receiver.add_argument(
'-w', '--wave', default=False, action='store_true')
receiver.add_argument(
'--skip', type=int, default=128,
help='skip initial N samples, due to spurious spikes')
@@ -99,39 +93,55 @@ def main():
'--plot', dest='plt', action='store_true', default=False,
help='plot results using pylab module')
receiver.set_defaults(
main=recv.main,
main=run_recv,
input_type=FileType('rb', wave.record),
output_type=FileType('wb')
)
# Audio recording tool
fmt = 'a raw audio file (16 bits at {:.1f} kHz)'.format(wave.Fs / 1e3)
recorder = subparsers.add_parser('record', help='record ' + fmt)
recorder.add_argument(
'filename', default='-',
help='path to the audio file to record (otherwise, use stdout)')
recorder.set_defaults(main=partial(_run, process=wave.record))
# Audio playing tool
player = subparsers.add_parser('play', help='play ' + fmt)
player.add_argument(
'filename', default='-',
help='path to the audio file to play (otherwise, use stdin)')
player.set_defaults(main=partial(_run, process=wave.play))
args = p.parse_args()
logging.basicConfig(
level='DEBUG', filename='amodem.log',
format=('%(asctime)s %(levelname)-10s %(message)-100s '
'%(filename)s:%(lineno)d'))
# Parsing and execution
args = p.parse_args()
if hasattr(args, 'input_type'):
args.input = args.input_type(args.input)
if hasattr(args, 'output_type'):
args.output = args.output_type(args.output)
if getattr(args, 'plt', 'None'):
import pylab
args.plt = pylab
log.debug('MODEM settings: %r', config.settings)
args.main(args)
def join_process(process):
exitcode = 0
try:
exitcode = process.wait()
except KeyboardInterrupt:
process.kill()
exitcode = process.wait()
sys.exit(exitcode)
def run_modem(args, func):
args.input = args.input_type(args.input)
args.output = args.output_type(args.output)
func(args)
def run_send(args):
if args.calibrate:
calib.send()
elif args.wave:
join_process(wave.play(fname=args.input))
else:
run_modem(args, send.main)
def run_recv(args):
if args.calibrate:
calib.recv()
elif args.wave:
join_process(wave.record(fname=args.output))
else:
run_modem(args, recv.main)
if __name__ == '__main__':
main()