Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad sample length after normalize #103

Closed
arturomf opened this issue Oct 29, 2019 · 3 comments
Closed

Bad sample length after normalize #103

arturomf opened this issue Oct 29, 2019 · 3 comments
Labels
bug This is an identified bug that should be fixed. help wanted The great thing about open-source is that everyone can contribute!

Comments

@arturomf
Copy link

arturomf commented Oct 29, 2019

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!

@edimuj
Copy link
Owner

edimuj commented Nov 27, 2019

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.

@edimuj edimuj added bug This is an identified bug that should be fixed. help wanted The great thing about open-source is that everyone can contribute! labels Nov 27, 2019
@JoergHansmann
Copy link

JoergHansmann commented Jun 12, 2023

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

edimuj added a commit that referenced this issue Jun 12, 2023
@edimuj
Copy link
Owner

edimuj commented Jun 12, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This is an identified bug that should be fixed. help wanted The great thing about open-source is that everyone can contribute!
Projects
None yet
Development

No branches or pull requests

3 participants