singe/util/makeCrackle.py

28 lines
1.1 KiB
Python

# Writes a short looping fire-crackle WAV (filtered noise with random pops) for the test scenes.
# Usage: python3 util/makeCrackle.py testScripts/crackle.wav [seconds]
import sys, wave, struct, random, math
out = sys.argv[1]
seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 2.0
rate = 22050
count = int(rate * seconds)
random.seed(11)
samples = []
low = 0.0
pop = 0.0
for i in range(count):
noise = random.uniform(-1.0, 1.0)
low += (noise - low) * 0.08 # a rumble under the hiss
if random.random() < 0.004: # a pop now and then
pop = random.uniform(0.4, 1.0)
pop *= 0.93
value = 0.25 * low + 0.06 * noise + pop * random.uniform(-1.0, 1.0)
# Fade the ends so the loop point does not click.
edge = min(i, count - 1 - i) / (rate * 0.05)
value *= min(1.0, edge)
samples.append(max(-1.0, min(1.0, value)))
with wave.open(out, 'wb') as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(rate)
w.writeframes(b''.join(struct.pack('<h', int(s * 32767)) for s in samples))
print(out, count, 'samples')