mirror of
https://github.com/romanz/amodem.git
synced 2026-02-06 16:48:06 +08:00
main: use amodem package for recv.main() and send.main() API
This commit is contained in:
24
amodem-cli
24
amodem-cli
@@ -19,11 +19,8 @@ import logging
|
||||
|
||||
log = logging.getLogger('__name__')
|
||||
|
||||
from amodem import recv, send, audio, calib
|
||||
from amodem.config import bitrates
|
||||
from amodem import recv
|
||||
from amodem import send
|
||||
from amodem import audio
|
||||
from amodem import calib
|
||||
|
||||
null = open('/dev/null', 'wb')
|
||||
|
||||
@@ -76,12 +73,6 @@ def main():
|
||||
'-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')
|
||||
sender.add_argument(
|
||||
'--silence-stop', type=float, default=1.0,
|
||||
help='seconds of silence after transmission stops')
|
||||
|
||||
sender.set_defaults(
|
||||
main=run_send,
|
||||
@@ -101,9 +92,6 @@ def main():
|
||||
'-c', '--calibrate', default=False, action='store_true')
|
||||
receiver.add_argument(
|
||||
'-w', '--wave', default=False, action='store_true')
|
||||
receiver.add_argument(
|
||||
'--skip', type=int, default=320,
|
||||
help='skip initial N samples, due to spurious spikes')
|
||||
receiver.add_argument(
|
||||
'--plot', action='store_true', default=False,
|
||||
help='plot results using pylab module')
|
||||
@@ -152,10 +140,10 @@ def join_process(process):
|
||||
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_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):
|
||||
@@ -173,7 +161,7 @@ def run_recv(args):
|
||||
elif args.wave:
|
||||
join_process(audio.record(Fs=config.Fs).launch(fname=args.output))
|
||||
else:
|
||||
run_modem(args, recv.main)
|
||||
run_modem(args, recv.main, plt=args.plot)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -8,6 +8,13 @@ class Configuration(object):
|
||||
Npoints = 64
|
||||
F0 = 1e3
|
||||
|
||||
# sender config
|
||||
silence_start = 1.0
|
||||
silence_stop = 1.0
|
||||
|
||||
# receiver config
|
||||
skip_start = 0.1
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(**kwargs)
|
||||
|
||||
|
||||
@@ -209,24 +209,24 @@ class Receiver(object):
|
||||
self.plt.title(title)
|
||||
|
||||
|
||||
def main(args):
|
||||
config = args.config
|
||||
reader = stream.Reader(args.input, data_type=common.loads)
|
||||
def main(config, src, dst, plt=None):
|
||||
reader = stream.Reader(src, data_type=common.loads)
|
||||
signal = itertools.chain.from_iterable(reader)
|
||||
|
||||
skipped = common.take(signal, args.skip)
|
||||
log.debug('Skipping %.3f seconds', len(skipped) / float(config.baud))
|
||||
to_skip = int(config.skip_start * config.Fs)
|
||||
log.debug('Skipping %.3f seconds', config.skip_start)
|
||||
common.take(signal, to_skip)
|
||||
|
||||
reader.check = common.check_saturation
|
||||
|
||||
detector = detect.Detector(config=config)
|
||||
receiver = Receiver(config=config, plt=args.plot)
|
||||
receiver = Receiver(config=config, plt=plt)
|
||||
success = False
|
||||
try:
|
||||
log.info('Waiting for carrier tone: %.1f kHz', config.Fc / 1e3)
|
||||
signal, amplitude = detector.run(signal)
|
||||
receiver.start(signal, gain=1.0/amplitude)
|
||||
receiver.run(args.output)
|
||||
receiver.run(dst)
|
||||
success = True
|
||||
except Exception:
|
||||
log.exception('Decoding failed')
|
||||
|
||||
@@ -52,19 +52,19 @@ class Sender(object):
|
||||
log.debug('Sent %8.1f kB', total_bits / 8e3)
|
||||
|
||||
|
||||
def main(args):
|
||||
sender = Sender(args.output, config=args.config)
|
||||
Fs = args.config.Fs
|
||||
def main(config, src, dst):
|
||||
sender = Sender(dst, config=config)
|
||||
Fs = config.Fs
|
||||
|
||||
# pre-padding audio with silence
|
||||
sender.write(np.zeros(int(Fs * args.silence_start)))
|
||||
sender.write(np.zeros(int(Fs * config.silence_start)))
|
||||
|
||||
sender.start()
|
||||
|
||||
training_duration = sender.offset
|
||||
log.info('Sending %.3f seconds of training audio', training_duration / Fs)
|
||||
|
||||
reader = stream.Reader(args.input, bufsize=(64 << 10), eof=True)
|
||||
reader = stream.Reader(src, bufsize=(64 << 10), eof=True)
|
||||
data = itertools.chain.from_iterable(reader)
|
||||
bits = framing.encode(data)
|
||||
log.info('Starting modulation')
|
||||
@@ -75,4 +75,4 @@ def main(args):
|
||||
reader.total / 1e3, data_duration / Fs)
|
||||
|
||||
# post-padding audio with silence
|
||||
sender.write(np.zeros(int(Fs * args.silence_stop)))
|
||||
sender.write(np.zeros(int(Fs * config.silence_stop)))
|
||||
|
||||
@@ -29,8 +29,7 @@ class Args(object):
|
||||
def run(size, chan=None, df=0, success=True):
|
||||
tx_data = os.urandom(size)
|
||||
tx_audio = BytesIO()
|
||||
send.main(Args(config=config, silence_start=1, silence_stop=1,
|
||||
input=BytesIO(tx_data), output=tx_audio))
|
||||
send.main(config=config, src=BytesIO(tx_data), dst=tx_audio)
|
||||
|
||||
data = tx_audio.getvalue()
|
||||
data = common.loads(data)
|
||||
@@ -45,8 +44,7 @@ def run(size, chan=None, df=0, success=True):
|
||||
rx_audio = BytesIO(data)
|
||||
|
||||
rx_data = BytesIO()
|
||||
result = recv.main(Args(config=config,
|
||||
skip=0, input=rx_audio, output=rx_data))
|
||||
result = recv.main(config=config, src=rx_audio, dst=rx_data)
|
||||
rx_data = rx_data.getvalue()
|
||||
|
||||
assert result == success
|
||||
|
||||
Reference in New Issue
Block a user