Am I doing the right thing to convert decibel from -120 - 0 to 0 - 120

后端 未结 5 1811
忘了有多久
忘了有多久 2020-12-23 23:47

I will like to measure the sound volume of the surrounding, not too sure if I am doing the right thing.

I will like to create a VU meter of a range of 0(quiet) to 12

5条回答
  •  一整个雨季
    2020-12-24 00:26

    The formula for converting a linear amplitude to decibels when you want to use 1.0 as your reference (for 0db), is

    20 * log10(amp);
    

    So I'm not sure about the intent from looking at your code, but you probably want

    float db = 20 * log10([recorder averagePowerForChannel:0]);
    

    This will go from -infinity at an amplitude of zero, to 0db at an amplitude of 1. If you really need it to go up to between 0 and 120 you can add 120 and use a max function at zero.

    So, after the above line:

    db += 120;
    db = db < 0 ? 0 : db;
    

    The formula you are using appears to be the formula for converting DB to amp, which I think is the opposite of what you want.

    Edit: I reread and it seems you may already have the decibel value.

    If this is the case, just don't convert to amplitude and add 120.

    So Change

    double peakPowerForChannel = pow(10, (0.05 * [recorder averagePowerForChannel:0]));
    

    to

    double peakPowerForChannel = [recorder averagePowerForChannel:0];
    

    and you should be okay to go.

提交回复
热议问题