84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
"""Make the planter timer's chime: a short, quiet sound built from soft sine tones.
|
|
|
|
There are three styles to pick from. Each is a 16-bit mono WAV, which the 1.12 client
|
|
plays from the addon folder with PlaySoundFile. Peaks stay around -10 dB so the chime
|
|
is gentler than the game's own alert sounds.
|
|
|
|
usage: py -I make_chime.py <out folder> [style]
|
|
style: bells, wind or marimba. Leave it out to write all three as chime-<style>.wav.
|
|
"""
|
|
import math
|
|
import os
|
|
import struct
|
|
import sys
|
|
import wave
|
|
|
|
RATE = 44100
|
|
PEAK = 0.3 # about -10.5 dBFS
|
|
|
|
# style -> list of (start seconds, frequency, decay seconds, partials as (ratio, level))
|
|
STYLES = {
|
|
# two soft bells going up
|
|
"bells": [
|
|
(0.00, 880.00, 0.55, [(1, 1.0), (2.0, 0.25), (3.01, 0.06)]),
|
|
(0.20, 1318.51, 0.70, [(1, 1.0), (2.0, 0.22), (3.01, 0.05)]),
|
|
],
|
|
# three light wind-chime notes
|
|
"wind": [
|
|
(0.00, 1046.50, 0.60, [(1, 1.0), (2.41, 0.18), (3.9, 0.05)]),
|
|
(0.13, 1318.51, 0.60, [(1, 1.0), (2.41, 0.16), (3.9, 0.04)]),
|
|
(0.26, 1567.98, 0.80, [(1, 1.0), (2.41, 0.14), (3.9, 0.04)]),
|
|
],
|
|
# two warm, low wooden notes
|
|
"marimba": [
|
|
(0.00, 392.00, 0.30, [(1, 1.0), (4.0, 0.30), (9.9, 0.04)]),
|
|
(0.18, 523.25, 0.40, [(1, 1.0), (4.0, 0.28), (9.9, 0.03)]),
|
|
],
|
|
}
|
|
|
|
|
|
def render(notes):
|
|
length = max(start + decay * 4 for start, _, decay, _ in notes)
|
|
n = int(length * RATE)
|
|
out = [0.0] * n
|
|
for start, freq, decay, partials in notes:
|
|
s0 = int(start * RATE)
|
|
for i in range(n - s0):
|
|
t = i / RATE
|
|
attack = min(1.0, t / 0.006) # 6 ms fade-in, no click
|
|
body = 0.0
|
|
for ratio, level in partials:
|
|
# higher partials die away faster, which keeps it soft
|
|
body += level * math.exp(-t * ratio / decay) * math.sin(2 * math.pi * freq * ratio * t)
|
|
out[s0 + i] += attack * body
|
|
top = max(abs(v) for v in out) or 1.0
|
|
fade = int(0.05 * RATE)
|
|
for i in range(fade): # 50 ms fade-out at the very end
|
|
out[n - 1 - i] *= i / fade
|
|
return [v / top * PEAK for v in out]
|
|
|
|
|
|
def write(path, samples):
|
|
with wave.open(path, "wb") as w:
|
|
w.setnchannels(1)
|
|
w.setsampwidth(2)
|
|
w.setframerate(RATE)
|
|
w.writeframes(b"".join(struct.pack("<h", int(v * 32767)) for v in samples))
|
|
|
|
|
|
def main():
|
|
out_dir = sys.argv[1]
|
|
os.makedirs(out_dir, exist_ok=True)
|
|
if len(sys.argv) > 2:
|
|
style = sys.argv[2]
|
|
write(os.path.join(out_dir, "chime.wav"), render(STYLES[style]))
|
|
print("wrote chime.wav (%s)" % style)
|
|
return
|
|
for style, notes in STYLES.items():
|
|
name = "chime-%s.wav" % style
|
|
write(os.path.join(out_dir, name), render(notes))
|
|
print("wrote", name)
|
|
|
|
|
|
main()
|