Home | History | Annotate | Download | only in lib
      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-2009             *
      9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
     10  *                                                                  *
     11  ********************************************************************
     12 
     13  function: linear scale -> dB, Bark and Mel scales
     14  last mod: $Id: scales.h 16227 2009-07-08 06:58:46Z xiphmont $
     15 
     16  ********************************************************************/
     17 
     18 #ifndef _V_SCALES_H_
     19 #define _V_SCALES_H_
     20 
     21 #include <math.h>
     22 #include "os.h"
     23 
     24 #ifdef _MSC_VER
     25 /* MS Visual Studio doesn't have C99 inline keyword. */
     26 #define inline __inline
     27 #endif
     28 
     29 /* 20log10(x) */
     30 #define VORBIS_IEEE_FLOAT32 1
     31 #ifdef VORBIS_IEEE_FLOAT32
     32 
     33 static inline float unitnorm(float x){
     34   union {
     35     ogg_uint32_t i;
     36     float f;
     37   } ix;
     38   ix.f = x;
     39   ix.i = (ix.i & 0x80000000U) | (0x3f800000U);
     40   return ix.f;
     41 }
     42 
     43 /* Segher was off (too high) by ~ .3 decibel.  Center the conversion correctly. */
     44 static inline float todB(const float *x){
     45   union {
     46     ogg_uint32_t i;
     47     float f;
     48   } ix;
     49   ix.f = *x;
     50   ix.i = ix.i&0x7fffffff;
     51   return (float)(ix.i * 7.17711438e-7f -764.6161886f);
     52 }
     53 
     54 #define todB_nn(x) todB(x)
     55 
     56 #else
     57 
     58 static float unitnorm(float x){
     59   if(x<0)return(-1.f);
     60   return(1.f);
     61 }
     62 
     63 #define todB(x)   (*(x)==0?-400.f:log(*(x)**(x))*4.34294480f)
     64 #define todB_nn(x)   (*(x)==0.f?-400.f:log(*(x))*8.6858896f)
     65 
     66 #endif
     67 
     68 #define fromdB(x) (exp((x)*.11512925f))
     69 
     70 /* The bark scale equations are approximations, since the original
     71    table was somewhat hand rolled.  The below are chosen to have the
     72    best possible fit to the rolled tables, thus their somewhat odd
     73    appearance (these are more accurate and over a longer range than
     74    the oft-quoted bark equations found in the texts I have).  The
     75    approximations are valid from 0 - 30kHz (nyquist) or so.
     76 
     77    all f in Hz, z in Bark */
     78 
     79 #define toBARK(n)   (13.1f*atan(.00074f*(n))+2.24f*atan((n)*(n)*1.85e-8f)+1e-4f*(n))
     80 #define fromBARK(z) (102.f*(z)-2.f*pow(z,2.f)+.4f*pow(z,3.f)+pow(1.46f,z)-1.f)
     81 #define toMEL(n)    (log(1.f+(n)*.001f)*1442.695f)
     82 #define fromMEL(m)  (1000.f*exp((m)/1442.695f)-1000.f)
     83 
     84 /* Frequency to octave.  We arbitrarily declare 63.5 Hz to be octave
     85    0.0 */
     86 
     87 #define toOC(n)     (log(n)*1.442695f-5.965784f)
     88 #define fromOC(o)   (exp(((o)+5.965784f)*.693147f))
     89 
     90 #endif
     91