anarchuser / mic_stream

Flutter plugin to get an infinite audio stream from the microphone
https://pub.dev/packages/mic_stream
GNU General Public License v3.0
100 stars 68 forks source link

getting noise when using Mac built-in microphone #49

Closed xloc closed 1 year ago

xloc commented 1 year ago

Thanks for bringing us a microphone library that works across multiple platforms!

I got a strange problem:

  1. Tested on M2 MacBook Air. When I use the built-in microphone, it collects noise
  2. Tested on M2 MacBook Air. When I connect my Mac to AirPods (it is automatically set as default), I get the signal.
  3. Tested on iPhone. The built-in mic of the iPhone works just fine.

I used the demo recorder app for all the testing. So, there shouldn't be any int16 conversion mistakes.

anarchuser commented 1 year ago

Thanks for bringing this up.

In order to find the source of this issue, can you describe your noise in more detail? I.e., is it white noise on top of the normal audio, or do you get completely distorted values? You can also send me some raw audio data from each microphone.

Aso, for reference, can you please compare the audio from your built-in vs. your AirPods microphones through a different recording tool?

xloc commented 1 year ago

That responds was fast. Thank you so much!

The noise is more of a white noise, and it doesn't seem to relate to the input in any way. No matter how loud I go, the waveform doesn't seem to related to my input pattern.

Let me first share a few snapshots of the demo app. I will record some raw audio and do the comparison later.

This is the noise, from the built-in mic:

This is normal audio, from the AirPods:

anarchuser commented 1 year ago

yes, it looks like white noise.

On another note, when you start recording, the app should print the audio configuration it is recording with; can you check whether this config looks sane? The MacOS part should be able to handle the default example config but since I didn't write that part I cannot be sure.

anarchuser commented 1 year ago

One more question: how many microphones does your MacBook have built in? It may be possible that the app tries to record in mono while MacOS supplies stereo audio.

xloc commented 1 year ago

Thanks for the suggestion! Yes, the config looks the same

AirPods mic
[Optional(0), Optional(44100), Optional(16), Optional(2)]
flutter: Start Listening to the microphone, sample rate is 24000.0, bit depth is 32, bufferSize: 4096

Built-in mic
Optional(0), Optional(44100), Optional(16), Optional(2)]
flutter: Start Listening to the microphone, sample rate is 24000.0, bit depth is 32, bufferSize: 4096

I noticed the sample rate and bit depth are different from what I set. Is that wrong?

xloc commented 1 year ago

I also recorded the stream and made some plots: Archive.zip

The files without extension are the raw binary files. I recorded with sample rate 44100 and ENCODING_PCM_16BIT. The wav files are parsed audio.

In the AirPods audio (which I was thinking is good), although can hear what I said, the sound is also overlapped with a strong white noise. Please see the following spectrogram and wave plots

From AirPods: Unknown

From built-in mic: Unknown-1

anarchuser commented 1 year ago

I'm not too sure about the config parts. I'm not a mac developer, I only merged the mac-native implementation into the repo. From what I can tell, MacOS does not always respect the user-provided configuration and uses its own preferred settings instead. As such, assume that the config that was printed by the example application, i.e., the latter config, to be the real one that the audio was recorded with.

I suggest you look at different configurations for sample rate and bit depth, see how this changes the actual used config, and whether this affects the white noise in any way.

Additionally, examine the audio data as if recorded from 24 kHz and 32 bit depth, or 24 kHz and float bit depth, and see if that makes more sense to you.

anarchuser commented 1 year ago

Also, I won't be able to analyse the audio data in depth until in about two weeks, sorry for that.

xloc commented 1 year ago

No worry. The knowledge you provided is already very helpful to me. I appreciated it.

I still can't figure out why the noise is here. But, you are right. The sample rate and bit depth indeed differ from what I set them to be. I read through the native swift code. In fact, the bit depth and sample rate are never used. AVCaptureAudioDataOutputSampleBufferDelegate doesn't allow the user to config these settings.

However, that doesn't explain why I got white noise when I use the built-in mic. I tested with all possible ways to parse the data. None of them worked.

Instead, I found a different native API AVAudioEngine and it worked. Now I can get valid audio from both built-in and wireless earphones. It looks like this: Unknown-2

Do you want me to submit a PR for it?

anarchuser commented 1 year ago

Looks great! Please open a PR; I'll merge and publish it once I'm back to office. I highly appreciate your patience and efforts; this is what keeps such voluntary open source projects alive!

xloc commented 1 year ago

PR created. I'm also happy to contribute to an open source project to make it better. Thank you for your kind support these days!