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

The buffer size in background mode is wrong

nicobytes opened this issue · comments

When the app runs in foreground mode the buffer size is perfect but when the app runs in background mode the buffer size change and is not a power of two.

The video with issue: https://youtu.be/Hw20iCzX8j0

In foreground:

Screen Shot 2020-03-12 at 3 39 36 PM

In background:

Screen Shot 2020-03-12 at 3 39 54 PM

My code is:

ngOnInit() {
    this.platform.ready().then(() => {
      this.backgroundMode.enable();
      window.addEventListener('audioinput', (event: any) => {
        const data = event.data;
        const raw = this.normalizeAudio(data);
        console.log('raw is power two', this.isPowerOfTwo(raw.length));
        console.log('raw length', raw.length);
      }, false);

      this.requestPermissions();
    });
  }

  startCapture() {
    audioinput.start({normalize: false});
    this.isCapturing = true;
  }

  stopCapture() {
    audioinput.stop();
    this.isCapturing = false;
  }

 private isPowerOfTwo(num: number) {
    while (((num % 2) === 0) && num > 1) {
      num /= 2;
    }
    return (num === 1);
  }

  private normalizeAudio(pcmData) {
    return Float32Array.from(pcmData, (i: any) => {
      return parseFloat(i) / 32767.0;
    });
  }

The repo example: https://github.com/nicobytes/audio-input-background-mode/blob/master/src/app/home/home.page.ts

I use my own normalize function because: #103

My environment:

cordova-plugin-audioinput 1.0.2 "Audio Input"
cordova-plugin-background-mode 0.7.3 "BackgroundMode"
cordova-plugin-device 2.0.3 "Device"
cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 4.1.3 "cordova-plugin-ionic-webview"
cordova-plugin-splashscreen 5.0.2 "Splashscreen"
cordova-plugin-statusbar 2.4.2 "StatusBar"
cordova-plugin-whitelist 1.3.3 "Whitelist"


Ionic:

   Ionic CLI                     : 6.2.1 (/Users/nicolas/.nvm/versions/node/v13.9.0/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.0.5
   @angular-devkit/build-angular : 0.803.25
   @angular-devkit/schematics    : 8.3.25
   @angular/cli                  : 8.3.25
   @ionic/angular-toolkit        : 2.2.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : ios 5.1.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.1.3, (and 6 other plugins)

Utility:

   cordova-res                          : not installed
   native-run (update available: 0.3.0) : 0.2.9

System:

   Android SDK Tools : 26.1.1 (/Users/nicolas/Development/android-sdk)
   ios-deploy        : 1.9.4
   ios-sim           : 8.0.2
   NodeJS            : v13.9.0 (/Users/nicolas/.nvm/versions/node/v13.9.0/bin/node)
   npm               : 6.14.2
   OS                : macOS Catalina
   Xcode             : Xcode 11.3.1 Build version 11C504
commented

I honestly don't know why this happens, perhaps the app priority is lowered which intermittently leads to shorter interval. If somebody knows, please let me know.