1 /***************************************************************************
2 * Copyright (C) 2011 by Pere R�fols Soler *
3 * sapista2@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 /***************************************************************************
22 This file contains a VU meter definitions
23 ****************************************************************************/
24
25 #include <stdint.h>
26 #include <math.h>
27
28 #ifndef VU_H
29 #define VU_H
30
31 typedef struct
32 {
33 float vu_value, vu_output, vu_max, m_min, m_decay;
34 }Vu;
35
36 //Initialize the VU meter
37 Vu *VuInit(double rate);
38
39 //Destroy a Vu instance
40 void VuClean(Vu *vu);
41
42 //Clear the VU
resetVU(Vu * vu)43 static inline void resetVU(Vu *vu)
44 {
45 vu->vu_max = 0.0;
46 vu->vu_value = 0.0;
47 }
48
49 //Inputs a sample to VU
SetSample(Vu * vu,float sample)50 static inline void SetSample(Vu *vu, float sample)
51 {
52 vu->vu_value = fabs(sample);
53 vu->vu_max = vu->vu_value > vu->vu_max ? vu->vu_value : vu->vu_max;
54 }
55
56 //Compute the VU's
ComputeVu(Vu * vu,uint32_t nframes)57 static inline float ComputeVu(Vu *vu, uint32_t nframes)
58 {
59 const float fVuOut = vu->vu_max > vu->m_min ? vu->vu_max : 0;
60 if (vu->vu_max > vu->m_min)
61 vu->vu_max *= pow(vu->m_decay, nframes); ///TODO: estas perdent rendiment amb akest pow!!!
62 else
63 vu->vu_max = 0.0;
64 return fVuOut;
65 }
66 #endif