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