harvest/game/tools/synth_audio.gd
Forrest 4b86f655c4 #30: forced lighting, intro sounds, scarecrow chest glow
- DayNight.forced ("" / "morning" / "night") overrides target(); snap()
  still works. DayNight.morning() is the MORNING phase's look.
  lanterns_dark keeps every lantern at 0.
- synth_audio.gd: ufo_hum (4 s loop), ufo_whine, part_fall, life_flash,
  ufo_zoom, car_door, each on its own seed (existing wavs unchanged).
  FarmAudio cues for those plus door_creak (kenney creak1/2), all in
  LOCAL_CUES.
- PlayerCharacter: a blue core glow on the chest bone (BoneAttachment3D):
  an unshaded gem, an additive halo pulled toward the camera, and a weak
  pulsing OmniLight3D. On for every scarecrow; set_core_glow(on) hides
  it. set_tint skips it; the hit flash overlay and ToonLook leave it alone.
- test_look: forced/lanterns_dark, the intro cues, the glow on both
  scarecrows, hit flash intact, set_core_glow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 21:51:14 -04:00

542 lines
21 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)
## alien_beam.wav a buzzing, warbling ray hum over a high whine and a fizz (an alien ray
## tick; back-to-back plays blend into one hum)
## and the opening intro's (OpeningIntro):
## ufo_hum.wav 4 s loop: the big UFO's low, wobbling, beating drone and a faint warble
## ufo_whine.wav a 2 s spin-up: a whine rising faster and faster, pulsing as it spins
## part_fall.wav a 1.5 s falling whistle, louder as it comes down (the thud is "fall")
## life_flash.wav the blue flash: a crack, a rising zap, a low whomp, and a shimmer of
## sparkles dying away
## ufo_zoom.wav the UFO leaving: a whoosh and a hum whose pitch drops away (doppler)
## car_door.wav a car door slamming: a latch click and a dull metal thunk
## 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)
_rng.seed = 19 # its own seed too
_write("alien_beam.wav", _beam(0.34), false)
# The opening intro's, each on its own seed as well.
_rng.seed = 23
_write("ufo_hum.wav", _hum(4.0), true)
_rng.seed = 29
_write("ufo_whine.wav", _whine(2.0), false)
_rng.seed = 31
_write("part_fall.wav", _whistle(1.5), false)
_rng.seed = 37
_write("life_flash.wav", _life_flash(1.4), false)
_rng.seed = 41
_write("ufo_zoom.wav", _zoom(2.2), false)
_rng.seed = 43
_write("car_door.wav", _door(0.5), 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)
## The big UFO hovering: two buzzy low tones a little apart (so they beat), both wobbling in
## pitch, under a faint high warble, throbbing, over a low rumble. Every cycle and wobble is a
## whole number of times in the loop (the wobbles' phase is integrated exactly), so it's seamless.
func _hum(seconds: float) -> PackedFloat32Array:
var n := int(seconds * RATE)
var rumble := _noise_lowpass(n, 120.0)
var buf := PackedFloat32Array()
buf.resize(n)
# (Hz, wobble Hz, wobble depth, level): 220, 227, and 1320 cycles in 4 s; wobbles of 6 and 20.
var voices := [[55.0, 1.5, 0.03, 1.0], [56.75, 1.5, 0.03, 0.7], [330.0, 5.0, 0.015, 0.12]]
for i in n:
var t := float(i) / RATE
var s := 0.0
for v: Array in voices:
var f: float = v[0]
var rate: float = v[1]
var phase := f * t + f * float(v[2]) * (1.0 - cos(TAU * rate * t)) / (TAU * rate)
if f < 100.0:
for h in range(1, 7):
s += float(v[3]) * sin(TAU * phase * h) / pow(h, 1.2)
else:
s += float(v[3]) * (0.8 + 0.2 * sin(TAU * 0.5 * t)) * sin(TAU * phase)
var throb := 0.75 + 0.25 * sin(TAU * 3.0 * t) # 12 throbs in 4 s
buf[i] = throb * s + 0.35 * rumble[i]
return _normalize(buf, 0.6)
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)
## An alien ray: a buzzy low tone (odd harmonics) warbling fast, a high whine sliding down, and a
## fizz of bright noise. It swells in fast and fades slowly, so plays one ray tick apart (shorter
## than it) overlap into one steady hum while the ray is held.
func _beam(length: float) -> PackedFloat32Array:
var n := int(length * RATE)
var fizz := _noise_lowpass(n, 7000.0)
var fizz_low := _noise_lowpass(n, 2500.0)
var buf := PackedFloat32Array()
buf.resize(n)
var phase := 0.0
var whine := 0.0
for i in n:
var t := float(i) / RATE
var u := float(i) / n
var env := minf(u / 0.08, 1.0) * minf((1.0 - u) / 0.5, 1.0)
phase += 196.0 * (1.0 + 0.06 * sin(TAU * 31.0 * t)) / RATE
whine += lerpf(1900.0, 1500.0, u) * (1.0 + 0.01 * sin(TAU * 13.0 * t)) / RATE
var buzz := 0.0
for h in [1, 3, 5, 7, 9]:
buzz += sin(TAU * phase * h) / h
buf[i] = env * (0.8 * buzz + 0.18 * sin(TAU * whine) + 0.3 * (fizz[i] - fizz_low[i]))
return _normalize(buf, 0.7)
## The UFO spinning up: a whine (and a fifth above it) sweeping up faster and faster from a low
## hum, pulsing quicker as it spins, over a rising rush of air; it ends at its peak.
func _whine(length: float) -> PackedFloat32Array:
var n := int(length * RATE)
var air := _band_sweep(n, 300.0, 3000.0, 0.15)
var buf := PackedFloat32Array()
buf.resize(n)
var phase := 0.0
var spin := 0.0
for i in n:
var u := float(i) / n
var f := 140.0 * pow(1800.0 / 140.0, pow(u, 1.3))
phase += f / RATE
spin += lerpf(3.0, 28.0, u * u) / RATE
var pulse := 1.0 - 0.35 * (0.5 + 0.5 * sin(TAU * spin))
var tone := sin(TAU * phase) + 0.3 * sin(TAU * phase * 2.0) + 0.12 * sin(TAU * phase * 3.0) \
+ 0.4 * sin(TAU * phase * 1.5)
var env := minf(u / 0.08, 1.0) * (0.45 + 0.55 * u) * minf((1.0 - u) / 0.04, 1.0)
buf[i] = env * (pulse * tone + 0.6 * u * air[i])
return _normalize(buf, 0.7)
## A falling whistle: a near-pure tone gliding down (with a breath of air), swelling as it
## comes; it stops dead where the part lands.
func _whistle(length: float) -> PackedFloat32Array:
var n := int(length * RATE)
var air := _noise_lowpass(n, 5000.0)
var air_low := _noise_lowpass(n, 1500.0)
var buf := PackedFloat32Array()
buf.resize(n)
var phase := 0.0
for i in n:
var t := float(i) / RATE
var u := float(i) / n
phase += 1900.0 * pow(620.0 / 1900.0, u) * (1.0 + 0.008 * sin(TAU * 6.0 * t)) / RATE
var env := minf(u / 0.13, 1.0) * (0.4 + 0.6 * u) * minf((1.0 - u) / 0.025, 1.0)
buf[i] = env * (sin(TAU * phase) + 0.15 * sin(TAU * phase * 2.0) + 0.25 * (air[i] - air_low[i]))
return _normalize(buf, 0.6)
## The blue flash that brings the scarecrow to life: a sharp crack, a buzzy zap sweeping up, a
## low whomp under it, and a shimmer of high sparkles that flicker and die away.
func _life_flash(length: float) -> PackedFloat32Array:
var n := int(length * RATE)
var white := _noise_lowpass(n, 9000.0)
var white_low := _noise_lowpass(n, 2000.0)
var buf := PackedFloat32Array()
buf.resize(n)
var zap := 0.0
var whomp := 0.0
for i in n:
var t := float(i) / RATE
zap += (220.0 * pow(8.0, minf(t / 0.2, 1.0))) / RATE
whomp += lerpf(110.0, 55.0, minf(t / 0.4, 1.0)) / RATE
var buzz := 0.0
for h in [1, 3, 5]: # (the 7th would pass the Nyquist limit at the top of the sweep)
buzz += sin(TAU * zap * h) / h
var zap_env := minf(t / 0.01, 1.0) * (1.0 if t < 0.2 else exp(-(t - 0.2) * 8.0))
buf[i] = 0.7 * minf(t / 0.001, 1.0) * (white[i] - white_low[i]) * exp(-t * 30.0) + 0.5 * zap_env * buzz \
+ 0.5 * minf(t / 0.005, 1.0) * sin(TAU * whomp) * exp(-t * 6.0)
for k in 14: # the sparkles
var f := _rng.randf_range(2000.0, 6500.0)
var trem := _rng.randf_range(8.0, 20.0)
var at := _rng.randf_range(0.0, 0.25)
var amp := _rng.randf_range(0.04, 0.08)
for i in range(int(at * RATE), n):
var t := float(i) / RATE - at
var flicker := 0.5 + 0.5 * sin(TAU * trem * t)
buf[i] += amp * minf(t / 0.02, 1.0) * exp(-t * 2.6) * flicker * sin(TAU * f * t)
return _normalize(buf, 0.8)
## The UFO zooming off: a rush of air and a buzzy hum, both dropping in pitch as it goes (fast
## at first, then settling: a doppler fall) and fading away.
func _zoom(length: float) -> PackedFloat32Array:
var n := int(length * RATE)
var air := _band_sweep(n, 2600.0, 350.0, 0.15)
var buf := PackedFloat32Array()
buf.resize(n)
var phase := 0.0
for i in n:
var t := float(i) / RATE
var u := float(i) / n
phase += (260.0 + 690.0 * exp(-u * 4.0)) * (1.0 + 0.02 * sin(TAU * 7.0 * t)) / RATE
var tone := 0.0
for h in [1, 3, 5]:
tone += sin(TAU * phase * h) / h
var env := minf(u / 0.03, 1.0) * pow(1.0 - u, 1.6)
buf[i] = env * (0.5 * tone + 0.8 * air[i])
return _normalize(buf, 0.75)
## A car door slamming: the latch's click (and a second as it catches), then a dull thunk: a low
## thump, a few damped, out-of-tune panel tones, and a puff of low noise.
func _door(length: float) -> PackedFloat32Array:
var n := int(length * RATE)
var click := _noise_lowpass(n, 9000.0)
var click_low := _noise_lowpass(n, 3000.0)
var thud := _noise_lowpass(n, 400.0)
var buf := PackedFloat32Array()
buf.resize(n)
var hit := 0.006 # the thunk starts just after the first click
var panel := [[143.0, 14.0, 0.5], [231.0, 18.0, 0.35], [389.0, 24.0, 0.22], [612.0, 30.0, 0.14], [980.0, 40.0, 0.08]]
var phase := 0.0
for i in n:
var t := float(i) / RATE
var s := 0.35 * minf(t / 0.0005, 1.0) * (click[i] - click_low[i]) * exp(-t / 0.0015)
if t > 0.035:
s += 0.2 * (click[i] - click_low[i]) * exp(-(t - 0.035) / 0.0012)
if t >= hit:
var th := t - hit
phase += lerpf(85.0, 62.0, minf(th / 0.15, 1.0)) / RATE
var attack := minf(th / 0.002, 1.0)
s += attack * sin(TAU * phase) * exp(-th * 16.0)
for p: Array in panel:
s += attack * float(p[2]) * sin(TAU * float(p[0]) * th) * exp(-th * float(p[1]))
s += 0.6 * attack * thud[i] * exp(-th * 25.0)
buf[i] = s
return _normalize(buf, 0.85)
## White noise through a resonant band whose center glides from f0 to f1 (q: lower is narrower).
func _band_sweep(n: int, f0: float, f1: float, q: float) -> PackedFloat32Array:
var buf := PackedFloat32Array()
buf.resize(n)
var low := 0.0
var band := 0.0
for i in n:
var u := float(i) / n
var w := TAU * f0 * pow(f1 / f0, u) / RATE
var high := _rng.randf_range(-1.0, 1.0) - low - q * band
band += w * high
low += w * band
buf[i] = band
return _normalize(buf, 1.0)
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 ""])