Music and Tech Progress Update 3
Adding audio output to the string simulator by sampling physics data directly
Generating Audio from Physics
The string simulator now produces actual sound. You can try it here.
How It Works
The audio is generated by sampling the actual string displacement from the physics simulation at multiple positions along the string. I'm sampling at positions 0.14, 0.23, 0.31, 0.39, and 0.46 of the string length. These positions are deliberately all on one side of the string to avoid cancellation of even harmonics that would occur with symmetric sampling.
A ScriptProcessorNode runs at 44100 Hz, advancing the simulation by the appropriate number of steps per audio sample. The displacement values at each sample position are averaged to produce each audio output sample.
Real-Time Synchronization
One interesting consequence of this approach: when audio is enabled, it drives the simulation at real-time speed rather than the visual timescale. The audio callback dictates the pace, ensuring the sound matches what you'd actually hear from a string vibrating at these frequencies.
Current Issues
The audio is currently choppy and has a pulsing quality. This is likely due to buffer timing issues with ScriptProcessorNode or the computational cost of running the full simulation inside the audio callback. This is the next thing to fix.
Possible solutions I'm considering:
- Migrating to
AudioWorkletfor more reliable timing - Decoupling the audio generation from the main simulation loop
- Pre-computing simulation steps in larger batches
Try It
You can experiment with the current version at string.neelema.net. Enable audio and drag the forcing point around to hear how the sound changes based on where you're driving the string.