Example #1
0
void psycho_3_init(options *glopts) {
  int i;
  int cbase = 0; /* current base index for the bark range calculation */

  fft_buf = (D1408 *) mem_alloc ((long) sizeof (D1408) * 2, "fft_buf");
  
  /* Initialise the tables for the adding dB */
  psycho_3_init_add_db();
  
  /* For each spectral line calculate the bark and the ATH (in dB) */
  FLOAT sfreq = (FLOAT) s_freq[header->version][header->sampling_frequency] * 1000;
  for (i=1;i<HBLKSIZE; i++) {
    FLOAT freq = i * sfreq/BLKSIZE;
    bark[i] = toolame_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.  */
    
    numlines = (int *)calloc(HBLKSIZE, sizeof(int));
    cbval = (float *)calloc(HBLKSIZE, sizeof(float));
    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 */

    /* 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(stdout,"%i critical bands\n",cbands);
    for (i=0;i<cbands;i++)
      fprintf(stdout,"cband %i spectral line index %i\n",i,cbandindex[i]);
    fprintf(stdout,"%i Subsampled spectral lines\n",SUBSIZE);
    for (i=0;i<SUBSIZE;i++) 
      fprintf(stdout,"%i Spectral line %i Bark %.2f\n",i,freq_subset[i], bark[freq_subset[i]]);
  }
}
Example #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);
}