v0.67.1: the air horn is one continuous tone
The old recording was a two-note phrase that wobbled 9% in pitch, so no loop of it could hold a tone. A steady CC0 can (guitarguy1985, 359-361 Hz for 7 s) replaces it: held, it plays on and laps a long crossfade; a click is at least half a second; letting go bends the pitch down as it fades.
This commit is contained in:
+1
-35
@@ -44,41 +44,7 @@ def _envelope(mono, seconds=0.015):
|
||||
return np.sqrt(np.convolve(mono * mono, np.ones(win) / win, mode="same"))
|
||||
|
||||
|
||||
class TestHorn:
|
||||
def test_a_click_is_a_short_blast_that_trails_off(self, recording):
|
||||
x, _ = _play(recording, [("press", 0.01), ("release", 0.0)])
|
||||
assert 0.3 < len(x) / RATE < 0.45
|
||||
env = _envelope(x)
|
||||
assert env[-int(0.02 * RATE):].max() < 0.05 * env.max() # fades to nothing
|
||||
# …gradually: the last 120 ms fall, they don't drop off a cliff
|
||||
tail = env[-int(0.12 * RATE):]
|
||||
assert tail[0] > 4 * tail[-1]
|
||||
|
||||
def test_a_hold_sustains_without_clicks_or_a_beat(self, recording):
|
||||
x, _ = _play(recording, [("press", 3.0), ("release", 0.0)])
|
||||
source = recording.astype(np.float32).mean(axis=1) / 32768
|
||||
own_step = np.abs(np.diff(source[:int(0.6 * RATE)])).max()
|
||||
assert np.abs(np.diff(x)).max() <= own_step + 1e-3 # no seam clicks
|
||||
held = _envelope(x)[int(0.6 * RATE):int(2.6 * RATE)]
|
||||
assert held.min() / held.max() > 0.7 # steady, no pulse
|
||||
assert len(x) / RATE < 3.0 + 0.45 # stops on release
|
||||
|
||||
def test_taps_stay_separate_then_a_hold_carries(self, recording):
|
||||
x, horn = _play(recording, [("press", 0.06), ("release", 0.12)] * 6
|
||||
+ [("press", 2.5), ("release", 0.0)])
|
||||
held = _envelope(x)[int(1.6 * RATE):int(3.2 * RATE)]
|
||||
assert held.min() / held.max() > 0.7
|
||||
assert not horn._voices
|
||||
|
||||
def test_a_retrigger_cuts_the_old_blast_quickly(self, recording):
|
||||
horn = Horn(recording)
|
||||
horn.press()
|
||||
horn.render(4410)
|
||||
horn.press()
|
||||
states = [v["state"] for v in horn._voices]
|
||||
assert states == ["cut", "held"]
|
||||
horn.render(int(0.03 * RATE))
|
||||
assert [v["state"] for v in horn._voices] == ["held"]
|
||||
# The horn's tests moved to test_round98 with its new recording.
|
||||
|
||||
|
||||
class TestLoops:
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Round 98: the air horn is one continuous tone. A steady recording
|
||||
(guitarguy1985, CC0) replaces the two-note "Hype" one; held, it plays on
|
||||
and laps a long crossfade; a click is a real blast; letting go bends the
|
||||
pitch down as it fades ("bweeeew"); taps are separate blasts."""
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from lintunes.dj.audio import RATE, Horn
|
||||
from lintunes.dj.loader import decode
|
||||
from tests.test_round97 import _envelope, _play
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def recording():
|
||||
from importlib import resources
|
||||
with resources.as_file(resources.files("lintunes") / "sounds" / "airhorn.mp3") as path:
|
||||
return decode(str(path))
|
||||
|
||||
|
||||
def _pitches(x, start, stop, window=4096):
|
||||
"""The strongest frequency (250–450 Hz) in each window."""
|
||||
found = []
|
||||
for i in range(int(start * RATE), int(stop * RATE) - window, window // 2):
|
||||
spectrum = np.abs(np.fft.rfft(x[i:i + window] * np.hanning(window), window * 8))
|
||||
freqs = np.fft.rfftfreq(window * 8, 1 / RATE)
|
||||
band = (freqs > 250) & (freqs < 450)
|
||||
found.append(freqs[band][spectrum[band].argmax()])
|
||||
return np.array(found)
|
||||
|
||||
|
||||
class TestHorn:
|
||||
def test_a_held_horn_is_one_tone_past_the_recording(self, recording):
|
||||
x, horn = _play(recording, [("press", 12.0), ("release", 0.0)])
|
||||
pitch = _pitches(x, 0.4, 11.9)
|
||||
assert pitch.max() / pitch.min() < 1.02 # one note
|
||||
held = _envelope(x, 0.05)[int(0.4 * RATE):int(11.9 * RATE)]
|
||||
assert held.min() / held.max() > 0.5 # never drops out
|
||||
source = recording.astype(np.float32).mean(axis=1) / 32768
|
||||
own_step = np.abs(np.diff(source)).max() / np.abs(source).max()
|
||||
assert np.abs(np.diff(x)).max() <= own_step * horn.LEVEL + 1e-3 # no seam clicks
|
||||
assert not horn._voices
|
||||
|
||||
def test_a_click_is_a_real_blast_that_bends_down(self, recording):
|
||||
x, _ = _play(recording, [("press", 0.05), ("release", 0.0)])
|
||||
assert 0.7 < len(x) / RATE < 0.9
|
||||
pitch = _pitches(x, 0.1, 0.45)
|
||||
assert pitch.max() / pitch.min() < 1.03 # no "bee-doop"
|
||||
assert _pitches(x, 0.6, 0.75).mean() < pitch.mean() * 0.98 # the "-ew"
|
||||
env = _envelope(x)
|
||||
assert env[-int(0.02 * RATE):].max() < 0.05 * env.max()
|
||||
|
||||
def test_taps_then_a_hold_carry_one_tone(self, recording):
|
||||
x, horn = _play(recording, [("press", 0.06), ("release", 0.12)] * 6
|
||||
+ [("press", 4.0), ("release", 0.0)])
|
||||
start = 6 * 0.18 + 0.4
|
||||
pitch = _pitches(x, start, start + 3.4)
|
||||
assert pitch.max() / pitch.min() < 1.02
|
||||
held = _envelope(x, 0.05)[int(start * RATE):int((start + 3.4) * RATE)]
|
||||
assert held.min() / held.max() > 0.5
|
||||
assert not horn._voices
|
||||
|
||||
def test_a_retrigger_cuts_the_old_blast_quickly(self, recording):
|
||||
horn = Horn(recording)
|
||||
horn.press()
|
||||
horn.render(4410)
|
||||
horn.release()
|
||||
horn.render(RATE // 2) # now fading out
|
||||
horn.press()
|
||||
assert [v["state"] for v in horn._voices] == ["cut", "held"]
|
||||
horn.render(int(0.03 * RATE))
|
||||
assert [v["state"] for v in horn._voices] == ["held"]
|
||||
Reference in New Issue
Block a user