I am using XAudioJS (http://ift.tt/1RgILaf) for a current project and I need to do the following:
-
On mousedown, a sine wave should be played. I did this by generating a sine wave in the background and when the event is fired, the volume is set to 1 or 0 accordingly
-
When a user clicks on a div, a sequence of samples is being played. Other userinput is paused during this time
I could manage to do both independently but I can't bring them both together. When the user clicks on the div, the audio sequence is played but first a bit of the sine wave which was muted was also played
I suspect that it has to do with the buffer of XAudioJS. It is not cleared and therefore plays parts of the sine wave which is still in the buffer. I can't figure out a way though, how to clear it before playing the sequence.
Also I think I don't really understand what buffer-low and buffer-high for XAudioJS exactly means or which values are better for performance and which are not.
Here is my underRunCallback function I gave to XAudioJS:
var sample = []; // samples for the sample sequence
var samplePos = 0; // counter for samples
var sampleRateUsed = 8000;
var frequencyUsed = 550; //frequency for sine wave
var frequencyCounter = 0;
var counterIncrementAmount = Math.PI * 2 * frequencyUsed / sampleRateUsed;
function underRunCallback(samplesToGenerate) {
//Generate audio on the fly=>
if (samplesToGenerate == 0) {
return [];
}
var tempBuffer = []; // buffer that is returned
// create sine wave on the fly:
if(!isPlaying){
while (samplesToGenerate--) {
tempBuffer.push(Math.sin(frequencyCounter));
frequencyCounter += counterIncrementAmount;
}
return tempBuffer;
}
// NEED TO CLEAR BUFFER HERE
// play sequence of samples:
samplesToGenerate = Math.min(samplesToGenerate, sample.length - samplePos);
if(samplePos == sample.length){ // if all samples of sequence played
input_paused = false; // allow new userinput
isPlaying = false;
xaudioHandle.changeVolume(0); // mute audio
}
if (samplesToGenerate > 0) { // if new slice of samples is needed
tempBuffer = sample.slice(samplePos, samplePos + samplesToGenerate);
samplePos += samplesToGenerate;
return tempBuffer;
} else {
isPlayingB = false;
return [];
}
}
Aucun commentaire:
Enregistrer un commentaire