| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- """Diamond Arrow effects: soft recorded wood/hand percussion for frequent interactions.
- Other celebration cues retain their original deterministic D-major synthesis.
- Layered bell partials, filtered noise, pitch sweeps and short ambience. Requires numpy.
- """
- from pathlib import Path
- import json, wave
- import numpy as np
- from acoustic_sampler import sample
- SR = 44100
- ROOT = Path(__file__).resolve().parents[2]
- OUT = ROOT / 'Art/Audio/SFX'
- OUT.mkdir(parents=True, exist_ok=True)
- rng = np.random.default_rng(112026)
- def time(seconds):
- return np.arange(round(seconds * SR)) / SR
- def soften(y):
- n = min(len(y)//2, round(.008*SR))
- y[:n] *= np.linspace(0, 1, n)
- y[-n:] *= np.linspace(1, 0, n)
- return y
- def bell(midi, seconds, decay=7):
- t = time(seconds); f = 440 * 2**((midi-69)/12)
- y = np.sin(2*np.pi*f*t + .65*np.sin(2*np.pi*2*f*t)*np.exp(-t*22))
- y += .23*np.sin(2*np.pi*f*2.003*t)*np.exp(-t*9)
- y += .07*np.sin(2*np.pi*f*3.99*t)*np.exp(-t*30)
- return soften(y*(1-np.exp(-t*900))*np.exp(-t*decay))
- def sweep(start, end, seconds):
- t=time(seconds); f=start+(end-start)*t/seconds
- return soften(np.sin(2*np.pi*np.cumsum(f)/SR)*np.sin(np.pi*t/seconds)**1.6)
- def noise(seconds, cutoff=12):
- y=rng.normal(0, 1, len(time(seconds)))
- y=np.convolve(y, np.ones(cutoff)/cutoff, mode='same')
- return soften(y*np.sin(np.linspace(0, np.pi, len(y)))**2)
- def mix(seconds, layers):
- y=np.zeros(round(seconds*SR))
- for offset, signal, gain in layers:
- start=round(offset*SR); count=min(len(signal), len(y)-start)
- y[start:start+count] += signal[:count]*gain
- return y
- def phrase(notes, spacing, seconds, gain=.5):
- return mix(seconds, [(i*spacing,bell(n, .65, 6),gain) for i,n in enumerate(notes)])
- def wood_tap():
- y = sample('wood.wav', .10, -5)
- return y * np.exp(-np.arange(len(y))/SR*35)
- clips={
- 'Tap': wood_tap(),
- 'Slide': mix(.27, [(0,noise(.23,40),.5),(.025,sample('wood.wav',.10,-7),.10)]),
- 'Blocked': mix(.20, [(0,sample('conga.wav',.18,-3),.5),(0,sample('wood.wav',.09,-9),.12)]),
- 'Hammer': mix(.48, [(0,sweep(190,55,.23),.55),(0,noise(.12,3),.35),(.05,bell(81,.38,10),.20),(.10,bell(90,.32,11),.13)]),
- 'Hint': phrase([78,81,86],.105,.88,.42),
- 'Heart': mix(.80, [(0,bell(74,.65,5),.36),(.10,bell(78,.62,5),.33),(.20,bell(81,.58,5),.32)]),
- 'Reward': phrase([74,78,81,86],.095,1.14,.45),
- 'Clear': mix(1.9, [(0,phrase([74,78,81,86],.13,1.2),.8),(.55,bell(74,1.2,3),.22),(.55,bell(81,1.2,3),.20),(.55,bell(86,1.2,3),.30),(.82,bell(93,.8,6),.14)]),
- 'Fail': mix(1.05, [(0,bell(69,.75,6),.40),(.18,bell(66,.70,5),.35),(.38,bell(62,.65,5),.38)]),
- 'Countdown': mix(.14, [(0,bell(81,.14,24),.35),(0,noise(.035,8),.08)]),
- 'ChestOpen': mix(.85, [(0,noise(.10,8),.35),(0,sweep(170,85,.13),.35),(.12,noise(.35,22),.50),(.16,sweep(280,1150,.43),.10),(.44,bell(81,.4,8),.22)]),
- }
- report=[]
- preview=[]
- for name,y in clips.items():
- # Short room tail; gentler than reverberant notes that mask repeated taps.
- dry=y.copy()
- for delay,gain in ([] if name in ('Tap','Slide','Blocked') else [(.031,.09),(.057,.045)]):
- shift=round(delay*SR)
- y[shift:]+=dry[:-shift]*gain
- y=soften(y)
- y *= ({'Tap':.24,'Slide':.33,'Blocked':.4,'Countdown':.47}.get(name,.68)) / max(abs(y).max(), 1e-9)
- with wave.open(str(OUT/(name+'.wav')), 'wb') as f:
- f.setnchannels(1); f.setsampwidth(2); f.setframerate(SR)
- f.writeframes((y*32767).astype('<i2').tobytes())
- report.append(dict(name=name,seconds=len(y)/SR,peakDb=round(20*np.log10(abs(y).max()),2),rmsDb=round(20*np.log10(np.sqrt(np.mean(y*y))),2),clippedSamples=int((abs(y)>=1).sum())))
- preview.extend([y,np.zeros(round(.35*SR))])
- (OUT/'SFX.json').write_text(json.dumps(report,indent=2)+'\n',encoding='utf-8')
- preview_path=ROOT/'Editor/Validation/Audio~'
- preview_path.mkdir(parents=True,exist_ok=True)
- with wave.open(str(preview_path/'SFX-showcase.wav'),'wb') as f:
- f.setnchannels(1); f.setsampwidth(2); f.setframerate(SR)
- f.writeframes((np.concatenate(preview)*32767).astype('<i2').tobytes())
- print(json.dumps(report,indent=2))
|