Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: linear scale -> dB, Bark and Mel scales Chris@1: last mod: $Id: scales.h 16227 2009-07-08 06:58:46Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #ifndef _V_SCALES_H_ Chris@1: #define _V_SCALES_H_ Chris@1: Chris@1: #include Chris@1: #include "os.h" Chris@1: Chris@1: #ifdef _MSC_VER Chris@1: /* MS Visual Studio doesn't have C99 inline keyword. */ Chris@1: #define inline __inline Chris@1: #endif Chris@1: Chris@1: /* 20log10(x) */ Chris@1: #define VORBIS_IEEE_FLOAT32 1 Chris@1: #ifdef VORBIS_IEEE_FLOAT32 Chris@1: Chris@1: static inline float unitnorm(float x){ Chris@1: union { Chris@1: ogg_uint32_t i; Chris@1: float f; Chris@1: } ix; Chris@1: ix.f = x; Chris@1: ix.i = (ix.i & 0x80000000U) | (0x3f800000U); Chris@1: return ix.f; Chris@1: } Chris@1: Chris@1: /* Segher was off (too high) by ~ .3 decibel. Center the conversion correctly. */ Chris@1: static inline float todB(const float *x){ Chris@1: union { Chris@1: ogg_uint32_t i; Chris@1: float f; Chris@1: } ix; Chris@1: ix.f = *x; Chris@1: ix.i = ix.i&0x7fffffff; Chris@1: return (float)(ix.i * 7.17711438e-7f -764.6161886f); Chris@1: } Chris@1: Chris@1: #define todB_nn(x) todB(x) Chris@1: Chris@1: #else Chris@1: Chris@1: static float unitnorm(float x){ Chris@1: if(x<0)return(-1.f); Chris@1: return(1.f); Chris@1: } Chris@1: Chris@1: #define todB(x) (*(x)==0?-400.f:log(*(x)**(x))*4.34294480f) Chris@1: #define todB_nn(x) (*(x)==0.f?-400.f:log(*(x))*8.6858896f) Chris@1: Chris@1: #endif Chris@1: Chris@1: #define fromdB(x) (exp((x)*.11512925f)) Chris@1: Chris@1: /* The bark scale equations are approximations, since the original Chris@1: table was somewhat hand rolled. The below are chosen to have the Chris@1: best possible fit to the rolled tables, thus their somewhat odd Chris@1: appearance (these are more accurate and over a longer range than Chris@1: the oft-quoted bark equations found in the texts I have). The Chris@1: approximations are valid from 0 - 30kHz (nyquist) or so. Chris@1: Chris@1: all f in Hz, z in Bark */ Chris@1: Chris@1: #define toBARK(n) (13.1f*atan(.00074f*(n))+2.24f*atan((n)*(n)*1.85e-8f)+1e-4f*(n)) Chris@1: #define fromBARK(z) (102.f*(z)-2.f*pow(z,2.f)+.4f*pow(z,3.f)+pow(1.46f,z)-1.f) Chris@1: #define toMEL(n) (log(1.f+(n)*.001f)*1442.695f) Chris@1: #define fromMEL(m) (1000.f*exp((m)/1442.695f)-1000.f) Chris@1: Chris@1: /* Frequency to octave. We arbitrarily declare 63.5 Hz to be octave Chris@1: 0.0 */ Chris@1: Chris@1: #define toOC(n) (log(n)*1.442695f-5.965784f) Chris@1: #define fromOC(o) (exp(((o)+5.965784f)*.693147f)) Chris@1: Chris@1: #endif