"""Original stereo roulette / rainy-city sound design, compiled offline to PCM.""" import math from pathlib import Path import random import struct import wave ROOT = Path(__file__).resolve().parents[2] RATE = 22050 def bell(age, hz, strength): if age < 0: return 0 envelope = (1-math.exp(-age*35))*math.exp(-age*1.6) return strength*envelope*(math.sin(math.tau*hz*age)+ .22*math.sin(math.tau*hz*2.003*age)) def render(name, duration, studio): rng = random.Random(0x45424C41) payload = bytearray() filtered = 0 clicks = [] t_click = .25 while t_click < 3.4: clicks.append(t_click) t_click += .045 + t_click*.032 for i in range(round(RATE*duration)): t = i/RATE noise = rng.uniform(-1,1) filtered = .94*filtered + .06*noise left = right = 0. if studio: # The rolling ball crosses the stereo field as the camera orbits. for onset in clicks: age = t-onset if 0 <= age < .035: hit = .055*math.exp(-age*160)*math.sin(math.tau*1900*age) pan = math.sin(onset*2.4)*.7 left += hit*(1-pan) right += hit*(1+pan) swell = math.exp(-((t-3.45)/.85)**2) air = filtered*.24*swell chord = sum(bell(t-onset,hz,strength) for onset,hz,strength in [(3.65,73.42,.09),(3.85,146.83,.07),(4.1,220,.055), (4.4,293.66,.035),(4.7,587.33,.024)]) left += chord+air right += chord+air*.83 else: # Rain, a distant traffic pass, then a warm low title impact. rain = noise*.008 + filtered*.06 traffic = math.exp(-((t-3.4)/1.8)**2)*(.035*math.sin(math.tau*(110*t-3*t*t))+.07*filtered) pan = math.tanh((t-3.4)*.7)*.7 chord = sum(bell(t-onset,hz,strength) for onset,hz,strength in [(7.85,55,.10),(7.9,146.83,.07),(8.1,220,.055), (8.3,293.66,.04),(8.7,587.33,.03)]) sweep = filtered*.18*math.exp(-((t-7.9)/.65)**2) left = rain+traffic*(1-pan)+chord+sweep right = rain+traffic*(1+pan)+chord+sweep*.86 fade = min(1,t/.12,max(0,(duration-t)/.7)) for v in (left,right): payload.extend(struct.pack('