File size: 11,436 Bytes
6f2099e
f98b09c
 
 
 
 
 
 
 
 
 
6f2099e
 
 
 
 
 
 
 
 
 
 
f98b09c
6f2099e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f98b09c
 
 
 
 
6f2099e
 
 
 
 
 
 
 
 
 
 
 
 
f98b09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135f2e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f2099e
135f2e5
6f2099e
 
 
 
 
 
 
 
f98b09c
 
135f2e5
 
6f2099e
 
 
 
 
 
 
 
 
 
 
 
 
 
f98b09c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Procedural Broadcast UI & Presentation Sound Effects (SFX) Synthesizer v2.0
Generates 8 studio-grade 48kHz 16-bit Stereo WAV files with zero external dependencies:
  1. `whoosh`    — Airy chapter/slide transition whoosh + warm sub-thump
  2. `pop`       — Linear/Apple-style glass card focus pop
  3. `tick`      — Precision digital counter / bar-chart roll-up ticks
  4. `type`      — Tactile dark terminal command typing burst
  5. `highlight` — Silky shimmer sweep for quote highlighter
  6. `chime`     — Warm Cmaj9 glass bell chime for takeaway reveal
  7. `page_flip` — Realistic crisp paper page-turn rustle for 3D Open-Book / Notebook
  8. `pulse`     — Soft futuristic UI lock / timeline node activation pulse
"""

from pathlib import Path
import wave
import numpy as np

SAMPLE_RATE = 48000


def _save_stereo_wav(path: Path, left: np.ndarray, right: np.ndarray, peak: float = 0.85) -> Path:
    path.parent.mkdir(parents=True, exist_ok=True)
    max_val = max(float(np.max(np.abs(left))), float(float(np.max(np.abs(right)))), 1e-6)
    scale = peak / max_val
    stereo = np.stack([left * scale, right * scale], axis=-1)
    pcm = np.clip(stereo * 32767.0, -32768, 32767).astype(np.int16)
    with wave.open(str(path), "wb") as wf:
        wf.setnchannels(2)
        wf.setsampwidth(2)
        wf.setframerate(SAMPLE_RATE)
        wf.writeframes(pcm.tobytes())
    return path


def _one_pole_lowpass(signal: np.ndarray, cutoff_hz: np.ndarray) -> np.ndarray:
    out = np.zeros_like(signal)
    dt = 1.0 / SAMPLE_RATE
    rc = 1.0 / (2.0 * np.pi * np.maximum(cutoff_hz, 20.0))
    alpha = dt / (rc + dt)
    prev = 0.0
    for i in range(len(signal)):
        prev = prev + alpha[i] * (signal[i] - prev)
        out[i] = prev
    return out


def synth_whoosh_transition(out_path: Path) -> Path:
    dur = 0.56
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)
    rng = np.random.default_rng(42)
    noise_l = rng.standard_normal(n)
    noise_r = rng.standard_normal(n)

    bell = np.sin(np.pi * (t / dur)) ** 1.6
    cutoff = 180.0 + 1500.0 * bell
    swept_l = _one_pole_lowpass(noise_l, cutoff) - _one_pole_lowpass(noise_l, cutoff * 0.28)
    swept_r = _one_pole_lowpass(noise_r, cutoff * 1.04) - _one_pole_lowpass(noise_r, cutoff * 0.28)

    env = np.sin(np.pi * (t / dur)) ** 1.8
    pan_l = np.cos(0.5 * np.pi * (0.25 + 0.5 * (t / dur)))
    pan_r = np.sin(0.5 * np.pi * (0.25 + 0.5 * (t / dur)))

    sub_freq = 78.0 - 26.0 * (t / dur)
    sub_phase = 2.0 * np.pi * np.cumsum(sub_freq) / SAMPLE_RATE
    sub = np.sin(sub_phase) * np.exp(-((t - 0.22) ** 2) / (2.0 * 0.085**2)) * 0.45

    left = swept_l * env * pan_l * 0.9 + sub
    right = swept_r * env * pan_r * 0.9 + sub
    return _save_stereo_wav(out_path, left, right, peak=0.72)


def synth_card_focus_pop(out_path: Path) -> Path:
    dur = 0.22
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)
    freq = 440.0 + 280.0 * np.exp(-t * 38.0)
    phase = 2.0 * np.pi * np.cumsum(freq) / SAMPLE_RATE
    attack = np.minimum(t / 0.004, 1.0)
    decay = np.exp(-t * 21.0)

    tone = np.sin(phase) * 0.75 + np.sin(phase * 2.0 + 0.3) * 0.25 * np.exp(-t * 34.0)
    shimmer_l = np.sin(2.0 * np.pi * 1320.0 * t) * np.exp(-t * 42.0) * 0.14
    shimmer_r = np.sin(2.0 * np.pi * 1326.0 * t + 0.5) * np.exp(-t * 42.0) * 0.14

    left = (tone + shimmer_l) * attack * decay
    right = (tone + shimmer_r) * attack * decay
    return _save_stereo_wav(out_path, left, right, peak=0.68)


def synth_counter_tick(out_path: Path) -> Path:
    dur = 0.95
    n = int(SAMPLE_RATE * dur)
    left = np.zeros(n)
    right = np.zeros(n)
    u = np.linspace(0.0, 1.0, 13)
    tick_times = 0.02 + 0.78 * (1.0 - (1.0 - u) ** 1.75)

    for idx, ts in enumerate(tick_times):
        start_i = int(ts * SAMPLE_RATE)
        tick_len = int(0.032 * SAMPLE_RATE)
        end_i = min(n, start_i + tick_len)
        seg_t = np.arange(end_i - start_i) / SAMPLE_RATE
        pitch = 980.0 + idx * 28.0
        env = np.minimum(seg_t / 0.0015, 1.0) * np.exp(-seg_t * 135.0)
        gain = 0.55 + 0.45 * (idx / max(len(tick_times) - 1, 1))
        sig = (np.sin(2.0 * np.pi * pitch * seg_t) + 0.35 * np.sin(2.0 * np.pi * pitch * 2.4 * seg_t)) * env * gain
        pan = 0.42 + 0.16 * (idx % 2)
        left[start_i:end_i] += sig * (1.0 - pan * 0.3)
        right[start_i:end_i] += sig * (0.7 + pan * 0.3)

    return _save_stereo_wav(out_path, left, right, peak=0.62)


def synth_terminal_type(out_path: Path) -> Path:
    dur = 1.10
    n = int(SAMPLE_RATE * dur)
    left = np.zeros(n)
    right = np.zeros(n)
    rng = np.random.default_rng(108)
    key_offsets = [0.02, 0.09, 0.15, 0.23, 0.29, 0.36, 0.46, 0.53, 0.60, 0.68, 0.75, 0.84, 0.93]
    for idx, ts in enumerate(key_offsets):
        start_i = int(ts * SAMPLE_RATE)
        klen = int(0.028 * SAMPLE_RATE)
        end_i = min(n, start_i + klen)
        seg_t = np.arange(end_i - start_i) / SAMPLE_RATE
        click_noise = rng.standard_normal(len(seg_t)) * np.exp(-seg_t * 260.0) * 0.35
        thock_freq = 310.0 + (idx % 4) * 24.0
        thock = np.sin(2.0 * np.pi * thock_freq * seg_t) * np.exp(-seg_t * 145.0) * 0.65
        sig = (click_noise + thock) * np.minimum(seg_t / 0.001, 1.0)
        left[start_i:end_i] += sig * 0.95
        right[start_i:end_i] += sig * 1.0

    return _save_stereo_wav(out_path, left, right, peak=0.58)


def synth_highlighter_sweep(out_path: Path) -> Path:
    dur = 0.65
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)
    rng = np.random.default_rng(77)
    brush = _one_pole_lowpass(rng.standard_normal(n), np.linspace(900.0, 2400.0, n)) * 0.18
    freq = np.linspace(587.33, 880.0, n)
    phase = 2.0 * np.pi * np.cumsum(freq) / SAMPLE_RATE
    env = np.sin(np.pi * (t / dur)) ** 1.4 * np.exp(-t * 1.8)

    left = (brush + np.sin(phase) * 0.55 + np.sin(phase * 1.5) * 0.25) * env
    right = (brush + np.sin(phase + 0.35) * 0.55 + np.sin(phase * 1.5 + 0.2) * 0.25) * env
    return _save_stereo_wav(out_path, left, right, peak=0.56)


def synth_insight_chime(out_path: Path) -> Path:
    dur = 1.25
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)
    notes = [
        (523.25, 0.00, 0.42),
        (659.25, 0.04, 0.34),
        (783.99, 0.08, 0.30),
        (987.77, 0.12, 0.22),
        (1174.66, 0.16, 0.18),
    ]
    left = np.zeros(n)
    right = np.zeros(n)
    for freq, delay, amp in notes:
        dt = np.maximum(0.0, t - delay)
        active = (t >= delay).astype(float)
        env = np.minimum(dt / 0.008, 1.0) * np.exp(-dt * 3.4) * active
        left += np.sin(2.0 * np.pi * freq * dt) * env * amp
        right += np.sin(2.0 * np.pi * (freq * 1.0015) * dt + 0.25) * env * amp

    return _save_stereo_wav(out_path, left, right, peak=0.65)


def synth_page_flip(out_path: Path) -> Path:
    """Crisp tactile paper page-turn flutter for 3D Open-Book & Notebook scenes (0.48s)."""
    dur = 0.48
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)
    rng = np.random.default_rng(303)

    # High-passed crisp paper friction noise + subtle page settle thud
    n_l = rng.standard_normal(n)
    n_r = rng.standard_normal(n)
    hi_l = _one_pole_lowpass(n_l, np.linspace(4800.0, 2200.0, n)) - _one_pole_lowpass(n_l, np.full(n, 950.0))
    hi_r = _one_pole_lowpass(n_r, np.linspace(5100.0, 2100.0, n)) - _one_pole_lowpass(n_r, np.full(n, 950.0))

    flutter_env = (np.sin(np.pi * np.clip(t / 0.36, 0, 1)) ** 1.5) * (0.75 + 0.25 * np.sin(2.0 * np.pi * 19.0 * t))
    settle = np.sin(2.0 * np.pi * 135.0 * np.maximum(0.0, t - 0.30)) * np.exp(-np.maximum(0.0, t - 0.30) * 45.0) * (t >= 0.30) * 0.28

    left = hi_l * flutter_env * 0.9 + settle
    right = hi_r * flutter_env * 1.0 + settle
    return _save_stereo_wav(out_path, left, right, peak=0.60)


def synth_data_pulse(out_path: Path) -> Path:
    """Warm futuristic dual-harmonic pulse for Bento Grid, VS Comparison & Roadmap nodes (0.38s)."""
    dur = 0.38
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)

    f1 = np.linspace(330.0, 660.0, n)
    p1 = 2.0 * np.pi * np.cumsum(f1) / SAMPLE_RATE
    env = np.minimum(t / 0.006, 1.0) * np.exp(-t * 11.5)

    sig_l = (np.sin(p1) * 0.65 + np.sin(2.0 * np.pi * 990.0 * t) * 0.25 * np.exp(-t * 18.0)) * env
    sig_r = (np.sin(p1 + 0.25) * 0.65 + np.sin(2.0 * np.pi * 994.0 * t) * 0.25 * np.exp(-t * 18.0)) * env
    return _save_stereo_wav(out_path, sig_l, sig_r, peak=0.62)


def synth_scramble_decode(out_path: Path) -> Path:
    """Rapid high-tech cipher/text-scramble telemetry flutter (0.62s)."""
    dur = 0.62
    n = int(SAMPLE_RATE * dur)
    left = np.zeros(n)
    right = np.zeros(n)
    rng = np.random.default_rng(512)

    freqs = [1480, 1760, 1320, 1980, 1620, 2120, 1540, 1880, 2240, 1680, 2400]
    for idx, f in enumerate(freqs):
        ts = 0.02 + idx * 0.048
        s0 = int(ts * SAMPLE_RATE)
        slen = int(0.026 * SAMPLE_RATE)
        s1 = min(n, s0 + slen)
        st = np.arange(s1 - s0) / SAMPLE_RATE
        env = np.minimum(st / 0.0015, 1.0) * np.exp(-st * 150.0)
        blip = np.sin(2.0 * np.pi * f * st) * env * (0.45 + 0.35 * rng.random())
        pan = 0.3 + 0.4 * (idx % 2)
        left[s0:s1] += blip * (1.0 - pan * 0.35)
        right[s0:s1] += blip * (0.65 + pan * 0.35)

    return _save_stereo_wav(out_path, left, right, peak=0.52)


def synth_sonar_ping(out_path: Path) -> Path:
    """Deep cinematic orbital / 3D wireframe briefing ping (0.88s)."""
    dur = 0.88
    n = int(SAMPLE_RATE * dur)
    t = np.linspace(0.0, dur, n, endpoint=False)

    env = np.minimum(t / 0.005, 1.0) * np.exp(-t * 4.8)
    sub_env = np.minimum(t / 0.01, 1.0) * np.exp(-t * 6.5)
    ping_l = (np.sin(2.0 * np.pi * 784.0 * t) * 0.55 + np.sin(2.0 * np.pi * 1568.0 * t) * 0.18) * env
    ping_r = (np.sin(2.0 * np.pi * 786.0 * t + 0.3) * 0.55 + np.sin(2.0 * np.pi * 1572.0 * t) * 0.18) * env
    sub = np.sin(2.0 * np.pi * 98.0 * t) * sub_env * 0.35

    return _save_stereo_wav(out_path, ping_l + sub, ping_r + sub, peak=0.58)


def ensure_sfx_pack(sfx_dir: Path) -> dict[str, Path]:
    """Generates all 10 UI presentation SFX WAVs into `sfx_dir` and returns their paths."""
    sfx_dir.mkdir(parents=True, exist_ok=True)
    builders = {
        "whoosh": ("sfx_whoosh.wav", synth_whoosh_transition),
        "pop": ("sfx_card_pop.wav", synth_card_focus_pop),
        "tick": ("sfx_counter_tick.wav", synth_counter_tick),
        "type": ("sfx_terminal_type.wav", synth_terminal_type),
        "highlight": ("sfx_highlighter_sweep.wav", synth_highlighter_sweep),
        "chime": ("sfx_insight_chime.wav", synth_insight_chime),
        "page_flip": ("sfx_page_flip.wav", synth_page_flip),
        "pulse": ("sfx_data_pulse.wav", synth_data_pulse),
        "scramble": ("sfx_scramble_decode.wav", synth_scramble_decode),
        "sonar": ("sfx_sonar_ping.wav", synth_sonar_ping),
    }
    out = {}
    for key, (fname, fn) in builders.items():
        target = sfx_dir / fname
        if not target.exists() or target.stat().st_size < 1000:
            fn(target)
        out[key] = target
    return out


if __name__ == "__main__":
    out_dir = Path(__file__).resolve().parent / "assets" / "sfx"
    pack = ensure_sfx_pack(out_dir)
    for k, p in pack.items():
        print(f"Generated SFX [{k:10s}]: {p} ({p.stat().st_size:,} bytes)")