1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _LVPSA_PRIVATE_H_
19 #define _LVPSA_PRIVATE_H_
20 
21 #include "LVPSA.h"
22 #include "BIQUAD.h"
23 #include "LVPSA_QPD.h"
24 #include "LVM_Macros.h"
25 
26 
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31 
32 /**********************************************************************************
33    CONSTANT DEFINITIONS
34 ***********************************************************************************/
35 
36 /* Memory */
37 #define LVPSA_INSTANCE_ALIGN             4      /* 32-bit alignment for structures                                  */
38 #define LVPSA_SCRATCH_ALIGN              4      /* 32-bit alignment for long data                                   */
39 #define LVPSA_COEF_ALIGN                 4      /* 32-bit alignment for long words                                  */
40 #define LVPSA_DATA_ALIGN                 4      /* 32-bit alignment for long data                                   */
41 
42 #define LVPSA_MEMREGION_INSTANCE         0      /* Offset to instance memory region in memory table                 */
43 #define LVPSA_MEMREGION_PERSISTENT_COEF  1      /* Offset to persistent coefficients  memory region in memory table */
44 #define LVPSA_MEMREGION_PERSISTENT_DATA  2      /* Offset to persistent taps  memory region in memory table         */
45 #define LVPSA_MEMREGION_SCRATCH          3      /* Offset to scratch  memory region in memory table                 */
46 #ifndef HIGHER_FS
47 #define LVPSA_NR_SUPPORTED_RATE          9      /* From 8000Hz to 48000Hz*/
48 #else
49 #define LVPSA_NR_SUPPORTED_RATE          11      /* From 8000Hz to 192000Hz*/
50 #endif
51 #define LVPSA_NR_SUPPORTED_SPEED         3      /* LOW, MEDIUM, HIGH                                                */
52 
53 #define LVPSA_MAXBUFFERDURATION          4000   /* Maximum length in ms of the levels buffer                        */
54 #define LVPSA_MAXINPUTBLOCKSIZE          5000   /* Maximum length in mono samples of the block to process           */
55 #define LVPSA_NBANDSMIN                  1      /* Minimum number of frequency band                                 */
56 #define LVPSA_NBANDSMAX                  30     /* Maximum number of frequency band                                 */
57 #define LVPSA_MAXCENTERFREQ              20000  /* Maximum possible center frequency                                */
58 #define LVPSA_MINPOSTGAIN              (-15)    /* Minimum possible post gain                                       */
59 #define LVPSA_MAXPOSTGAIN                15     /* Maximum possible post gain                                       */
60 #define LVPSA_MINQFACTOR                 25     /* Minimum possible Q factor                                        */
61 #define LVPSA_MAXQFACTOR                 1200   /* Maximum possible Q factor                                        */
62 
63 #define LVPSA_MAXLEVELDECAYFACTOR        0x4111 /* Decay factor for the maximum values calculation                  */
64 #define LVPSA_MAXLEVELDECAYSHIFT         14     /* Decay shift for the maximum values calculation                   */
65 
66 #define LVPSA_MAXUNSIGNEDCHAR            0xFF
67 
68 #define LVPSA_FsInvertShift              31
69 #define LVPSA_GAINSHIFT                  11
70 #define LVPSA_FREQSHIFT                  25
71 
72 /**********************************************************************************
73    TYPES DEFINITIONS
74 ***********************************************************************************/
75 
76 #define LVPSA_InternalRefreshTime       0x0014    /* 20 ms (50Hz) in Q16.0      */
77 #define LVPSA_InternalRefreshTimeInv    0x0666    /* 1/20ms left shifted by 15  */
78 #define LVPSA_InternalRefreshTimeShift  15
79 
80 
81 /* Precision of the filter */
82 typedef enum
83 {
84     LVPSA_SimplePrecisionFilter,    /* Simple precision */
85     LVPSA_DoublePrecisionFilter     /* Double precision */
86 } LVPSA_BPFilterPrecision_en;
87 
88 typedef struct
89 {
90     LVM_CHAR                    bControlPending;                    /* Flag incating a change of the control parameters                                             */
91     LVM_UINT16                  nBands;                             /* Number of bands of the spectrum analyzer                                                     */
92     LVM_UINT16                  MaxInputBlockSize;                  /* Maximum input data buffer size                                                               */
93 
94     LVPSA_ControlParams_t       CurrentParams;                      /* Current control parameters of the module                                                     */
95     LVPSA_ControlParams_t       NewParams;                          /* New control parameters given by the user                                                     */
96     LVPSA_MemTab_t              MemoryTable;
97 
98     LVPSA_BPFilterPrecision_en *pBPFiltersPrecision;                /* Points a nBands elements array that contains the filter precision for each band              */
99 #ifndef BUILD_FLOAT
100     Biquad_Instance_t          *pBP_Instances;                      /* Points a nBands elements array that contains the band pass filter instance for each band     */
101     Biquad_1I_Order2_Taps_t    *pBP_Taps;                           /* Points a nBands elements array that contains the band pass filter taps for each band         */
102     QPD_State_t                *pQPD_States;                        /* Points a nBands elements array that contains the QPD filter instance for each band           */
103     QPD_Taps_t                 *pQPD_Taps;                          /* Points a nBands elements array that contains the QPD filter taps for each band               */
104 #else
105     Biquad_FLOAT_Instance_t          *pBP_Instances;
106     /* Points a nBands elements array that contains the band pass filter taps for each band */
107     Biquad_1I_Order2_FLOAT_Taps_t    *pBP_Taps;
108     /* Points a nBands elements array that contains the QPD filter instance for each band */
109     QPD_FLOAT_State_t                *pQPD_States;
110     /* Points a nBands elements array that contains the QPD filter taps for each band */
111     QPD_FLOAT_Taps_t                 *pQPD_Taps;
112 #endif
113 
114 #ifndef BUILD_FLOAT
115     LVM_UINT16                 *pPostGains;                         /* Points a nBands elements array that contains the post-filter gains for each band             */
116 #else
117     /* Points a nBands elements array that contains the post-filter gains for each band */
118     LVM_FLOAT                  *pPostGains;
119 #endif
120     LVPSA_FilterParam_t        *pFiltersParams;                     /* Copy of the filters parameters from the input parameters                                     */
121 
122 
123     LVM_UINT16                  nSamplesBufferUpdate;               /* Number of samples to make 20ms                                                               */
124     LVM_INT32                   BufferUpdateSamplesCount;           /* Counter used to know when to put a new value in the buffer                                   */
125     LVM_UINT16                  nRelevantFilters;                   /* Number of relevent filters depending on sampling frequency and bands center frequency        */
126     LVM_UINT16                  LocalSamplesCount;                  /* Counter used to update the SpectralDataBufferAudioTime                                       */
127 
128     LVM_UINT16                  DownSamplingFactor;                 /* Down sampling factor depending on the sampling frequency                                     */
129     LVM_UINT16                  DownSamplingCount;                  /* Counter used for the downsampling handling                                                   */
130 
131     LVM_UINT16                  SpectralDataBufferDuration;         /* Length of the buffer in time (ms) defined by the application                                 */
132     LVM_UINT8                  *pSpectralDataBufferStart;           /* Starting address of the buffer                                                               */
133     LVM_UINT8                  *pSpectralDataBufferWritePointer;    /* Current position of the writting pointer of the buffer                                       */
134     LVPSA_Time                  SpectralDataBufferAudioTime;        /* AudioTime at which the last value save occured in the buffer                                 */
135     LVM_UINT32                  SpectralDataBufferLength;           /* Number of spectrum data value that the buffer can contain (per band)
136                                                                        = SpectralDataBufferDuration/20ms                                                            */
137 
138     LVM_UINT8                  *pPreviousPeaks;                     /* Points to a nBands elements array that contains the previous peak value of the level
139                                                                      detection. Those values are decremented after each call to the GetSpectrum function          */
140 
141 }LVPSA_InstancePr_t, *pLVPSA_InstancePr_t;
142 
143 
144 
145 /**********************************************************************************
146    FUNCTIONS PROTOTYPE
147 ***********************************************************************************/
148 /************************************************************************************/
149 /*                                                                                  */
150 /* FUNCTION:            LVPSA_ApplyNewSettings                                      */
151 /*                                                                                  */
152 /* DESCRIPTION:                                                                     */
153 /*  Reinitialize some parameters and changes filters' coefficients if               */
154 /*  some control parameters have changed.                                           */
155 /*                                                                                  */
156 /* PARAMETERS:                                                                      */
157 /*  pInst               Pointer to the instance                                     */
158 /*                                                                                  */
159 /* RETURNS:                                                                         */
160 /*  LVPSA_OK            Always succeeds                                             */
161 /*                                                                                  */
162 /************************************************************************************/
163 LVPSA_RETURN LVPSA_ApplyNewSettings (LVPSA_InstancePr_t     *pInst);
164 
165 #ifdef __cplusplus
166 }
167 #endif /* __cplusplus */
168 
169 #endif /* _LVPSA_PRIVATE_H */
170