edimuj / cordova-plugin-audioinput

This iOS/Android Cordova/PhoneGap plugin enables audio capture from the device microphone, by in near real-time forwarding audio to the web layer of your application. A typical usage scenario for this plugin would be to use the captured audio as source for a web audio node chain, where it then can be analyzed, manipulated and/or played.

Home Page:https://github.com/edimuj/app-audioinput-demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Audio buffer is not passed to ScriptProcessorNode after iPhone is locked and unlocked

arpitvjaiswal opened this issue · comments

Hi,

Steps to reproduce:

  1. In audioinput-demo's www/js/webaudio-demo.js, replace filterNode with a processor node.
var processorNode;
/**
 * Start capturing audio.
 */
var startCapture = function() {
    try {
        if (window.audioinput && !window.audioinput.isCapturing()) {
            getMicrophonePermission(function() { // See utils.js
                // Connect the audioinput to the speaker(s) in order to hear the captured sound.
                // We're using a filter here to avoid too much feedback looping...
                // Start with default values and let the plugin handle conversion from raw data to web audio.

                consoleMessage("Microphone input starting...");
                window.audioinput.start({
                    streamToWebAudio: true
                });
                consoleMessage("Microphone input started!");

                // UPDATED CODE
                processorNode = audioinput.getAudioContext().createScriptProcessor(4096, 1, 1);
                processorNode.addEventListener('audioprocess', function(event) {
                    var inputBuffer = event.inputBuffer;
                    console.log('New Buffer', inputBuffer);
                });

                audioinput.connect(processorNode);
                processorNode.connect(audioinput.getAudioContext().destination);

                consoleMessage("Capturing audio!");

                disableStartButton();
            }, function(deniedMsg) {
                consoleMessage(deniedMsg);
            }, function(errorMsg) {
                consoleMessage(errorMsg);
            });
        }
        else {
            alert("Already capturing!");
        }
    }
    catch (ex) {
        alert("startCapture exception: " + ex);
    }
};


/**
 * Stop capturing audio.
 */
var stopCapture = function() {

    if (window.audioinput && window.audioinput.isCapturing()) {
        window.audioinput.stop();
        if (filterNode) filterNode.disconnect();
        if (processorNode) processorNode.disconnect();
        window.audioinput.disconnect();
        disableStopButton();
    }

    consoleMessage("Stopped!");
};
  1. Build the sample for ios afterwards and run it on iOS device.
  2. Connect to safari browser console, and run Web Audio demo. On start, the browser console shows messages on receiving audio buffer.
[Log] Microphone input started! (cordova.js, line 1413)
[Log] Capturing audio! (cordova.js, line 1413)
[Log] New Buffer  AudioBuffer {length: 4096, duration: 0.08533333333333333, sampleRate: 48000,} (cordova.js, line 1413)
AudioBuffer {length: 4096, duration: 0.08533333333333333, sampleRate: 48000, numberOfChannels: 1, gain: 1,}
  1. Lock the phone and unlock after a couple seconds. Run the Web Audio demo again.

Expectation - Same logs as before
Observation - The processor's onaudioprocess callback is not called. Log shows as following:

[Log] Microphone input starting... (cordova.js, line 1413)
[Log] Microphone input started! (cordova.js, line 1413)
[Log] Capturing audio! (cordova.js, line 1413)
commented

Since this is on iOS, I think think there is an issue with the webaudio audiocontext is disabled when the device is locked, and it isn't automatically enabled again when it is unlocked. If I remeber correctly one must call resume on the audiocontext from within a user interaction (tap).