Blame lib/scales.h

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