Binaural Sound with the Web Audio API

The simulation

Use headphones and click on any point around the person below to choose a direction for the incoming sound. The blue dots are in perpendicular directions relative to the listener. Try different head-related impulse responses (HRIR). Some of them will work better than others, depending on the individual. Note that the simulation has only been tested on Firefox and Chrome! Also, some people get errors with their web audio context having a different sample rate than the HRIR:s*.

  You need headphones for the following simulation!

http://kaistale.com/blog/141226hrir/

The theory

Head-related transfer functions describe the cues we receive that enable us to determine the direction a sound arrives from. We only have two ears. To be able to determine the direction the sound arrives from in 3D, our brain has to use all the information it can.

For example, the sound will often arrive at the other ear with a small delay. Also, there will often be a difference in the sound level at one ear, as compared to the other (especially at high frequencies). But, additionally, there is a ton of information available for our brain to use. Our shoulders reflect sound. Sound reflects and diffracts around our external ears (pinna). As our features, such as the shape of our pinna, are individual, so is the way our brain perceives sound in 3D.

Still, our heads are often similar enough, which enables us to approximate 3D sound by ready made head-related transfer functions. Once we have a description of how sound arrives at our ears from different angles, we can take any sound and play it back from some direction in 3D.

The simulation in this post uses head-related transfer functions from the CIPIC HRTF database. This paper provides some nice additional information about head-related transfer functions.

The source code

The source code is here: https://github.com/kai5z/hrtf-simulation

*) If your web audio context has a different sample rate as compared to the HRIR:s sample rate (44.1 kHz), the audio won’t work. Apparently the sample rate of the context isn’t definable (please correct me if I’m wrong!), so the HRIR should be resampled for it to work.

7 thoughts on “Binaural Sound with the Web Audio API

  1. Rajiv

    Works great on my system. I am wondering if the Web Audio API can be used for noise reduction. Something like the one in Audacity. Would be interested in taking it up?

    Reply
    1. Kai Post author

      Well, here’s my quick opinion. Rudimentary filter-based approaches should work just fine with the filtering features provided by the Web Audio API. More advanced algorithms would require more advanced calculations, which I think would be more practical to do with something like Python.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.