Ears for coding agents: let AI hear, review and make sound. CPU-only, open source.
fit renderer: takes ring out, no DC offset, steady spectral loudness, residual roll-off, 3-seed scoring
Leonxlnx29c4abda1e52
diff --git a/earshot/forge/bank.py b/earshot/forge/bank.pyindex 3da61ca..370733c 100644--- a/earshot/forge/bank.py+++ b/earshot/forge/bank.py@@ -31,7 +31,8 @@ Entry types:gain_db (+ gain): the render block; the delivered WAV = normalise(fn(params, rng(seed)), peak_db) * gainfit category 'fit', member, model (an `earshot fit` model, its fit_engine report dropped), seed, vary, source{id, title, author, license, url}, pitch_hz (the measured note of a tonal fit, else null): rendered by- earshot.forge.fit.render(model, seed, vary=vary)+ earshot.forge.fit.render(model, seed, vary=vary, take=True) (a take: it rings out to 60 dB down instead+ of stopping where the trimmed recording stopped, no DC offset)Every entry carries `meta`, measured on its render at peak -1 dBFS: dur_s, onset_s, peak_s, end_s, attack_ms,centroid_hz, lufs_m_max, channels and a short fingerprint `fp` (50 ms RMS frames in dB re the loudest frame, to thepoint where the sound stays 60 dB down) so a later re-render (another machine, numpy, scipy) can be checked against@@ -243,7 +244,7 @@ def renderer(entry, seed=None):from . import fit as _fitdef run(edit=None, fit_knobs=None):- kn = {'vary': float(entry.get('vary') or 0.0), **(fit_knobs or {})}+ kn = {'vary': float(entry.get('vary') or 0.0), 'take': True, **(fit_knobs or {})}if not kn['vary']:kn.pop('vary')return _valid(np.asarray(_fit.render(entry['model'], seed=seed, **kn), np.float64), entry)diff --git a/earshot/forge/fit/__init__.py b/earshot/forge/fit/__init__.pyindex 33c38c1..7889ab5 100644--- a/earshot/forge/fit/__init__.py+++ b/earshot/forge/fit/__init__.py@@ -68,9 +68,10 @@ def _write_wav(path, y):def run_fit(path, kind='auto', layers=None, output=None, out_dir=None, render_path=None, report=False, clap=False,- describe=False, seed=0, target=None, fast=False, verbose=True, log=print):+ describe=False, seed=0, target=None, fast=False, verbose=True, log=print, takes=False):"""the `earshot fit <file>` pipeline as a function: fit, write the recipe / WAV / PNG / model JSON, measure.- Returns the result dict (paths, quality, floor, chosen kind, timings)."""+ takes=True scores the candidates on seeds 0, 1, 2 (auto.TAKE_SEEDS), not seed 0 alone. Returns the result dict+ (paths, quality, floor, chosen kind, timings)."""import timeimport numpy as np@@ -87,7 +88,7 @@ def run_fit(path, kind='auto', layers=None, output=None, out_dir=None, render_pat0 = time.time()X = Q.load(path)m = auto.fit(X, kind=kind, layers=layers, clap=clap, fast=fast, name=os.path.basename(path),- verbose=verbose and kind == 'auto')+ verbose=verbose and kind == 'auto', seeds=auto.TAKE_SEEDS if takes else (0,))t_fit = time.time() - t0codegen.write_recipe(P['recipe'], m, name=P['name'], source=path, target=target)rec = codegen.load_recipe(P['recipe'])@@ -174,6 +175,8 @@ def add_cli(sub):p.add_argument('--target', help='critic description for the recipe (default: from the refset category / sound)')p.add_argument('--seed', type=int, default=0)p.add_argument('--fast', action='store_true', help='cheaper fitter settings (previews)')+ p.add_argument('--takes', action='store_true',+ help='a fit meant to make takes: score every candidate on seeds 0, 1, 2 (not seed 0 alone)')p.add_argument('--morph', nargs=2, metavar=('A', 'B'),help='fit two recordings with one kind and write a morph recipe')p.add_argument('--amount', type=float, default=0.5, help='with --morph: 0 = A, 1 = B (default 0.5)')@@ -216,7 +219,7 @@ def add_cli(sub):return 2layers = tuple(a.layers.split(',')) if a.layers else Noner = run_fit(a.file, a.kind, layers, a.output, a.dir, a.render, a.report, a.clap, a.describe, a.seed, a.target,- a.fast, verbose=not a.quiet and not a.json)+ a.fast, verbose=not a.quiet and not a.json, takes=a.takes)lines = [f"{os.path.basename(a.file)} -> {r['kind']} model" + (f" (auto chose {r['chosen']})"if a.kind == 'auto' else '')+ f" in {r['seconds']['fit']:.1f} s",diff --git a/earshot/forge/fit/auto.py b/earshot/forge/fit/auto.pyindex c63dfc8..30dff92 100644--- a/earshot/forge/fit/auto.py+++ b/earshot/forge/fit/auto.py@@ -31,7 +31,9 @@ kind='auto' (fit):3. each candidate is rendered (seed 0) and scored against the original (quality.score: mel dB, tone, envelope,onset, excess / missing energy, + CLAP with clap=True); the lowest score wins. Hybrids reuse the single-kindfits as their first layer. The model gets a 'fit_engine' entry: the character, every candidate's metrics, the- choice.+ choice. seeds=TAKE_SEEDS (`earshot fit --takes`) scores the mean over seeds 0, 1, 2 instead: a candidate with+ noise in it can win on one lucky seed-0 draw and then make weaker takes (on 10 of the 12 validation fits the+ chosen candidate's seed-0 score sat 0.1-2.4 under its mean over seeds 0-7).Pure numpy / scipy (CLAP only with clap=True). Deterministic: render(model, seed, knobs) is a pure function."""import copy@@ -43,6 +45,8 @@ import numpy as npfrom . import quality as QSR = 48000+TAKE_DC_HZ = 8.0 # a take (render(take=True)) is high-passed here: no DC offset, no infrasound+TAKE_SEEDS = (0, 1, 2) # fit(seeds=TAKE_SEEDS): score candidates on these seeds (a fit meant to make takes)KINDS = ('modal', 'spectral', 'sines', 'hybrid')ROLE = {'sines': 'tone', 'modal': 'body', 'spectral': 'air'}HYBRIDS = {'modal+spectral': ('modal', 'spectral'), 'sines+spectral': ('sines', 'spectral'),@@ -521,8 +525,8 @@ def fit(x, kind='auto', layers=None, candidates=None, clap=False, fast=False, secollect=None, post=True):"""a model of x (path or 48 kHz array). kind: 'auto' | 'modal' | 'sines' | 'spectral' | 'hybrid' (layers=('modal', 'spectral') by default). auto: candidates (default plan(character(x))) are all fitted, rendered with- `seeds` and scored (quality.score; clap=True adds CLAP), the best is returned with a 'fit_engine' entry.- collect: a dict that receives every candidate's model ({candidate: model})"""+ `seeds` and scored (quality.score; clap=True adds CLAP; the mean over seeds), the best is returned with a+ 'fit_engine' entry. collect: a dict that receives every candidate's model ({candidate: model})"""import osif name is None and isinstance(x, (str, os.PathLike)):name = os.path.basename(str(x))@@ -541,6 +545,7 @@ def fit(x, kind='auto', layers=None, candidates=None, clap=False, fast=False, secands = list(candidates or plan(c))base, rows, best = {}, [], Noneclap_e = None+ seeds = tuple(int(s) for s in seeds) or (0,)if clap:clap_e = Q.clap_embed([X])[0]for cand in cands:@@ -559,11 +564,12 @@ def fit(x, kind='auto', layers=None, candidates=None, clap=False, fast=False, seif post:r0 = [render(m, seed=s) for s in (0, 1)]m['post'] = fit_post(X, r0)- for s in seeds:- y = render(m, seed=s)+ ys = [render(m, seed=s) for s in seeds]+ E = Q.clap_embed(ys) if clap_e is not None else None+ for i, y in enumerate(ys):q = Q.quality(X, y)- if clap_e is not None:- q['clap_cos'] = round(float(clap_e @ Q.clap_embed([y])[0]), 4)+ if E is not None:+ q['clap_cos'] = round(float(clap_e @ E[i]), 4)qs.append(q)sc = float(np.mean([Q.score(q) for q in qs]))row = {'candidate': cand, 'score': round(sc, 3), 'fit_s': round(tf, 2), 'quality': qs[0]}@@ -576,7 +582,7 @@ def fit(x, kind='auto', layers=None, candidates=None, clap=False, fast=False, seraise RuntimeError('every fitter failed: ' + '; '.join(r.get('error', '') for r in rows))m = best[2]m['fit_engine'] = {'kind': 'auto', 'chosen': best[1], 'character': c, 'candidates': rows,- 'seconds': round(time.time() - t0, 2)}+ 'seeds': list(seeds), 'seconds': round(time.time() - t0, 2)}return m@@ -589,7 +595,7 @@ def _layer_render(m, seed, two, **kn):elif k == 'modal':y = F['modal'].render(m, seed, **kn)else:- y = np.asarray(F['spectral'].render(m, seed, **kn), np.float64)+ y = np.asarray(F['spectral'].render(m, seed, limit=False, steady=False, **kn), np.float64)y = np.asarray(y, np.float64)if y.ndim == 1:y = y[:, None]@@ -601,8 +607,9 @@ def _layer_render(m, seed, two, **kn):def _render_hybrid(model, seed=0, pitch=0.0, decay=1.0, bright=0.0, vary=0.0, width=1.0, tone_db=0.0,- body_db=0.0, air_db=0.0, stereo=None):+ body_db=0.0, air_db=0.0, stereo=None, ring=None):two = (model.get('channels', 1) == 2) if stereo is None else bool(stereo)+ ring = vary > 0 if ring is None else bool(ring)gains = {'tone': tone_db, 'body': body_db, 'air': air_db}ys = []for i, L in enumerate(model['layers']):@@ -610,11 +617,11 @@ def _render_hybrid(model, seed=0, pitch=0.0, decay=1.0, bright=0.0, vary=0.0, wik = kind_of(m)s = (int(seed) * 7 + 101 * (i + 1)) & 0x7FFFFFFFif k == 'sines':- kn = {'transpose': pitch, 'decay': decay, 'brightness': bright, 'vary': vary}+ kn = {'transpose': pitch, 'decay': decay, 'brightness': bright, 'vary': vary, 'ring': ring}elif k == 'modal':size = 2.0 ** (-pitch / 12.0)kn = {'size': size, 'damping': size / max(decay, 1e-3), 'brightness': bright, 'vary': vary,- 'stereo': False if m.get('side') is None else two, 'width': width}+ 'stereo': False if m.get('side') is None else two, 'width': width, 'ring': ring}else:kn = {'shift': pitch / 12.0, 'tilt': bright, 'width': width}y = _layer_render(m, s, two, **kn) * 10 ** ((float(L.get('gain_db', 0.0)) + gains[L['role']]) / 20)@@ -633,7 +640,7 @@ def _render_raw(model, seed=0, **knobs):if k == 'morph':w = knobs.pop('morph', model.get('amount', 0.5))return render(morph(model['a'], model['b'], w), seed, **knobs)- allowed = set(knobs_of(model)) | {'stereo', 'gain', 'gain_db'}+ allowed = set(knobs_of(model)) | {'stereo', 'gain', 'gain_db', 'ring'}bad = set(knobs) - allowedif bad:raise ValueError(f'unknown knobs for a {k} model: {sorted(bad)} (known: {sorted(allowed)})')@@ -654,25 +661,43 @@ def _render_raw(model, seed=0, **knobs):else:if 'gain' in knobs:knobs['gain_db'] = knobs.pop('gain')- y = np.asarray(F['spectral'].render(model, seed, **knobs), np.float64)+ knobs.pop('ring', None) # a noise gesture has no ringing modes+ y = np.asarray(F['spectral'].render(model, seed, limit=False, **knobs), np.float64)if not two:y = y.mean(1)return np.asarray(y, np.float64)-def render(model, seed=0, **knobs):+def render(model, seed=0, take=None, **knobs):"""the sound of a model (float64, 48 kHz): (n, 2) when the source was stereo, else (n,), through the model's poststage (fitted EQ + gain) when it has one. knobs: knobs_of(model) names (identity values re-render the fit);- unknown names raise"""- y = _render_raw(model, seed, **dict(knobs))+ unknown names raise.+ take (not a search knob; default None = True when vary > 0): render a take to use, not the fit to measure.+ Modes and partials ring out past the recording's length to 60 dB down with a fade instead of stopping where+ the trimmed recording stopped (modal / sines ring=True), and the result is high-passed at TAKE_DC_HZ (no DC+ offset, no infrasound: a noise band modulated by its envelope spreads energy down to DC, and a new seed of a+ fit could carry an offset of 1-2 % of its peak). take=False keeps the recording's length and every bit of its+ low end: what the fit's quality is measured on (the identity render, seed 0, is that)."""+ knobs = dict(knobs)+ take = float(knobs.get('vary') or 0.0) > 0 if take is None else bool(take)if kind_of(model) == 'morph':- return y+ w = knobs.pop('morph', model.get('amount', 0.5))+ return render(morph(model['a'], model['b'], w), seed, take=take, **knobs)+ knobs.setdefault('ring', take)+ y = _render_raw(model, seed, **knobs)k = kind_of(model)scale = float(knobs.get('length', 1.0)) / float(knobs.get('speed', 1.0)) if k == 'spectral' else 1.0tail = {'modal': lambda: float(knobs.get('size', 1.0)) / float(knobs.get('damping', 1.0)),'sines': lambda: float(knobs.get('stretch', 1.0)) * float(knobs.get('decay', 1.0)),'hybrid': lambda: float(knobs.get('decay', 1.0))}.get(k, lambda: 1.0)()- return apply_post(y, model.get('post'), scale, tail)+ y = apply_post(y, model.get('post'), scale, tail)+ return _dc_block(y) if take else y+++def _dc_block(y, hz=TAKE_DC_HZ):+ """a causal 2nd-order Butterworth high-pass (per channel)"""+ from scipy import signal+ return signal.sosfilt(signal.butter(2, hz, 'highpass', fs=SR, output='sos'), np.asarray(y, np.float64), axis=0)def identity(model):diff --git a/earshot/forge/fit/modal.py b/earshot/forge/fit/modal.pyindex 0d636d5..56baa0d 100644--- a/earshot/forge/fit/modal.py+++ b/earshot/forge/fit/modal.py@@ -47,6 +47,14 @@ the noise bands), width (x side), room (0..1 wet of a small stochastic room), vafrequencies / decays / amplitudes, strike timing and noise bands: new takes of the same object), spacing (x thetime between strikes), gain (dB). seed changes only the noise (and, with vary > 0, the jitter). The side of astereo render is the mid's Hilbert transform shaped per band by `side`.+ring: a take that rings out (default None: on when vary > 0; off, the render keeps the recording's length and+fade-out, what the fit is measured against). A trimmed recording stops while its modes still ring (a key's release+70 ms before the cut), and vary lengthens some decays further; with ring the render goes on until every mode is+RING_DB (60 dB) under the loudest strike's mode sum, at least RING_MIN_S (RING_LOUD_S when the modes are still+above RING_LOUD_DB at the cut) and at most RING_EXT x the sound's span longer, and the added stretch fades 60 dB+(linear in dB) instead of the recording's fade-out (the noise keeps falling at its last slope). A fitted model often+holds a few faint poles with decays of seconds (noise the fitter took for modes); the cap keeps a take short, the+fade keeps it from stopping on them. The fit engine's takes (auto.render(take=True), the bank) ring out.What it is not for: glides / chirps (a rising pop, a zip) split into many false strikes; designed impacts with longreverb tails fit, but the tail is mostly noise bands.Pure numpy / scipy.@@ -69,6 +77,11 @@ NOISE_H = 0.4 # crossover half-width, ERB-rate unitsGRID = 0.0005 # dense envelope grid, sSUB_HZ = 20.0 # below this the residual is kept as a decimated waveform (a pressure pulse)SUB_DEC = 480 # its decimation (100 Hz)+RING_DB = 60.0 # ring=True: the render goes on until every mode is this far under the loudest strike's+RING_EXT = 0.5 # mode sum, at most this x the sound's span (first strike to the nominal end) longer, at+RING_MIN_S = 0.06 # least this much longer; the added stretch fades RING_DB (linear in dB)+RING_LOUD_DB = -50.0 # modes still above this at the recording's end (re the loudest strike's mode sum) ...+RING_LOUD_S = 0.2 # ... get at least this long a fade (a fast fade from a loud ring reads as a cut)# ------------------------------------------------------------------------------------------------ helpers@@ -740,9 +753,10 @@ def _fit(x, max_strikes=10, kmax=16, nmax=384, passes=2, skip_ms=0.5, prune_db=-# ------------------------------------------------------------------------------------------------ render# Everything below `render_model` uses only numpy / scipy and the constants SR, GRID: to_code() copies these# functions verbatim into a standalone script, so the script renders the same samples as render().-def _plan(model, seed, size, damping, brightness, hardness, noise, vary, spacing):+def _plan(model, seed, size, damping, brightness, hardness, noise, vary, spacing, ring=False):"""the model with the knobs applied: strikes (onset s, ramp s, f, tau, amp, phase arrays), the dense noise- envelope (bands x grid, dB) on a GRID-second grid, the band centres and the output length (samples)"""+ envelope (bands x grid, dB) on a GRID-second grid, the band centres and the output length (samples). ring: when+ a mode outlives the recording's length, render on until every mode is RING_DB down (see render)"""rv = np.random.default_rng([int(seed) & 0xFFFFFFFF, 1])st = model['strikes']t0 = [s['t'] for s in st]@@ -773,6 +787,15 @@ def _plan(model, seed, size, damping, brightness, hardness, noise, vary, spacingL0 = model['length_s']t_last0, t_last = t0[-1], tn[-1]length = t_last + (L0 - t_last0) * max(1.0, stretch)+ n_nom = int(round(length * SR))+ if ring:+ t_ring, lv_end = _ring_end(strikes, length)+ ring = None+ if t_ring > length + 0.001:+ lo = RING_LOUD_S if lv_end > RING_LOUD_DB else RING_MIN_S+ ext = min(max(t_ring - length, lo), max(lo, RING_EXT * (length - tn[0])))+ length += ext+ ring = (n_nom, int(round(length * SR)))n_out = int(round(length * SR))# dense noise envelope on the original's time axisnz = model['noise']@@ -818,7 +841,26 @@ def _plan(model, seed, size, damping, brightness, hardness, noise, vary, spacingsub = model.get('sub')sub = (int(round((tn[0] - t0[0]) * SR)), sub['x']) if sub else Nonereturn {'strikes': strikes, 'E': E, 'cen': cen, 'edges': edges, 'h': nz['h_erb'], 'n': n_out, 'sub': sub,- 'fade': float(model.get('fade_out_s', 0.0))}+ 'fade': 0.0 if ring else float(model.get('fade_out_s', 0.0)), 'ring': ring or None}+++def _ring_end(strikes, t_end):+ """(when every mode of the planned strikes has decayed RING_DB under the loudest strike's mode sum (s), the+ modes' summed level at t_end in dB re that sum); the contact modes are a few ms long and do not count"""+ ref = max([float(np.sqrt(np.sum(np.asarray(s[4]) ** 2))) for s in strikes] + [0.0])+ if ref <= 0:+ return 0.0, -300.0+ thr = ref * 10 ** (-RING_DB / 20)+ end, p_end = 0.0, 0.0+ for s in strikes:+ a = np.abs(np.asarray(s[4]))+ tau = np.asarray(s[3], float)+ k = a > thr+ if k.any():+ end = max(end, s[0] + float(np.max(tau[k] * np.log(a[k] / thr))))+ if t_end >= s[0]:+ p_end += float(np.sum((a * np.exp(-(t_end - s[0]) / np.maximum(tau, 1e-9))) ** 2))+ return end, float(10 * np.log10(p_end / ref ** 2 + 1e-30))def _masks(n_fft, edges, h):@@ -911,11 +953,15 @@ def _room(y, rng, wet, rt60=0.35):def render_model(model, seed=0, size=1.0, damping=1.0, brightness=0.0, hardness=1.0, noise=0.0, width=1.0,- room=0.0, vary=0.0, spacing=1.0, gain=0.0, stereo=None):- plan = _plan(model, seed, size, damping, brightness, hardness, noise, vary, spacing)+ room=0.0, vary=0.0, spacing=1.0, gain=0.0, stereo=None, ring=None):+ ring = vary > 0 if ring is None else bool(ring)+ plan = _plan(model, seed, size, damping, brightness, hardness, noise, vary, spacing, ring)n = plan['n']s = int(seed) & 0xFFFFFFFFmid = _modes(plan, n) + _noise(plan, n, np.random.default_rng([s, 2]))+ if plan.get('ring'): # the ring-out past the recording's length fades RING_DB (linear in dB)+ a, b = plan['ring']+ mid[a:b] *= 10 ** (-RING_DB / 20 * np.arange(b - a) / max(b - a, 1))two = (model.get('channels', 1) == 2) if stereo is None else bool(stereo)if two:side = np.zeros(n)@@ -941,7 +987,7 @@ def render_model(model, seed=0, size=1.0, damping=1.0, brightness=0.0, hardness=def render(model, seed=0, **knobs):"""re-synthesise a modal model: (n, 2) for a stereo original (or stereo=True), else (n,); knobs: size, damping,- brightness, hardness, noise, width, room, vary, spacing, gain, stereo (see the module doc)"""+ brightness, hardness, noise, width, room, vary, spacing, gain, stereo, ring (see the module doc)"""check(model)return render_model(model, seed, **knobs)@@ -981,7 +1027,7 @@ def summary(model):return '\n'.join(lines)-_CODE_FUNCS = (_plan, _masks, _ring, _modes, _noise, _room, render_model)+_CODE_FUNCS = (_plan, _ring_end, _masks, _ring, _modes, _noise, _room, render_model)def _literal(model):@@ -1018,7 +1064,9 @@ def to_code(model, name='sound'):f'{model["version"]}).\n\n{summary(model)}\n\nrender(seed, size=, damping=, brightness=, hardness=, 'f'noise=, width=, room=, vary=, spacing=, gain=) -> float array at 48 kHz.\n"""\n')body = [head, 'import sys', '', 'import numpy as np', 'from scipy import fft as sfft', 'from scipy import signal', '',- f'SR = {SR}', f'GRID = {GRID!r}', f'SUB_DEC = {SUB_DEC}', '', _literal(model), '', '']+ f'SR = {SR}', f'GRID = {GRID!r}', f'SUB_DEC = {SUB_DEC}', f'RING_DB = {RING_DB!r}', f'RING_EXT = {RING_EXT!r}',+ f'RING_MIN_S = {RING_MIN_S!r}', f'RING_LOUD_DB = {RING_LOUD_DB!r}', f'RING_LOUD_S = {RING_LOUD_S!r}', '',+ _literal(model), '', '']for fn in _CODE_FUNCS:body.append(inspect.getsource(fn))body.append('')diff --git a/earshot/forge/fit/sines.py b/earshot/forge/fit/sines.pyindex e3641ee..f2d601f 100644--- a/earshot/forge/fit/sines.py+++ b/earshot/forge/fit/sines.py@@ -36,9 +36,15 @@ Model (plain JSON-serialisable dict):'partials': [{'t0': birth s, 'rise': s, 't1': last breakpoint s, 'rel': release s, 'ph': phase at t0 (rad),'freq': [[t, Hz], ...], 'amp': [[t, dBFS], ...]}, ...] (strongest first)'transients': [{'t': start s, 'atoms': [[f Hz, tau s, amplitude, phase], ...]}, ...] (one per strike)- 'residual': {'bands': [Hz centres, 1/3 octave], 'times': [s, ...], 'db': [[dBFS per time] per band]},+ 'residual': {'bands': [Hz centres, 1/3 octave], 'times': [s, ...], 'db': [[dBFS per time] per band],+ 'hf': [[Hz, dB], ...]},'width': side/mid energy of the source in dB (stereo renders), 'analysis': {...}, 'summary': {...}}The residual bands are triangles on log frequency that sum to one; a band's dB is the RMS of the band-passed residual.+The top band (16 kHz) is flat to Nyquist and the lowest (50 Hz) flat to DC. Above 16 kHz the noise follows the+recording's own roll-off `hf` (its residual's spectrum re 16 kHz, made monotone); models fitted before 0.3 have no+`hf` and keep the flat top band, which leaves noise above 20 kHz that the recording did not have (hf_rolloff(x)+measures it for them). The lowest band, modulated by its envelope, spreads energy down to DC: a new seed can carry an+offset of 1-2 % of its peak; the fit engine's takes (auto.render(take=True)) are DC-blocked for that.render(model, seed, knobs) knobs (all default to the identity, which re-renders the fit):transpose (semitones; every partial and the residual's spectrum move together, relative partials kept),@@ -48,7 +54,9 @@ render(model, seed, knobs) knobs (all default to the identity, which re-renderstransient (gain on the noise in the first 15 ms after each strike), vary (0-1: seeded per-partial detune,level and decay jitter, band jitter: new takes of the same object), partials (keep the N strongest),inharm (stretch of partial ratios about the lowest strong partial: f/f_low -> (f/f_low)^(1+inharm)),- phases ('fit' or 'random'), gain_db, stereo (False: mono; True: (n, 2) with the residual decorrelated by width).+ phases ('fit' or 'random'), gain_db, stereo (False: mono; True: (n, 2) with the residual decorrelated by width),+ ring (None: True when vary > 0; True renders until the last partial's release ends, so a decay that vary+ lengthens is not cut at the recording's length, and fades the noise out past the last residual time).Deterministic: render(model, seed, knobs) is a pure function (the noise and every jitter come from `seed`).Pure numpy / scipy; no recorded audio is stored in a model, only parameters.@@ -67,7 +75,8 @@ WINDOWS = (512, 1024, 2048, 4096)DEFAULT_KNOBS = {'transpose': 0.0, 'stretch': 1.0, 'decay': 1.0, 'brightness': 0.0, 'vibrato': 0.0,'vibrato_rate': 5.5, 'drift': 0.0, 'residual': 1.0, 'transient': 1.0, 'vary': 0.0, 'partials': None,- 'inharm': 0.0, 'phases': 'fit', 'gain_db': 0.0, 'stereo': False}+ 'inharm': 0.0, 'phases': 'fit', 'gain_db': 0.0, 'stereo': False, 'ring': None}+HF_POINTS = (16000.0, 17000.0, 18000.0, 19000.0, 20000.0, 21000.0, 22000.0, 23000.0, 24000.0) # the roll-off grid# ================================================================================================ small helpers@@ -196,13 +205,21 @@ def synth_partials(partials, n, sr=48000, cents=None):return y-def synth_noise(bands, times, levels, n, seed=0, sr=48000, pad=4096):+def synth_noise(bands, times, levels, n, seed=0, sr=48000, pad=4096, hf=None):"""noise shaped by band envelopes: white noise split by band_masks(), each band at unit RMS times its dB envelope- (levels[b] at `times`, linear in dB between them)"""+ (levels[b] at `times`, linear in dB between them). hf ([[Hz, dB], ...]): above its first frequency the masks+ follow it, a roll-off that takes the energy out (the band's level below it stays as fitted)"""r = np.random.default_rng(int(seed) & 0xFFFFFFFF)nfft = 1 << int(np.ceil(np.log2(n + 2 * pad)))spec = np.fft.rfft(r.standard_normal(nfft))masks = band_masks(bands, nfft, sr)+ keep = np.ones(len(masks))+ if hf:+ f = np.fft.rfftfreq(nfft, 1.0 / sr)+ h = np.asarray(hf, float)+ shaped = masks * np.where(f > h[0, 0], 10.0 ** (np.interp(f, h[:, 0], h[:, 1]) / 20.0), 1.0)+ keep = np.sqrt((shaped ** 2).sum(1) / np.maximum((masks ** 2).sum(1), 1e-30))+ masks = shapedt = np.arange(n) / sry = np.zeros(n)for b in range(len(bands)):@@ -210,11 +227,29 @@ def synth_noise(bands, times, levels, n, seed=0, sr=48000, pad=4096):if lv.max() < -140:continueu = np.fft.irfft(spec * masks[b], nfft)[pad:pad + n]- u /= np.sqrt(np.mean(u ** 2)) + 1e-20+ u *= keep[b] / (np.sqrt(np.mean(u ** 2)) + 1e-20)y += u * 10.0 ** (np.interp(t, times, lv) / 20.0)return y+def hf_rolloff(x, sr=48000, points=(16000.0, 17000.0, 18000.0, 19000.0, 20000.0, 21000.0, 22000.0, 23000.0,+ 24000.0)):+ """a recording's (or a residual's) roll-off above points[0]: [[Hz, dB re points[0]], ...] from its long-term+ spectrum (1/6-octave means), made monotone (a partial up there does not lift the noise), floored at -80 dB"""+ m = np.asarray(x, float)+ m = m.mean(1) if m.ndim == 2 else m+ nper = int(min(4096, 1 << int(np.floor(np.log2(max(len(m), 256))))))+ f, P = signal.welch(m, sr, nperseg=nper)+ out, run, ref = [], 0.0, None+ for p in points:+ k = (f >= p * 2 ** (-1 / 12)) & (f <= min(p * 2 ** (1 / 12), sr / 2))+ v = 10 * np.log10(float(np.mean(P[k])) + 1e-30) if k.any() else -300.0+ ref = v if ref is None else ref+ run = min(run, v - ref)+ out.append([float(p), round(max(run, -80.0), 1)])+ return out++def synth_atoms(transients, n, sr=48000):"""strike transients: each a start time and damped sinusoids [f Hz, tau s, amp, phase] that start at fullamplitude (a click is a burst of very short modes)"""@@ -833,6 +868,7 @@ def _analyze(x, sr, W, onsets, width, opts):res2 = res - synth_atoms(trans, len(x), sr)centers = _band_centers()resid = _residual_model(res2, sr, centers, floor_db=peak_db - opts['res_range_db'], onsets=onsets)+ resid['hf'] = hf_rolloff(res2, sr, HF_POINTS)e_x = float((x ** 2).sum()) + 1e-20model = {'kind': KIND, 'version': VERSION, 'sr': SR, 'duration': round(len(x) / sr, 5),'onset': round(onsets[0]['t'], 5) if onsets else 0.0,@@ -1031,7 +1067,13 @@ def render(model, seed=0, knobs=None, **kw):lev = lev + 20 * np.log10(max(float(k['residual']), 1e-6))lev = np.where(np.asarray(res['db'], float) <= -149, -150.0, lev)ends = [p['t1'] + p['rel'] for p in new] + [float(T(model['duration']))]- n = int(np.ceil(max(ends) * sr)) + 1 if (d_ > 1.0 or s_ != 1.0) else int(round(model['duration'] * sr))+ ring = vary > 0 if k['ring'] is None else bool(k['ring'])+ n_id = int(round(model['duration'] * sr))+ n = int(np.ceil(max(ends) * sr)) + 1 if (d_ > 1.0 or s_ != 1.0 or ring) else n_id+ if ring and n > n_id and len(times): # the noise fades out (10 ms) past the recording's length+ t_end = max(float(times[-1]), n_id / sr)+ times = np.append(times, t_end + 0.01)+ lev = np.column_stack([lev, np.full(len(lev), -150.0)])cents = Noneif k['vibrato'] or k['drift']:tt = np.arange(n) / sr@@ -1048,7 +1090,8 @@ def render(model, seed=0, knobs=None, **kw):* np.log2(max(f * ratio, 20.0) / f_ref)) / 20), ph]for f, tau, a, ph in tr['atoms']]} for tr in trans]y = y + synth_atoms(trans, n, sr)- noise = synth_noise(bands, times, lev, n, seed, sr) if len(bands) else np.zeros(n)+ hf = res.get('hf')+ noise = synth_noise(bands, times, lev, n, seed, sr, hf=hf) if len(bands) else np.zeros(n)if k['transient'] != 1.0:tt = np.arange(n) / srw = np.zeros(n)@@ -1060,7 +1103,7 @@ def render(model, seed=0, knobs=None, **kw):if k['gain_db']:y = y * 10 ** (float(k['gain_db']) / 20)if k['stereo']:- side = synth_noise(bands, times, lev, n, seed + 1000003, sr) * 10 ** (model.get('width', -120.0) / 20)+ side = synth_noise(bands, times, lev, n, seed + 1000003, sr, hf=hf) * 10 ** (model.get('width', -120.0) / 20)side *= 10 ** (float(k['gain_db']) / 20)return np.column_stack([y + side, y - side])return y@@ -1100,6 +1143,7 @@ def to_code(model, name='sound'):lines.append(f" ({tr['t']}, {_lit(tr['atoms'])}),")lines.append(']')lines.append(f"BANDS = {_lit(res['bands'])}")+ lines.append(f"HF = {_lit(res.get('hf'))} # the noise above 16 kHz: the recording's roll-off [Hz, dB]")lines.append(f"TIMES = {_lit(res['times'])}")lines.append('LEVELS = [')for row in res['db']:@@ -1117,7 +1161,7 @@ def to_code(model, name='sound'):' for p in PARTIALS]'," trans = [{'t': t, 'atoms': a} for t, a in TRANSIENTS]",' return (synth_partials(parts, n, SR) + synth_atoms(trans, n, SR)',- ' + synth_noise(BANDS, TIMES, LEVELS, n, seed, SR))',+ ' + synth_noise(BANDS, TIMES, LEVELS, n, seed, SR, hf=HF))','','',"if __name__ == '__main__':",diff --git a/earshot/forge/fit/spectral.py b/earshot/forge/fit/spectral.pyindex a533613..14d5611 100644--- a/earshot/forge/fit/spectral.py+++ b/earshot/forge/fit/spectral.py@@ -51,8 +51,15 @@ Rendering (`render`): each call draws NEW noise (seeded, deterministic per seed)statistically identical sound; knobs: length (time-stretch of the gesture, texture rates kept), speed (movementspeed: shorter, higher by SPEED_OCT octaves per doubling, faster flutter), sweep (scales the spectral centroid'sexcursion around its mean), shift (octaves), tilt (dB / octave about 1 kHz), roughness (texture depth; 0 = smooth-Gaussian), width (side / pan amount), ridges (tonal ridge gain), gain_db. Output is levelled to the original's-loudest 50 ms RMS (+ gain_db), sample peak capped at -0.5 dBFS.+Gaussian), width (side / pan amount), ridges (tonal ridge gain), gain_db. Level: the gain that puts the EXPECTED+loudest 50 ms RMS (computed from the gesture: band envelopes, their overlaps, the ridges) on the original's+(+ gain_db), times the take's own correction to its expected K-weighted energy (BS.1770 weighting, the loudness a+meter reads). A noise band holds few independent bins over a short gesture, so a take's loudness wanders with the+seed; levelled by its own loudest 50 ms, as before 0.3, takes of one model came out 2-7 LU apart (a cloth whoosh),+now within about 0.5 LU. A scalar per take: the fit-quality numbers, which level both sounds, do not move+(steady=False keeps the old levelling: a hybrid's air layer, whose post stage was measured on it). Sample peak capped+at -0.5 dBFS (limit=False: not capped; the fit engine renders so, as the cap would make a high-crest take quieter+than the others).Judging a fit: a fresh-noise render can never match the samples, and even two renders of one model differ by a fewdB cell by cell (a noise band's level estimate fluctuates). So `fit_report` also gives the floor: compare(render,@@ -334,9 +341,19 @@ def _clicks(N, rate, rng, spread=SPREAD):return c / p if p > 0 else c+def _k_gain2(f):+ """|K-weighting|^2 (ITU-R BS.1770, 48 kHz) at frequencies f (Hz)"""+ from scipy import signal+ from ..polish import K_SOS+ return np.abs(signal.sosfreqz(K_SOS, worN=np.asarray(f, np.float64), fs=SR)[1]) ** 2++def _synth(n, tk, env, cents, seed, stereo=None, texture=None, ridges=(), mono=False, rate_scale=1.0,- rough=1.0, fine=None, fine_shift=0.0):- """the renderer core. tk (K,) key times in s, env (K, B) density dB. Returns (n,) if mono else (n, 2)."""+ rough=1.0, fine=None, fine_shift=0.0, power=False):+ """the renderer core. tk (K,) key times in s, env (K, B) density dB. Returns (n,) if mono else (n, 2).+ power=True returns (y, p, pk): the expected channel-mean power of the render, plain and K-weighted ((n,) each:+ the bands' envelopes with their neighbours' overlap, the ridges; flutter has E[mod^2] = 1), the same for every+ seed (render levels by them)"""n = int(n)ss = np.random.SeedSequence(abs(int(seed)) % (2 ** 32))rA, rB, rC, rM, rR = [np.random.default_rng(s) for s in ss.spawn(5)]@@ -393,6 +410,10 @@ def _synth(n, tk, env, cents, seed, stereo=None, texture=None, ridges=(), mono=FL = np.zeros(n)R = None if mono else np.zeros(n)+ P = np.zeros(n) if power else None+ PK = np.zeros(n) if power else None+ K2 = _k_gain2(f) if power else None+ prev = None # (w, amp) of the band below: neighbours overlap (their cross power)for b in range(len(cents)):w = _wband(ef, ec, b)nz = np.nonzero(w)[0]@@ -404,6 +425,15 @@ def _synth(n, tk, env, cents, seed, stereo=None, texture=None, ridges=(), mono=FnA = sfft.irfft(Xb, N, workers=wk)[:n]amp = 10 ** (np.interp(ts, tk, env[:, b]) / 20)g0, g1, a = blend[b]+ if P is not None: # expected power: sum over bins of (sum_b amp_b w_b)^2 x 2 / N+ ww = w[lo:hi] ** 2+ P += amp * amp * (2.0 * float(np.sum(ww)) / N)+ PK += amp * amp * (2.0 * float(np.sum(ww * K2[lo:hi])) / N)+ if prev is not None:+ wx = prev[0][lo:hi] * w[lo:hi]+ P += 2.0 * (2.0 * float(np.sum(wx)) / N) * prev[1] * amp+ PK += 2.0 * (2.0 * float(np.sum(wx * K2[lo:hi])) / N) * prev[1] * amp+ prev = (w, amp)if mods is not None:amp = amp * ((1 - a) * mods[g0] + a * mods[g1])if mono:@@ -432,6 +462,9 @@ def _synth(n, tk, env, cents, seed, stereo=None, texture=None, ridges=(), mono=F# fade the ridge in / out over 5 ms at its endse = np.clip(np.minimum(ts - rt[0], rt[-1] - ts) / 0.005, 0, 1)y = y * e+ if P is not None:+ P += 0.5 * (A * e) ** 2+ PK += 0.5 * (A * e) ** 2 * float(_k_gain2([max(float(np.median(rd['f'])), 1.0)])[0])if mono or ctl is None:L += yelse:@@ -439,10 +472,31 @@ def _synth(n, tk, env, cents, seed, stereo=None, texture=None, ridges=(), mono=FL += y * ctl[2, g]R += y * ctl[3, g]if mono:- return L+ return (L, P, PK) if power else Lif ctl is None:R = L.copy()- return np.stack([L, R], 1)+ y = np.stack([L, R], 1)+ return (y, P, PK) if power else y+++def _take_gain(y, pk):+ """the gain that puts a take's K-weighted energy (channel mean) on its expected one"""+ from scipy import signal+ from ..polish import K_SOS+ y2 = np.asarray(y, np.float64)+ y2 = y2 if y2.ndim == 2 else y2[:, None]+ act = float(np.mean([np.sum(signal.sosfilt(K_SOS, y2[:, c]) ** 2) for c in range(y2.shape[1])]))+ want = float(np.sum(pk))+ return float(np.sqrt(want / act)) if act > 1e-300 and want > 0 else 1.0+++def _peak_mean_db(p, win_s=0.05):+ """the loudest win_s mean of a power curve (dB)"""+ k = max(1, int(win_s * SR))+ if len(p) < k:+ return float(_db(np.mean(p)))+ c = np.concatenate([[0.0], np.cumsum(p)])+ return float(_db(((c[k:] - c[:-k]) / k).max()))def _peak_rms_db(x, win_s=0.05):@@ -1006,8 +1060,10 @@ def _centroid_oct(E, cents, bw):def render(model, seed=0, length=1.0, speed=1.0, sweep=1.0, shift=0.0, tilt=0.0, roughness=1.0, width=1.0,- ridges=1.0, gain_db=0.0, level=True):- """render a model: (n, 2) float32 at 48 kHz. New `seed` = new noise, same statistics. See the module doc."""+ ridges=1.0, gain_db=0.0, level=True, limit=True, steady=True):+ """render a model: (n, 2) float32 at 48 kHz. New `seed` = new noise, same statistics. See the module doc.+ limit=False skips the -0.5 dBFS sample-peak cap (a per-take gain: the fit engine levels takes itself).+ steady=False levels the take by its own loudest 50 ms (the 0.2 renderer; a hybrid's air layer was fitted so)"""check(model)cents = centers(model['bands']['n'])bw = bandwidths(cents)@@ -1036,13 +1092,14 @@ def render(model, seed=0, length=1.0, speed=1.0, sweep=1.0, shift=0.0, tilt=0.0,o = oct_ + (float(sweep) - 1.0) * (np.interp(rt, np.asarray(model['env']['t']), co) - cm)rds.append({'t': rt * s, 'f': np.minimum(np.asarray(rd['f']) * 2 ** o, SR * 0.45),'db': np.asarray(rd['db']) + 20 * np.log10(float(ridges)), 'jitter': rd.get('jitter', 0.002)})- y = _synth(n, tk, E, cents, seed, stereo=st, texture=model.get('texture'), ridges=rds, rate_scale=speed,- fine=model.get('fine'), fine_shift=oct_,- rough=float(roughness))- if level:+ y, p, pk = _synth(n, tk, E, cents, seed, stereo=st, texture=model.get('texture'), ridges=rds,+ rate_scale=speed, fine=model.get('fine'), fine_shift=oct_, rough=float(roughness), power=True)+ if level and steady: # the expected loudest 50 ms on the original's, this take's K-weighted energy on its own+ y *= 10 ** ((model['level_db'] + float(gain_db) - _peak_mean_db(p)) / 20) * _take_gain(y, pk) # expectation+ elif level:y *= 10 ** ((model['level_db'] + float(gain_db) - _peak_rms_db(y)) / 20)pk = np.abs(y).max()- if pk > 10 ** (-0.5 / 20):+ if limit and pk > 10 ** (-0.5 / 20):y *= 10 ** (-0.5 / 20) / pkreturn y.astype(np.float32)diff --git a/tests/test_fit_modal.py b/tests/test_fit_modal.pyindex a8da7d2..06fd993 100644--- a/tests/test_fit_modal.py+++ b/tests/test_fit_modal.py@@ -127,8 +127,9 @@ def test_knobs(fitted):assert np.mean(modal.render(md, 0, damping=2.0)[late] ** 2) < 0.2 * e0assert centroid(modal.render(md, 0, brightness=6.0)) > 1.3 * centroid(y0)assert centroid(modal.render(md, 0, brightness=-6.0)) < centroid(y0)- yv = modal.render(md, 5, vary=0.6)+ yv = modal.render(md, 5, vary=0.6, ring=False)assert len(yv) == len(y0) and not np.allclose(yv, y0)+ assert len(modal.render(md, 5, vary=0.6)) >= len(y0) # a vary take rings out (ring=None)assert np.allclose(modal.render(md, 0, gain=-6.0), y0 * 10 ** (-6 / 20))s = modal.render(md, 0, stereo=True, room=0.3)assert s.shape == (len(y0), 2) and np.isfinite(s).all()diff --git a/tests/test_fit_takes.py b/tests/test_fit_takes.pynew file mode 100644index 0000000..515eef1--- /dev/null+++ b/tests/test_fit_takes.py@@ -0,0 +1,140 @@+"""Takes of fitted models (earshot 0.3 renderer fixes), on synthetic sounds only:+- a modal take rings out past a trimmed recording's end instead of stopping on a ringing tail (ring / take);+- the fit itself (identity render, seed 0) keeps the recording's length;+- a sines fit's residual noise follows the recording's roll-off above 16 kHz (no noise above 20 kHz it lacked);+- takes carry no DC offset; spectral takes keep one loudness across seeds; candidates can be scored on 3 seeds."""+import numpy as np+import pytest++from earshot.forge import polish+from earshot.forge.fit import auto, modal, sines, spectral++SR = 48000+++def _trimmed_strike(length=0.15, fade=0.03, seed=0):+ """a struck object whose slowest mode (tau 0.25 s) is still ringing where the 'recording' was trimmed, with the+ trim's linear fade-out (what a refset cut does)"""+ n = int(length * SR)+ t = np.arange(n) / SR+ o = int(0.005 * SR)+ tt = np.clip(t - t[o], 0, None)+ x = np.zeros(n)+ for f, tau, a in ((420.0, 0.25, 0.4), (1310.0, 0.04, 0.3), (2875.0, 0.015, 0.2)):+ x += (t >= t[o]) * a * np.exp(-tt / tau) * np.cos(2 * np.pi * f * tt)+ x += 1e-4 * np.random.default_rng(seed).standard_normal(n)+ nf = int(fade * SR)+ x[-nf:] *= np.linspace(1, 0, nf)+ return x++[email protected](scope='module')+def strike_model():+ x = _trimmed_strike()+ return x, modal.fit(x)+++def test_modal_fit_keeps_the_recording_length(strike_model):+ x, m = strike_model+ y = modal.render(m, 0)+ assert len(y) == len(x) # the fit, as measured against the recording+ assert np.array_equal(y, modal.render(m, 0, ring=False)) # ring is off at vary 0 (None -> vary > 0)+ assert np.array_equal(auto.render(m, seed=0), auto.render(m, seed=0, take=False))+++def test_modal_take_rings_out_and_ends_clean(strike_model):+ x, m = strike_model+ y = modal.render(m, 0, ring=True)+ longest = max(modal.RING_LOUD_S, modal.RING_EXT * len(x) / SR)+ assert len(x) + int(modal.RING_MIN_S * SR) - 2 <= len(y) <= len(x) + int(longest * SR) + 2+ e = 10 * np.log10(np.convolve(y ** 2, np.ones(240) / 240, 'valid') + 1e-20)+ assert e[-1] < e.max() - 55 # faded out, not cut on a ringing mode+ assert polish.truncated(y)['trunc_db'] is None+ yv = modal.render(m, 3, vary=0.3) # a vary take rings out by default+ assert len(yv) > len(x) and polish.truncated(yv)['trunc_db'] is None+++def test_modal_ring_in_the_standalone_script(strike_model):+ _, m = strike_model+ ns = {}+ exec(compile(modal.to_code(m, 'strike'), 'strike.py', 'exec'), ns)+ assert np.array_equal(ns['render'](2, ring=True, vary=0.2), modal.render(m, 2, ring=True, vary=0.2))+++def _lossy_tone(dur=0.4, cut_hz=16000.0, seed=3):+ """a decaying tone over noise, band-limited at cut_hz like a lossy-coded recording"""+ n = int(dur * SR)+ t = np.arange(n) / SR+ rng = np.random.default_rng(seed)+ env = np.exp(-t / 0.08)+ x = 0.3 * env * np.sin(2 * np.pi * 880 * t) + 0.05 * env * rng.standard_normal(n)+ X = np.fft.rfft(x)+ X[np.fft.rfftfreq(n, 1 / SR) > cut_hz] = 0+ return np.fft.irfft(X, n)+++def _above_20k_db(y):+ m = y.mean(1) if y.ndim == 2 else y+ P = np.abs(np.fft.rfft(m)) ** 2+ f = np.fft.rfftfreq(len(m), 1 / SR)+ return 10 * np.log10(P[f >= 20000].sum() / P.sum() + 1e-30)+++def test_hf_rolloff_is_monotone_and_finds_a_band_limit():+ hf = sines.hf_rolloff(_lossy_tone())+ lv = [v for _, v in hf]+ assert [f for f, _ in hf] == list(sines.HF_POINTS) and lv[0] == 0.0+ assert all(b <= a for a, b in zip(lv, lv[1:]))+ assert lv[-1] < -40 # nothing above the 16 kHz cut+++def test_sines_residual_follows_the_recordings_rolloff():+ x = _lossy_tone()+ m = sines.fit(x, windows=(1024,))+ assert m['residual']['hf'][0] == [16000.0, 0.0]+ flat = dict(m, residual={k: v for k, v in m['residual'].items() if k != 'hf'}) # a model fitted before 0.3+ y, y_flat = sines.render(m, 0), sines.render(flat, 0)+ assert _above_20k_db(y) < _above_20k_db(y_flat) - 10+ assert _above_20k_db(y) < -60+ ns = {}+ exec(compile(sines.to_code(m, 'tone'), 'tone.py', 'exec'), ns)+ assert np.allclose(ns['render'](0), y, atol=1e-9)+++def test_takes_have_no_dc_offset():+ x = _lossy_tone()+ m = sines.fit(x, windows=(1024,))+ res = m['residual']+ res['db'][0] = [v + 30.0 if v > -149 else v for v in res['db'][0]] # a loud lowest band (flat to DC)+ dc = lambda y: polish.dc(y)['dc_db']+ plain = [dc(auto.render(m, seed=s, vary=0.3, take=False)) for s in (1, 2, 3)]+ takes = [dc(auto.render(m, seed=s, vary=0.3)) for s in (1, 2, 3)] # take=None -> True at vary > 0+ assert max(takes) < -60 and max(takes) < max(plain) - 10+++def _gesture(n, rng):+ """a low-heavy noise swell (a cloth / air pass): 40 Hz - 3 kHz, a 0.1 s rise and a decay"""+ X = np.fft.rfft(rng.standard_normal(n))+ f = np.fft.rfftfreq(n, 1 / SR)+ X *= np.where((f > 40) & (f < 3000), (np.maximum(f, 40) / 100.0) ** -0.7, 0.0)+ t = np.arange(n) / SR+ env = np.clip(t / 0.1, 0, 1) ** 2 * np.exp(-np.maximum(t - 0.1, 0) / 0.08)+ return np.fft.irfft(X, n) * env+++def test_spectral_takes_keep_one_loudness():+ x = _gesture(int(0.4 * SR), np.random.default_rng(5))+ x = 0.5 * x / np.abs(x).max()+ m = spectral.fit(x, texture=False, iters=1, seeds=(1,))+ lu = [polish.lufs_max(auto.render(m, seed=s)) for s in range(6)]+ assert max(lu) - min(lu) < 1.0+ # the expected level stays the original's (within the noise of one take)+ assert abs(np.mean([spectral._peak_rms_db(spectral.render(m, s, limit=False)) for s in range(6)])+ - m['level_db']) < 2.0+++def test_fit_scores_candidates_on_three_seeds():+ x = _trimmed_strike(0.12)+ m = auto.fit(x, candidates=['modal', 'spectral'], fast=True, seeds=auto.TAKE_SEEDS)+ assert m['fit_engine']['seeds'] == [0, 1, 2]+ assert auto.fit(x, candidates=['modal'], fast=True)['fit_engine']['seeds'] == [0]