"""Fixed-speed offline PCM-to-Morse-text reference.""" import argparse import math import os import struct import wave from itertools import islice from contextlib import contextmanager from concurrent.futures import CancelledError from reference import Evidence, analyze, mark_symbol _CODES = '.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.' MORSE = dict(zip(_CODES.split(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')) def decode_evidence(evidence, rate=8000, wpm=20, threshold=0.7, min_power=0.002): """Decode contiguous windows; '?' preserves malformed marks/gaps/codes.""" if (not all(math.isfinite(x) for x in (rate, wpm, threshold, min_power)) or rate <= 0 or wpm <= 0 or not -1 < threshold < 1 or min_power < 0): raise ValueError('invalid decoder configuration') def runs(): expected, previous, length = 0, None, 0 for item in evidence: if item.start_sample != expected or item.end_sample <= expected: raise ValueError('evidence must be contiguous from sample zero') if not all(math.isfinite(x) for x in (item.score, item.tone_power, item.noise_power)): raise ValueError('nonfinite evidence') on = item.score >= threshold and item.tone_power >= min_power if previous is not None and on != previous: yield previous, length length = 0 length += item.end_sample - item.start_sample expected, previous = item.end_sample, on if previous is not None: yield previous, length text, code = [], '' def flush(): nonlocal code if code: text.append(MORSE.get(code, '?')) code = '' gap = None for on, length in runs(): if not on: gap = length / rate * wpm / 1.2 continue if code and gap is not None: if gap < 2: if not 0.5 <= gap < 1.5: code += '?' else: flush() if gap >= 5: text.append(' ') elif not 2.5 <= gap < 3.5: text.append('?') code += mark_symbol(length * 1000 / rate, 1200 / wpm) gap = None flush() return ''.join(text).strip() def _configuration(rate, tone, wpm): if not math.isfinite(rate) or rate <= 0 or not math.isfinite(wpm) or wpm <= 0: raise ValueError('positive finite rate and WPM required') if not math.isfinite(tone) or not 0 < tone - 150 < tone + 150 < rate / 2: raise ValueError('tone and noise probes must fit inside Nyquist') count = round(rate / 100) if count < 2 or 1200 / wpm < 40: raise ValueError('10 ms windows require dit duration of at least 40 ms') return count def _evidence(blocks, rate, tone, count, total, cancelled, progress): offset = 0 for block in blocks: if cancelled is not None and cancelled(): raise CancelledError('CW decode cancelled') if any(not math.isfinite(x) or abs(x) > 1 for x in block): raise ValueError('finite normalized PCM required') actual = len(block) if not actual: continue block.extend([0.] * (count - actual)) item = analyze(block, rate, tone, 10)[0] yield Evidence(offset, offset + count, item.tone_power, item.noise_power, item.score) offset += count if progress is not None: progress(min(offset, total), total) if cancelled is not None and cancelled(): raise CancelledError('CW decode cancelled') def decode_samples(samples, rate=8000, tone=700, wpm=20, min_power=0.002, *, cancelled=None, progress=None): """Decode sized finite mono PCM using one 10 ms working window.""" count = _configuration(rate, tone, wpm) if cancelled is not None and cancelled(): raise CancelledError('CW decode cancelled') iterator = iter(samples) blocks = iter(lambda: list(islice(iterator, count)), []) total = len(samples) + (-len(samples)) % count return decode_evidence(_evidence(blocks, rate, tone, count, total, cancelled, progress), rate, wpm, min_power=min_power) @contextmanager def wav_evidence(path, tone=700, wpm=20, *, cancelled=None, progress=None, max_seconds=30, max_frames=240000, max_bytes=1048576): """Yield (rate, evidence iterator) while the bounded mono WAV is open.""" if any(not math.isfinite(x) or x <= 0 for x in (max_seconds, max_frames, max_bytes)): raise ValueError('positive finite WAV limits required') if cancelled is not None and cancelled(): raise CancelledError('CW decode cancelled') with open(path, 'rb') as source: if os.fstat(source.fileno()).st_size > max_bytes: raise ValueError('WAV exceeds byte limit') with wave.open(source, 'rb') as audio: if audio.getnchannels() != 1 or audio.getsampwidth() != 2 or audio.getcomptype() != 'NONE': raise ValueError('uncompressed mono 16-bit PCM WAV required') rate, frames = audio.getframerate(), audio.getnframes() count = _configuration(rate, tone, wpm) if rate > 48000 or frames > max_frames or frames / rate > max_seconds: raise ValueError('WAV exceeds duration/sample/rate limit') def blocks(): remaining = frames while remaining: if cancelled is not None and cancelled(): raise CancelledError('CW decode cancelled') size = min(count, remaining) raw = audio.readframes(size) if len(raw) != size * 2: raise ValueError('truncated WAV') remaining -= size yield [x[0] / 32768 for x in struct.iter_unpack('