Hit test: _band_hit replaces the torch's cone. It hits the enemies in a torch_width (1.6 m) wide band along the gun's line, from the scarecrow's center out to torch_range (4.5 m) past the muzzle, each enemy's hit radius added. The nearest torch_targets with a clear line are hit; _cone_hit and _band_hit share _hit_nearest. torch_arc is gone, and the torch joins MUZZLE_WEAPONS so scripted aim matches the mouse. Flame: TorchFlame (CPUParticles3D jet plus a flickering light) is sized to the band. WeaponModels gains a reusable held-effect pattern for #18: - add_held_effect / lit / douse / advance. - attack() lights the effect for HOLD_TICKS (1.5) tick intervals instead of replaying the kick. - douse() puts it out the moment fire is let go; it then fades for the particle lifetime. - Timers run on the sim clock (ScarecrowCombat.tick), or on physics frames when nothing advances them. That covers the other scarecrow on the partner's machine, which the existing "attack" events light, so there is no protocol change. Sound: a new burned signal and a "burn" cue (synth torch_roar.wav, generated by synth_audio.gd with its own seed) replace the pitchfork's stab sound. Tests (test_weapons): band in, beside, behind and beyond; a melee hugger is burned; held fire over 5 intervals ticks 5 times and stops on release; the flame is lit at every step while held and hidden after release; the replica and attack-event paths; the flame follows the model's yaw. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
329 lines
12 KiB
GDScript
329 lines
12 KiB
GDScript
extends SceneTree
|
|
## Synthesizes the sounds the Kenney CC0 packs don't have (assets/audio/kenney/) into
|
|
## assets/audio/synth/ as 16-bit mono WAV files, deterministically (fixed seeds):
|
|
## day_ambience.wav 24 s loop: soft wind, rustling leaves, birdsong
|
|
## night_ambience.wav 24 s loop: low wind, three crickets, an owl
|
|
## crow_caw_1/2.wav a crow's caw (arrivals), crow_squawk.wav (a crow shot down)
|
|
## truck_engine.wav 2 s loop: the farmers' truck idling / driving
|
|
## truck_horn.wav beep-beep (the farmers are home)
|
|
## pitchfork_whoosh.wav a quick swish through the air (a stab)
|
|
## pumpkin_boom.wav a deep thump, a burst of noise, and a wet splat (a pumpkin bomb)
|
|
## torch_roar.wav a gas jet's flickering roar and hiss with a few crackles (a propane
|
|
## torch tick; back-to-back plays blend into one roar)
|
|
## Loops carry a WAV 'smpl' loop point, so the importer's "Detect From WAV" loops them; every
|
|
## periodic part of a loop has a whole number of cycles in it and filtered noise is warmed up
|
|
## on the loop's own tail, so the seam is silent. Run with the editor closed, then let the
|
|
## editor import the files:
|
|
## godot --headless --path . --script res://game/tools/synth_audio.gd
|
|
|
|
const RATE := 22050
|
|
const OUT := "res://assets/audio/synth/"
|
|
|
|
var _rng := RandomNumberGenerator.new()
|
|
|
|
|
|
func _initialize() -> void:
|
|
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(OUT))
|
|
_rng.seed = 7
|
|
_write("day_ambience.wav", _day(24.0), true)
|
|
_rng.seed = 11
|
|
_write("night_ambience.wav", _night(24.0), true)
|
|
_rng.seed = 3
|
|
_write("crow_caw_1.wav", _caw(0.46, 560.0, 470.0, 1), false)
|
|
_write("crow_caw_2.wav", _caw(0.62, 640.0, 520.0, 2), false)
|
|
_write("crow_squawk.wav", _caw(0.2, 820.0, 610.0, 1), false)
|
|
_rng.seed = 5
|
|
_write("truck_engine.wav", _engine(2.0), true)
|
|
_write("truck_horn.wav", _horn(), false)
|
|
_rng.seed = 13
|
|
_write("pitchfork_whoosh.wav", _whoosh(0.22), false)
|
|
_write("pumpkin_boom.wav", _boom(0.9), false)
|
|
_rng.seed = 17 # its own seed, so adding it changed none of the sounds above
|
|
_write("torch_roar.wav", _roar(0.42), false)
|
|
quit(0)
|
|
|
|
|
|
# --- Pieces -----------------------------------------------------------------------------------
|
|
|
|
## White noise through two one-pole low-passes, warmed up on the buffer's own tail so the start
|
|
## continues the end (seamless when looped).
|
|
func _noise_lowpass(n: int, cutoff: float) -> PackedFloat32Array:
|
|
var white := PackedFloat32Array()
|
|
white.resize(n)
|
|
for i in n:
|
|
white[i] = _rng.randf_range(-1.0, 1.0)
|
|
var a := exp(-TAU * cutoff / RATE)
|
|
var y1 := 0.0
|
|
var y2 := 0.0
|
|
var out := PackedFloat32Array()
|
|
out.resize(n)
|
|
for pass_i in 2:
|
|
for i in n:
|
|
y1 = a * y1 + (1.0 - a) * white[i]
|
|
y2 = a * y2 + (1.0 - a) * y1
|
|
if pass_i == 1:
|
|
out[i] = y2
|
|
return _normalize(out, 1.0)
|
|
|
|
|
|
func _normalize(buf: PackedFloat32Array, peak: float) -> PackedFloat32Array:
|
|
var top := 0.0
|
|
for v in buf:
|
|
top = maxf(top, absf(v))
|
|
if top > 0.0:
|
|
for i in buf.size():
|
|
buf[i] *= peak / top
|
|
return buf
|
|
|
|
|
|
## Adds a tone with a frequency glide and an envelope into buf at t0 (wrapping around the end).
|
|
func _tone(buf: PackedFloat32Array, t0: float, length: float, f0: float, f1: float, amp: float,
|
|
vibrato: float = 0.0, harmonics: PackedFloat32Array = PackedFloat32Array([1.0])) -> void:
|
|
var n := buf.size()
|
|
var count := int(length * RATE)
|
|
var phase := 0.0
|
|
var start := int(t0 * RATE)
|
|
for k in count:
|
|
var u := float(k) / count
|
|
var f := lerpf(f0, f1, u) * (1.0 + vibrato * sin(TAU * 6.0 * k / RATE))
|
|
phase += f / RATE
|
|
var env := sin(PI * u)
|
|
env *= env
|
|
var s := 0.0
|
|
for h in harmonics.size():
|
|
s += harmonics[h] * sin(TAU * phase * (h + 1))
|
|
buf[(start + k) % n] += amp * env * s
|
|
|
|
|
|
# --- Loops ------------------------------------------------------------------------------------
|
|
|
|
func _day(seconds: float) -> PackedFloat32Array:
|
|
var n := int(seconds * RATE)
|
|
var wind := _noise_lowpass(n, 480.0)
|
|
var leaves := _noise_lowpass(n, 2600.0)
|
|
var leaves_low := _noise_lowpass(n, 900.0)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
for i in n:
|
|
var t := float(i) / n # 0..1 over the loop: whole cycles only
|
|
var gust := 0.55 + 0.45 * (0.5 + 0.5 * sin(TAU * 2.0 * t + 0.7)) * (0.7 + 0.3 * sin(TAU * 5.0 * t))
|
|
var rustle := 0.3 + 0.7 * pow(0.5 + 0.5 * sin(TAU * 7.0 * t + 1.9), 3.0)
|
|
buf[i] = 0.07 * gust * wind[i] + 0.025 * rustle * (leaves[i] - leaves_low[i])
|
|
var t := 0.6
|
|
while t < seconds:
|
|
_bird(buf, t)
|
|
t += _rng.randf_range(1.6, 3.8)
|
|
return _normalize(buf, 0.5)
|
|
|
|
|
|
func _bird(buf: PackedFloat32Array, t0: float) -> void:
|
|
var amp := _rng.randf_range(0.05, 0.12)
|
|
match _rng.randi_range(0, 2):
|
|
0: # rising chirps
|
|
var f := _rng.randf_range(2800.0, 3400.0)
|
|
for k in _rng.randi_range(3, 5):
|
|
_tone(buf, t0 + k * 0.11, 0.07, f, f * 1.35, amp)
|
|
1: # two-note whistle
|
|
var f := _rng.randf_range(2200.0, 2700.0)
|
|
_tone(buf, t0, 0.2, f, f * 1.02, amp, 0.01)
|
|
_tone(buf, t0 + 0.26, 0.24, f * 0.8, f * 0.78, amp, 0.01)
|
|
_: # trill
|
|
var f := _rng.randf_range(4600.0, 5400.0)
|
|
for k in 10:
|
|
_tone(buf, t0 + k * 0.04, 0.025, f, f * 0.95, amp * 0.7)
|
|
|
|
|
|
func _night(seconds: float) -> PackedFloat32Array:
|
|
var n := int(seconds * RATE)
|
|
var wind := _noise_lowpass(n, 320.0)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
for i in n:
|
|
var t := float(i) / n
|
|
buf[i] = 0.05 * (0.6 + 0.4 * sin(TAU * 3.0 * t + 0.3)) * wind[i]
|
|
# Crickets: (carrier Hz, chirps per loop, level). A chirp is 3 short pulses.
|
|
var crickets := [[4400.0, 50, 0.07], [4800.0, 39, 0.05], [5150.0, 44, 0.04]]
|
|
for c: Array in crickets:
|
|
var period := seconds / int(c[1])
|
|
var offset := _rng.randf_range(0.0, period)
|
|
for k in int(c[1]):
|
|
var swell := 0.7 + 0.3 * sin(TAU * 2.0 * k / int(c[1]) + offset)
|
|
for p in 3:
|
|
_tone(buf, offset + k * period + p * 0.032, 0.016, c[0], c[0] * 0.99, c[2] * swell)
|
|
for at in [6.0, 17.5]: # the owl: hoo ... hoo-hoo
|
|
var h := PackedFloat32Array([1.0, 0.18, 0.05])
|
|
_tone(buf, at, 0.42, 410.0, 380.0, 0.08, 0.004, h)
|
|
_tone(buf, at + 0.75, 0.22, 400.0, 385.0, 0.065, 0.004, h)
|
|
_tone(buf, at + 1.05, 0.38, 395.0, 360.0, 0.07, 0.004, h)
|
|
return _normalize(buf, 0.45)
|
|
|
|
|
|
func _engine(seconds: float) -> PackedFloat32Array:
|
|
var n := int(seconds * RATE)
|
|
var rumble := _noise_lowpass(n, 180.0)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
var f := 42.0 # 84 cycles in 2 s
|
|
for i in n:
|
|
var t := float(i) / RATE
|
|
var s := 0.0
|
|
for h in range(1, 9):
|
|
s += sin(TAU * f * h * t) / h
|
|
var throb := 0.8 + 0.2 * sin(TAU * 7.0 * t) # 14 cycles in 2 s
|
|
buf[i] = throb * (0.5 * s + 0.6 * rumble[i])
|
|
return _normalize(buf, 0.6)
|
|
|
|
|
|
# --- One-shots --------------------------------------------------------------------------------
|
|
|
|
## A crow: a harmonic-rich glide shaped by two formants, roughened by fast random amplitude
|
|
## wobble, with breathy noise; caws = how many in a row.
|
|
func _caw(length: float, f_start: float, f_end: float, caws: int) -> PackedFloat32Array:
|
|
var one := int(length * RATE)
|
|
var gap := int(0.09 * RATE)
|
|
var n := one * caws + gap * (caws - 1)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
var breath := _noise_lowpass(n, 3000.0)
|
|
var breath_low := _noise_lowpass(n, 900.0)
|
|
for c in caws:
|
|
var start := c * (one + gap)
|
|
var phase := 0.0
|
|
var rough := 1.0
|
|
var shift := 1.0 - 0.06 * c
|
|
for k in one:
|
|
var u := float(k) / one
|
|
var f := lerpf(f_start, f_end, pow(u, 0.7)) * shift
|
|
phase += f / RATE
|
|
if k % 160 == 0:
|
|
rough = _rng.randf_range(0.45, 1.0)
|
|
var s := 0.0
|
|
for h in range(1, 13):
|
|
var hf := f * h
|
|
var formant := exp(-pow((hf - 1350.0) / 420.0, 2.0)) + 0.6 * exp(-pow((hf - 2500.0) / 600.0, 2.0)) + 0.12
|
|
s += formant / pow(h, 0.6) * sin(TAU * phase * h)
|
|
var env := minf(u / 0.04, 1.0) * (1.0 if u < 0.6 else exp(-(u - 0.6) * 9.0))
|
|
buf[start + k] = env * (rough * s + 0.5 * (breath[start + k] - breath_low[start + k]))
|
|
return _normalize(buf, 0.8)
|
|
|
|
|
|
## Noise swept through a resonant band that rises then falls, swelling and fading.
|
|
func _whoosh(length: float) -> PackedFloat32Array:
|
|
var n := int(length * RATE)
|
|
var white := PackedFloat32Array()
|
|
white.resize(n)
|
|
for i in n:
|
|
white[i] = _rng.randf_range(-1.0, 1.0)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
var low := 0.0
|
|
var band := 0.0
|
|
for i in n:
|
|
var u := float(i) / n
|
|
var f := lerpf(500.0, 2400.0, sin(PI * pow(u, 0.7))) # the band's center
|
|
var q := 0.12
|
|
var w := TAU * f / RATE
|
|
# State-variable band-pass.
|
|
var high := white[i] - low - q * band
|
|
band += w * high
|
|
low += w * band
|
|
buf[i] = band * pow(sin(PI * u), 1.5)
|
|
return _normalize(buf, 0.7)
|
|
|
|
|
|
## A pumpkin bursting: a low sine thump dropping in pitch, a burst of low-passed noise, and a
|
|
## short splat of brighter noise just after.
|
|
func _boom(length: float) -> PackedFloat32Array:
|
|
var n := int(length * RATE)
|
|
var rumble := _noise_lowpass(n, 260.0)
|
|
var splat := _noise_lowpass(n, 2200.0)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
var phase := 0.0
|
|
for i in n:
|
|
var t := float(i) / RATE
|
|
phase += lerpf(95.0, 38.0, minf(t / 0.35, 1.0)) / RATE
|
|
var attack := minf(t / 0.006, 1.0)
|
|
var thump := sin(TAU * phase) * exp(-t * 7.0)
|
|
var crack := rumble[i] * exp(-t * 5.5)
|
|
var wet := splat[i] * exp(-maxf(t - 0.03, 0.0) * 16.0) * (1.0 if t > 0.03 else 0.0)
|
|
buf[i] = attack * (1.0 * thump + 0.9 * crack + 0.35 * wet)
|
|
return _normalize(buf, 0.85)
|
|
|
|
|
|
## A propane torch: a low roar of noise and a breathy hiss, flickering, with a few crackles. It
|
|
## swells in fast and dies away slowly, so plays one torch tick apart (shorter than it) overlap
|
|
## into one steady roar while the torch is held.
|
|
func _roar(length: float) -> PackedFloat32Array:
|
|
var n := int(length * RATE)
|
|
var roar := _noise_lowpass(n, 650.0)
|
|
var hiss := _noise_lowpass(n, 5200.0)
|
|
var hiss_low := _noise_lowpass(n, 1700.0)
|
|
var flicker := _noise_lowpass(n, 16.0)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
for i in n:
|
|
var u := float(i) / n
|
|
var env := minf(u / 0.12, 1.0) * minf((1.0 - u) / 0.45, 1.0)
|
|
buf[i] = env * (0.75 + 0.25 * flicker[i]) * (roar[i] + 0.4 * (hiss[i] - hiss_low[i]))
|
|
for k in 7: # crackles: tiny sharp clicks
|
|
var at := _rng.randi_range(int(0.08 * n), int(0.75 * n))
|
|
var amp := _rng.randf_range(0.25, 0.55)
|
|
for j in int(0.005 * RATE):
|
|
buf[at + j] += amp * exp(-float(j) / (0.0012 * RATE)) * _rng.randf_range(-1.0, 1.0)
|
|
return _normalize(buf, 0.7)
|
|
|
|
|
|
func _horn() -> PackedFloat32Array:
|
|
var n := int(0.9 * RATE)
|
|
var buf := PackedFloat32Array()
|
|
buf.resize(n)
|
|
for beep in [[0.0, 0.3], [0.42, 0.34]]:
|
|
var start := int(beep[0] * RATE)
|
|
var count := int(beep[1] * RATE)
|
|
for k in count:
|
|
var t := float(k) / RATE
|
|
var env := minf(minf(t / 0.012, 1.0), minf((beep[1] - t) / 0.03, 1.0))
|
|
var s := 0.0
|
|
for f in [410.0, 520.0]:
|
|
for h in [1, 3, 5, 7]:
|
|
s += sin(TAU * f * h * t) / h
|
|
buf[start + k] = env * s
|
|
return _normalize(buf, 0.7)
|
|
|
|
|
|
# --- WAV --------------------------------------------------------------------------------------
|
|
|
|
func _write(file: String, buf: PackedFloat32Array, loop: bool) -> void:
|
|
var frames := buf.size()
|
|
var data := PackedByteArray()
|
|
data.resize(frames * 2)
|
|
for i in frames:
|
|
data.encode_s16(i * 2, clampi(roundi(buf[i] * 32767.0), -32768, 32767))
|
|
var smpl := PackedByteArray()
|
|
if loop:
|
|
smpl.resize(60)
|
|
var fields := [0, 0, int(1e9 / RATE), 60, 0, 0, 0, 1, 0, 0, 0, 0, frames - 1, 0, 0]
|
|
for i in fields.size():
|
|
smpl.encode_u32(i * 4, fields[i])
|
|
var f := FileAccess.open(OUT + file, FileAccess.WRITE)
|
|
f.store_buffer("RIFF".to_ascii_buffer())
|
|
f.store_32(4 + (8 + 16) + ((8 + smpl.size()) if loop else 0) + 8 + data.size())
|
|
f.store_buffer("WAVE".to_ascii_buffer())
|
|
f.store_buffer("fmt ".to_ascii_buffer())
|
|
f.store_32(16)
|
|
f.store_16(1) # PCM
|
|
f.store_16(1) # mono
|
|
f.store_32(RATE)
|
|
f.store_32(RATE * 2)
|
|
f.store_16(2)
|
|
f.store_16(16)
|
|
if loop:
|
|
f.store_buffer("smpl".to_ascii_buffer())
|
|
f.store_32(smpl.size())
|
|
f.store_buffer(smpl)
|
|
f.store_buffer("data".to_ascii_buffer())
|
|
f.store_32(data.size())
|
|
f.store_buffer(data)
|
|
f.close()
|
|
print("synth_audio: %s %.2f s%s" % [file, float(frames) / RATE, " (loop)" if loop else ""])
|