carp-dk / carp-dk/flutter-plugins
[noise_meter] What? The computations don't make sense.
- Dominant language
- Dart
- Stars
- 608
- Forks
- 735
- Avg merge
- 1m
- Merged PRs (30d)
- 3
Description
This noise_meter plugin does truly weird things. https://github.com/cph-cachet/flutter-plugins/blob/0a3b5e450755692cb900d70e06630caddf6c4ee9/packages/noise_meter/lib/noise_meter.dart#L13-L27
First it sorts an array (why?) then proceeds to use the largest and the smallest samples (which would probably be close to the same, just with opposite signs), and then takes the mean, which should be close to zero. Then zero is multiplied by a giant number, to then apply the formula of decibel scale on the wrong input. None of this makes sense. Depending on the randomness of the signal, half of the samples produced for "meanDecibel" will be NaN, due to negative mean value.
Looking at the formula for dB scale for audio, you need the RMS energy of the signal:

```
dB = 20 * log10(sqrt(mean(square(audio_frames))))
= 10 * log10(mean(square(audio_frames)))
```
Taking a minimum and a maximum single frame is insensible, and takes O(n*log(n)) time for no reason. If you would be interested in a "mean" volume and a "maximum" volume, then you should time-bin the audio samples and apply the RMS formula on those bins. You just cannot apply the decibel formula on a single sample.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.