scales.h 2.8 KB

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