In Steam Audio 2.0 beta 4, we added the ability for developers to use their own HRTF data instead of the HRTF data built into Steam Audio. This allows developers and researchers to experiment with new HRTF data sets and measurement techniques, while retaining the speed and ease of use of Steam Audio. In this blog post, we'll describe how to use the Custom HRTF feature with the Steam Audio C API.
HRTF Representation
When HRTF data is measured, the data are obtained in the form of two Head-Related Impulse Response (HRIR) – one for each ear – for each measured direction. The HRIR is a time-domain digital filter that describes how sound from the measured direction is modified by the listener's head and torso before it reaches the corresponding ear. Below is an example of an HRIR from Steam Audio's data set:
Each HRIR contains a finite number of samples. The time between two samples is equal to the inverse of the sampling rate. For example, at 44100 Hz, Steam Audio's HRIRs contain 200 samples, with about 22.7 microseconds between samples.
For efficiency, Steam Audio converts HRIRs into the frequency domain, using a Fast Fourier Transform (FFT). The Fourier transform of an HRIR is called the Head-Related Transfer Function (HRTF).
Configuring a Custom HRTF
The first step in using a custom HRTF data set with Steam Audio, is to configure the Binaural Renderer object appropriately. Recall that the Binaural Renderer object must be created before creating any Effect objects for performing binaural rendering. Here's how you set up a Binaural Renderer object for a custom HRTF:
// Assume that context and renderingSettings are initialized elsewhere.
IPLContext context = ...;
IPLRenderingSettings renderingSettings = ...;
IPLHrtfParams hrtfParams = {};
hrtfParams.type = IPL_HRTFDATABASETYPE_CUSTOM; // Indicate that we'll use custom HRTF data.
hrtfParams.numHrirSamples = 200; // Our HRIRs are each 200 samples long.
hrtfParams.loadCallback = onLoadHrtf; // Specify our custom HRTF callbacks.
hrtfParams.unloadCallback = onUnloadHrtf;
hrtfParams.lookupCallback = onLookupHrtf;
// Create the Binaural Renderer object.
IPLhandle renderer = nullptr;
iplCreateBinauralRenderer(context, renderingSettings, hrtfParams, &renderer);
The IPLHrtfParams structure is used to tell Steam Audio about your custom HRTF data set, and to specify the various callbacks your application implements. Below is a summary of the relevant members of this structure:
- type This is set to IPL_HRTFDATABASETYPE_CUSTOM to indicate that custom HRTF data will be used.
- numHrirSamples The number of samples in each HRIR in the data set you're using. In the above example, we're telling Steam Audio that our HRIRs are all 200 samples long.
- loadCallback A function that Steam Audio will call to allow the application to preprocess the HRIRs when the application calls iplCreateBinauralRenderer.
- unloadCallback A function that Steam Audio will call to allow the application to free resources acquired during loadCallback, when the the application calls iplDestroyBinauralRenderer.
- lookupCallback A function that Steam Audio will call to query the HRTF data set whenever iplApplyBinauralEffect is called.
Implementing Custom HRTF Callbacks
In this section, we'll look at an example implementation of the three custom HRTF callback functions.
Load Callback
The load callback is called by Steam Audio to give your application a chance to apply a Fourier transform to all of your HRIRs, and carry out any other initialization tasks needed by your HRTF data set or lookup algorithm. Below is an example implementation:
// The following variables are assumed to be declared elsewhere.
int numHrirs = ...;
float** leftEarHrirs = nullptr;
float** rightEarHrirs = nullptr;
IPLComplex** leftEarHrtfs = nullptr;
IPLComplex** rightEarHrtfs = nullptr;
void onLoadHrtf(IPLint32 numSamples, IPLint32 numSpectrumSamples, IPLFFTHelper fftHelper, void* fftHelperData)
{
// (1) Allocate memory for zero-padded HRIR and HRTF arrays.
leftEarHrirs = new float*[numHrirs];
leftEarHrtfs = new IPLComplex*[numHrirs];
for (int j = 0; j < numHrirs; ++j)
{
// (2) Allocate memory for the zero-padded HRIR.
leftEarHrirs[j] = new float[numSamples];
// (3) Zero-pad the HRIR data.
memset(leftEarHrirs[j], 0, numSamples * sizeof(float));
memcpy(leftEarHrirs[j], ..., 200 * sizeof(float));
// (4) Allocate memory for the HRTF.
leftEarHrtfs[j] = new IPLComplex[numSpectrumSamples];
// (5) Transform time-domain HRIRs into frequency-domain HRTFs.
fftHelper(fftHelperData, leftEarHrirs[j], leftEarHrtfs[j]);
}
// Repeat the above code for right-ear HRIRs and HRTFs.
}
The above code shows the processing required for the left-ear HRTFs only; the right-ear HRTFs must be processed in an identical manner. Some important points about the above implementation:
- We first allocate an array of numHrirs real-valued arrays for the HRIRs, and an array of numHrirs complex-valued arrays for the HRTFs.
- For each HRIR in the data set, we allocate a real-valued array containing numSamples samples. The value of numSamples is at least (hrtfParams.numHrirSamples + renderingSettings.frameSize - 1), and may be more. For example, if your HRIR data is 200 samples long, and your audio engine's frame size is 1024 samples, then numSamples is at least (200 + 1024 - 1) = 1223.
- We then zero-pad the HRIRs: populate the
leftEarHrirs[i]array with the actual HRIR data. In our example, the first 200 samples of the zero-padded HRIR contain actual HRIR samples, and the remaining samples are all zero. Note that you cannot skip the zero-padding step, or the resulting audio will be incorrect. - For each HRTF in the data set, we then allocate a complex-valued array containing numSpectrumSamples samples. The value of numSpectrumSamples is (numSamples / 2) + 1, where / represents integer division.
- We use the provided fftHelper function to transform the zero-padded HRIRs into HRTFs. Note that you must pass the provided fftHelperData pointer to the fftHelper function.
Lookup Callback
The lookup callback is called by Steam Audio whenever it needs to apply binaural rendering to a frame of audio data. The bulk of the implementation of this callback will depend on how your HRTF data set is designed, but below is the outline of a typical implementation:
// The following variables are assumed to be declared elsewhere.
int numHrirs = ...;
int numSpectrumSamples = ...;
float** leftEarHrirs = ...;
float** rightEarHrirs = ...;
IPLComplex** leftEarHrtfs = ...;
IPLComplex** rightEarHrtfs = ...;
void onLookupHrtf(float* direction, IPLComplex* leftHrtf, IPLComplex* rightHrtf)
{
// (1) Get the direction from the listener to the source.
float x = direction[0];
float y = direction[1];
float z = direction[2];
// (2) Look up (or calculate) the HRTF for the source direction.
int index = ...;
// (3) Copy the left and right ear HRTFs into the provided buffers.
memcpy(leftHrtf, leftEarHrtfs[index], numSpectrumSamples * sizeof(IPLComplex));
memcpy(rightHrtf, rightEarHrtf[index], numSpectrumSamples * sizeof(IPLComplex));
}
Some important points about the above implementation:
- The (unit-length) direction vector from the listener to the source is passed as the direction parameter.
- Using the direction, we look up a corresponding HRTF from the data set. Note that this is essentially a nearest-neighbor lookup that we're using in this example, but you can implement any other interpolation technique.
- We then copy the left and right ear HRTFs to the buffers provided by the callback. The sizes of the buffers match the numSpectrumSamples parameter passed to the load callback.
Unload Callback
The unload callback is called by Steam Audio to give your application a chance to release all resources acquired during the load callback. Below is an example implementation that frees all memory allocated in the load callback described above.
// The following variables are assumed to be declared elsewhere.
int numHrirs = ...;
float** leftEarHrirs = ...;
float** rightEarHrirs = ...;
IPLComplex** leftEarHrtfs = ...;
IPLComplex** rightEarHrtfs = ...;
void onUnloadHrtf()
{
for (int j = 0; j < numHrirs; ++j)
{
delete[] leftEarHrtfs[j];
delete[] leftEarHrirs[j];
}
delete[] leftEarHrtfs;
delete[] leftEarHrirs;
}
Caveats
Using the above three implementations of the custom HRTF callbacks, you can use Steam Audio with any HRTF data set you like. Note, however, that the following restrictions apply:
- The sampling rate of the HRIRs must match the sampling rate used by the audio engine. It is the application's responsibility to perform any resampling if the rates do not match.
- When calling iplApplyBinauralEffect while using a custom HRTF, the interpolation parameter must be set to IPL_HRTFINTERPOLATION_BILINEAR, even if your custom HRTF callbacks do not implement bilinear (or any other type of) interpolation.
- Custom HRTF data cannot currently be used with Virtual Surround Effect objects or Ambisonics Binaural Effect objects.
We hope that this overview serves as a good starting point for your experiments with custom HRTF data. Let us know what you think!
In an upcoming blog post, we will describe how to use custom HRTF data with the Steam Audio Unity plugin.
