#!/usr/bin/env python3 """Render original, deterministic sound textures; no downloaded samples or runtime synthesis.""" import hashlib import json from pathlib import Path import numpy as np import soundfile as sf ROOT = Path(__file__).resolve().parents[1] RATE = 24000 SECONDS = 24 N = RATE * SECONDS T = np.arange(N) / RATE F = np.fft.rfftfreq(N, 1 / RATE) OUT = ROOT / 'assets/audio' def render(scene, seed): 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, depth, phase): return 1 + depth * np.sin(2 * np.pi * cycles * T / SECONDS + phase) if scene == 'cottage': # Air through trees beyond the shutter, with very soft timber movement. signal = .65 * noise(100, 1250) * swell(2, .35, .3) signal += .12 * noise(600, 3200) * swell(5, .4, 1.8) for centre in [5.2, 14.1, 20.3]: distance = (T - centre + SECONDS / 2) % SECONDS - SECONDS / 2 signal += .045 * np.sin(2 * np.pi * 270 * T) * np.exp(-(distance / .12) ** 2) elif scene == 'harbour': # Uneven small swells and a brighter wash as water meets the harbour wall. signal = .8 * noise(70, 1700) * swell(7, .5, .5) * swell(3, .3, 1.3) signal += .25 * noise(900, 4800) * swell(7, .6, 1.6) elif scene == 'bridge': # Continuous current under the stones, with irregular surface turbulence. signal = .8 * noise(260, 6500) * swell(3, .18, .9) * swell(11, .15, 2.1) signal += .4 * noise(80, 1800) * swell(5, .28, 1.8) elif scene == 'tower': # Sheltered low wind around the tower, with gentle gusts above it. signal = .8 * noise(45, 700) * swell(2, .55, 1.2) signal += .2 * noise(400, 2200) * swell(4, .65, .2) else: raise ValueError(scene) signal -= np.mean(signal) # Ease the last 50 ms onto the first sample, without a discontinuity at wrap. ramp = np.linspace(0, 1, RATE // 20) signal[-len(ramp):] -= (signal[-1] - signal[0]) * ramp * ramp * (3 - 2 * ramp) signal *= .13 / np.sqrt(np.mean(signal ** 2)) signal *= min(1, .72 / np.max(np.abs(signal))) return signal if __name__ == '__main__': tracks = {} for scene, seed in [('cottage', 104), ('harbour', 205), ('bridge', 306), ('tower', 407)]: path = OUT / f'ambience_{scene}.wav' sf.write(path, render(scene, seed), RATE, subtype='PCM_16') samples, rate = sf.read(path) assert rate == RATE and len(samples) == N assert .05 < np.sqrt(np.mean(samples ** 2)) < .15 assert np.max(np.abs(samples)) < .73 # A loop join must have no exceptional jump compared with normal samples. assert abs(samples[-1] - samples[0]) <= 1 / 32768 tracks[scene] = { 'file': path.name, 'sampleRate': RATE, 'channels': 1, 'durationMs': SECONDS * 1000, 'frames': N, 'seed': seed, 'wavSha256': hashlib.sha256(path.read_bytes()).hexdigest(), } print(f'{scene}: {SECONDS}s loop, peak {np.max(np.abs(samples)):.3f}, RMS {np.sqrt(np.mean(samples ** 2)):.3f}') (OUT / 'ambience_manifest.json').write_text(json.dumps({ 'method': 'Original procedural sound textures rendered offline by scripts/render_ambience.py; no third-party samples.', 'tracks': tracks, }, indent=2) + '\n')