Binaural rendering (w Sofa)
Binaural extension provides effects for binaural spatialization.
Binaural extension doc is here.
Binaural spatialization sample
This sample spatialize audio for one listener.
For multilistener samples, please see Asio Extension samples, since they enable multi-output rendering.
[...]
enum:int{helloId = 0, outputId = 1, busId, spatId};
AudiostackContext context;
context.setLicenseKeyFromFile("LICENSE.aslc");
context.createInput(helloId, HelloInput);
context.createOutput(outputId, WindowsCoreAudioOutput,true);
// Output is stereo, because binaural effect produces a stereo output.
context.createBus(busId);
context.createEffect(spatId,busId,BinauralSpatializationAspic);
context.connect(helloId,busId);
context.connect(busId,outputId);
context.play();
float sourcePos[] = {0.0, 0.0, -1.0};
float listenerPos[] = {0.0, 0.0, 1.0};
float listenerRot[] = {0.0, 0.0, 0.0};
context.setParameter("listener/1/position",listenerPos);
char c;
do{
std::cout<<"Press r or l to rotate the listener, press q to exit"<<std::endl;
std::cin>>c;
switch(c){
case 'l':
listenerRot[1]-=10; // Rotate around Y axis (=> azimuth)
context.setParameter("listener/1/rotation",listenerRot);
err(context);
break;
case 'r':
listenerRot[1]+=10;
context.setParameter("listener/1/rotation",listenerRot);
err(context);
break;
}
}while(c!='q');
context.stop();
[...]
Custom HRTF w. SOFA
This sample loads a personalized HRTF file using SOFA and then renders binaural with your own HRTFs.
[...]
enum:int{helloIn=0, audioOut=1, bus, effect};
AudiostackContext context;
context.setLicenseKeyFromFile("LICENSE.aslc");
BinauralExtension::Load(context->impl);
// Register and load HRTFs in context
char hrtfFilename[] = "path/to/myHRTFs.sofa";
context.registerAsset("my_hrtf", 4 /* SofaAsset */);
context.loadAssetFromFile("my_hrtf", hrtfFilename);
context.createInput(helloIn, HelloInput);
context.createBus(bus);
context.createEffect(effect, bus, BinauralSpatializationAspic);
context.createOutput(audioOut, WindowsCoreAudioOutput, true);
context.setParameter("listener/1/subject_name", "my_hrtf"); // Bind registered hrtf to this listener rendering
context.connect(helloIn, bus);
context.connect(bus, audioOut);
context.play();
char c;
do {
std::cin >> c;
// Move source and listener
} while(c!='q');
context.stop();
[...]