HRTF binaural 3D positional sounds for .NET

Working on a VR app it's really not about rendering graphics only: if you want to feel inside a scene, you'll also need convincing sounds.

The only way a sound can be "convincing" in VR, is by being detectable in the space, so that users can clearly understand the position of a sound source.

The most convincing way of achieving this result is either surround yourself with physical speakers (not so cheap) or using a pair of headphones and use head-related transfer functions when rendering virtual sounds.

In real life sounds hit our ears with slightly different timing and intensity, and this is the same happening using this technique: a single channel audio will be cloned, each one will be modified according to the position in space relative to a listener and surrounding environment then sent to a different channel depending on the target ear.

It really is not different from the concept of stereoscopic rendering for graphics media, and it seems like VR and AR really evolves around these tricks for the human senses.

Even though this may seem a simple concept, there are limited libraries (that I know of) that offer real binaural audio rendering.

One is OpenAL Soft (source here) a re-implementation of OpenAL with added support of hrtfs, and just recently, with the Microsoft Hololens incoming, brand new support for XAudio 2.9 in Windows 10.

I am currently building a prototype for my app, to focus more on the idea and quickly develop a working product: to do so I chose C# as my choice language: this quickly became a problem when dealing with the sound part of it.

This led me, of course, to choose the XAudio solution over OpenAL: but the problem was still there, as the library itself is c++ only and SharpDX, the best managed DirectX wrapper currently available is not supporting this effect as of now (there's an open issue though, hope this will be included soon).

A project rewrite was impossible to do, so I started developing a small library to expose a minimalistic API on top of XAudio2 HRTF xAPO, and I'm releasing today a first alpha build that I'm currently using in some of my personal projects.

It's really simple to use:

PositionalSound sound = new PositionalSound("path_to/sound_file.wav");
sound.Play();

//Sets position, no need to stop or pause
sound.X = 1.0f;
sound.Y = 10;
sound.Z = -2.0f;
sound.SetPosition(1.0f, 10, -2.0f);

//Changes volume
sound.Volume = 0.5f;

//Sets an environment this sound will react to
//choose between Small, Medium, Large or Outdoors
sound.Environment = HrtfEnvironment.Medium;

Pretty much what I needed to bootstrap my idea.

There's also a small test project to play around with the library to see the code in action.

Test project in action

If someone else needs something like this, it's open sourced and free to use in my github repository.

There's a lot of work to do to consider this small lib solid and ready to use in production, but I still think it's a good start: as soon as I have time I think I'll implement a native wav loader (to get rid of MFC dependencies), I'll rewrite exceptions and error handling as well as custom decay and directional sound sources.

Contributions are more than welcome and really appreciated!