diff --git a/README.md b/README.md index b84a181..4a0a47e 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ soundfile. Model-based backends are optional extras, and the test suite never do > **Status: alpha (0.1.0.dev0).** Every tool runs and is tested on synthetic signals. Interfaces may still change before 1.0. +Experimental room-tone synthesis is available as `opendubstage roomtone`, with an opt-in +`specrepair --synthesize-tone` fallback. See [method and limitations](docs/roomtone.md). + ## Quickstart ```bash diff --git a/ROADMAP.md b/ROADMAP.md index 2246f43..fb7f369 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -33,7 +33,13 @@ A sentence-level leveller in the spirit of a dialogue-leveller plug-in, as oppos - Tests: synthetic phrases at known levels converge to the target within ±1 LU, gain never moves inside a word, and the output is bit-exact where no ride is needed. -### Room-tone synthesis +### Room-tone synthesis (experimental mono implementation) + +Implemented as `opendubstage roomtone`, with opt-in `specrepair --synthesize-tone` fallback; +see [method, defaults and limitations](docs/roomtone.md). Synthetic tests cover spectral/level +match, long-lag nonperiodicity and protected samples. Real-world listening validation remains pending. + +Original goal: Generate arbitrary lengths of a recording's own room tone, for fills longer than any clean stretch available (an "ambience match"): - Learn the noise floor's spectral envelope and its slow modulation from gaps between words; synthesise by diff --git a/docs/roomtone.md b/docs/roomtone.md new file mode 100644 index 0000000..f553239 --- /dev/null +++ b/docs/roomtone.md @@ -0,0 +1,78 @@ +# Room-tone synthesis (experimental) + +`opendubstage roomtone` generates a mono statistical approximation of the recording's +own ambience. It does not loop a captured waveform, download a model, detect words, +or promise that matching spectra sounds indistinguishable. Listening validation is pending. + +```sh +opendubstage roomtone input.wav tone.wav --duration 30 --protected words.json \ + --excluded events.json --seed 0 --report tone.json +``` + +Timing files use seconds: `[[start, end], ...]` or `{"words": [{"start": 0.5, "end": 1.2}]}`. +Protected words/kept sounds and excluded events are never learning material. Their complement +must genuinely be clean room tone. Without timings the entire source is explicitly assumed +clean. Mark breaths, music and noises as protected/excluded too; this is not a voice detector. +Invalid/out-of-bounds intervals are rejected, overlaps are merged. Output is floating-point WAV +with the source sample rate. Output and report paths must not overwrite either audio or timing inputs. +A supplied but unrecognized/malformed timing schema fails closed, never silently +removes protection. + +## Method and defaults + +- Pad protected/excluded intervals by **20 ms** as a conservative word-edge guard. +- Analyze only complete **128 ms** Hann frames within clean gaps, with **75% overlap**. + No analysis frame spans a word, event or separate gap. This resolves useful low bands + without requiring multi-second clean stretches. Gaps shorter than one frame are discarded. +- Require **0.5 seconds total** usable clean material and at least **four frames**. More, + representative clean material is better. Insufficient evidence raises an error, never + substitutes unrelated noise. A genuinely silent reference produces silence. +- Normalize each analysis spectrum by its frame power, average it, and independently retain + smoothed frame-level statistics. Synthesis randomizes every frame's phase, Hann-overlap-adds + frames, and compensates the sum of squared windows. Padding/cropping removes boundary + startup fades. DC and Nyquist bins have random real signs. +- Approximate slow amplitude variation by drawing from smoothed log-RMS statistics at + **0.5 second** knots with cosine interpolation. Deviations are clamped to **±6 dB**. + This retains approximate modulation depth, not the original trajectory or modulation spectrum. +- Match overall RMS to the mean source analysis-frame power. This is an RMS measure, + not LUFS. Output duration is rounded to the nearest sample. Peak above full scale is an + error rather than silent clipping/attenuation. There is no limiter. +- A seed (default **0**) makes output reproducible. Different seeds produce different waveforms; + duration is not constrained by the clean material's length. Processing is in-memory and + long requested output uses proportionate memory/time. + +These are engineering defaults, not a perceptual standard. Mono only: stereo width, +interchannel correlation, narrow tonal hum, and strongly nonstationary room events are not +claimed to be reproduced faithfully. Random phase is particularly inappropriate when a +specific tonal phase trajectory must be preserved. + +The JSON report records method, seed, sample count/rate, used clean intervals, analysis-frame +count, source/output RMS, sample peak, learned modulation deviation, and limitations. It does +not claim a quality score or hide insufficient-evidence failures. + +## Opt-in spectral-repair fallback + +```sh +opendubstage specrepair input.wav repaired.wav --events events.json --protected words.json \ + --synthesize-tone --tone-seed 0 --report repairs.json +``` + +Existing contiguous-tone selection remains first choice and all defaults remain unchanged. +Only if no candidate meets `tone_min_len` does the opt-in fallback gather shorter quiet islands +inside `context`. It retains the repairer's protected/excluded/event-gap exclusions and quiet +energy limits, then requires the minimum evidence above. It reports `room_tone_synthesis` and +embeds the synthesis report (clean intervals use absolute source time, with the analyzed +window offset also recorded). If unavailable it reports a skipped repair and reason. The same +patch splicing and integer-PCM writer preserve every sample outside reported patches exactly. +The synthesizer learns the short islands' level directly, so the legacy post-synthesis EQ match +is not applied again. Users must still review the repaired transition by ear. + +## Verification + +Synthetic tests verify, for stationary colored noise, third-octave spectral power within +**±1.5 dB** over resolved bands **100–6300 Hz at 16 kHz**, RMS within **0.2 dB**, and maximum +absolute normalized autocorrelation below **0.05** at **0.1–5 s** lags. Short-lag correlation is +expected from the spectral colour; the bound is not universal for arbitrary tonal recordings. +Tests also cover modulation, timing exclusion, repeatable/fresh seeds, exact length, silence, +malformed input, opt-in fallback, insufficient/context-limited evidence, CLI, and bit-exact +integer-PCM samples outside patches. Numerical checks do not substitute for blind listening. diff --git a/opendubstage/cli.py b/opendubstage/cli.py index ef68453..59de402 100644 --- a/opendubstage/cli.py +++ b/opendubstage/cli.py @@ -11,6 +11,7 @@ # tool -> (module, one-line help). Modules are imported lazily so `--help` stays fast and light. TOOLS = { + "roomtone": ("opendubstage.restore.roomtone", "synthesize non-looping mono ambience from clean gaps"), "leveller": ("opendubstage.mix.leveller", "phrase-safe dialogue gain rides from word timings"), "specrepair": ("opendubstage.restore.specrepair", "spectral repair: room-tone replace, attenuate, LF thumps"), "memofix": ("opendubstage.restore.memofix", "remove car/room rumble learned from word gaps, LTASS matching EQ"), diff --git a/opendubstage/restore/roomtone.py b/opendubstage/restore/roomtone.py new file mode 100644 index 0000000..c4e750c --- /dev/null +++ b/opendubstage/restore/roomtone.py @@ -0,0 +1,211 @@ +"""Room tone from explicitly clean gaps, using random-phase overlap-add synthesis. + +Input timing JSON accepts [[start, end], ...] or {"words": [{"start": ..., "end": ...}]}. +Words/exclusions are protected, NOT automatically detected. Complete Hann analysis +frames are collected inside their complement; no frame crosses a protected boundary. +A normalized average power spectrum learns colour independently of level. Slow level +variation is approximated by randomly resampled, smoothed frame RMS statistics, not +by replaying a recording. Independent random phases avoid a copied loop period. + +Engineering defaults: 128 ms FFT for low-frequency resolution; 75% overlap to smooth +random frames; 20 ms word guard; 0.5 s minimum total clean material (at least four +frames); 0.5 s envelope knots, +/-6 dB envelope clamp for conservative modulation. +These are not a perceptual guarantee. Mono only; no stereo-coherence claims. +""" +from __future__ import annotations + +import argparse +import json +from dataclasses import dataclass +from pathlib import Path + +import numpy as np +from scipy import signal +from scipy.ndimage import uniform_filter1d + + +@dataclass(frozen=True) +class Params: + frame_s: float = 0.128 + guard_s: float = 0.020 + min_clean_s: float = 0.5 + modulation_s: float = 0.5 + modulation_limit_db: float = 6.0 + + +def _audio(x, sr): + x = np.asarray(x, dtype=np.float64) + if x.ndim != 1 or not len(x) or not np.isfinite(x).all(): + raise ValueError("input must be nonempty finite mono audio") + if not isinstance(sr, (int, np.integer)) or isinstance(sr, bool) or sr < 1000: + raise ValueError("sample rate must be an integer >= 1000") + return x + + +def _params(p): + vals = (p.frame_s, p.guard_s, p.min_clean_s, p.modulation_s, p.modulation_limit_db) + if not all(np.isfinite(v) for v in vals) or p.frame_s <= 0 or p.guard_s < 0 or p.min_clean_s <= 0 or p.modulation_s <= 0 or p.modulation_limit_db < 0: + raise ValueError("invalid room-tone parameters") + + +def clean_intervals(duration, protected=(), excluded=(), guard_s=0.02): + """Complement of validated, padded protected/excluded intervals in seconds.""" + busy = [] + for a, b in list(protected) + list(excluded): + if not np.isfinite([a, b]).all() or a < 0 or b <= a or b > duration + 1e-9: + raise ValueError("intervals must satisfy 0 <= start < end <= duration") + busy.append((max(0., a - guard_s), min(duration, b + guard_s))) + cursor, out = 0., [] + for a, b in sorted(busy): + if a > cursor: + out.append((cursor, a)) + cursor = max(cursor, b) + if cursor < duration: + out.append((cursor, duration)) + return out + + +def learn(x, sr, protected=(), excluded=(), p=Params()): + """Learn model from gap samples only. Raises rather than inventing missing tone.""" + x = _audio(x, sr); _params(p) + nfft = max(32, int(round(p.frame_s * sr / 2)) * 2) + hop = nfft // 4 + win = signal.windows.hann(nfft, sym=False) + spectra, levels, slow = [], [], [] + used, intervals = 0, [] + for a, b in clean_intervals(len(x) / sr, protected, excluded, p.guard_s): + i, j = int(np.ceil(a * sr)), int(np.floor(b * sr)) + if j - i < nfft: + continue + seg = x[i:j] + frames = np.lib.stride_tricks.sliding_window_view(seg, nfft)[::hop] + powers = np.mean(frames ** 2, axis=1) + # Silent frames cannot teach spectral colour; silence-only input is handled separately. + keep = powers > 1e-30 + if keep.any(): + z = np.fft.rfft(frames[keep] * win, axis=1) + spectra.append(np.abs(z) ** 2 / powers[keep, None] / np.sum(win ** 2) * nfft) + lv = 10 * np.log10(powers[keep]) + slow.extend(uniform_filter1d(lv, size=max(1, int(p.modulation_s * sr / hop)), mode="nearest")) + levels.extend(powers) + used += len(seg); intervals.append([i / sr, j / sr]) + if used < p.min_clean_s * sr or len(levels) < 4: + raise ValueError("insufficient clean gaps: need minimum clean duration and four complete analysis frames") + power = float(np.mean(levels)) + spec = np.mean(np.concatenate(spectra), axis=0) if spectra else np.zeros(nfft // 2 + 1) + log_level = np.asarray(slow) if slow else np.array([0.]) + variation = np.clip(log_level - np.mean(log_level), -p.modulation_limit_db, p.modulation_limit_db) + return dict(sr=int(sr), nfft=nfft, hop=hop, spectrum=spec, rms=np.sqrt(power), modulation_db=variation, + modulation_s=p.modulation_s, clean_seconds=used / sr, frames=len(levels), intervals=intervals) + + +def synthesize(model, duration, seed=0): + """Return (mono audio, JSON-safe report). Seed makes synthesis repeatable.""" + if not np.isfinite(duration) or duration <= 0: + raise ValueError("duration must be positive and finite") + sr, nfft, hop = model["sr"], model["nfft"], model["hop"] + count = int(round(duration * sr)) + if count < 1: + raise ValueError("duration is shorter than one sample") + if not isinstance(seed, (int, np.integer)) or isinstance(seed, bool) or seed < 0: + raise ValueError("seed must be a nonnegative integer") + seed = int(seed) + rng = np.random.default_rng(seed) + # Pad both ends, then crop into the stationary overlap region. + total = count + 2 * nfft + y, weight = np.zeros(total + nfft), np.zeros(total + nfft) + win = signal.windows.hann(nfft, sym=False) + mag = np.sqrt(model["spectrum"]) + for start in range(0, total, hop): + phase = rng.uniform(-np.pi, np.pi, len(mag)) + z = mag * np.exp(1j * phase) + z[0] = mag[0] * rng.choice([-1., 1.]) + z[-1] = mag[-1] * rng.choice([-1., 1.]) + y[start:start+nfft] += np.fft.irfft(z, n=nfft) * win + weight[start:start+nfft] += win ** 2 + y = y[nfft:nfft+count] / np.sqrt(weight[nfft:nfft+count]) + step = max(1, int(round(model["modulation_s"] * sr))) + knots = np.arange(0, count + step, step) + env = rng.choice(model["modulation_db"], len(knots)) + # Cosine interpolation gives zero slope at every knot, with no repeated audio blocks. + pos = np.arange(count) / step + index = pos.astype(int); blend = (1 - np.cos(np.pi * (pos-index))) / 2 + y *= 10 ** ((env[index] * (1-blend) + env[index+1] * blend) / 20) + measured = np.sqrt(np.mean(y ** 2)) + if measured > 0: + y *= model["rms"] / measured + peak = float(np.max(np.abs(y))) + # Never silently clip or lower learned level. + if peak > 1: + raise ValueError("synthesized tone exceeds full scale; supply quieter clean material") + report = dict(method="random_phase_overlap_add", seed=seed, sample_rate=sr, samples=count, + duration=count/sr, clean_seconds=model["clean_seconds"], analysis_frames=model["frames"], + clean_intervals=model["intervals"], frame_samples=nfft, + source_rms=float(model["rms"]), output_rms=float(np.sqrt(np.mean(y*y))), peak=peak, + modulation_std_db=float(np.std(model["modulation_db"])), + limitations=["mono", "explicit clean-gap selection", "statistical approximation; listening validation required"]) + return y, report + + +def generate(x, sr, duration, protected=(), excluded=(), seed=0, p=Params()): + return synthesize(learn(x, sr, protected, excluded, p), duration, seed) + + + +def read_intervals(path): + """Strict timing schema: a supplied but unrecognized file must never mean no protection.""" + if not path: + return [] + data = json.loads(Path(path).read_text()) + if isinstance(data, dict): + keys = [key for key in ("words", "events", "intervals") if key in data] + if len(keys) != 1: + raise ValueError("timing object must contain exactly one of words/events/intervals") + data = data[keys[0]] + if not isinstance(data, list): + raise ValueError("timings must be a list") + result = [] + for item in data: + if isinstance(item, list) and len(item) == 2: + a, b = item + elif isinstance(item, dict) and "start" in item and "end" in item: + a, b = item["start"], item["end"] + elif isinstance(item, dict) and "t" in item and "e" in item: + a, b = item["t"], item["e"] + else: + raise ValueError("each timing needs [start,end] or start/end (t/e) fields") + if any(isinstance(value, bool) or not isinstance(value, (int, float)) for value in (a,b)): + raise ValueError("timing endpoints must be numbers") + if not np.isfinite([a,b]).all() or a < 0 or b <= a: + raise ValueError("timings must have finite 0 <= start < end") + result.append((float(a),float(b))) + return result + + +def main(argv=None): + import soundfile as sf + ap = argparse.ArgumentParser(prog="opendubstage roomtone", description=__doc__) + ap.add_argument("src"); ap.add_argument("dst") + ap.add_argument("--duration", type=float, required=True) + ap.add_argument("--protected", help="word/kept-sound timing JSON; all other samples must be clean tone") + ap.add_argument("--excluded", help="other unwanted event timing JSON") + ap.add_argument("--seed", type=int, default=0) + ap.add_argument("--report") + a = ap.parse_args(argv) + inputs = {Path(v).resolve() for v in (a.src, a.protected, a.excluded) if v} + outputs = [Path(v).resolve() for v in (a.dst, a.report) if v] + if inputs.intersection(outputs) or len(set(outputs)) != len(outputs): + ap.error("output/report must not overwrite audio or timing inputs, or each other") + try: + x, sr = sf.read(a.src, dtype="float64") + y, report = generate(x, sr, a.duration, read_intervals(a.protected), read_intervals(a.excluded), a.seed) + except ValueError as exc: + ap.error(str(exc)) + sf.write(a.dst, y, sr, subtype="FLOAT") + if a.report: + Path(a.report).write_text(json.dumps(report, indent=2, allow_nan=False) + "\n") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/opendubstage/restore/specrepair.py b/opendubstage/restore/specrepair.py index 4a2e2b7..fb083f6 100644 --- a/opendubstage/restore/specrepair.py +++ b/opendubstage/restore/specrepair.py @@ -52,6 +52,8 @@ class Params: thump_fmax: float = 250.0 # attenuate only bins up to this thump_lf_over_mid_db: float = -3.0 # at the peak, LF band must be within this of the 300-3000 Hz band # (speech is mid-heavy; a thump is LF-heavy) -> keeps voiced speech out + synthesize_tone: bool = False # opt-in fallback when no contiguous tone meets tone_min_len + tone_seed: int = 0 # reproducible randomized-phase synthesis @dataclass @@ -268,17 +270,40 @@ def fill_room_tone(x, sr, t0, s0, s1, active, fdb, floor, protected, excluded, g pieces.append(sl(r0, r1)); got += len(pieces[-1]) - xf if got >= need + 2 * xf: break + synthesis_report = None if not pieces: - return Patch(0, np.zeros(0), dict(rep, skipped="no room tone found")) - tone = eqp_concat(pieces, xf) + if not p.synthesize_tone: + return Patch(0, np.zeros(0), dict(rep, skipped="no room tone found")) + from .roomtone import Params as ToneParams, generate + # Only complete quiet frames inside context may teach the fallback. Never + # learn from a repaired event, protected word, or the active event's gap. + tc = t0 + np.arange(len(fdb)) * HOP + FR / 2 + quiet = ok & (tc - FR / 2 >= centre - p.context) & (tc + FR / 2 <= centre + p.context) + clean = [(max(0., r0 * HOP), min(len(x) / sr, (r1 - 1) * HOP + FR)) + for r0, r1 in runs(quiet)] + blocked, cursor = [], 0. + for a, b in merge_intervals(clean): + if a > cursor: + blocked.append((cursor, a)) + cursor = max(cursor, b) + if cursor < len(x) / sr: + blocked.append((cursor, len(x) / sr)) + try: + tone, synthesis_report = generate(x, sr, need / sr, protected=blocked, + seed=p.tone_seed, p=ToneParams(guard_s=0.)) + except ValueError as exc: + return Patch(0, np.zeros(0), dict(rep, skipped="room-tone synthesis unavailable", reason=str(exc))) + else: + tone = eqp_concat(pieces, xf) while len(tone) < need: tone = eqp_concat([tone, tone], xf) tone = tone[:need] loc = frames_in(len(fdb), t0, [(s0 - 3, s1 + 3)]) & (~busy) & (fdb <= floor + 6) ref = np.concatenate([sl(r0, r1) for r0, r1 in runs(loc)] or [np.zeros(0)]) - if len(ref) < sr * 0.2: + if len(ref) < sr * 0.2 and pieces: ref = np.concatenate(pieces) - tone = eq_match(tone, ref, sr, p.eq_clamp_db) + if synthesis_report is None: + tone = eq_match(tone, ref, sr, p.eq_clamp_db) # equal-power fades; where the event already runs at the gap edge (no room for a full fade before it), # use a short 8 ms fade so the event does not leak through the crossfade kmax = max(1, min(xf, need // 4)); short = max(1, min(int(0.008 * sr), kmax)) @@ -291,9 +316,13 @@ def fill_room_tone(x, sr, t0, s0, s1, active, fdb, floor, protected, excluded, g y = x[n0:n1] * w_out + tone * w_in ai, bi = int(round((active[0] - t0) * sr)) - n0, int(round((active[1] - t0) * sr)) - n0 before, after = level_db(x[n0:n1][ai:bi]), level_db(y[ai:bi]) - rep.update(method="room_tone", start=round(s0, 4), end=round(s1, 4), + rep.update(method="room_tone_synthesis" if synthesis_report is not None else "room_tone", start=round(s0, 4), end=round(s1, 4), event_window=[round(active[0], 4), round(active[1], 4)], tone_pieces=len(pieces), before_db=round(before, 2), after_db=round(after, 2), reduction_db=round(before - after, 2)) + if synthesis_report is not None: + synthesis_report["source_offset_seconds"] = t0 + synthesis_report["clean_intervals"] = [[a + t0, b + t0] for a, b in synthesis_report["clean_intervals"]] + rep["synthesis"] = synthesis_report return Patch(n0, y, rep) @@ -513,12 +542,29 @@ def main(argv=None) -> int: ap.add_argument("--thumps", nargs="?", const="all", help="look for LF thumps: 'all' (default when given) or a JSON list of [start, end] ranges") ap.add_argument("--report", help="write the per-region reports as JSON") + ap.add_argument("--synthesize-tone", action="store_true", help="opt in to random-phase tone when no long clean stretch is found") + ap.add_argument("--tone-seed", type=int, default=0) a = ap.parse_args(argv) if a.thumps == "all": thumps = [(0.0, sf.info(a.src).duration)] else: thumps = _intervals(a.thumps) - reps = repair_file(a.src, a.dst, _intervals(a.events), _intervals(a.protected), thumps) + parser = _intervals + if a.synthesize_tone: + from pathlib import Path + from .roomtone import read_intervals + parser = read_intervals + inputs = {Path(v).resolve() for v in (a.src, a.events, a.protected, + a.thumps if a.thumps != "all" else None) if v} + outputs = [Path(v).resolve() for v in (a.dst, a.report) if v] + if inputs.intersection(outputs) or len(set(outputs)) != len(outputs): + ap.error("output/report must not overwrite audio or timing inputs, or each other") + try: + events, protected = parser(a.events), parser(a.protected) + except ValueError as exc: + ap.error(str(exc)) + reps = repair_file(a.src, a.dst, events, protected, thumps, + p=Params(synthesize_tone=a.synthesize_tone, tone_seed=a.tone_seed)) if a.report: with open(a.report, "w") as fh: json.dump(reps, fh, indent=1, default=float) diff --git a/tests/test_roomtone.py b/tests/test_roomtone.py new file mode 100644 index 0000000..0661937 --- /dev/null +++ b/tests/test_roomtone.py @@ -0,0 +1,216 @@ +"""Synthetic-only checks for statistical room tone; no listening-quality claim.""" +import json +import numpy as np +import pytest +import soundfile as sf +from scipy import signal +from opendubstage.restore.roomtone import Params, clean_intervals, generate, learn +from opendubstage.restore import specrepair +from opendubstage.cli import main + +SR = 16000 + + +def noise(seconds=12, seed=4): + return .002 * signal.lfilter([1], [1, -.85], np.random.default_rng(seed).normal(size=int(SR * seconds))) + + +def test_spectrum_level_and_nonperiodicity(): + x = noise(20) + y, rep = generate(x, SR, 30, seed=9) + f, a = signal.welch(x, SR, nperseg=4096) + _, b = signal.welch(y, SR, nperseg=4096) + # Resolved bands, not unmeasurable near-DC or Nyquist slivers. + for centre in [100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, + 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300]: + band = (f >= centre / 2 ** (1/6)) & (f < centre * 2 ** (1/6)) + assert abs(10 * np.log10(b[band].sum() / a[band].sum())) < 1.5 + assert abs(20 * np.log10(np.sqrt(np.mean(y*y)) / np.sqrt(np.mean(x*x)))) < .2 + # Long-lag autocorrelation excludes expected short-lag colour correlation. + ac = signal.fftconvolve(y, y[::-1], mode='full')[len(y)-1:] + assert np.max(np.abs(ac[int(.1*SR):int(5*SR)] / ac[0])) < .05 + assert len(y) == SR * 30 + assert rep['output_rms'] == pytest.approx(rep['source_rms']) + # No seam at any analysis hop: boundary differences have normal noise scale. + hop = rep['frame_samples'] // 4 + assert np.mean(np.diff(y)[hop-1::hop] ** 2) < 1.5 * np.mean(np.diff(y)**2) + + +def test_protected_and_excluded_samples_never_learned(): + x = noise(5); other = x.copy() + other[SR:2*SR] = .8 + other[3*SR:4*SR] = -.6 + a, _ = generate(x, SR, 2, protected=[(1, 2)], excluded=[(3, 4)]) + b, rep = generate(other, SR, 2, protected=[(1, 2)], excluded=[(3, 4)]) + assert np.array_equal(a, b) + assert all(not (start < 2.02 and end > .98) for start, end in rep['clean_intervals']) + assert np.array_equal(x, noise(5)) + + +def test_seed_repeatability_and_fresh_randomness(): + x = noise() + a, _ = generate(x, SR, 2, seed=3) + b, _ = generate(x, SR, 2, seed=3) + c, _ = generate(x, SR, 2, seed=4) + assert np.array_equal(a, b) + assert not np.array_equal(a, c) + + +def test_learns_slow_modulation(): + x = noise(20) + t = np.arange(len(x)) / SR + x *= 10 ** (4 * np.sin(2*np.pi*.3*t) / 20) + model = learn(x, SR) + assert np.std(model['modulation_db']) > 2 + y, rep = generate(x, SR, 30) + # Learned modulation survives synthesis, rather than stationary white noise. + block = int(.25 * SR) + rms = np.sqrt(np.mean(y[:len(y)//block*block].reshape(-1,block)**2, axis=1)) + assert np.std(20*np.log10(rms)) > 1 + assert rep['modulation_std_db'] > 2 + + +def test_silent_source_stays_silent(): + y, rep = generate(np.zeros(SR), SR, .2) + assert not y.any() + assert rep['source_rms'] == 0 + json.dumps(rep, allow_nan=False) + + +@pytest.mark.parametrize('duration', [0, -1, float('nan'), float('inf'), 1e-10]) +def test_invalid_durations(duration): + with pytest.raises(ValueError): + generate(noise(1), SR, duration) + + +@pytest.mark.parametrize('intervals', [[(-1,1)], [(1,.5)], [(0,2)], [(0,float('nan'))]]) +def test_invalid_intervals(intervals): + with pytest.raises(ValueError): + generate(noise(1), SR, 1, protected=intervals) + + +def test_insufficient_gaps_and_mono_only(): + with pytest.raises(ValueError, match='insufficient'): + generate(noise(1), SR, 1, protected=[(.1, .9)]) + with pytest.raises(ValueError, match='mono'): + generate(np.zeros((SR,2)), SR, 1) + with pytest.raises(ValueError): + generate(np.array([np.nan]*SR), SR, 1) + with pytest.raises(ValueError): + generate(noise(1), SR, 1, p=Params(guard_s=-1)) + + +def test_merge_protection(): + assert clean_intervals(5, [(1,3),(2,4)], guard_s=0) == [(0.,1), (4,5)] + + +def test_cli_roundtrip_and_report(tmp_path): + src, dst, report = [tmp_path / v for v in ('src.wav','dst.wav','report.json')] + sf.write(src, noise(2), SR, subtype='FLOAT') + assert main(['roomtone',str(src),str(dst),'--duration','3','--report',str(report)]) == 0 + y, sr = sf.read(dst) + assert len(y) == SR * 3 and sr == SR + assert json.loads(report.read_text())['samples'] == len(y) + with pytest.raises(SystemExit): + main(['roomtone',str(src),str(src),'--duration','3']) + + +def _fallback_fixture(): + x = noise(6) + # Artificial short quiet islands. Tests directly pin candidate selection, + # independent of noisy event-detector statistics. + fdb = np.full(len(specrepair.frame_db(x, SR)), -20.) + for start in [.2,.8,1.4,4.,4.6,5.2]: + fdb[int(start/.01):int(start/.01)+18] = -50 + return x, fdb + + +def test_opt_in_fallback_and_untouched_samples(): + x, fdb = _fallback_fixture() + args = (x, SR, 0., 2.5, 3.5, (2.6,3.4), fdb, -50., [], [(2.5,3.5)], (2.4,3.6)) + old = specrepair.fill_room_tone(*args, specrepair.Params(), {}) + assert old.report['skipped'] == 'no room tone found' + patch = specrepair.fill_room_tone(*args, specrepair.Params(synthesize_tone=True), {}) + assert patch.report['method'] == 'room_tone_synthesis' + y = x.copy(); y[patch.start_n:patch.start_n+len(patch.new)] = patch.new + assert np.array_equal(y[:patch.start_n], x[:patch.start_n]) + assert np.array_equal(y[patch.start_n+len(patch.new):], x[patch.start_n+len(patch.new):]) + assert patch.report['synthesis']['clean_seconds'] >= .5 + + +def test_fallback_respects_context_and_insufficient_evidence(): + x, fdb = _fallback_fixture() + patch = specrepair.fill_room_tone(x, SR, 0., 2.5, 3.5, (2.6,3.4), fdb, -50., [], + [(2.5,3.5)], (2.4,3.6), specrepair.Params(synthesize_tone=True, context=.5), {}) + assert patch.report['skipped'] == 'room-tone synthesis unavailable' + assert not len(patch.new) + + +def test_synthesized_patch_pcm_bit_exact_outside(tmp_path): + x, fdb = _fallback_fixture() + patch = specrepair.fill_room_tone(x, SR, 0., 2.5, 3.5, (2.6,3.4), fdb, -50., [], + [(2.5,3.5)], (2.4,3.6), specrepair.Params(synthesize_tone=True), {}) + src, dst = tmp_path/'in.wav', tmp_path/'out.wav' + sf.write(src, x, SR, subtype='PCM_24') + specrepair.write_patched(src,dst,[patch]) + a, _ = sf.read(src,dtype='int32'); b,_ = sf.read(dst,dtype='int32') + assert np.array_equal(a[:patch.start_n],b[:patch.start_n]) + end=patch.start_n+len(patch.new) + assert np.array_equal(a[end:],b[end:]) + + +def test_cli_report_cannot_overwrite_audio(tmp_path): + src, dst = tmp_path/'in.wav', tmp_path/'out.wav' + sf.write(src, noise(1), SR) + before = src.read_bytes() + with pytest.raises(SystemExit): + main(['roomtone',str(src),str(dst),'--duration','1','--report',str(src)]) + assert src.read_bytes() == before + assert not dst.exists() + + +def test_specrepair_cli_accepts_opt_in_without_changing_unpatched_pcm(tmp_path): + src, dst = tmp_path/'in.wav', tmp_path/'out.wav' + sf.write(src, noise(1), SR, subtype='PCM_16') + assert main(['specrepair',str(src),str(dst),'--synthesize-tone','--tone-seed','7']) == 0 + a,_ = sf.read(src,dtype='int32'); b,_ = sf.read(dst,dtype='int32') + assert np.array_equal(a,b) + + +@pytest.mark.parametrize('data', [{"wordz": [[0,1]]}, {"words": None}, {"words": [], "events": []}, + "oops", [[0,1,2]], [{"start": 0}], [[False,1]], [[0,"1"]]]) +def test_malformed_timing_schema_fails_closed(tmp_path, data): + src, dst, timings = tmp_path/'in.wav', tmp_path/'out.wav', tmp_path/'words.json' + sf.write(src, noise(1), SR) + timings.write_text(json.dumps(data)) + with pytest.raises(SystemExit): + main(['roomtone',str(src),str(dst),'--duration','1','--protected',str(timings)]) + assert not dst.exists() + with pytest.raises(SystemExit): + main(['specrepair',str(src),str(dst),'--synthesize-tone','--protected',str(timings)]) + assert not dst.exists() + + +@pytest.mark.parametrize('tool', ['roomtone', 'specrepair']) +@pytest.mark.parametrize('output', ['audio', 'report']) +def test_outputs_cannot_overwrite_timing_inputs(tmp_path, tool, output): + src, dst, timing = tmp_path/'in.wav', tmp_path/'out.wav', tmp_path/'words.json' + sf.write(src, noise(1), SR) + timing.write_text('[]') + args=[tool,str(src),str(timing if output=='audio' else dst),'--protected',str(timing)] + args += ['--duration','1'] if tool=='roomtone' else ['--synthesize-tone'] + if output=='report': + args += ['--report',str(timing)] + with pytest.raises(SystemExit): + main(args) + assert timing.read_text() == '[]' + assert not dst.exists() + + +def test_fallback_report_intervals_are_absolute_for_windowed_input(): + x, fdb = _fallback_fixture() + patch = specrepair.fill_room_tone(x, SR, 10., 12.5, 13.5, (12.6,13.4), fdb, -50., [], + [(12.5,13.5)], (12.4,13.6), specrepair.Params(synthesize_tone=True), {}) + assert patch.report['method'] == 'room_tone_synthesis' + assert patch.report['synthesis']['source_offset_seconds'] == 10. + assert all(10 <= a < b <= 16 for a,b in patch.report['synthesis']['clean_intervals'])