v0.67.2: the air horn is the classic rap horn
trav's recording from instantrapairhorn.com replaces the steady can. A click plays the whole blast untouched; held, it laps the steady stretch through a crossfade matched to the recording's own loudness; let go after a lap, it jumps straight to the recording's own ending.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""Round 99: the classic rap air horn (trav's recording, from
|
||||
instantrapairhorn.com). A click plays the whole blast as that page does;
|
||||
held, it laps its steady stretch at the recording's own loudness; let go
|
||||
after a lap, it goes straight to the recording's own ending; taps are
|
||||
separate blasts. (Round 98's steady-can recording and its tests are gone.)"""
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from lintunes.dj.audio import RATE, Horn
|
||||
from lintunes.dj.loader import decode
|
||||
from tests.test_round97 import _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 _loudness(x, start, stop, window=0.05):
|
||||
w = int(window * RATE)
|
||||
return np.array([np.sqrt((x[i:i + w] ** 2).mean())
|
||||
for i in range(int(start * RATE), int(stop * RATE) - w, w)])
|
||||
|
||||
|
||||
def _pitches(x, start, stop, window=4096):
|
||||
"""The fundamental (250–350 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 < 350)
|
||||
found.append(freqs[band][spectrum[band].argmax()])
|
||||
return np.array(found)
|
||||
|
||||
|
||||
def _own(horn):
|
||||
"""The recording's steady stretch: its own loudness range."""
|
||||
a, b = (t / RATE for t in horn._loop)
|
||||
return _loudness(horn._src.mean(axis=1), a, b)
|
||||
|
||||
|
||||
class TestHorn:
|
||||
def test_a_click_plays_the_whole_blast_untouched(self, recording):
|
||||
x, horn = _play(recording, [("press", 0.01), ("release", 0.0)])
|
||||
whole = horn._src[horn._onset:].mean(axis=1)
|
||||
assert len(x) >= len(whole)
|
||||
np.testing.assert_allclose(x[:len(whole)], whole, atol=1e-6)
|
||||
assert not horn._voices
|
||||
|
||||
def test_a_hold_is_one_steady_blast_then_its_own_ending(self, recording):
|
||||
x, horn = _play(recording, [("press", 6.0), ("release", 0.0)])
|
||||
own = _own(horn)
|
||||
held = _loudness(x, 0.7, 5.9)
|
||||
assert held.min() > 0.9 * own.min() and held.max() < 1.1 * own.max()
|
||||
pitch = _pitches(x, 0.7, 5.9)
|
||||
assert pitch.max() / pitch.min() < 1.03 # one note
|
||||
assert len(x) / RATE < 6.0 + 0.4 # ends at once
|
||||
source = horn._src.mean(axis=1)
|
||||
assert np.abs(np.diff(x)).max() <= np.abs(np.diff(source)).max() + 0.02
|
||||
|
||||
def test_letting_go_before_a_lap_plays_out_naturally(self, recording):
|
||||
x, horn = _play(recording, [("press", 0.8), ("release", 0.0)])
|
||||
whole = horn._src[horn._onset:].mean(axis=1)
|
||||
np.testing.assert_allclose(x[:len(whole)], whole, atol=1e-6)
|
||||
|
||||
def test_taps_then_a_hold_carry_one_blast(self, recording):
|
||||
x, horn = _play(recording, [("press", 0.06), ("release", 0.12)] * 6
|
||||
+ [("press", 4.0), ("release", 0.0)])
|
||||
start = 6 * 0.18 + 0.6
|
||||
held = _loudness(x, start, start + 3.3)
|
||||
own = _own(horn)
|
||||
assert held.min() > 0.9 * own.min() and held.max() < 1.1 * own.max()
|
||||
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.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