Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide



Rockbox mail archive

Subject: [patch] peakmeter
From: Andreas Zwirtes (zwirtes_at_gmx.de)
Date: 2002-10-07


Hi !

I finished the code for the dB Peakmeter. It compiles well for recorder.
Although I cannt test it, it worked 100% on my windows machine. Seems I
left the USB cable in the office, so i cannot copy the binary to my box.

Since i'm new to rockbox and know nothing about CVS, i changed as little
as possible in /apps/peakmeter.c. I've added the function calc_dB and edited
peak_meter_draw (). I think I stuck to the contribution rules. Comments
highly
appreciated.

Bye,
Andreas Zwirtes aka radhard

The following lines were changed in peak_meter_draw:

---------snip--------
        /* scale the samples */
        left /= (MAX_PEAK / meterwidth);
        right /= (MAX_PEAK / meterwidth);
---------snip--------

has been replaced by:

---------snip--------
        /* scale the samples (old linear version)
        left /= (MAX_PEAK / meterwidth);
        right /= (MAX_PEAK / meterwidth); */

        /* scale the samples (newer dB version)*/
        left = calc_dB(left) * meterwidth / 9000;
        right = calc_dB(right) * meterwidth / 9000;
---------snip--------

The new code should be inserted before peak_meter_draw and looks like this:

---------snip--------
/**
 * Calculates dB Value for the peak meter, uses peak value as input
 * @param int sample - The input value
 * Make sure that 0 <= value < SAMPLE_RANGE
 *
 * @return int - The 2 digit fixed comma result of the euation
 * 20 * log (sample / SAMPLE_RANGE) + 90
 * Output range is 0-8961 (that is 0,0 - 89,6 dB).
 * Normally 0dB is full scale, here it is shifted +90dB.
 * The calculation is based on the results of a linear
 * approximation tool written specifically for this problem
 * by Andreas Zwirtes (radhard_at_gmx.de). The result hat an
 * accurracy of better than 2%. It is highly runtime
optimized,
 * the cascading if-clauses do an successive approximation on
 * the input value. This avoids big lookup-tables and
 * for-loops.
 */

int calc_dB (int iSample)
{
    //return n+m*(iSample-iStart)/100
    int n;
    long m;
    int iStart;
    if (iSample < 119) //Range 1-4
    {
        if (iSample < 5) //Range 1-2
        {
            if (iSample < 1) //Range 1
            {
                iStart = 0;
                n = 0;
                m = 5900;
            }
            else //Range 2
            {
                iStart = 1;
                n = 59;
                m = 34950;
            }
        }
        else //Range 3-4
        {
            if (iSample < 24) //Range 3
            {
                iStart = 5;
                n = 1457;
                m = 7168;
            }
            else //Range 4
            {
                iStart = 24;
                n = 2819;
                m = 1464;
            }
        }
    }
    else //Range 5-8
    {
        if (iSample < 2918) //Range 5-6
        {
            if (iSample < 592) //Range 5
            {
                iStart = 119;
                n = 4210;
                m = 295;
            }
            else //Range 6
            {
                iStart = 592;
                n = 5605;
                m = 60;
            }
        }
        else //Range 7-8
        {
            if (iSample < 15352) //Range 7
            {
                iStart = 2918;
                n = 7001;
                m = 12;
            }
            else //Range 8
            {
                iStart = 15352;
                n = 8439;
                m = 3;
            }
        }
    }
   
    return n + (m * (long)(iSample - iStart)) / 100L;
}
---------snip--------



Page was last modified "Jan 10 2012" The Rockbox Crew
aaa