Skip to content

Ears for coding agents: let AI hear, review and make sound. CPU-only, open source.

fit renderer: a sines take fades its residual noise out past the recording

Leonxlnx20b49bac59d3
diff --git a/earshot/forge/fit/sines.py b/earshot/forge/fit/sines.py
index f2d601f..d63c5dd 100644
--- a/earshot/forge/fit/sines.py
+++ b/earshot/forge/fit/sines.py
@@ -56,7 +56,8 @@ render(model, seed, knobs) knobs (all default to the identity, which re-renders
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),
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).
+ lengthens is not cut at the recording's length, and fades the residual noise out over RING_FADE_S past the
+ recording's length instead of stopping on it).
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.
@@ -77,6 +78,7 @@ DEFAULT_KNOBS = {'transpose': 0.0, 'stretch': 1.0, 'decay': 1.0, 'brightness': 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, '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
+RING_FADE_S = 0.06 # ring: the residual noise fades out over this past the recording's length
# ================================================================================================ small helpers
@@ -1070,9 +1072,10 @@ def render(model, seed=0, knobs=None, **kw):
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)
+ if ring and len(times): # the noise fades out over RING_FADE_S past the recording's length (linear in dB)
+ t_end = max(float(times[-1]), float(T(model['duration'])))
+ n = max(n, int(np.ceil((t_end + RING_FADE_S) * sr)) + 1)
+ times = np.append(times, t_end + RING_FADE_S)
lev = np.column_stack([lev, np.full(len(lev), -150.0)])
cents = None
if k['vibrato'] or k['drift']:
diff --git a/tests/test_fit_takes.py b/tests/test_fit_takes.py
index 515eef1..c9a2193 100644
--- a/tests/test_fit_takes.py
+++ b/tests/test_fit_takes.py
@@ -3,6 +3,8 @@
- 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 json
+
import numpy as np
import pytest
@@ -88,9 +90,13 @@ def test_hf_rolloff_is_monotone_and_finds_a_band_limit():
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,))
[email protected](scope='module')
+def tone_model():
+ return sines.fit(_lossy_tone(), windows=(1024,))
+
+
+def test_sines_residual_follows_the_recordings_rolloff(tone_model):
+ m = tone_model
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)
@@ -101,9 +107,22 @@ def test_sines_residual_follows_the_recordings_rolloff():
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,))
+def test_sines_take_fades_its_noise_out(tone_model):
+ """a trimmed tone whose noise is still sounding at the cut: the take fades it over RING_FADE_S, the fit keeps
+ the recording's length"""
+ m = json.loads(json.dumps(tone_model))
+ n_id = int(round(m['duration'] * SR))
+ res = m['residual']
+ res['db'] = [row[:-1] + [max(row)] if max(row) > -149 else row for row in res['db']] # loud to the last time
+ assert len(sines.render(m, 0)) == n_id
+ y = sines.render(m, 3, knobs={'vary': 0.3}) # ring=None -> on
+ assert len(y) >= n_id + int(sines.RING_FADE_S * SR)
+ e = 10 * np.log10(np.convolve(y ** 2, np.ones(240) / 240, 'valid') + 1e-20)
+ assert e[-1] < e.max() - 55
+
+
+def test_takes_have_no_dc_offset(tone_model):
+ m = json.loads(json.dumps(tone_model))
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']