Files
lintunes/tests/test_round98.py
T
trav 8e39335988 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.
2026-10-17 22:57:26 -07:00

73 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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"]