scales.h 2.7 KB

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