comparison tools/cut_sounds.py @ 32:85d23328a215 0.10.0

added intro music, logic for browser audio triggering on splash screen
author ark
date 2026-10-20 18:39 +0200
parents 2d46729a022e
children a464bd43b76c
comparison
equal deleted inserted replaced
31:d1ae50439686 32:85d23328a215
10 10
11 each sound is filtered against low rumble, trimmed to the voice with a short 11 each sound is filtered against low rumble, trimmed to the voice with a short
12 margin, faded in and out, brought to the same peak level and written as mono 12 margin, faded in and out, brought to the same peak level and written as mono
13 mp3. a recording is split at its longest pauses: as many as it takes to give 13 mp3. a recording is split at its longest pauses: as many as it takes to give
14 the listed number of variants. pauses inside one sound are left alone. 14 the listed number of variants. pauses inside one sound are left alone.
15
16 a recording listed in SKIP loses its start up to the given time before any of
17 this. a sound listed in LOOP_GAP is meant to play in a loop: that much silence
18 is added after its end, so the loop has a pause where it starts again.
15 19
16 requires ffmpeg with libmp3lame, which the build does not need: the build 20 requires ffmpeg with libmp3lame, which the build does not need: the build
17 reads art/sounds/ only. run this script by hand after a recording changes. 21 reads art/sounds/ only. run this script by hand after a recording changes.
18 """ 22 """
19 23
33 "t1_shot": 6, 37 "t1_shot": 6,
34 "t2_shot": 4, 38 "t2_shot": 4,
35 "t3_shot": 7, 39 "t3_shot": 7,
36 "monster_enter": 3, 40 "monster_enter": 3,
37 "monster_die": 4, 41 "monster_die": 4,
42 }
43
44 # seconds cut off the start of a recording: intro has a burp before the music
45 SKIP = {
46 "intro": 3.0,
47 }
48
49 # seconds of silence after a looping sound. to adjust by ear
50 LOOP_GAP = {
51 "intro": 0.3,
38 } 52 }
39 53
40 RATE = 44100 54 RATE = 44100
41 BITRATE = "64k" 55 BITRATE = "64k"
42 # remove rumble below this frequency, Hz 56 # remove rumble below this frequency, Hz
146 160
147 161
148 def process(path): 162 def process(path):
149 name = path.stem 163 name = path.stem
150 count = VARIANTS.get(name, 1) 164 count = VARIANTS.get(name, 1)
151 samples = decode(path) 165 skip = int(SKIP.get(name, 0) * RATE)
166 samples = decode(path)[skip:]
152 runs = voice(samples) 167 runs = voice(samples)
153 if not runs: 168 if not runs:
154 sys.exit(f"{name}: no voice found") 169 sys.exit(f"{name}: no voice found")
155 sounds = split(runs, count, name) if count > 1 else [(runs[0][0], runs[-1][1])] 170 sounds = split(runs, count, name) if count > 1 else [(runs[0][0], runs[-1][1])]
156 for old in list(SOUNDS.glob(f"{name}.mp3")) + list(SOUNDS.glob(f"{name}_[0-9]*.mp3")): 171 for old in list(SOUNDS.glob(f"{name}.mp3")) + list(SOUNDS.glob(f"{name}_[0-9]*.mp3")):
157 old.unlink() 172 old.unlink()
158 for i, (start, end) in enumerate(sounds, start=1): 173 for i, (start, end) in enumerate(sounds, start=1):
159 dst = SOUNDS / (f"{name}_{i}.mp3" if count > 1 else f"{name}.mp3") 174 dst = SOUNDS / (f"{name}_{i}.mp3" if count > 1 else f"{name}.mp3")
160 encode(shape(samples, start, end), dst) 175 sound = shape(samples, start, end)
161 print(f" {dst.name}: {start / RATE:.2f}-{end / RATE:.2f} s of the recording," 176 sound.extend([0] * int(LOOP_GAP.get(name, 0) * RATE))
162 f" {(end - start) / RATE:.2f} s") 177 encode(sound, dst)
178 print(f" {dst.name}: {(skip + start) / RATE:.2f}-{(skip + end) / RATE:.2f} s of the"
179 f" recording, {(end - start) / RATE:.2f} s")
163 180
164 181
165 def main(names): 182 def main(names):
166 # any format ffmpeg reads 183 # any format ffmpeg reads
167 paths = sorted(p for p in SOURCES.iterdir() if p.is_file() and not p.name.startswith(".")) 184 paths = sorted(p for p in SOURCES.iterdir() if p.is_file() and not p.name.startswith("."))