Files
LihonT/scripts/make_store_music.py
cheb0chik 5df4cddf7d LihonT 3 Luna pre-alpha: the streak pet becomes an animal
A pair of people can have a kitten, a puppy or a lion cub in place of TikTok's streak creature: it grows with the days of the
streak (cub, teen, adult, elder), goes grey while the streak is about to burn out, can be dragged in the chat and is also shown
on the big streak screen. An animal is given by a link (lihont.space/pet) that the friend accepts. The drawings are made by
scripts/make_pet_designs.py and served as strips of frames. Also here: the promo video scripts (make_pet_video.py on top of
make_store_video.py), a retry pause for strips that failed to load, stricter checks in the answer to a gift.

Work in progress: the gift service is not switched on for everyone yet and there is no public build with the animals.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 15:14:48 +03:00

375 lines
14 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
An original soundtrack for the LihonT Store video, made of nothing but sine waves, noise and filters (numpy and scipy), so that it is ours.
Playful and bright, in A major, 120 beats a minute with eighth notes: mallet plucks and a bell hook over a soft kick, clap and hats, a
round bass and a warm pad. On top of it small sound effects fall exactly on the events of the video (the cursor clicks, the picture
drops into the window, a ding on the check mark, the window whooshes down, the tiles turn into a cat, the letters of the title pop).
It writes work/music/store.wav and store.mp3 and store.beats.json (the same format scripts/analyze_beats.py makes), so that the video script
takes its beats from it: a bar every 2 s, the strong beats at 1, 3, 5 ... 31 s.
py -X utf8 -B scripts/make_store_music.py
"""
from __future__ import annotations
import json
import math
import shutil
import subprocess
import wave
from pathlib import Path
import numpy as np
from scipy.signal import butter, fftconvolve, sosfilt
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "work" / "music"
SR = 44100
BPM = 120
BEAT = 60.0 / BPM
BAR = 4 * BEAT
FIRST_BAR = 1.0 # the first strong beat
BARS = 16
DURATION = 33.0
RNG = np.random.default_rng(7)
def midi(n):
return 440.0 * 2 ** ((n - 69) / 12)
def tt(d):
return np.arange(int(d * SR)) / SR
def lp(x, f, order=2):
return sosfilt(butter(order, f, "low", fs=SR, output="sos"), x)
def hp(x, f, order=2):
return sosfilt(butter(order, f, "high", fs=SR, output="sos"), x)
def bp(x, lo, hi, order=2):
return sosfilt(butter(order, [lo, hi], "band", fs=SR, output="sos"), x)
def attack(x, ms=3.0):
n = max(1, int(SR * ms / 1000))
x = x.copy()
x[:n] *= np.linspace(0, 1, n)
return x
# ---------------------------------------------------------------- instruments
def mallet(f, d=0.9):
t = tt(d)
s = np.sin(2 * math.pi * f * t + 1.6 * np.exp(-10 * t) * np.sin(2 * math.pi * f * 2 * t)) * np.exp(-4.5 * t)
s += 0.3 * np.sin(2 * math.pi * f * 4 * t) * np.exp(-16 * t)
return attack(s * 0.8)
def bell(f, d=2.4):
t = tt(d)
s = np.zeros_like(t)
for ratio, amp, decay in ((1, 1.0, 1.5), (2.0, 0.5, 2.2), (2.76, 0.4, 3.0), (5.4, 0.22, 4.5), (8.9, 0.1, 7.0)):
s += amp * np.sin(2 * math.pi * f * ratio * t) * np.exp(-decay * t)
return attack(s * 0.5, 4)
def pad(freqs, d):
t = tt(d)
s = np.zeros_like(t)
for f in freqs:
for detune in (0.997, 1.003):
for k in range(1, 6):
s += np.sin(2 * math.pi * f * detune * k * t) / k
s = lp(s, 1500)
rise = np.minimum(1, t / 0.35)
fall = np.minimum(1, (d - t) / 0.4)
return s * rise * fall * 0.035
def bass(f, d=0.42):
t = tt(d)
s = np.sin(2 * math.pi * f * t) + 0.35 * np.sin(2 * math.pi * f * 2 * t)
s = np.tanh(1.8 * s) * np.exp(-3.2 * t)
return attack(lp(s, 600) * 0.7, 6)
def kick():
t = tt(0.45)
f = 46 + 90 * np.exp(-t * 38)
ph = 2 * math.pi * np.cumsum(f) / SR
s = np.sin(ph) * np.exp(-t * 9)
s += 0.3 * hp(RNG.standard_normal(len(t)), 3000) * np.exp(-t * 140)
return attack(s * 0.95, 1)
def clap():
t = tt(0.3)
s = np.zeros_like(t)
for o in (0, 0.011, 0.022):
i = int(o * SR)
s[i:] += RNG.standard_normal(len(t) - i) * np.exp(-(t[: len(t) - i]) * 60)
s += RNG.standard_normal(len(t)) * np.exp(-t * 16) * 0.4
return bp(s, 900, 4500) * 0.55
def hat(open_=False):
t = tt(0.22 if open_ else 0.06)
return hp(RNG.standard_normal(len(t)), 7000) * np.exp(-t * (14 if open_ else 70)) * 0.35
def tick(f=2200.0):
t = tt(0.05)
s = hp(RNG.standard_normal(len(t)), 2500) * np.exp(-t * 160) + 0.5 * np.sin(2 * math.pi * f * t) * np.exp(-t * 90)
return s * 0.6
def pop(f0=950.0, f1=260.0, d=0.12):
t = tt(d)
ph = 2 * math.pi * np.cumsum(f1 + (f0 - f1) * np.exp(-t * 45)) / SR
return attack(np.sin(ph) * np.exp(-t * 26) * 0.8, 1)
def blup(f0=420.0, f1=900.0, d=0.18):
t = tt(d)
ph = 2 * math.pi * np.cumsum(f0 + (f1 - f0) * (t / d) ** 0.7) / SR
return attack(np.sin(ph) * np.exp(-t * 17) * 0.7, 2)
def whoosh(d=0.8, up=True):
t = tt(d)
noise = RNG.standard_normal(len(t))
out = np.zeros_like(t)
bands = [(300, 900), (700, 2200), (1800, 5200), (4000, 9000)]
centre = np.linspace(0, 1, len(bands))
pos = (t / d) if up else (1 - t / d)
for b, (lo, hi) in enumerate(bands):
w = np.exp(-((pos - centre[b]) ** 2) / 0.05)
out += bp(noise, lo, hi) * w
env = np.sin(np.pi * np.clip(t / d, 0, 1)) ** 1.5
return out * env * 0.5
def riser(d=0.9):
t = tt(d)
noise = hp(RNG.standard_normal(len(t)), 1800) * (t / d) ** 2
tone = np.sin(2 * math.pi * np.cumsum(220 + 880 * (t / d) ** 2) / SR) * (t / d) ** 1.5 * 0.25
return (noise * 0.35 + tone) * np.minimum(1, (d - t) / 0.03 + 0.0)
def boing(d=0.35):
t = tt(d)
f = 280 + 520 * np.sin(np.pi * np.clip(t / d, 0, 1)) ** 0.8
ph = 2 * math.pi * np.cumsum(f * (1 + 0.02 * np.sin(2 * math.pi * 24 * t))) / SR
return attack(np.sin(ph) * np.exp(-t * 6) * 0.7, 2)
def meow(d=0.5):
"""A short, soft mew: a gliding saw through a formant-ish band, rising and then falling."""
t = tt(d)
f = 520 + 380 * np.sin(np.pi * np.clip(t / d * 0.9, 0, 1)) - 140 * (t / d)
ph = np.cumsum(f) / SR
saw = 2 * (ph % 1) - 1
s = bp(saw, 700, 2600, 2) * np.sin(np.pi * np.clip(t / d, 0, 1)) ** 0.8
return attack(s * 0.9, 12)
def sparkle(d=1.3, n=9, lo=84, seed=0):
r = np.random.default_rng(seed)
out = np.zeros(int(d * SR))
for i in range(n):
note = bell(midi(lo + int(r.choice([0, 2, 4, 7, 9, 12]))), 0.7) * 0.28
o = int(r.uniform(0, d * 0.7) * SR)
m = min(len(note), len(out) - o)
out[o:o + m] += note[:m]
return out
# ---------------------------------------------------------------- the mix
class Mix:
def __init__(self, seconds):
self.n = int(seconds * SR)
self.left = np.zeros(self.n)
self.right = np.zeros(self.n)
self.send = np.zeros(self.n)
self.duck_times = []
def add(self, sig, t0, gain=1.0, pan=0.0, send=0.0, bus=None):
i = int(t0 * SR)
if i >= self.n or i + len(sig) <= 0:
return
lo = max(0, -i)
hi = min(len(sig), self.n - i)
piece = sig[lo:hi] * gain
a = (pan + 1) * math.pi / 4
self.left[i + lo:i + hi] += piece * math.cos(a)
self.right[i + lo:i + hi] += piece * math.sin(a)
if send:
self.send[i + lo:i + hi] += piece * send
def build() -> np.ndarray:
m = Mix(DURATION + 1.0)
bars = [FIRST_BAR + BAR * b for b in range(BARS)]
chords = [[57, 61, 64], [52, 56, 59], [54, 57, 61], [50, 54, 57]] # A, E, F#m, D (low triads)
roots = [45, 40, 42, 38]
hook = [[76, None, 73, 76, 78, None, 76, None], [73, None, 71, 73, 76, None, 73, None],
[73, 76, 78, None, 76, 73, 71, None], [69, None, 71, 73, 74, None, 73, 71]]
# which layers play in which bar (see the scenes of the video)
layers = {
0: "pad arp", 1: "pad arp kick hat", 2: "pad arp kick hat bass", 3: "pad arp kick hat bass clap hook",
4: "pad arp kick hat bass clap hook", 5: "pad arp kick hat bass clap hook", 6: "pad arp kick hat bass clap hook",
7: "pad arp kick hat bass clap hook", 8: "pad arp kick hat bass clap hook", 9: "pad arp kick hat bass clap hook",
10: "pad arp kick hat bass clap hook", 11: "pad arp kick hat bass clap hook", 12: "pad arp bass hook",
13: "pad arp bass hat roll", 14: "pad arp kick hat bass hook", 15: "pad arp",
}
kicks = []
# a rising swell before the first bar
m.add(riser(FIRST_BAR) * 0.5, 0.0, 0.5, send=0.2)
for b, t0 in enumerate(bars):
L = layers[b].split()
c = b % 4
tones = chords[c]
if "pad" in L:
notes = [midi(n + 12) for n in tones] + [midi(tones[0])]
m.add(pad(notes, BAR + 0.5), t0 - 0.05, 1.0, send=0.25)
if "arp" in L:
for k in range(8):
idx = [0, 1, 2, 1, 0, 1, 2, 1][k]
n = tones[idx] + 24 + (12 if idx == 2 else 0)
soft = 0.34 if b < 3 or b in (12, 15) else 0.42
m.add(mallet(midi(n), 0.55), t0 + k * BEAT / 2, soft, pan=-0.35 + 0.1 * (k % 2), send=0.22)
if "hook" in L:
for k, n in enumerate(hook[c]):
if n is not None:
m.add(bell(midi(n + 12), 1.3), t0 + k * BEAT / 2, 0.30 if b != 12 else 0.2, pan=0.3, send=0.4)
if "bass" in L:
for k in (0, 3, 4, 6):
m.add(bass(midi(roots[c])), t0 + k * BEAT / 2, 0.6 if b != 12 else 0.4)
for beat in range(4):
tb = t0 + beat * BEAT
if "kick" in L and beat in (0, 2):
m.add(kick(), tb, 0.95)
kicks.append(tb)
if "kick" in L and beat == 3 and b % 2 == 1:
m.add(kick(), tb + BEAT / 2, 0.7)
kicks.append(tb + BEAT / 2)
if "clap" in L and beat in (1, 3):
m.add(clap(), tb, 0.75, pan=0.1, send=0.3)
if "hat" in L:
for k in range(8):
m.add(hat(k == 7), t0 + k * BEAT / 2, 0.9 if k % 2 else 0.55, pan=0.25)
if "roll" in L: # a build before the title
for k in range(16):
m.add(hat(), t0 + k * BEAT / 4, 0.25 + 0.5 * k / 16, pan=0.2)
for k in range(8):
m.add(clap(), t0 + BAR + k * BEAT / 4 - 1.0 + 0.0, 0.0)
# ---- sound effects on the events of the video (their times are the same as in scripts/make_store_video.py)
S = [FIRST_BAR + BAR * k for k in range(16)]
sfx = lambda sig, t0, g=0.8, pan=0.0, send=0.15: m.add(sig, t0, g, pan, send)
sfx(tick(), S[1], 0.9) # the click on the button
sfx(pop(700, 300, 0.1), S[1] + 0.2, 0.5, send=0.2)
sfx(whoosh(0.5, True), S[1] + 0.2, 0.35, 0.3) # the window grows
sfx(tick(1800), S[1] + 1.25, 0.9) # the picture is taken
sfx(whoosh(0.7, True), S[1] + 1.4, 0.3, -0.2) # the drag
sfx(pop(), S[2], 0.9, send=0.3) # the drop
sfx(kick(), S[2] + 0.02, 0.6)
for i, tp in enumerate(np.arange(S[2] + 0.4, S[3] - 0.15, 0.13)): # the upload ticks, rising
sfx(mallet(midi(72 + (i % 8) * 2), 0.18), float(tp), 0.18, 0.4, 0.2)
sfx(bell(midi(88), 1.8), S[3], 0.5, 0.0, 0.5) # the ding of the check mark
sfx(bell(midi(95), 1.4), S[3] + 0.08, 0.35, 0.2, 0.5)
sfx(sparkle(1.4, 10, 86, 1), S[3] + 0.1, 0.7, 0.0, 0.4)
sfx(whoosh(0.8, False), S[3] + 0.5, 0.45, 0.0) # the window slides down
sfx(pop(1100, 330, 0.14), S[3] + 0.9, 0.9, send=0.3) # the avatar pops
sfx(sparkle(1.3, 12, 84, 2), S[3] + 0.95, 0.8, 0.0, 0.4)
sfx(bell(midi(81), 1.6), S[4], 0.45, 0.0, 0.5)
sfx(whoosh(1.0, True), S[4] + 0.5, 0.4, 0.2) # the tiles come in
for k in range(3):
sfx(tick(2500 + 300 * k), S[4] + 0.7 + 0.22 * k, 0.4, -0.4 + 0.4 * k)
sfx(whoosh(0.5, True), S[7] - 0.15, 0.55, 0.0) # the tiles gather
sfx(kick(), S[7], 0.9)
sfx(boing(), S[7] + 0.3, 0.7, 0.0, 0.2) # ... and turn into a cat
sfx(meow(), S[7] + 0.45, 0.7, 0.1, 0.3)
sfx(sparkle(1.0, 8, 88, 3), S[7] + 0.3, 0.6, 0.0, 0.4)
sfx(riser(0.9), S[8] - 0.1, 0.7) # the camera flies in
sfx(tick(2000), S[8] + 1.0, 0.9) # "+ add"
sfx(bell(midi(88), 1.4), S[8] + 1.02, 0.5, 0.0, 0.5)
sfx(bell(midi(95), 1.2), S[8] + 1.1, 0.3, 0.2, 0.5)
for k, s in enumerate((S[9], S[10], S[11])): # the bubble pictures change on every beat
sfx(blup(), s + 0.05, 0.8, -0.3 + 0.3 * k, 0.25)
sfx(pop(900, 300, 0.1), s + 0.0, 0.5, 0.2)
sfx(sparkle(1.6, 14, 84, 4), S[11] + 0.02, 0.8, 0.0, 0.4)
for k in range(3): # the three lines
t0 = S[12] + k * (S[14] - S[12]) / 3
for i in range(4):
sfx(mallet(midi(76 + [0, 4, 2, 7][i]), 0.4), t0 + i * 0.11, 0.32, -0.2 + 0.15 * i, 0.3)
sfx(riser(0.9), S[14] - 0.9, 0.7)
sfx(bell(midi(69), 2.2), S[14] + 0.1, 0.5, -0.2, 0.5) # LihonT
for i, n in enumerate([76, 78, 80, 81, 83]): # S T O R E
sfx(mallet(midi(n), 0.6), S[14] + 0.35 + i * 0.09, 0.55, -0.3 + 0.15 * i, 0.4)
sfx(bell(midi(n + 12), 0.9), S[14] + 0.35 + i * 0.09, 0.2, 0.0, 0.5)
sfx(sparkle(1.8, 16, 86, 5), S[15], 0.9, 0.0, 0.4) # the logo
for n in (57, 61, 64, 69):
sfx(bell(midi(n + 12), 2.6), S[15] + 0.02, 0.28, 0.0, 0.6)
sfx(kick(), S[15], 0.7)
# ---- the pad and the plucks breathe with the kick (a light sidechain)
n = m.n
duck = np.ones(n)
for tk in kicks:
i = int(tk * SR)
seg = min(int(0.28 * SR), n - i)
if seg > 0:
duck[i:i + seg] = np.minimum(duck[i:i + seg], 1 - 0.28 * np.exp(-np.arange(seg) / SR / 0.09))
# reverb on the send bus (a different tail for each ear)
ir_len = int(1.5 * SR)
tail = np.exp(-np.arange(ir_len) / SR * 3.2)
irl = bp(RNG.standard_normal(ir_len), 250, 7500) * tail
irr = bp(RNG.standard_normal(ir_len), 250, 7500) * tail
wet_l = fftconvolve(m.send, irl)[:n] * 0.035
wet_r = fftconvolve(m.send, irr)[:n] * 0.035
left = m.left * duck + wet_l
right = m.right * duck + wet_r
out = np.stack([left, right], axis=1)
out = np.tanh(out * 0.8)
out[: int(0.01 * SR)] *= np.linspace(0, 1, int(0.01 * SR))[:, None]
out = out[: int(DURATION * SR)]
fade = int(1.4 * SR)
out[-fade:] *= np.linspace(1, 0, fade)[:, None] ** 1.5
out /= max(1e-9, np.abs(out).max()) / 0.8
return out.astype(np.float32)
def write(path: Path, data: np.ndarray) -> None:
pcm = (data * 32767).astype("<i2")
with wave.open(str(path), "wb") as f:
f.setnchannels(2)
f.setsampwidth(2)
f.setframerate(SR)
f.writeframes(pcm.tobytes())
def main() -> None:
OUT.mkdir(parents=True, exist_ok=True)
audio = build()
wav = OUT / "store.wav"
write(wav, audio)
mp3 = OUT / "store.mp3"
subprocess.run([shutil.which("ffmpeg") or "ffmpeg", "-y", "-loglevel", "error", "-i", str(wav), "-c:a", "libmp3lame", "-b:a", "224k", str(mp3)], check=True)
beats = [round(i * BEAT, 3) for i in range(int(DURATION / BEAT))]
strong = [round(FIRST_BAR + BAR * k, 3) for k in range(BARS)]
(OUT / "store.beats.json").write_text(json.dumps({"tempo": BPM, "duration": DURATION, "beats": beats, "strong_beats": strong}), encoding="utf-8")
print("wrote", mp3, "| peak %.2f | rms %.3f" % (float(np.abs(audio).max()), float(np.sqrt((audio ** 2).mean()))))
if __name__ == "__main__":
main()