File size: 5,722 Bytes
0f47b4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | """Swaps the plucked/bell soundtrack for a melodious bansuri-style melody, then renders."""
import runpy
from huggingface_hub import hf_hub_download
REPO = "coderdecoder01/retirement-invitation-mamta"
NEW = r'''
def make_tanpura(freq, dur, seed=0, damp=0.9978, bright=0.55):
rng = np.random.default_rng(seed)
N = max(2, int(round(SR / float(freq))))
buf = rng.uniform(-1.0, 1.0, N).astype(np.float32)
for _ in range(5):
buf = 0.5 * (buf + np.roll(buf, 1))
buf /= (np.abs(buf).max() + 1e-9)
n = int(dur * SR)
out = np.empty(n, dtype=np.float32)
a, b = 0.5 * (1.0 + bright), 0.5 * (1.0 - bright)
idx = 0
for i in range(n):
cur = buf[idx]
out[i] = cur
buf[idx] = damp * (a * cur + b * buf[(idx + 1) % N])
idx = (idx + 1) % N
att = np.minimum(1.0, np.arange(n, dtype=np.float32) / (0.015 * SR))
return (out * att).astype(np.float32)
def make_flute(freq, dur, seed=0, glide=-0.055, vib=4.8, vib_depth=0.008, breath=0.05):
n = int(dur * SR)
t = np.arange(n, dtype=np.float32) / SR
slide = np.exp(-t * 3.2) * glide
vibf = 1.0 + vib_depth * np.sin(2.0 * np.pi * vib * t) * (1.0 - np.exp(-t * 2.0))
f = freq * (1.0 + slide) * vibf
ph = 2.0 * np.pi * np.cumsum(f) / SR
tone = (np.sin(ph) + 0.28 * np.sin(2.0 * ph) + 0.10 * np.sin(3.0 * ph)
+ 0.045 * np.sin(4.0 * ph) + 0.018 * np.sin(6.0 * ph))
rng = np.random.default_rng(seed)
nz = rng.standard_normal(n).astype(np.float32)
nz = np.diff(nz, prepend=nz[0])
env = np.minimum(1.0, t / 0.30) * np.clip((dur - t) / 0.9, 0.0, 1.0)
sig = (tone + breath * nz) * env
return (sig / (np.abs(sig).max() + 1e-9)).astype(np.float32)
def make_pad(dur, chord=(146.83, 220.0, 293.66), amp=1.0):
n = int(dur * SR)
t = np.arange(n, dtype=np.float32) / SR
out = np.zeros(n, dtype=np.float32)
for i, f in enumerate(chord):
wob = 1.0 + 0.0015 * np.sin(2.0 * np.pi * (0.11 + 0.04 * i) * t)
out += np.sin(2.0 * np.pi * f * wob * t) / (i + 2.5)
out *= np.minimum(1.0, t / 5.0)
return (amp * out / (np.abs(out).max() + 1e-9)).astype(np.float32)
def reverb(x, seed, mix=0.36, decay=1.9, sr=SR):
rng = np.random.default_rng(seed)
n = int(decay * sr)
ir = (rng.standard_normal(n) * np.exp(-np.arange(n) / (0.34 * sr))).astype(np.float32)
ir[0] = 1.0
ir /= np.abs(ir).sum() ** 0.5
L = 1
while L < len(x) + n:
L *= 2
y = np.fft.irfft(np.fft.rfft(x, L) * np.fft.rfft(ir, L), L)[:len(x)].astype(np.float32)
y = y / (np.abs(y).max() + 1e-9) * (np.abs(x).max() + 1e-9)
return (1.0 - mix) * x + mix * y
def build_soundtrack(total):
S = int((total + 1.2) * SR)
bus = {k: np.zeros(S, dtype=np.float32) for k in ("drone", "lead", "bass", "pad")}
def add(which, t0, sig, amp):
i = int(t0 * SR)
if i >= S:
return
j = min(S, i + len(sig))
bus[which][i:j] += sig[:j - i] * amp
SA = 146.83
def nf(semi):
return SA * (2.0 ** (semi / 12.0))
cycle = [(7, 0.90), (0, 0.86), (12, 0.74), (12, 0.68)]
t0, k = 0.0, 0
while t0 < total:
semi, amp = cycle[k % len(cycle)]
add("drone", t0, make_tanpura(nf(semi), 3.2, seed=k * 5 + 2), 0.070 * amp)
t0 += 2.40
k += 1
melody = [(0.60, 0, 2.6), (2.10, 4, 2.2), (3.50, 7, 2.4), (5.20, 9, 2.6),
(6.90, 12, 3.0), (9.40, 9, 2.2), (10.90, 7, 2.2), (12.40, 4, 2.2),
(14.00, 2, 2.4), (16.40, 4, 2.4), (18.10, 9, 2.4), (19.80, 12, 2.6),
(21.60, 14, 2.6), (23.40, 16, 2.8), (25.40, 14, 2.4), (27.20, 12, 2.6),
(29.00, 9, 2.6), (30.70, 7, 2.4), (32.10, 12, 2.6)]
for i, (t0, semi, dur) in enumerate(melody):
if t0 >= total:
continue
amp = 0.26 if i % 3 else 0.30
add("lead", t0, make_flute(nf(semi), dur, seed=200 + i * 17), amp)
for i, (t0, semi, dur) in enumerate(melody):
if t0 + 0.28 >= total:
continue
add("bass", t0 + 0.28, make_flute(nf(semi - 12), dur * 0.95, seed=400 + i * 11,
glide=0.0, vib=3.6, breath=0.03), 0.085)
add("pad", 0.4, make_pad(total + 1.0), 0.058)
kernel = np.hanning(11)
kernel /= kernel.sum()
def lp(x):
return np.convolve(x, kernel, mode="same").astype(np.float32)
for kk in bus:
bus[kk] = lp(bus[kk])
L = reverb(bus["drone"] * 0.90 + bus["lead"] + bus["bass"] + bus["pad"], seed=5)
R = reverb(bus["drone"] * 1.00 + bus["lead"] * 0.96 + bus["bass"] + bus["pad"], seed=9)
st = np.stack([L, R], axis=1)
n = st.shape[0]
fi, fo = int(1.1 * SR), int(3.2 * SR)
env = np.ones(n, dtype=np.float32)
env[:fi] = np.linspace(0.0, 1.0, fi) ** 0.7
env[n - fo:] = np.linspace(1.0, 0.0, fo) ** 1.1
st *= env[:, None]
peak = np.abs(st).max() + 1e-9
st = np.tanh(st / peak * 1.1) * 0.68
st = st[:int(total * SR)]
data = np.clip(st, -1.0, 1.0)
with wave.open(WAV, "wb") as w:
w.setnchannels(2); w.setsampwidth(2); w.setframerate(SR)
w.writeframes((data * 32767.0).astype("<i2").tobytes())
print("soundtrack (melodious flute): %s %.2fs peak=%.2f"
% (WAV, st.shape[0] / float(SR), np.abs(st).max()), flush=True)
'''
src_path = hf_hub_download(REPO, "render_invitation.py", repo_type="dataset")
src = open(src_path).read()
i = src.index("def ks_pluck(")
j = src.index('print("building assets')
out = "/tmp/render_v6.py"
open(out, "w").write(src[:i] + NEW + src[j:])
print("patched soundtrack block: %d -> %d chars" % (len(src), len(src[:i] + NEW + src[j:])), flush=True)
runpy.run_path(out, run_name="__main__")
|