简体   繁体   中英

Imprecise output when calculating dBFS value from 16-bit audio sample value using C#

I am calculating a dBFS value from a 16-bit wave file sample (-32768 to +32767) using c# as follows:

int sampleValue = -32700;

double dBFSvalue = 20 * Math.Log10(Math.Abs(sampleValue) / 32768);

But when I try to print the dBFS value, a sampleValue of 32768 results in "0" as it should, but any other value of sampleValue results in "-infinity".

MessageBox.Show($"Result: {dBFSvalue}dBFS");

Is this something to do with the displaying of type Double? How should I convert the number to display it properly in the form of "-60.5 dBFS"?

Thanks.

Replace your second line of code by

double dBFSvalue = 20 * Math.Log10(Math.Abs(sampleValue) / 32768.0);

You need the .0 to calculate it as floating point number. Otherwise it is calculated as integer and the term in the brackets is evaluated as 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM