mirror of
https://github.com/romanz/amodem.git
synced 2026-03-06 23:05:57 +08:00
fix stream bugs
This commit is contained in:
@@ -41,7 +41,7 @@ def load(fileobj):
|
||||
|
||||
|
||||
def loads(data):
|
||||
x = np.fromstring(data, dtype='int16')
|
||||
x = np.fromstring(str(data), dtype='int16')
|
||||
x = x / scaling
|
||||
return x
|
||||
|
||||
|
||||
@@ -17,7 +17,8 @@ def end_of_stream(size):
|
||||
def encode(data, nsym=DEFAULT_NSYM):
|
||||
chunk_size = BLOCK_SIZE - nsym - 1
|
||||
|
||||
for _, chunk in common.iterate(data, chunk_size, bytearray, truncate=False):
|
||||
for _, chunk in common.iterate(data=data, size=chunk_size,
|
||||
func=bytearray, truncate=False):
|
||||
size = len(chunk)
|
||||
if size < chunk_size:
|
||||
padding = [0] * (chunk_size - size)
|
||||
|
||||
@@ -238,11 +238,16 @@ def decode(bits_iterator):
|
||||
return ecc.decode(blocks())
|
||||
|
||||
|
||||
def iread(fd):
|
||||
reader = stream.Reader(fd, data_type=common.loads)
|
||||
return itertools.chain.from_iterable(reader)
|
||||
|
||||
|
||||
def main(args):
|
||||
|
||||
log.info('Running MODEM @ {:.1f} kbps'.format(modem.modem_bps / 1e3))
|
||||
|
||||
signal = stream.iread(args.input)
|
||||
signal = iread(args.input)
|
||||
skipped = common.take(signal, args.skip)
|
||||
log.debug('Skipping first %.3f seconds', len(skipped) / float(modem.baud))
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ def main(args):
|
||||
log.info('%.3f seconds of training audio',
|
||||
training_size / wave.bytes_per_second)
|
||||
|
||||
reader = stream.Reader(args.input, 64 << 10, eof=True)
|
||||
reader = stream.Reader(args.input, bufsize=(64 << 10), eof=True)
|
||||
data = itertools.chain.from_iterable(reader)
|
||||
encoded = itertools.chain.from_iterable(ecc.encode(data))
|
||||
modulate(args.output, bits=common.to_bits(encoded))
|
||||
@@ -99,7 +99,9 @@ if __name__ == '__main__':
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('--silence-start', type=float, default=1.0)
|
||||
p.add_argument('--silence-stop', type=float, default=1.0)
|
||||
p.add_argument('-i', '--input', type=argparse.FileType('r'), default=sys.stdin)
|
||||
p.add_argument('-o', '--output', type=argparse.FileType('w'), default=sys.stdout)
|
||||
p.add_argument('-i', '--input', type=argparse.FileType('r'),
|
||||
default=sys.stdin)
|
||||
p.add_argument('-o', '--output', type=argparse.FileType('w'),
|
||||
default=sys.stdout)
|
||||
args = p.parse_args()
|
||||
main(args)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import time
|
||||
import itertools
|
||||
|
||||
import common
|
||||
import wave
|
||||
|
||||
|
||||
@@ -12,12 +10,13 @@ class Reader(object):
|
||||
WAIT = 0.1
|
||||
TIMEOUT = 2.0
|
||||
|
||||
def __init__(self, fd, bufsize=None, eof=False):
|
||||
def __init__(self, fd, data_type=None, bufsize=None, eof=False):
|
||||
self.fd = fd
|
||||
self.check = None
|
||||
self.total = 0
|
||||
self.bufsize = bufsize if (bufsize is not None) else self.BUFSIZE
|
||||
self.eof = eof
|
||||
self.data_type = data_type if (data_type is not None) else lambda x: x
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
@@ -27,6 +26,15 @@ class Reader(object):
|
||||
|
||||
def next(self):
|
||||
block = bytearray()
|
||||
if self.eof:
|
||||
data = self.fd.read(self.BUFSIZE)
|
||||
if data:
|
||||
self.total += len(data)
|
||||
block.extend(data)
|
||||
return block
|
||||
else:
|
||||
raise StopIteration()
|
||||
|
||||
finish_time = time.time() + self.TIMEOUT
|
||||
while time.time() <= finish_time:
|
||||
left = self.BUFSIZE - len(block)
|
||||
@@ -34,11 +42,9 @@ class Reader(object):
|
||||
if data:
|
||||
self.total += len(data)
|
||||
block.extend(data)
|
||||
elif self.eof: # handle EOF condition by stopping iteration
|
||||
raise StopIteration()
|
||||
|
||||
if len(block) == self.BUFSIZE:
|
||||
values = common.loads(str(block))
|
||||
values = self.data_type(block)
|
||||
if self.check:
|
||||
self.check(values)
|
||||
return values
|
||||
@@ -46,7 +52,3 @@ class Reader(object):
|
||||
time.sleep(self.WAIT)
|
||||
|
||||
raise IOError('timeout')
|
||||
|
||||
|
||||
def iread(fd):
|
||||
return itertools.chain.from_iterable(Reader(fd))
|
||||
|
||||
@@ -25,7 +25,7 @@ def test_read():
|
||||
j = 0
|
||||
for i, buf in result:
|
||||
assert i == j
|
||||
assert len(buf) == f.SAMPLES
|
||||
assert len(buf) == f.bufsize
|
||||
j += 1
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user