naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.47k stars 1.09k forks source link

¿dB SPL? #378

Open ghost opened 6 years ago

ghost commented 6 years ago

Hi!, i have a new question, it's about db SPL (Sound Pressure Level).

This is formula for calculate dB SPL:

image https://en.wikipedia.org/wiki/Sound_pressure

But my question starts here.

How can i get RMS from my computer?, my computer uses an audio codec called ALC233 from Realtek, i dont know if i am in the right way but im doing an audiometer app with NAudio and this problem stop me now. Thanks for help and im sorry for my bad english, im trying to write this without Google Translate :P

Thanks!

markheath commented 5 years ago

RMS means "root mean square" so you take the samples (usually floating point in range -1 to 1), square them all, add them together, divide by the number of samples, and then square root.

But as for correct calculation of decibel values, I'm not an expert on that - I'd recommend asking on stack overflow. I've added a very simplistic decibel helper to NAudio you can find here: https://github.com/naudio/NAudio/blob/e359ca0566e9f9b14fee1ba6e0ec17e4482c7844/NAudio/Utils/Decibels.cs

bentorkington commented 4 years ago

Basically, SPL doesn't exist outside of the physical atmosphere - it depends on the audio amplifier, the sensitivity of the loudspeakers, the environment, and the distance from the speakers you measure from.

There's no way to know how much a given audio sample will be amplified ahead of time without calibrating all these variables in your setup. Outside of this, all you can really do is reference 0dB to the highest attainable and call it dB-FS. All values will be negative dB, and will relate to dB SPL by a fixed amount, depending on all the above things.

If, through measurement with a sound level meter, you determine that 0dB FS = 80 dB SPL @ 1m from your source, then you can know that -20 dB FS = 60 dB SPL @1m.

MichaelRW commented 2 years ago

How is scaling done in NAudio?

markheath commented 2 years ago

Not sure what you mean. NAudio uses IEEE floating point samples where +/-1.0 is considered full scale (0dB).

MichaelRW commented 2 years ago

If the volume controls on the PC are not changed, how can the playback level be changed using NAudio?

markheath commented 2 years ago

Ah I see, the simplest is to just put a VolumeSampleProvider into your signal chain and set the volume with the Volume property. It's a multiplier so 0 to mute, 1 for full volume, <1 for quieter and >1 to amplify (but use with caution)