compose_sfx.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Original sample-free Diamond Arrow effects; deterministic, D-major tuned to the BGM.
  2. Layered bell partials, filtered noise, pitch sweeps and short ambience. Requires numpy.
  3. """
  4. from pathlib import Path
  5. import json, wave
  6. import numpy as np
  7. SR = 44100
  8. ROOT = Path(__file__).resolve().parents[2]
  9. OUT = ROOT / 'Art/Audio/SFX'
  10. OUT.mkdir(parents=True, exist_ok=True)
  11. rng = np.random.default_rng(112026)
  12. def time(seconds):
  13. return np.arange(round(seconds * SR)) / SR
  14. def soften(y):
  15. n = min(len(y)//2, round(.008*SR))
  16. y[:n] *= np.linspace(0, 1, n)
  17. y[-n:] *= np.linspace(1, 0, n)
  18. return y
  19. def bell(midi, seconds, decay=7):
  20. t = time(seconds); f = 440 * 2**((midi-69)/12)
  21. y = np.sin(2*np.pi*f*t + .65*np.sin(2*np.pi*2*f*t)*np.exp(-t*22))
  22. y += .23*np.sin(2*np.pi*f*2.003*t)*np.exp(-t*9)
  23. y += .07*np.sin(2*np.pi*f*3.99*t)*np.exp(-t*30)
  24. return soften(y*(1-np.exp(-t*900))*np.exp(-t*decay))
  25. def sweep(start, end, seconds):
  26. t=time(seconds); f=start+(end-start)*t/seconds
  27. return soften(np.sin(2*np.pi*np.cumsum(f)/SR)*np.sin(np.pi*t/seconds)**1.6)
  28. def noise(seconds, cutoff=12):
  29. y=rng.normal(0, 1, len(time(seconds)))
  30. y=np.convolve(y, np.ones(cutoff)/cutoff, mode='same')
  31. return soften(y*np.sin(np.linspace(0, np.pi, len(y)))**2)
  32. def mix(seconds, layers):
  33. y=np.zeros(round(seconds*SR))
  34. for offset, signal, gain in layers:
  35. start=round(offset*SR); count=min(len(signal), len(y)-start)
  36. y[start:start+count] += signal[:count]*gain
  37. return y
  38. def phrase(notes, spacing, seconds, gain=.5):
  39. return mix(seconds, [(i*spacing,bell(n, .65, 6),gain) for i,n in enumerate(notes)])
  40. clips={
  41. 'Tap': mix(.13, [(0,bell(86,.13,30),.5),(0,noise(.045,4),.12)]),
  42. 'Slide': mix(.38, [(0,noise(.29,15),.9),(0,sweep(420,1050,.23),.12),(.08,bell(86,.26,13),.25),(.17,bell(93,.20,15),.13)]),
  43. 'Blocked': mix(.26, [(0,sweep(260,105,.20),.55),(0,noise(.085,7),.22),(.045,bell(50,.20,16),.22)]),
  44. '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)]),
  45. 'Hint': phrase([78,81,86],.105,.88,.42),
  46. 'Heart': mix(.80, [(0,bell(74,.65,5),.36),(.10,bell(78,.62,5),.33),(.20,bell(81,.58,5),.32)]),
  47. 'Reward': phrase([74,78,81,86],.095,1.14,.45),
  48. '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)]),
  49. 'Fail': mix(1.05, [(0,bell(69,.75,6),.40),(.18,bell(66,.70,5),.35),(.38,bell(62,.65,5),.38)]),
  50. 'Countdown': mix(.14, [(0,bell(81,.14,24),.35),(0,noise(.035,8),.08)]),
  51. '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)]),
  52. }
  53. report=[]
  54. preview=[]
  55. for name,y in clips.items():
  56. # Short room tail; gentler than reverberant notes that mask repeated taps.
  57. dry=y.copy()
  58. for delay,gain in ((.031,.09),(.057,.045)):
  59. shift=round(delay*SR)
  60. y[shift:]+=dry[:-shift]*gain
  61. y=soften(y)
  62. y *= (.47 if name=='Countdown' else .68) / max(abs(y).max(), 1e-9)
  63. with wave.open(str(OUT/(name+'.wav')), 'wb') as f:
  64. f.setnchannels(1); f.setsampwidth(2); f.setframerate(SR)
  65. f.writeframes((y*32767).astype('<i2').tobytes())
  66. 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())))
  67. preview.extend([y,np.zeros(round(.35*SR))])
  68. (OUT/'SFX.json').write_text(json.dumps(report,indent=2)+'\n',encoding='utf-8')
  69. preview_path=ROOT/'Editor/Validation/Audio~'
  70. preview_path.mkdir(parents=True,exist_ok=True)
  71. with wave.open(str(preview_path/'SFX-showcase.wav'),'wb') as f:
  72. f.setnchannels(1); f.setsampwidth(2); f.setframerate(SR)
  73. f.writeframes((np.concatenate(preview)*32767).astype('<i2').tobytes())
  74. print(json.dumps(report,indent=2))