Audio Experiments

While putting together sound effects for a game, I couldn't find or create the right sound. I was looking for a magical "sparkly" sound, kind of like wind chimes. So I wrote a script to make one.

The original script was written in Python, using Pydub. I took a single "ding" sample, then overlaid many copies of it with varying pitch, gain, and temporal offset.

import random

from pydub import AudioSegment


def pitch_shift(sound: AudioSegment, semitones: float) -> AudioSegment:
    return sound._spawn(sound.raw_data, overrides={
        "frame_rate": int(sound.frame_rate * (2.0 ** (semitones / 12.0)))
    }).set_frame_rate(sound.frame_rate)


def generate_sparkle(
    ding: AudioSegment,
    duration_ms: int = 5000,
    num_dings: int = 80,
    min_delay_ms: int = 50,
    max_delay_ms: int = 4000,
    pitch_range: tuple[float, float] = (-5.0, 5.0),
    volume_adjust_db: float = -15.0
) -> AudioSegment:
    output: AudioSegment = AudioSegment.silent(duration=duration_ms)

    ding_adjusted = ding.apply_gain(volume_adjust_db)

    for _ in range(num_dings):
        delay: int = random.randint(min_delay_ms, max_delay_ms)
        pitch: float = random.uniform(pitch_range[0], pitch_range[1])

        ding_shifted: AudioSegment = pitch_shift(ding_adjusted, pitch)
        output = output.overlay(ding_shifted, position=delay)

    return output

I decided to try this in the Godot game engine using GDScript. I wrote a WAV file parser that converts samples to 32bit floats, and recreated the necessary transforms.

This worked, but it was too slow. So I rewrote it as a C++ GDExtension, which was much faster. I've embedded a web version below. My "ding" sound is available here. Load that in or choose your own WAV file, then click Generate.

I also tried building this as a proper audio plugin that I could use in a DAW (Digital Audio Workstation) which works but doesn't sound as good.

Here are the LV2 and VST3 builds. I don't have a Windows build yet, though feel free to send me a message if you're interested in that.