I am having a problem trying to get sinewave.js-plugin to play several notes in succession.
Link to the sinewave.js Github page
Here is my code.
scalePlayer = function(){
var command = arguments[0];
if (command == 'play'){
sinewave.setFrequency(261.63); // C4
sinewave.play();
}else if (command == 'pause'){
sinewave.pause();
}else if (command == 'cmajor'){
// C major scale
var scale=[
"130.81",
"146.83",
"164.81",
"174.61",
"196.00",
"220.00",
"246.94",
"261.63"];
s = scale.length;
i=0;
while (i<=s){
setTimeout("sinewave.setFrequency("+parseFloat(scale[i])+")", 1000);
console.log(parseFloat(scale[i]));
i++;
}
}else if (command == 'test'){
setTimeout("sinewave.setFrequency(300)", 1000);
setTimeout("sinewave.setFrequency(500)", 1000);
}
}
The "test" command only plays the first note of the two, and the one that actually goes through the scale frequencies plays nothing, but prints the correct tones from the array.
What I am trying to achieve is that the "cmajor" command would play a scale, one note every 1 seconds.
I feel like the setTimeout is causing problems, and I can’t understand why the "test" command won’t play two different notes.
Try the following:
Because the timeouts are all set in one after the other in the loop without delay (at cpu speed, say a few milliseconds between assignments). So you need to spread them apart as if telling the first one, “wait one second”, the next one, “wait two” and so on.
While testing I also noticed
i <= sresulted in a NaN so I changed it toi < s.