I have the below code to play a particular sound loaded into a buffer.
sound1.gainNode = context.createGain();
sound1.source = context.createBufferSource();
sound1.source.buffer = soundBuffers[num];
sound1.source.connect(sound1.gainNode);
sound1.gainNode.connect(context.destination);
sound1.source.looping = true;
sound1.source.start(0);
Here's how I call the change volume method:
<input type="range" min="0" max="100" value="70" id="playBtn1_vol" onchange="changeVolume(this,sound1Gain)" style="display:none">
Here's the change volume method:
function changeVolume = function(element,soundNo){
var volume = element.value;
var fraction = parseInt(element.value) / parseInt(element.max);
// Using an x^2 progression as it gives a better sound than linear.
soundNo.gainNode.gain.value = fraction * fraction;
};
Before I tried to get the volume working using gain, it was playing just fine but now its broken and I can't find what's wrong with it. If anyone could point me in the right direction it would be very much appreciated.
Aucun commentaire:
Enregistrer un commentaire