6c91702130fb764239859ed6f1e8cca9698f2300
[platform/upstream/libvorbis.git] / lib / scales.h
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: linear scale -> dB, Bark and Mel scales
14  last mod: $Id: scales.h,v 1.21 2002/03/17 19:50:47 xiphmont Exp $
15
16  ********************************************************************/
17
18 #ifndef _V_SCALES_H_
19 #define _V_SCALES_H_
20
21 #include <math.h>
22 #include "os.h"
23
24 /* 20log10(x) */
25 #ifdef VORBIS_IEEE_FLOAT32
26
27 static float unitnorm(float x){
28   ogg_uint32_t *ix=(ogg_uint32_t *)&x;
29   *ix=(*ix&0x80000000UL)|(0x3f800000UL);
30   return(x);
31 }
32
33 static float FABS(float x){
34   ogg_uint32_t *ix=(ogg_uint32_t *)&x;
35   *ix&=0x7fffffffUL;
36   return(x);
37 }
38
39 static float todB_LOOKUP[256]={
40         -140.277330f, -139.633636f, -139.034372f, -138.473797f, 
41         -137.450747f, -136.535597f, -135.707743f, -134.951972f, 
42         -134.256730f, -133.613036f, -133.013772f, -132.453198f, 
43         -131.430147f, -130.514997f, -129.687144f, -128.931372f, 
44         -128.236130f, -127.592437f, -126.993172f, -126.432598f, 
45         -125.409547f, -124.494397f, -123.666544f, -122.910772f, 
46         -122.215530f, -121.571837f, -120.972572f, -120.411998f, 
47         -119.388947f, -118.473797f, -117.645944f, -116.890173f, 
48         -116.194930f, -115.551237f, -114.951972f, -114.391398f, 
49         -113.368347f, -112.453198f, -111.625344f, -110.869573f, 
50         -110.174331f, -109.530637f, -108.931372f, -108.370798f, 
51         -107.347748f, -106.432598f, -105.604744f, -104.848973f, 
52         -104.153731f, -103.510037f, -102.910773f, -102.350198f, 
53         -101.327148f, -100.411998f,  -99.584144f,  -98.828373f, 
54          -98.133131f,  -97.489437f,  -96.890173f,  -96.329598f, 
55          -95.306548f,  -94.391398f,  -93.563544f,  -92.807773f, 
56          -92.112531f,  -91.468837f,  -90.869573f,  -90.308998f, 
57          -89.285948f,  -88.370798f,  -87.542944f,  -86.787173f, 
58          -86.091931f,  -85.448237f,  -84.848973f,  -84.288398f, 
59          -83.265348f,  -82.350198f,  -81.522344f,  -80.766573f, 
60          -80.071331f,  -79.427637f,  -78.828373f,  -78.267799f, 
61          -77.244748f,  -76.329598f,  -75.501745f,  -74.745973f, 
62          -74.050731f,  -73.407038f,  -72.807773f,  -72.247199f, 
63          -71.224148f,  -70.308998f,  -69.481145f,  -68.725373f, 
64          -68.030131f,  -67.386438f,  -66.787173f,  -66.226599f, 
65          -65.203548f,  -64.288399f,  -63.460545f,  -62.704774f, 
66          -62.009531f,  -61.365838f,  -60.766573f,  -60.205999f, 
67          -59.182948f,  -58.267799f,  -57.439945f,  -56.684174f, 
68          -55.988932f,  -55.345238f,  -54.745973f,  -54.185399f, 
69          -53.162349f,  -52.247199f,  -51.419345f,  -50.663574f, 
70          -49.968332f,  -49.324638f,  -48.725374f,  -48.164799f, 
71          -47.141749f,  -46.226599f,  -45.398745f,  -44.642974f, 
72          -43.947732f,  -43.304038f,  -42.704774f,  -42.144199f, 
73          -41.121149f,  -40.205999f,  -39.378145f,  -38.622374f, 
74          -37.927132f,  -37.283438f,  -36.684174f,  -36.123599f, 
75          -35.100549f,  -34.185399f,  -33.357545f,  -32.601774f, 
76          -31.906532f,  -31.262838f,  -30.663574f,  -30.102999f, 
77          -29.079949f,  -28.164799f,  -27.336945f,  -26.581174f, 
78          -25.885932f,  -25.242238f,  -24.642974f,  -24.082400f, 
79          -23.059349f,  -22.144199f,  -21.316346f,  -20.560574f, 
80          -19.865332f,  -19.221639f,  -18.622374f,  -18.061800f, 
81          -17.038749f,  -16.123599f,  -15.295746f,  -14.539974f, 
82          -13.844732f,  -13.201039f,  -12.601774f,  -12.041200f, 
83          -11.018149f,  -10.103000f,   -9.275146f,   -8.519375f, 
84           -7.824132f,   -7.180439f,   -6.581174f,   -6.020600f, 
85           -4.997549f,   -4.082400f,   -3.254546f,   -2.498775f, 
86           -1.803533f,   -1.159839f,   -0.560574f,    0.000000f, 
87            1.023050f,    1.938200f,    2.766054f,    3.521825f, 
88            4.217067f,    4.860761f,    5.460025f,    6.020600f, 
89            7.043650f,    7.958800f,    8.786654f,    9.542425f, 
90           10.237667f,   10.881361f,   11.480625f,   12.041200f, 
91           13.064250f,   13.979400f,   14.807254f,   15.563025f, 
92           16.258267f,   16.901961f,   17.501225f,   18.061800f, 
93           19.084850f,   20.000000f,   20.827854f,   21.583625f, 
94           22.278867f,   22.922561f,   23.521825f,   24.082400f, 
95           25.105450f,   26.020600f,   26.848453f,   27.604225f, 
96           28.299467f,   28.943161f,   29.542425f,   30.102999f, 
97           31.126050f,   32.041200f,   32.869053f,   33.624825f, 
98           34.320067f,   34.963760f,   35.563025f,   36.123599f, 
99           37.146650f,   38.061800f,   38.889653f,   39.645424f, 
100           40.340667f,   40.984360f,   41.583625f,   42.144199f, 
101           43.167250f,   44.082399f,   44.910253f,   45.666024f, 
102           46.361266f,   47.004960f,   47.604225f,   48.164799f, 
103           49.187850f,   50.102999f,   50.930853f,   51.686624f
104 };
105
106 static float todB(const float *x){
107   ogg_int32_t *i=(ogg_int32_t *)x;
108   ogg_int32_t temp=((*i&0x7fffffff)-0x33c7ffff)>>20;
109   if(temp<0)return -400.f;
110   return(todB_LOOKUP[temp]);
111 }
112
113 #define todB_nn(x) todB(x)
114
115 #else
116
117 static float unitnorm(float x){
118   if(x<0)return(-1.f);
119   return(1.f);
120 }
121
122 #define FABS(x) fabs(*(x))
123
124 #define todB(x)   (*(x)==0?-400.f:log(*(x)**(x))*4.34294480f)
125 #define todB_nn(x)   (*(x)==0.f?-400.f:log(*(x))*8.6858896f)
126
127 #endif 
128
129 #define fromdB(x) (exp((x)*.11512925f))  
130
131 /* The bark scale equations are approximations, since the original
132    table was somewhat hand rolled.  The below are chosen to have the
133    best possible fit to the rolled tables, thus their somewhat odd
134    appearance (these are more accurate and over a longer range than
135    the oft-quoted bark equations found in the texts I have).  The
136    approximations are valid from 0 - 30kHz (nyquist) or so.
137
138    all f in Hz, z in Bark */
139
140 #define toBARK(n)   (13.1f*atan(.00074f*(n))+2.24f*atan((n)*(n)*1.85e-8f)+1e-4f*(n))
141 #define fromBARK(z) (102.f*(z)-2.f*pow(z,2.f)+.4f*pow(z,3.f)+pow(1.46f,z)-1.f)
142 #define toMEL(n)    (log(1.f+(n)*.001f)*1442.695f)
143 #define fromMEL(m)  (1000.f*exp((m)/1442.695f)-1000.f)
144
145 /* Frequency to octave.  We arbitrarily declare 63.5 Hz to be octave
146    0.0 */
147
148 #define toOC(n)     (log(n)*1.442695f-5.965784f)
149 #define fromOC(o)   (exp(((o)+5.965784f)*.693147f))
150
151 #endif
152