I’ve been playing around with the Audio Source component. I have a question about maxDistance. In the reference guide it says “The distance at which the volume of playback falls to zero” but in the API reference it says “The maximum distance from the listener at which audio falloff stops”. Those sound like two different things to me.
I’m bringing this up because I am unable to get an audio source to completely fade to zero volume. Here is what I did:
Modified “Platform Game” tutorial.
Attach audioListener to the player entity.
Create new entity with audio source component located behind player
I can succesfully get the audio to play and fade out to a lower volume. But I am unable to make the audio fade to zero. I have tried a variety of settings within the audio source component.
The audio source component uses the Web Audio API where available to do the positional sound. In particular it uses the PannerNode to calculate the volume at a distance. The PannerNode defaults to “inverse” falloff model which is defined in those docs as this:
Where in our case refDistance is what we call minDistance. So you can see that maxDistance isn’t actually used in the “inverse” falloff. It seems I missed that when I originally wrote the audio code. The maxDistance is only used in the “linear” falloff model. Which isn’t actually accessible through the designer. So with “inverse” falloff, you can never actually reduce the volume completely to zero.
We currently don’t expose the distance method in the Designer though now that you’ve brought it to my attention, I actually think it would be more intuitive to have the default set to “linear” and expose the falloff method in Designer. So I’m going to log that to be fixed.
For now, however, if you want to switch distance method by hand I’ve written a little test app which lets you cycle through “inverse”, “linear” and “exponential”. Press left and right arrows to move the source, and press space to cycle through the types. The code is pretty simple and should be easy to follow. You’ll notice if you switch to linear, you can get the volume to falloff to zero.
Just one thing to note: This will only work with Web Audio API capable browsers. Non-capable browsers will always use the “inverse” algorithm until I can fix this properly.
Oops, I notice that the code link is private. Here is the code:
pc.script.create('falloff_test', function (context) {
// Creates a new Falloff_test instance
var Falloff_test = function (entity) {
this.entity = entity;
};
Falloff_test.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.model = 0;
this.models = ['inverse', 'linear', 'exponential']
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
if (context.keyboard.wasPressed(pc.input.KEY_SPACE)) {
this.toggleMethod();
}
if (context.keyboard.isPressed(pc.input.KEY_LEFT)) {
this.entity.translate(-5*dt, 0, 0);
}
if (context.keyboard.isPressed(pc.input.KEY_RIGHT)) {
this.entity.translate(5*dt, 0, 0);
}
},
toggleMethod: function () {
var channel = this.entity.audiosource.channel;
if (channel.panner) {// Test this in case it isn't a 3D Channel or Web Audio API is not available
// Cycle through the three falloff models: "inverse", "linear", "exponential"
this.model++;
if (this.model > 2) {
this.model = 0;
}
channel.panner.distanceModel = this.models[this.model];
}
}
};
return Falloff_test;
});