I have a 2d canvas based web audio game which includes spatialised audio sources situated at specific pixel coordinates on the canvas using the web audio api.
Whilst I have successfully managed to locate each audio source precisely on the canvas element using the web audio pannerNode like so:
var canvas = document.getElementById("map");
var context = canvas.getContext("2d");
function audioFileLoader(fileDirectory) {
var soundObj = {};
var playSound = undefined;
var panner = undefined;
var gainNode = undefined;
var getSound = new XMLHttpRequest();
soundObj.fileDirectory = fileDirectory;
getSound.open("GET", soundObj.fileDirectory, true);
getSound.responseType = "arraybuffer";
getSound.onload = function() {
audioContext.decodeAudioData(getSound.response, function(buffer) { soundObj.soundToPlay = buffer;
}); };
getSound.send();
panner = audioContext.createPanner();
panner.panningModel = 'HRTF';
soundObj.position = function(x,y,z) {
panner.setPosition(x,y,z);
};
I am now attempting to upgrade the audio spatialisation using the Resonance Audio Web SDK so I can use its arguably more advanced audio spatialisation characteristics.
How can I define the position of the audio sources on the canvas element in pixels (x,y) with Resonance Audio's setPosition?
I can't seem to work out how to convert the native Resonance Audio scale (meters) into pixel coords on my canvas element. I'm assuming that if I can solve this I then define the size and locations of different audio rooms within the 2d game, which would be very cool.
Thanks.
So, turns out if you get the coords on the canvas in pixels where you want to locate your sources, then use the same units (pixels) to locate and update the position of the listener then all is well. As long as you use the same units for your sources and your listener, then they remain relative to each other and the resonance audio spatialisation works: