mirror of
https://github.com/romanz/amodem.git
synced 2026-04-20 13:16:42 +08:00
Reader: fix iteration logic.
This commit is contained in:
@@ -13,6 +13,7 @@ import wave
|
|||||||
import common
|
import common
|
||||||
import config
|
import config
|
||||||
import sigproc
|
import sigproc
|
||||||
|
import stream
|
||||||
|
|
||||||
modem = sigproc.MODEM(config)
|
modem = sigproc.MODEM(config)
|
||||||
|
|
||||||
@@ -64,24 +65,6 @@ def modulate(fd, bits):
|
|||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
class Reader(object):
|
|
||||||
def __init__(self, fd, size):
|
|
||||||
self.fd = fd
|
|
||||||
self.size = size
|
|
||||||
self.total = 0
|
|
||||||
|
|
||||||
def __next__(self):
|
|
||||||
block = self.fd.read(self.size)
|
|
||||||
if block:
|
|
||||||
self.total += len(block)
|
|
||||||
return block
|
|
||||||
else:
|
|
||||||
raise StopIteration()
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
import ecc
|
import ecc
|
||||||
log.info('Running MODEM @ {:.1f} kbps'.format(modem.modem_bps / 1e3))
|
log.info('Running MODEM @ {:.1f} kbps'.format(modem.modem_bps / 1e3))
|
||||||
@@ -96,7 +79,7 @@ def main(args):
|
|||||||
log.info('%.3f seconds of training audio',
|
log.info('%.3f seconds of training audio',
|
||||||
training_size / wave.bytes_per_second)
|
training_size / wave.bytes_per_second)
|
||||||
|
|
||||||
reader = Reader(args.input, 64 << 10)
|
reader = stream.Reader(args.input, 64 << 10)
|
||||||
data = itertools.chain.from_iterable(reader)
|
data = itertools.chain.from_iterable(reader)
|
||||||
encoded = itertools.chain.from_iterable(ecc.encode(data))
|
encoded = itertools.chain.from_iterable(ecc.encode(data))
|
||||||
modulate(args.output, bits=common.to_bits(encoded))
|
modulate(args.output, bits=common.to_bits(encoded))
|
||||||
|
|||||||
@@ -12,21 +12,30 @@ class Reader(object):
|
|||||||
WAIT = 0.1
|
WAIT = 0.1
|
||||||
TIMEOUT = 2.0
|
TIMEOUT = 2.0
|
||||||
|
|
||||||
def __init__(self, fd):
|
def __init__(self, fd, bufsize=None, eof=False):
|
||||||
self.fd = fd
|
self.fd = fd
|
||||||
self.check = None
|
self.check = None
|
||||||
|
self.total = 0
|
||||||
|
self.bufsize = bufsize if (bufsize is not None) else self.BUFSIZE
|
||||||
|
self.eof = eof
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
|
return self.next()
|
||||||
|
|
||||||
|
def next(self):
|
||||||
block = bytearray()
|
block = bytearray()
|
||||||
finish_time = time.time() + self.TIMEOUT
|
finish_time = time.time() + self.TIMEOUT
|
||||||
while time.time() <= finish_time:
|
while time.time() <= finish_time:
|
||||||
left = self.BUFSIZE - len(block)
|
left = self.BUFSIZE - len(block)
|
||||||
data = self.fd.read(left)
|
data = self.fd.read(left)
|
||||||
if data:
|
if data:
|
||||||
|
self.total += len(data)
|
||||||
block.extend(data)
|
block.extend(data)
|
||||||
|
elif self.eof: # handle EOF condition by stopping iteration
|
||||||
|
raise StopIteration()
|
||||||
|
|
||||||
if len(block) == self.BUFSIZE:
|
if len(block) == self.BUFSIZE:
|
||||||
values = common.loads(str(block))
|
values = common.loads(str(block))
|
||||||
|
|||||||
Reference in New Issue
Block a user