Spatial sounds for javascript

| tags: python, sound, javascript, blind

Statial ("3D") sound greatly enhances games for children who are visually impaired but the audio node in HTML5 doesn't even support panning left and right much less the time delay and filtering of true spatial audio. This post describes a python script I hacked to transform sound files so that they appear to originate from points surrounding the listener.

I used the HRTF Measurements of a KEMAR Dummy-Head Microphone made at MIT in 1994 and freely available on the web. The compact folder in my git repo is the result of unpacking compact.tar.Z. The files contain the impulse response for the left and right ear for a source positioned 1.4 meters away from an anatomically correct head at many different azimuth and elevation angles.

The script uses sox to convert whatever sound format you give it to a single channel sampled at 22050Hz. Then it steps in 5 degree increments from 0 degrees (directly in front) to 180 degrees (directly behind) around the right side. It filters the sound with the left and right ear impulse responses and encodes it into mp3 and ogg format. It then swaps the left and right channels to produce the corresponding file for the left side (negative azimuth angles). I only generate positions in the horizontal plane but other elevations from -40 to 90 degrees are possible with the provided HRTFs.

The github repository is here: You'll need python, numpy, and scipy as well as sox to run the script.

Running the script like this:

python laser.mp3 laser
would read a sound file named laser.mp3 and produce 142 files in a folder named laser.

Given the sound files, we should be able to simulate the sound of an enemy spaceship flyby starting with laser/a-90.ogg and advancing to laser/a90.ogg in whatever steps seem appropriate. I may write a little wrapper script for jsonic to automatically choose the sound file and volume based on angle and distance. Of course, not every game will need all the files; simply choose the ones you need. For example, I doubt we need 5 degree steps, 10 or even 15 degree steps should sound fine.

I'm hopeful we can have some new spatial sound games ready for Maze Day 2011.