summaryrefslogtreecommitdiffstats
path: root/libAACenc/src/tonality.cpp
blob: 54b4b6e21a9fdf5960845cbcd6431d6d071f38e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/******************************** MPEG Audio Encoder **************************

                       (c) copyright Fraunhofer IIS (1996)
                               All Rights Reserved

    Please be advised that this software and/or program delivery is
    Confidential Information of Fraunhofer and subject to and covered by the

    Fraunhofer IIS Software Evaluation Agreement
    between Google Inc. and  Fraunhofer
    effective and in full force since March 1, 2012.

    You may use this software and/or program only under the terms and
    conditions described in the above mentioned Fraunhofer IIS Software
    Evaluation Agreement. Any other and/or further use requires a separate agreement.


   $Id$
   author:   M. Werner
   contents/description: Convert chaos measure to the tonality index

   This software and/or program is protected by copyright law and international
   treaties. Any reproduction or distribution of this software and/or program,
   or any portion of it, may result in severe civil and criminal penalties, and
   will be prosecuted to the maximum extent possible under law.

******************************************************************************/

#include "tonality.h"
#include "chaosmeasure.h"

/*static const FIXP_SGL maxtone  =  FL2FXCONST_SGL(0.5);*/
/*static const FIXP_SGL mintone  =  FL2FXCONST_SGL(0.05);*/
/*static const float convtone =  2.0f;*/           /* 1.0/mintone               */

/* -1.0/log(maxtone/mintone) */
static const FIXP_DBL normlog = (FIXP_DBL)0xd977d949; /*FL2FXCONST_DBL(-0.4342944819f * FDKlog(2.0)/FDKlog(2.7182818)); */

static void FDKaacEnc_CalcSfbTonality(FIXP_DBL      *RESTRICT spectrum,
							INT           *RESTRICT sfbMaxScaleSpec,
                            FIXP_DBL      *RESTRICT chaosMeasure,
                            FIXP_SGL      *RESTRICT sfbTonality,
                            INT           sfbCnt,
                            const INT     *RESTRICT sfbOffset,
                            FIXP_DBL      *RESTRICT sfbEnergyLD64 );


void FDKaacEnc_CalculateFullTonality(FIXP_DBL      *RESTRICT spectrum,
						             INT           *RESTRICT sfbMaxScaleSpec,
                                     FIXP_DBL      *RESTRICT sfbEnergyLD64,
                                     FIXP_SGL      *RESTRICT sfbTonality,
                                     INT            sfbCnt,
                                     const INT     *sfbOffset,
                                     INT            usePns)
{
  INT j;
#if defined(ARCH_PREFER_MULT_32x16)
  FIXP_SGL alpha_0 = FL2FXCONST_SGL(0.25f);       /* used in smooth ChaosMeasure */
  FIXP_SGL alpha_1 = FL2FXCONST_SGL(1.0f-0.25f);  /* used in smooth ChaosMeasure */
#else
  FIXP_DBL alpha_0 = FL2FXCONST_DBL(0.25f);       /* used in smooth ChaosMeasure */
  FIXP_DBL alpha_1 = FL2FXCONST_DBL(1.0f-0.25f);  /* used in smooth ChaosMeasure */
#endif
  INT numberOfLines = sfbOffset[sfbCnt];

  if (!usePns)
    return;

  C_ALLOC_SCRATCH_START(chaosMeasurePerLine, FIXP_DBL, (1024));
  /* calculate chaos measure */
  FDKaacEnc_CalculateChaosMeasure(spectrum,
                        numberOfLines,
                        chaosMeasurePerLine);

  /* smooth ChaosMeasure */
  for (j=1;j<numberOfLines;j++) {
    FIXP_DBL tmp = fMultDiv2(alpha_1, chaosMeasurePerLine[j]);
    chaosMeasurePerLine[j] = fMultAdd(tmp, alpha_0, chaosMeasurePerLine[j-1]);
  }

  FDKaacEnc_CalcSfbTonality(spectrum,
                  sfbMaxScaleSpec,
                  chaosMeasurePerLine,
                  sfbTonality,
                  sfbCnt,
                  sfbOffset,
                  sfbEnergyLD64);

  C_ALLOC_SCRATCH_END(chaosMeasurePerLine, FIXP_DBL, (1024));
}


/*****************************************************************************

    functionname: CalculateTonalityIndex
    description:  computes tonality values out of unpredictability values
                  limits range and computes log()
    returns:
    input:        ptr to energies, ptr to chaos measure values,
                  number of sfb
    output:       sfb wise tonality values

*****************************************************************************/
static void FDKaacEnc_CalcSfbTonality(FIXP_DBL      *RESTRICT spectrum,
                            INT           *RESTRICT sfbMaxScaleSpec,
                            FIXP_DBL      *RESTRICT chaosMeasure,
                            FIXP_SGL      *RESTRICT sfbTonality,
                            INT           sfbCnt,
                            const INT     *RESTRICT sfbOffset,
                            FIXP_DBL      *RESTRICT sfbEnergyLD64 )
{
    INT i, j;

    for (i=0; i<sfbCnt; i++) {
      FIXP_DBL chaosMeasureSfbLD64;
      INT shiftBits = fixMax(0,sfbMaxScaleSpec[i] - 4);  /* max sfbWidth = 96 ; 2^7=128 => 7/2 = 4 (spc*spc) */

      FIXP_DBL chaosMeasureSfb = FL2FXCONST_DBL(0.0);

      /* calc chaosMeasurePerSfb */
      for (j=(sfbOffset[i+1]-sfbOffset[i])-1; j>=0; j--) {
        FIXP_DBL tmp = (*spectrum++)<<shiftBits;
        FIXP_DBL lineNrg = fMultDiv2(tmp, tmp);
        chaosMeasureSfb = fMultAddDiv2(chaosMeasureSfb, lineNrg, *chaosMeasure++);
      }

      /* calc tonalityPerSfb */
      if (chaosMeasureSfb != FL2FXCONST_DBL(0.0))
      {
        /* add ld(convtone)/64 and 2/64 bec.fMultDiv2 */
        chaosMeasureSfbLD64 = CalcLdData((chaosMeasureSfb)) - sfbEnergyLD64[i];
        chaosMeasureSfbLD64 += FL2FXCONST_DBL(3.0f/64) - ((FIXP_DBL)(shiftBits)<<(DFRACT_BITS-6));

        if (chaosMeasureSfbLD64 > FL2FXCONST_DBL(-0.0519051) )     /* > ld(0.05)+ld(2) */
        {
          if (chaosMeasureSfbLD64 <= FL2FXCONST_DBL(0.0) )
		    sfbTonality[i] = FX_DBL2FX_SGL(fMultDiv2( chaosMeasureSfbLD64 , normlog ) << 7);
          else
            sfbTonality[i] = FL2FXCONST_SGL(0.0);
        }
        else
          sfbTonality[i] = (FIXP_SGL)MAXVAL_SGL;
      }
      else
        sfbTonality[i] = (FIXP_SGL)MAXVAL_SGL;
    }
}