Beispiel #1
0
/* Convert ATH values from dB into energy values as required by the psycho model */
FLOAT ath_energy(FLOAT freq, FLOAT value) {
  FLOAT db;
  db = ath_db(freq, 0) + value; // Originally: ath_db(freq,value)
  /* The values in the standard, and from the ATH formula are in dB.
     In the psycho model we are working in the energy domain. Hence the values that 
     are in the absthr_X tables are not in dB. This function converts from dB into the energy domain.
     As noted on the LAME mailing list from years ago (MFC FIX find the reference), the 
     absolute threhsold of hearing values in the tables in the standard are dodgy - the
     ATH in the tables do not correspond to any previously known values of the ATH. 
     From ISO 11172 Tables D.4.x
       "A value of 0dB represents a level in the absolute threshold calculation of
        96dB below the energy of a sine wave of amplitude 32760."
     But I still don't know why the factor of 41.837375 is the value that it is. MFC Feb 2003 */
  return(pow(10.0, (db+41.837375)*0.1));
}
Beispiel #2
0
static psycho_3_mem *psycho_3_init(twolame_options * glopts)
{
    int i;
    int cbase = 0;              /* current base index for the bark range calculation */
    FLOAT sfreq;
    psycho_3_mem *mem;
    int numlines[HBLKSIZE];
    FLOAT cbval[HBLKSIZE];
    int partition[HBLKSIZE];
    int *freq_subset;
    FLOAT *bark, *ath;
    int cbands = 0;
    int *cbandindex;

    mem = (psycho_3_mem *) TWOLAME_MALLOC(sizeof(psycho_3_mem));
    mem->off[0] = mem->off[1] = 256;
    freq_subset = mem->freq_subset;
    bark = mem->bark;
    ath = mem->ath;
    cbandindex = mem->cbandindex;

    /* Initialise the tables for the adding dB */
    psycho_3_init_add_db(mem);

    /* For each spectral line calculate the bark and the ATH (in dB) */
    sfreq = (FLOAT) glopts->samplerate_out;
    for (i = 1; i < HBLKSIZE; i++) {
        FLOAT freq = i * sfreq / BLKSIZE;
        bark[i] = ath_freq2bark(freq);
        ath[i] = ath_db(freq, glopts->athlevel);
    }

    {                           /* Work out the critical bands Starting from line 0, all lines
                                   within 1 bark of the starting bark are added to the same
                                   critical band. When a line is greater by 1.0 of a bark, start a
                                   new critical band.  */

        cbandindex[0] = 1;
        for (i = 1; i < HBLKSIZE; i++) {
            if ((bark[i] - bark[cbase]) > 1.0) {    /* 1 critical band? 1 bark? */
                /* this frequency line is too different from the starting line, (in terms of the
                   bark distance) so make this spectral line the first member of the next critical
                   band */
                cbase = i;      /* Start the new critical band from this frequency line */
                cbands++;
                cbandindex[cbands] = cbase;
            }
            /* partition[i] tells us which critical band the i'th frequency line is in */
            partition[i] = cbands;
            /* keep a count of how many frequency lines are in each partition */
            numlines[cbands]++;
        }

        cbands++;
        cbandindex[cbands] = 513;   /* Set the top of the last critical band */
        mem->cbands = cbands;   // make a not of the number of cbands

        /* For each crtical band calculate the average bark value cbval [central bark value] */
        for (i = 1; i < HBLKSIZE; i++)
            cbval[partition[i]] += bark[i]; /* sum up all the bark values */
        for (i = 1; i < CBANDS; i++) {
            if (numlines[i] != 0)
                cbval[i] /= numlines[i];    /* divide by the number of values */
            else {
                cbval[i] = 0;   /* this isn't a partition */
            }
        }
    }

    {
        /* For Step6 - For the calculation of individual masking thresholds the spectral lines are
           subsampled i.e. no need to work out the masking for every single spectral line.
           Depending upon which subband the calculation is for, you can skip a number of lines
           There are 16 lines per subband -> 32 * 16 = 512 Subband 0-2 : Every line (3 * 16 = 48
           lines) Subband 3-5 : Every Second line (3 * 16/2 = 24 lines) Subband 6-11 : Every 4th
           line (6 * 16/4 = 24 lines) Subband 12-31 : Every 12th line (20 * 16/8 = 40 lines)

           create this subset of frequencies (freq_subset) */
        int freq_index = 0;
        for (i = 1; i < (3 * 16) + 1; i++)
            freq_subset[freq_index++] = i;
        for (; i < (6 * 16) + 1; i += 2)
            freq_subset[freq_index++] = i;
        for (; i < (12 * 16) + 1; i += 4)
            freq_subset[freq_index++] = i;
        for (; i < (32 * 16) + 1; i += 8)
            freq_subset[freq_index++] = i;
    }

    if (glopts->verbosity > 4) {
        fprintf(stderr, "%i critical bands\n", cbands);
        for (i = 0; i < cbands; i++)
            fprintf(stderr, "cband %i spectral line index %i\n", i, cbandindex[i]);
        fprintf(stderr, "%i Subsampled spectral lines\n", SUBSIZE);
        for (i = 0; i < SUBSIZE; i++)
            fprintf(stderr, "%i Spectral line %i Bark %.2f\n", i, freq_subset[i],
                    bark[freq_subset[i]]);
    }

    return (mem);
}