Skip to content Skip to sidebar Skip to footer

Use Scriptprocessornode In Iphone Safari

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 cont

Solution 1:

I found an answer by myself. It's need to add a source node to ScriptProcessorNode. Something like this.

bufferNode = context.createBufferSource()
var buffer = context.createBuffer(1, 1024, context.sampleRate)
var  data = buffer.getChannelData(0);
for (var i = 0; i < 2048; i++) 
{
  data[i] = 0;
}
bufferNode.buffer = buffer;
bufferNode.loop = true;

generatorNode = context.createJavaScriptNode(2048, 1, 1);
generatorNode.channelCount = 2;
generatorNode.channelCountMode = "max";
generatorNode.channelInterpretation = "speakers";
generatorNode.onaudioprocess = functiongenerateWhiteNoise(e) 
{
   var output = e.outputBuffer.getChannelData(0);
   console.log("onaudioprocess!");
   for (var i = 0; i < output.length; i++)
   {
     output[i] = ( Math.random() * 2 ) - 1;
   }
}
bufferNode.connect(generatorNode);
generatorNode.connect(context.destination);
bufferNode.noteOn(0);

This code will be work in iOS safari browser.

UPD. Update white noise generation code. I's not my target, just use it for test, but it will be bad, if somebody use my wrong code to generate real white noise.

Solution 2:

After having the same issue I tried the accepted answer here but it didn't work for me. It seems the issue is that when the generatorNode is conntected to the context.destination context.state remains suspended! and adding the context.resume() in the same block has no effect. Adding context.resume() triggered by a button click event works and also triggers the earlier resume! this is still too much of a workaround for my liking but is a workaround that works.

If anyone has any suggestions/ideas for a better workaround to trigger context.resume() or eliminate the need for it at all (like all other browsers) it would be much appreciated.

jsfiddle example is here!

var suspendBtn = document.getElementById("suspendBtn");
var resumeBtn = document.getElementById("resumeBtn");

suspendBtn.onclick = function() {
    context.suspend();
}

resumeBtn.onclick = function() {
    context.resume();
}

var context = newwebkitAudioContext();
var generatorNode = context.createJavaScriptNode(2048, 1, 2);

generatorNode.onaudioprocess = functiongenerateWhiteNoise(e) {
    var output = e.outputBuffer.getChannelData(0);

    for (var i = 0; i < output.length; i++) {
        output[i] = ( Math.random() * 2 ) - 1;
    }
}

// This connects the generatorNode but in a suspended state!
generatorNode.connect(context.destination);

// This has no effect! // seems that it needs to be called from another context/closure
context.resume();

Post a Comment for "Use Scriptprocessornode In Iphone Safari"