mirror of
https://github.com/romanz/amodem.git
synced 2026-04-19 20:55:59 +08:00
switch to PyAudio package for portability
This commit is contained in:
61
amodem-cli
61
amodem-cli
@@ -19,7 +19,7 @@ import logging
|
||||
|
||||
log = logging.getLogger('__name__')
|
||||
|
||||
from amodem import recv, send, audio, calib
|
||||
from amodem import recv, send, calib, audio
|
||||
from amodem.config import bitrates
|
||||
|
||||
null = open('/dev/null', 'wb')
|
||||
@@ -29,18 +29,18 @@ bitrate = os.environ.get('BITRATE', 1)
|
||||
config = bitrates.get(int(bitrate))
|
||||
|
||||
|
||||
def FileType(mode, process=None):
|
||||
def FileType(mode, audio_interface=None):
|
||||
def opener(fname):
|
||||
assert 'r' in mode or 'w' in mode
|
||||
if process is None and fname is None:
|
||||
if audio_interface is None and fname is None:
|
||||
fname = '-'
|
||||
|
||||
if fname is None:
|
||||
assert process is not None
|
||||
assert audio_interface is not None
|
||||
if 'r' in mode:
|
||||
return process.launch(stdout=audio.sp.PIPE, stderr=null).stdout
|
||||
return audio_interface.recorder()
|
||||
if 'w' in mode:
|
||||
return process.launch(stdin=audio.sp.PIPE, stderr=null).stdin
|
||||
return audio_interface.player()
|
||||
|
||||
if fname == '-':
|
||||
if 'r' in mode:
|
||||
@@ -58,6 +58,7 @@ def main():
|
||||
'Fs={3:.1f} kHz')
|
||||
description = fmt.format(config.modem_bps / 1e3, len(config.symbols),
|
||||
config.Nfreq, config.Fs / 1e3)
|
||||
interface = audio.Interface(config)
|
||||
p = argparse.ArgumentParser(description=description)
|
||||
subparsers = p.add_subparsers()
|
||||
|
||||
@@ -71,13 +72,11 @@ def main():
|
||||
' 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.set_defaults(
|
||||
main=run_send,
|
||||
main=send.main, calibration=calib.send,
|
||||
input_type=FileType('rb'),
|
||||
output_type=FileType('wb', audio.play(Fs=config.Fs))
|
||||
output_type=FileType('wb', interface)
|
||||
)
|
||||
|
||||
# Demodulator
|
||||
@@ -90,14 +89,12 @@ def main():
|
||||
'-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(
|
||||
'--plot', action='store_true', default=False,
|
||||
help='plot results using pylab module')
|
||||
receiver.set_defaults(
|
||||
main=run_recv,
|
||||
input_type=FileType('rb', audio.record(Fs=config.Fs)),
|
||||
main=recv.main, calibration=calib.recv,
|
||||
input_type=FileType('rb', interface),
|
||||
output_type=FileType('wb')
|
||||
)
|
||||
|
||||
@@ -125,43 +122,15 @@ def main():
|
||||
log.debug(description)
|
||||
if getattr(args, 'plot', False):
|
||||
import pylab
|
||||
args.plot = pylab
|
||||
args.config = config
|
||||
args.main(args)
|
||||
else:
|
||||
pylab = None
|
||||
|
||||
|
||||
def join_process(process):
|
||||
exitcode = 0
|
||||
try:
|
||||
exitcode = process.wait()
|
||||
except KeyboardInterrupt:
|
||||
process.kill()
|
||||
exitcode = process.wait()
|
||||
sys.exit(exitcode)
|
||||
|
||||
|
||||
def run_modem(args, main_func, **kwargs):
|
||||
src = args.input_type(args.input)
|
||||
dst = args.output_type(args.output)
|
||||
main_func(args.config, src, dst, **kwargs)
|
||||
|
||||
|
||||
def run_send(args):
|
||||
if args.calibrate:
|
||||
calib.send(config=config, verbose=args.verbose)
|
||||
elif args.wave:
|
||||
join_process(audio.play(Fs=config.Fs).launch(fname=args.input))
|
||||
args.calibration(config=config, src=src, dst=dst, verbose=args.verbose)
|
||||
else:
|
||||
run_modem(args, send.main)
|
||||
|
||||
|
||||
def run_recv(args):
|
||||
if args.calibrate:
|
||||
calib.recv(config=config, verbose=args.verbose)
|
||||
elif args.wave:
|
||||
join_process(audio.record(Fs=config.Fs).launch(fname=args.output))
|
||||
else:
|
||||
run_modem(args, recv.main, plt=args.plot)
|
||||
args.main(config=config, src=src, dst=dst, pylab=pylab)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user