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.
433 lines
18 KiB
Python
433 lines
18 KiB
Python
"""The decks and the mix, as pure numpy: render N frames, get N frames.
|
||
|
||
A deck holds its song in memory as int16 stereo at 44.1 kHz (about 50 MB for
|
||
five minutes) — plus, once Demucs has split it, the vocals and the
|
||
instrumental, which the vocals slider blends. It plays three ways:
|
||
|
||
- **keep pitch** (the default): WSOLA time-stretch, so a sped-up song stays
|
||
in its key. 1024-frame windows, half overlapping; each next window is the
|
||
one within ±256 frames of where the tempo says to read that best continues
|
||
the last (normalised cross-correlation), so the seams don't click.
|
||
- **pitch follows tempo** (Keep pitch off): plain varispeed, read at the
|
||
speed with linear interpolation, like a turntable's pitch fader.
|
||
- **scratching**: varispeed at whatever rate the hand on the record gives,
|
||
backwards included; letting go goes straight back to normal speed.
|
||
|
||
The mixer adds both decks after the crossfader's constant-power curve and the
|
||
master volume, and clips. Everything here runs on the sink's thread; the GUI
|
||
only sets plain attributes (one assignment each, so never torn).
|
||
"""
|
||
|
||
from collections import deque
|
||
|
||
import numpy as np
|
||
|
||
RATE = 44100
|
||
CHANNELS = 2
|
||
WINDOW = 1024
|
||
HOP = WINDOW // 2
|
||
SEARCH = 256
|
||
SPEED_RANGE = 0.08 # the speed faders' ±8 %
|
||
|
||
_HANN = (0.5 - 0.5 * np.cos(2 * np.pi * np.arange(WINDOW) / WINDOW)).astype(np.float32)
|
||
|
||
|
||
class Source:
|
||
"""A song in memory: the original, and the stems once they exist."""
|
||
|
||
def __init__(self, original: np.ndarray, rate: int = RATE):
|
||
self.original = original # (frames, 2) int16
|
||
self.rate = rate
|
||
self.vocals: np.ndarray | None = None
|
||
self.instrumental: np.ndarray | None = None
|
||
|
||
@property
|
||
def frames(self) -> int:
|
||
return len(self.original)
|
||
|
||
@property
|
||
def seconds(self) -> float:
|
||
return self.frames / self.rate
|
||
|
||
def has_stems(self) -> bool:
|
||
return self.vocals is not None and self.instrumental is not None
|
||
|
||
def read(self, start: int, count: int, vocal_mix: float) -> np.ndarray:
|
||
"""``count`` frames from ``start`` as float32, silence past either
|
||
end. ``vocal_mix`` −1 vocals only, 0 both, +1 instrumental only."""
|
||
out = np.zeros((count, CHANNELS), np.float32)
|
||
lo, hi = max(0, start), min(self.frames, start + count)
|
||
if hi <= lo:
|
||
return out
|
||
dest = slice(lo - start, hi - start)
|
||
if self.has_stems() and abs(vocal_mix) > 1e-3:
|
||
v_gain = min(1.0, 1.0 - vocal_mix)
|
||
i_gain = min(1.0, 1.0 + vocal_mix)
|
||
n = min(hi, len(self.vocals), len(self.instrumental))
|
||
if n > lo:
|
||
d = slice(lo - start, n - start)
|
||
out[d] = (self.vocals[lo:n].astype(np.float32) * v_gain
|
||
+ self.instrumental[lo:n].astype(np.float32) * i_gain)
|
||
else:
|
||
out[dest] = self.original[lo:hi]
|
||
out *= 1.0 / 32768.0
|
||
return out
|
||
|
||
|
||
class Deck:
|
||
"""One turntable. Attributes the GUI sets: ``playing``, ``speed``,
|
||
``keep_pitch``, ``vocal_mix``, ``scratch_rate`` (None when no hand is on
|
||
the record). ``position`` is in source frames (float)."""
|
||
|
||
def __init__(self, name: str):
|
||
self.name = name
|
||
self.source: Source | None = None
|
||
self.position = 0.0
|
||
self.playing = False
|
||
self.speed = 1.0
|
||
self.keep_pitch = True
|
||
self.vocal_mix = 0.0
|
||
self.scratch_rate: float | None = None
|
||
self.volume = 1.0 # the deck's own fader, before the crossfader
|
||
self.loop: tuple[int, int] | None = None # (start, end) source frames
|
||
self.ended = False # reached the end by playing (GUI clears)
|
||
self._ola: np.ndarray | None = None # overlap-add tail
|
||
self._last: int | None = None # where the last window was read
|
||
self._pending: np.ndarray | None = None # stretched, not yet emitted
|
||
self._mode = None
|
||
|
||
def load(self, source: Source | None):
|
||
self.playing = False
|
||
self.loop = None
|
||
self.source = source
|
||
self.position = 0.0
|
||
self.ended = False
|
||
self.scratch_rate = None
|
||
self._reset()
|
||
|
||
def seek(self, frame: float):
|
||
if self.source is None:
|
||
return
|
||
self.position = float(max(0.0, min(frame, self.source.frames - 1)))
|
||
self._reset()
|
||
|
||
def _reset(self):
|
||
self._ola = None
|
||
self._last = None
|
||
self._pending = None
|
||
|
||
# ---- rendering ----
|
||
|
||
def render(self, count: int) -> np.ndarray:
|
||
"""``count`` frames out. With a loop set, playback that crosses its
|
||
end jumps back to its start — rendered in pieces that stop at the
|
||
end, so even a 1/32-beat loop lands where it should."""
|
||
loop = self.loop
|
||
if loop is None or self.scratch_rate is not None or not self.playing:
|
||
return self._render(count)
|
||
start, end = loop
|
||
pieces = []
|
||
left = count
|
||
while left > 0:
|
||
speed = max(self.speed, 1e-3)
|
||
n = left
|
||
if self.position < end:
|
||
to_end = int(np.ceil((end - self.position) / speed))
|
||
n = max(1, min(left, to_end))
|
||
before = self.position
|
||
pieces.append(self._render(n))
|
||
left -= n
|
||
if before < end <= self.position:
|
||
self.position -= (end - start)
|
||
if not self.playing:
|
||
break
|
||
out = np.concatenate(pieces) if pieces else np.zeros((0, CHANNELS), np.float32)
|
||
if len(out) < count:
|
||
out = np.concatenate([out, np.zeros((count - len(out), CHANNELS), np.float32)])
|
||
return out
|
||
|
||
def _render(self, count: int) -> np.ndarray:
|
||
src = self.source
|
||
if src is None:
|
||
return np.zeros((count, CHANNELS), np.float32)
|
||
if self.scratch_rate is not None:
|
||
return self._varispeed(count, self.scratch_rate, "scratch")
|
||
if not self.playing:
|
||
self._mode = None
|
||
return np.zeros((count, CHANNELS), np.float32)
|
||
if self.position >= src.frames:
|
||
self.playing = False
|
||
self.ended = True
|
||
return np.zeros((count, CHANNELS), np.float32)
|
||
if self.keep_pitch and abs(self.speed - 1.0) > 1e-4:
|
||
return self._stretch(count)
|
||
if abs(self.speed - 1.0) <= 1e-4:
|
||
return self._straight(count)
|
||
return self._varispeed(count, self.speed, "vari")
|
||
|
||
def _straight(self, count: int) -> np.ndarray:
|
||
if self._mode != "straight":
|
||
self._mode = "straight"
|
||
self._reset()
|
||
start = int(round(self.position))
|
||
out = self.source.read(start, count, self.vocal_mix)
|
||
self.position = start + count
|
||
return out
|
||
|
||
def _varispeed(self, count: int, rate: float, mode: str) -> np.ndarray:
|
||
if self._mode != mode:
|
||
self._mode = mode
|
||
self._reset()
|
||
src = self.source
|
||
steps = self.position + rate * np.arange(count, dtype=np.float64)
|
||
lo = int(np.floor(steps.min()))
|
||
hi = int(np.ceil(steps.max())) + 2
|
||
block = src.read(lo, hi - lo, self.vocal_mix)
|
||
idx = steps - lo
|
||
i0 = np.clip(np.floor(idx).astype(np.int64), 0, len(block) - 2)
|
||
frac = (idx - i0)[:, None].astype(np.float32)
|
||
out = block[i0] * (1 - frac) + block[i0 + 1] * frac
|
||
self.position = float(self.position + rate * count)
|
||
if self.position < 0:
|
||
self.position = 0.0
|
||
if mode == "vari" and self.position >= src.frames:
|
||
self.playing = False
|
||
self.ended = True
|
||
if mode == "scratch":
|
||
self.position = min(self.position, src.frames - 1.0)
|
||
return out
|
||
|
||
def _stretch(self, count: int) -> np.ndarray:
|
||
"""WSOLA: emit HOP frames per window until ``count`` are out."""
|
||
if self._mode != "stretch":
|
||
self._mode = "stretch"
|
||
self._reset()
|
||
src = self.source
|
||
pieces = []
|
||
made = 0
|
||
if self._pending is not None and len(self._pending):
|
||
pieces.append(self._pending)
|
||
made += len(self._pending)
|
||
while made < count:
|
||
nominal = int(round(self.position))
|
||
if self._last is None:
|
||
start = nominal
|
||
else:
|
||
natural = src.read(self._last + HOP, WINDOW, self.vocal_mix).mean(axis=1)
|
||
region = src.read(nominal - SEARCH, WINDOW + 2 * SEARCH,
|
||
self.vocal_mix).mean(axis=1)
|
||
start = nominal - SEARCH + _best_offset(region, natural)
|
||
frame = src.read(start, WINDOW, self.vocal_mix) * _HANN[:, None]
|
||
if self._ola is None:
|
||
self._ola = np.zeros((WINDOW, CHANNELS), np.float32)
|
||
self._ola += frame
|
||
pieces.append(self._ola[:HOP].copy())
|
||
made += HOP
|
||
self._ola = np.concatenate(
|
||
[self._ola[HOP:], np.zeros((HOP, CHANNELS), np.float32)])
|
||
self._last = start
|
||
self.position += HOP * self.speed
|
||
if self.position >= src.frames:
|
||
self.playing = False
|
||
self.ended = True
|
||
break
|
||
out = np.concatenate(pieces) if pieces else np.zeros((0, CHANNELS), np.float32)
|
||
if len(out) < count:
|
||
out = np.concatenate([out, np.zeros((count - len(out), CHANNELS), np.float32)])
|
||
self._pending = None
|
||
else:
|
||
self._pending = out[count:]
|
||
out = out[:count]
|
||
return out
|
||
|
||
|
||
def _best_offset(region: np.ndarray, natural: np.ndarray) -> int:
|
||
"""The offset into ``region`` where ``natural`` fits best (normalised
|
||
cross-correlation over the window's first half)."""
|
||
probe = natural[:HOP]
|
||
if not np.any(probe):
|
||
return SEARCH
|
||
corr = np.correlate(region[:len(region) - WINDOW + HOP], probe, mode="valid")
|
||
energy = np.convolve(region[:len(region) - WINDOW + HOP] ** 2,
|
||
np.ones(len(probe)), mode="valid")
|
||
score = corr / np.sqrt(np.maximum(energy, 1e-9))
|
||
return int(np.argmax(score))
|
||
|
||
|
||
def crossfade_gains(x: float) -> tuple[float, float]:
|
||
"""Constant power: 0 = all A, 1 = all B, the middle ~71 % each."""
|
||
x = min(1.0, max(0.0, x))
|
||
return float(np.cos(x * np.pi / 2)), float(np.sin(x * np.pi / 2))
|
||
|
||
|
||
class Horn:
|
||
"""The air horn: one continuous tone for as long as it's held.
|
||
|
||
The recording ("airhorn.wav" by guitarguy1985, CC0 —
|
||
lintunes/sounds/CREDITS.md) is a real canned horn: a quarter-second
|
||
swell gliding down into the note, then one steady tone (359–361 Hz,
|
||
within 0.5 %) for seven seconds. Measured, because the one before it
|
||
("Air Horn Hype", Rounds 93–97) was a two-note phrase that wobbled 9 %
|
||
— no amount of looping could make it hold a tone.
|
||
|
||
- **press**: a fresh blast from its attack; one still sounding is cut
|
||
in ``CUT`` (rapid taps stay separate blasts).
|
||
- **held**: the recording plays on; past ``LOOP``'s end it laps back to
|
||
its start, a long equal-power crossfade apart — inaudible on a tone
|
||
this steady.
|
||
- **release**: after at least ``MIN_BLAST`` (a click is a real blast,
|
||
not a blip), the tone falls ``BEND`` in pitch while fading over
|
||
``RELEASE`` — a can running out of air: the "-ew" of "bweeeew".
|
||
"""
|
||
|
||
ONSET = 0.04 # s: where the swell starts in the file
|
||
LOOP = (1.0, 7.3) # s: the steady stretch it laps within
|
||
XFADE = 0.30 # s: the lap's crossfade
|
||
MIN_BLAST = 0.50 # s: what a click always gets
|
||
RELEASE = 0.30 # s: the fade on letting go…
|
||
BEND = 0.94 # …while the pitch falls to this (~1 semitone)
|
||
CUT = 0.025
|
||
VOICES = 4
|
||
LEVEL = 0.9
|
||
|
||
def __init__(self, sample: np.ndarray | None = None):
|
||
self.on = False
|
||
self._src = None
|
||
self._voices: list[dict] = []
|
||
if sample is not None:
|
||
self.set_sample(sample)
|
||
|
||
def set_sample(self, sample: np.ndarray):
|
||
"""``sample``: (frames, 2) int16 or float."""
|
||
src = sample.astype(np.float32)
|
||
if sample.dtype == np.int16:
|
||
src /= 32768.0
|
||
peak = float(np.abs(src).max()) or 1.0
|
||
n = len(src)
|
||
a, b = (min(n - 1, int(t * RATE)) for t in self.LOOP)
|
||
if b - a < RATE // 2: # some other, shorter sound: lap its middle
|
||
a, b = n // 4, n - n // 20
|
||
xf = min(int(self.XFADE * RATE), max(1, (b - a) // 2))
|
||
# The lap's seam baked in: the stretch's last xf frames fade from
|
||
# themselves into what comes before its start, so b → a lands on a
|
||
# continuation (equal power — the tone is steady, so they agree).
|
||
looped = src.copy()
|
||
if a >= xf:
|
||
t = np.linspace(0, 1, xf, dtype=np.float32)[:, None]
|
||
looped[b - xf:b] = (src[b - xf:b] * np.cos(t * np.pi / 2)
|
||
+ src[a - xf:a] * np.sin(t * np.pi / 2))
|
||
self._src = looped / peak
|
||
self._loop = (a, b)
|
||
|
||
# ---- the button ----
|
||
|
||
def press(self):
|
||
self.on = True
|
||
for voice in self._voices:
|
||
if voice["state"] == "ending": # cut from where its fade is
|
||
k = voice["fade"] / max(1, int(self.RELEASE * RATE))
|
||
voice["gain"] = float(np.cos(k * np.pi / 2) ** 2)
|
||
if voice["state"] != "cut":
|
||
voice["state"] = "cut"
|
||
voice["fade"] = 0
|
||
self._voices.append({"state": "held", "pos": float(int(self.ONSET * RATE)),
|
||
"started": 0, "gain": 1.0, "rate": 1.0, "fade": 0})
|
||
del self._voices[:-self.VOICES]
|
||
|
||
def release(self):
|
||
self.on = False
|
||
for voice in self._voices:
|
||
if voice["state"] == "held":
|
||
voice["state"] = "releasing"
|
||
|
||
# ---- sound ----
|
||
|
||
def _read(self, pos: float, rates: np.ndarray) -> tuple[np.ndarray, float]:
|
||
"""Frames from ``pos`` at the given per-frame rates (1 = as
|
||
recorded), lapping within the loop; linear interpolation."""
|
||
a, b = self._loop
|
||
steps = pos + np.concatenate([[0.0], np.cumsum(rates[:-1])])
|
||
if b > a:
|
||
over = steps >= b
|
||
steps[over] = a + (steps[over] - a) % (b - a)
|
||
i0 = np.minimum(steps.astype(np.int64), len(self._src) - 2)
|
||
frac = (steps - i0)[:, None].astype(np.float32)
|
||
frames = self._src[i0] * (1 - frac) + self._src[i0 + 1] * frac
|
||
end = steps[-1] + rates[-1]
|
||
if b > a and end >= b:
|
||
end = a + (end - a) % (b - a)
|
||
return frames, float(end)
|
||
|
||
def render(self, count: int) -> np.ndarray | None:
|
||
if self._src is None or not self._voices:
|
||
return None
|
||
out = np.zeros((count, CHANNELS), np.float32)
|
||
min_blast = int(self.MIN_BLAST * RATE)
|
||
release = int(self.RELEASE * RATE)
|
||
cut = max(1, int(self.CUT * RATE))
|
||
kept = []
|
||
for voice in self._voices:
|
||
done = 0
|
||
alive = True
|
||
while done < count and alive:
|
||
room = count - done
|
||
state = voice["state"]
|
||
if state == "releasing" and voice["started"] >= min_blast:
|
||
state = voice["state"] = "ending"
|
||
if state in ("held", "releasing"):
|
||
n = room if state == "held" else max(1, min(room, min_blast - voice["started"]))
|
||
frames, voice["pos"] = self._read(voice["pos"], np.ones(n))
|
||
out[done:done + n] += frames
|
||
voice["started"] += n
|
||
elif state == "cut":
|
||
n = min(room, cut - voice["fade"])
|
||
frames, voice["pos"] = self._read(voice["pos"], np.ones(n))
|
||
k = voice["fade"] + np.arange(1, n + 1)
|
||
gain = voice["gain"] * np.clip(1 - k / cut, 0, 1)
|
||
out[done:done + n] += frames * gain[:, None]
|
||
voice["fade"] += n
|
||
alive = voice["fade"] < cut
|
||
else: # ending: fade out while the pitch falls
|
||
n = min(room, release - voice["fade"])
|
||
k = (voice["fade"] + np.arange(n)) / release
|
||
rates = 1.0 - (1.0 - self.BEND) * k
|
||
frames, voice["pos"] = self._read(voice["pos"], rates)
|
||
gain = np.cos(k * np.pi / 2) ** 2
|
||
out[done:done + n] += frames * gain[:, None]
|
||
voice["fade"] += n
|
||
alive = voice["fade"] < release
|
||
done += n
|
||
if alive:
|
||
kept.append(voice)
|
||
self._voices = kept
|
||
return out * self.LEVEL
|
||
|
||
|
||
class Mixer:
|
||
def __init__(self):
|
||
self.a = Deck("A")
|
||
self.b = Deck("B")
|
||
self.crossfader = 0.5
|
||
self.master = 1.0
|
||
self.horn = Horn()
|
||
# What was rendered, for the visualizer: (first frame, mono) blocks,
|
||
# appended here (the sink's thread), drained by the GUI.
|
||
self.tap = deque(maxlen=128)
|
||
self.rendered = 0
|
||
|
||
def deck_gain(self, deck: Deck) -> float:
|
||
a, b = crossfade_gains(self.crossfader)
|
||
return (a if deck is self.a else b) * deck.volume * self.master
|
||
|
||
def render(self, count: int) -> np.ndarray:
|
||
ga, gb = crossfade_gains(self.crossfader)
|
||
out = self.a.render(count) * (ga * self.a.volume * self.master)
|
||
out += self.b.render(count) * (gb * self.b.volume * self.master)
|
||
horn = self.horn.render(count)
|
||
if horn is not None:
|
||
out += horn * self.master
|
||
np.clip(out, -1.0, 1.0, out=out)
|
||
self.tap.append((self.rendered, out.mean(axis=1)))
|
||
self.rendered += count
|
||
return out
|