[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / cores / AudioEngine / Utils / AEUtil.h
1 #pragma once
2 /*
3  *      Copyright (C) 2010-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "../AEAudioFormat.h"
23 #include "utils/StdString.h"
24 #include "PlatformDefs.h"
25 #include <math.h>
26
27 #ifdef TARGET_WINDOWS
28 #if _M_IX86_FP>0 && !defined(__SSE__)
29 #define __SSE__
30 #endif
31 #endif
32
33 #ifdef __SSE__
34 #include <xmmintrin.h>
35 #else
36 #define __m128 void
37 #endif
38
39 #ifdef __GNUC__
40   #define MEMALIGN(b, x) x __attribute__((aligned(b)))
41 #else
42   #define MEMALIGN(b, x) __declspec(align(b)) x
43 #endif
44
45 class CAEUtil
46 {
47 private:
48   static unsigned int m_seed;
49   #ifdef __SSE__
50     static __m128i m_sseSeed;
51   #endif
52
53   static float SoftClamp(const float x);
54
55 public:
56   static CAEChannelInfo          GuessChLayout     (const unsigned int channels);
57   static const char*             GetStdChLayoutName(const enum AEStdChLayout layout);
58   static const unsigned int      DataFormatToBits  (const enum AEDataFormat dataFormat);
59   static const char*             DataFormatToStr   (const enum AEDataFormat dataFormat);
60
61   /*! \brief convert a volume percentage (as a proportion) to a dB gain
62    We assume a dB range of 60dB, i.e. assume that 0% volume corresponds
63    to a reduction of 60dB.
64    \param value the volume from 0..1
65    \return the corresponding gain in dB from -60dB .. 0dB.
66    \sa GainToScale
67    */
68   static inline const float PercentToGain(const float value)
69   {
70     static const float db_range = 60.0f;
71     return (value - 1)*db_range;
72   }
73
74   /*! \brief convert a dB gain to volume percentage (as a proportion)
75    We assume a dB range of 60dB, i.e. assume that 0% volume corresponds
76    to a reduction of 60dB.
77    \param the corresponding gain in dB from -60dB .. 0dB.
78    \return value the volume from 0..1
79    \sa ScaleToGain
80    */
81   static inline const float GainToPercent(const float gain)
82   {
83     static const float db_range = 60.0f;
84     return 1+(gain/db_range);
85   }
86
87   /*! \brief convert a dB gain to a scale factor for audio manipulation
88    Inverts gain = 20 log_10(scale)
89    \param dB the gain in decibels.
90    \return the scale factor (equivalent to a voltage multiplier).
91    \sa PercentToGain
92    */
93   static inline const float GainToScale(const float dB)
94   {
95     return pow(10.0f, dB/20);
96   }
97
98   /*! \brief convert a scale factor to dB gain for audio manipulation
99    Inverts GainToScale result
100    \param the scale factor (equivalent to a voltage multiplier).
101    \return dB the gain in decibels.
102    \sa GainToScale
103    */
104   static inline const float ScaleToGain(const float scale)
105   {
106     return 20*log10(scale);
107   }
108
109   #ifdef __SSE__
110   static void SSEMulArray     (float *data, const float mul, uint32_t count);
111   static void SSEMulAddArray  (float *data, float *add, const float mul, uint32_t count);
112   #endif
113   static void ClampArray(float *data, uint32_t count);
114
115   /*
116     Rand implementations based on:
117     http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/
118     This is NOT safe for crypto work, but perfectly fine for audio usage (dithering)
119   */
120   static float FloatRand1(const float min, const float max);
121   static void  FloatRand4(const float min, const float max, float result[4], __m128 *sseresult = NULL);
122
123   static bool S16NeedsByteSwap(AEDataFormat in, AEDataFormat out);
124 };