Files
lintunes/tests/test_round136.py
T

88 lines
2.7 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 136: Kaleidostrobe's beat bins hear what people hear.
The plugin's cone depth comes from spectrum bins 0–3. They are now four
octave bands, 100–1600 Hz, with a fast attack and a slow release, instead
of the 0–170 Hz a literal 1024-point FFT gives — which pumped the cones on
every sub-bass note. These pin the shape: which frequencies reach those
bins, and how the envelope moves.
"""
import math
import numpy as np
import pytest
from lintunes.gui import visualizer_kaleido as vk
RATE = 44100
def _tone(hz, seconds=0.2, amp=0.5, rate=RATE):
t = np.arange(int(seconds * rate)) / rate
return (amp * np.sin(2 * np.pi * hz * t)).astype(np.float32)
@pytest.mark.parametrize("hz, band", [(150, 0), (300, 1), (600, 2), (1200, 3)])
def test_each_octave_lands_in_its_own_beat_bin(hz, band):
bins = vk.itunes_spectrum(_tone(hz), RATE)[0, :4]
assert int(bins.argmax()) == band
assert bins[band] > 100
def test_sub_bass_barely_reaches_the_beat_bins():
sub = vk.itunes_spectrum(_tone(45), RATE)[0, :4]
body = vk.itunes_spectrum(_tone(300), RATE)[0, :4]
assert sub.max() < body.max() / 3
@pytest.mark.parametrize("rate", [44100, 48000, 96000])
def test_the_bands_follow_hertz_at_any_rate(rate):
bins = vk.itunes_spectrum(_tone(600, rate=rate), rate)[0, :4]
assert int(bins.argmax()) == 2
def test_the_rest_of_the_spectrum_is_unchanged():
tone = _tone(1000)
n = vk.FFT_AT_44K
mag = np.abs(np.fft.rfft(tone[-n:] * np.hanning(n)))[:512] / (n / 4)
expect = np.clip(np.sqrt(mag) * vk.SPECTRUM_GAIN, 0, 255).astype(np.uint8)
assert (vk.itunes_spectrum(tone, RATE)[0, 4:] == expect[4:]).all()
def _spec(level):
s = np.zeros((2, 512), np.uint8)
s[:, :4] = level
s[:, 4:] = 77
return s
def test_a_hit_lands_at_once():
r = vk.KaleidostrobeRenderer()
r._enveloped(_spec(10), 0.016)
assert (r._enveloped(_spec(220), 0.016)[:, :4] == 220).all()
def test_it_falls_back_with_the_release():
r = vk.KaleidostrobeRenderer()
r._enveloped(_spec(200), 0.016)
out = r._enveloped(_spec(0), vk.BEAT_RELEASE_S)
assert abs(int(out[0, 0]) - round(200 / math.e)) <= 1
assert (out[:, 4:] == 77).all() # only the beat bins are held
def test_the_release_follows_time_not_calls():
a, b = vk.KaleidostrobeRenderer(), vk.KaleidostrobeRenderer()
a._enveloped(_spec(200), 0.0)
b._enveloped(_spec(200), 0.0)
for _ in range(6):
a._enveloped(_spec(0), 1 / 60)
for _ in range(12):
b._enveloped(_spec(0), 1 / 120)
assert abs(int(a._beat[0, 0]) - int(b._beat[0, 0])) <= 1
def test_pause_forgets_the_envelope():
r = vk.KaleidostrobeRenderer()
r._enveloped(_spec(250), 0.016)
r.clear()
assert (r._enveloped(_spec(10), 0.016)[:, :4] == 10).all()