Files
Diabolo/tools/make_build_sfx.py
T

35 lines
1.5 KiB
Python
Raw Normal View History

"""Original, deterministic mechanical effects; no third-party samples."""
from pathlib import Path
import math, random, struct, wave
out=Path(__file__).resolve().parent.parent/'Demon_v0.14/audio'
rate=44100
def save(name,kind):
rng=random.Random(147+kind)
length=.38 if kind==0 else .48
samples=[];low=0.0
for i in range(int(rate*length)):
t=i/rate
noise=rng.uniform(-1,1);low=low*.80+noise*.20
if kind==0:
body=math.sin(2*math.pi*(105*t-55*t*t))*math.exp(-22*t)
wood=low*math.exp(-30*t)
metal=sum(math.sin(2*math.pi*f*t) for f in [740,1137,1820])/3*math.exp(-18*t)
tail=max(0,t-.085)
second=(math.sin(2*math.pi*175*tail)+low)*math.exp(-50*tail)*(.14 if t>.085 else 0)
sample=.56*body+.36*wood+.17*metal+second
else:
scrape=(noise-low)*math.exp(-8*t)*min(1,t/.012)
ring=sum(math.sin(2*math.pi*f*t) for f in [410,673,921])/3*math.exp(-10*t)
end=max(0,t-.14)
thud=math.sin(2*math.pi*82*end)*math.exp(-24*end)*(.36 if t>.14 else 0)
sample=.28*scrape+.25*ring+thud
sample*=min(1,t/.002)*min(1,(length-t)/.025)
samples.append(sample)
peak=max(abs(s) for s in samples)
with wave.open(str(out/name),'wb') as w:
w.setnchannels(1);w.setsampwidth(2);w.setframerate(rate)
w.writeframes(b''.join(struct.pack('<h',int(s/peak*.70*32767)) for s in samples))
print(name,len(samples),'samples')
save('apparatus_place.wav',0)
save('apparatus_remove.wav',1)