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

Bad sample length after normalize

arturomf opened this issue · comments

Hi! i'm using audioinput in a ionic app for iOS. I'm using it to capture audio from device mic and extract features through meyda library. Meyda library needs a 32 bit array so i might normalize signal. The problem is that after normalize, the sample array length is not a power of two (8192). It's 8191 length.

This is my code:

private BUFFER_SIZE_AUDIO_CAPTURE = 16384;

/**
  * Start Capturing Audio from mic
  */
 private startCapture() {
     window.addEventListener('audioinput',  (event: any) => {
         this.process_audio(event.data);
     }, false);
     audioinput.start({
         bufferSize: this.BUFFER_SIZE_AUDIO_CAPTURE,
         streamToWebAudio: false,
         normalize: true,
         format: audioinput.FORMAT.PCM_16BIT,
         channels: audioinput.CHANNELS.MONO,
         sampleRate: audioinput.SAMPLERATE.CD_AUDIO_44100Hz,
     });
     console.log('Start capturing audio...');
 }

 /**
  * Process the audio input
  * @param raw: any
  */
 private process_audio(raw: any) {
     // raw = this.normalizeAudio(raw);
     console.log('Processing audio...', raw.length);
}

Any idea of why is the raw data having bad length?

Thanks!

commented

I think it could be due to this: https://github.com/edimuj/cordova-plugin-audioinput/blob/master/www/audioInputCapture.js#L397

The reason for this code is that on iOS the last sample delivered from native is not a number.

Hi,

I recently got the same problem on iOS, that always the last sample is missing. After debugging the Objective-C code and finding that all samples are present, I finally ended up in "audioInputCapture.js":

In line 378 in function "normalizeToTyped(pcmData)":
The last sample shall be checked for NaN:

if (isNaN(out.subarray[out.length - 1])) {

However this is a bug in so far, that "out.subarray[out.length - 1]" returns an array and is never a Number. So isNaN always is true and the last sample is always removed.

The fix for this bug is:

Line 378:

if (isNaN(out[out.length - 1])) {

Now only the last sample is checked and if it is a Number, it will not be removed.

Applying this change fixed the problem of the last sample always missing and the size not being a power of 2 for me.

With kind regards, Joerg

commented

Thank you so much for this @JoergHansmann. I've pushed your suggested fix to version 1.0.3.