mirror of
https://github.com/romanz/amodem.git
synced 2026-02-07 01:18:02 +08:00
sender should not use tell() on stdin
This commit is contained in:
25
send.py
25
send.py
@@ -27,25 +27,28 @@ sym = Symbol()
|
||||
class Writer(object):
|
||||
def __init__(self):
|
||||
self.last = time.time()
|
||||
self.offset = 0
|
||||
|
||||
def write(self, fd, sym, n=1):
|
||||
fd.write(common.dumps(sym, n))
|
||||
data = common.dumps(sym, n)
|
||||
fd.write(data)
|
||||
self.offset += len(data)
|
||||
if time.time() > self.last + 1:
|
||||
log.debug('%10.3f seconds of data audio',
|
||||
fd.tell() / wave.bytes_per_second)
|
||||
self.offset / wave.bytes_per_second)
|
||||
self.last += 1
|
||||
|
||||
write = Writer().write
|
||||
writer = Writer()
|
||||
|
||||
|
||||
def start(fd, c):
|
||||
write(fd, c*1, n=400)
|
||||
write(fd, c*0, n=50)
|
||||
writer.write(fd, c*1, n=400)
|
||||
writer.write(fd, c*0, n=50)
|
||||
|
||||
|
||||
def training(fd, c):
|
||||
for b in train.equalizer:
|
||||
write(fd, c * b)
|
||||
writer.write(fd, c * b)
|
||||
|
||||
|
||||
def modulate(fd, bits):
|
||||
@@ -55,7 +58,7 @@ def modulate(fd, bits):
|
||||
while True:
|
||||
symbols = itertools.islice(symbols_iter, len(sym.carrier))
|
||||
symbols = np.array(list(symbols))
|
||||
write(fd, np.dot(symbols, carriers))
|
||||
writer.write(fd, np.dot(symbols, carriers))
|
||||
if all(symbols == 0): # EOF marker
|
||||
break
|
||||
|
||||
@@ -85,12 +88,12 @@ def main(args):
|
||||
fd = sys.stdout
|
||||
|
||||
# padding audio with silence
|
||||
write(fd, np.zeros(int(config.Fs * args.silence_start)))
|
||||
writer.write(fd, np.zeros(int(config.Fs * args.silence_start)))
|
||||
|
||||
start(fd, sym.carrier[config.carrier_index])
|
||||
for c in sym.carrier:
|
||||
training(fd, c)
|
||||
training_size = fd.tell()
|
||||
training_size = writer.offset
|
||||
log.info('%.3f seconds of training audio',
|
||||
training_size / wave.bytes_per_second)
|
||||
|
||||
@@ -99,12 +102,12 @@ def main(args):
|
||||
encoded = itertools.chain.from_iterable(ecc.encode(data))
|
||||
modulate(fd, bits=common.to_bits(encoded))
|
||||
|
||||
data_size = fd.tell() - training_size
|
||||
data_size = writer.offset - training_size
|
||||
log.info('%.3f seconds of data audio, for %.3f kB of data',
|
||||
data_size / wave.bytes_per_second, reader.total / 1e3)
|
||||
|
||||
# padding audio with silence
|
||||
write(fd, np.zeros(int(config.Fs * args.silence_stop)))
|
||||
writer.write(fd, np.zeros(int(config.Fs * args.silence_stop)))
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
|
||||
Reference in New Issue
Block a user