#!/usr/bin/env python3 """Original wind, kitchen and haunted-house textures for The Midnight Invitation.""" import hashlib import json from pathlib import Path import numpy as np import soundfile as sf ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / 'assets/stories/midnight-invitation/audio' RATE, SECONDS = 24000, 36 N = RATE * SECONDS T = np.arange(N) / RATE F = np.fft.rfftfreq(N, 1 / RATE) tracks = {} for name, seed in [('threshold', 103101), ('kitchen', 103102), ('hall', 103103)]: rng = np.random.default_rng(seed) def noise(low, high): spectrum = np.fft.rfft(rng.normal(size=N)) spectrum *= (1 - np.exp(-(F / low) ** 4)) * np.exp(-(F / high) ** 2) signal = np.fft.irfft(spectrum, n=N) return signal / np.sqrt(np.mean(signal ** 2)) def swell(cycles, phase=0): return .65 + .35 * np.sin(2 * np.pi * cycles * T / SECONDS + phase) if name == 'threshold': # Gentle air around a sheltered porch; leaves move in longer gusts. sound = .75 * noise(75, 950) * swell(2, .3) sound += .20 * noise(800, 3300) * swell(5, 1.2) ** 3 elif name == 'hall': # Sheltered air and distant timber strain, with no voices or clock ticks. sound = .50 * noise(45, 420) * swell(2, .7) sound += .09 * noise(300, 1600) * swell(5, 1.4) ** 3 for onset in [6.2, 18.7, 29.1]: distance = (T - onset + SECONDS / 2) % SECONDS - SECONDS / 2 envelope = np.exp(-(distance / .55) ** 2) sound += .035 * np.sin(2 * np.pi * (190 * T + .12 * np.sin(2 * np.pi * T))) * envelope else: # Low stove draft and irregular, small embers, without a musical bed. sound = .22 * noise(65, 650) * swell(2, 1.1) sound += .07 * noise(500, 2200) embers = noise(800, 4200) for onset in rng.uniform(1, SECONDS - 1, 48): dt = T - onset envelope = np.where(dt >= 0, np.exp(-np.maximum(dt, 0) * 35), 0) sound += rng.uniform(.13, .42) * embers * envelope sound -= np.mean(sound) ramp = np.linspace(0, 1, RATE // 20) sound[-len(ramp):] -= (sound[-1] - sound[0]) * ramp * ramp * (3 - 2 * ramp) sound *= .10 / np.sqrt(np.mean(sound ** 2)) sound *= min(1, .65 / np.max(np.abs(sound))) path = OUT / f'ambience_{name}.wav' sf.write(path, sound, RATE, subtype='PCM_16') pcm, rate = sf.read(path) assert abs(pcm[-1] - pcm[0]) <= 1 / 32768 tracks[name] = {'asset':path.relative_to(ROOT).as_posix(), 'sampleRate':RATE, 'channels':1, 'durationMs':SECONDS*1000, 'seed':seed, 'wavSha256':hashlib.sha256(path.read_bytes()).hexdigest(), 'peak':float(np.max(np.abs(pcm))), 'rms':float(np.sqrt(np.mean(pcm**2)))} print(f'{name}: {SECONDS}s, peak {tracks[name]["peak"]:.3f}, seamless PCM loop') (OUT/'atmosphere_provenance.json').write_text(json.dumps({ 'method':'Original procedural textures, rendered offline; no third-party recordings or runtime synthesis.', 'generator':'scripts/render_drama_atmosphere.py', 'tracks':tracks, },indent=2)+'\n')