carp-dk / carp-dk/flutter-plugins

[noise_meter] What? The computations don't make sense.

Open
#559 2 comments 0 reactions 1 assignee Claimed by @hoffmatteo View on GitHub
awaiting feedback bugfix
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:
![image](https://user-images.githubusercontent.com/845012/174059826-9a77422b-359e-4e69-8cfe-46585173727a.png)
```
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.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.