I'm having trouble creating a sequence of sounds using an oscillator with a for loop in p5js. I'm getting my parameters from an ajax request and I verify the data. I can't find any examples of this online, so maybe there's a more library specific way of generating sequences of sounds. My approach that doesn't work goes something like this:
getParams is called upon pushing a button. Nothing goes wrong here...
function getParams(mode){
console.log("trying to get tune...");
var csrftoken = getCookie('csrftoken');
var mode = mode;
$.ajax({
url: window.location.href,
type: "POST",
data : { mode : mode,
csrfmiddlewaretoken : csrftoken,
},
success: function(json){
genTune(json);
},
error : function(xhr, errmsg, err) {
console.log(xhr.status + ": " + xhr.responseText);
},
})
}
genTune is fed data from getParams.
function genTune(json){
playing = true;
var level = json["level"] + 4;
a = json["a"];
b = json["b"];
aFreq = json["a_freq"];
bFreq = json["b_freq"];
for (var i = 0; i < level; i++) {
console.log("i = " + i);
if (i===a){
osc.freq(aFreq * 10);
osc.amp(0.5, 0.05);
osc.start();
osc.stop(0.5);
} else if (i===b){
osc.freq(bFreq * 10);
osc.amp(0.5, 0.05);
osc.start();
osc.stop(0.5);
} else {
osc.freq(NOTE_FREQS[random(NOTE_FREQS.length-1)]);
osc.amp(0.5, 0.05);
osc.start();
osc.stop(0.5);
}
}
}
EDIT: The loop works and the message is iterated now that I've fixed a stupid error, but sound is only played once. Can anyone tell me how to make a sequence of sounds with a loop? Thanks!
To clarify, there are no console errors in Firebug.