I'm new in html5 and I want to use a ScriptProcessorNode to generate a sounds. My problem is that this code don't work in iPhone safari. But it works in safari on desktop.
var context; var isPlaying; var generatorNode; var isNeedShowAlert;
function myButtonClick(button)
{
isNeedShowAlert = true;
if (isPlaying)
{
isPlaying = false;
console.log("Stop!");
generatorNode.disconnect();
}
else
{
alert("Play!");
isPlaying = true;
console.log("Play!");
context = new webkitAudioContext();
generatorNode = context.createJavaScriptNode(2048, 1, 2);
generatorNode.onaudioprocess = function (e)
{
console.log("onaudioprocess!");
$("body").append("buffering<br/>");
var output = e.outputBuffer.getChannelData(0);
if (isNeedShowAlert)
{
isNeedShowAlert = false;
console.log("Length "+ output.length);
alert("Length "+ output.length);
}
for (var i = 0; i < output.length; i++)
{
output[i] = Math.random();
}
}
generatorNode.connect(context.destination);
alert("Node Connected");
}
}
Looks like onaudioprocess never called. here people write that ScriptProcessorNode can be destroyed be garbage collector, but in my case it is global variable. I'm try a lot and start to think, that trere is no way to use ScriptProcessorNode in iPhone Safari. Can someone do id?
UPD. But if i use AudioBufferSourceNode, it works.
bufferNode = context.createBufferSource()
var buffer = context.createBuffer(1, 1024, context.sampleRate)
var data = buffer.getChannelData(0);
for (var i = 0; i < 1024; i++)
{
data[i] = Math.random();
}
bufferNode.buffer = buffer;
bufferNode.loop = true;
bufferNode.connect(context.destination);
bufferNode.noteOn(0);
Looks like problem is specifically in ScriptProcessorNode and its onaudioprocess method.