• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**********************************************************************************
19    INCLUDE FILES
20 ***********************************************************************************/
21 
22 #include <system/audio.h>
23 
24 #include "LVC_Mixer_Private.h"
25 #include "VectorArithmetic.h"
26 #include "ScalarArithmetic.h"
27 
28 /**********************************************************************************
29    DEFINITIONS
30 ***********************************************************************************/
31 
32 #define TRUE 1
33 #define FALSE 0
34 
35 #define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof(*(a))))
36 
37 /**********************************************************************************
38    FUNCTION LVC_MixSoft_1St_MC_float_SAT
39 ***********************************************************************************/
40 /* This threshold is used to decide on the processing to be applied on
41  * front center and back center channels
42  */
43 #define LVM_VOL_BAL_THR (0.000016f)
LVC_MixSoft_1St_MC_float_SAT(LVMixer3_2St_FLOAT_st * ptrInstance,const LVM_FLOAT * src,LVM_FLOAT * dst,LVM_INT16 NrFrames,LVM_INT32 NrChannels,LVM_INT32 ChMask)44 void LVC_MixSoft_1St_MC_float_SAT(LVMixer3_2St_FLOAT_st* ptrInstance, const LVM_FLOAT* src,
45                                   LVM_FLOAT* dst, LVM_INT16 NrFrames, LVM_INT32 NrChannels,
46                                   LVM_INT32 ChMask) {
47     char HardMixing = TRUE;
48     LVM_FLOAT TargetGain;
49     Mix_Private_FLOAT_st Target_lfe = {LVM_MAXFLOAT, LVM_MAXFLOAT, LVM_MAXFLOAT};
50     Mix_Private_FLOAT_st Target_ctr = {LVM_MAXFLOAT, LVM_MAXFLOAT, LVM_MAXFLOAT};
51     Mix_Private_FLOAT_st* pInstance1 =
52             (Mix_Private_FLOAT_st*)(ptrInstance->MixerStream[0].PrivateParams);
53     Mix_Private_FLOAT_st* pInstance2 =
54             (Mix_Private_FLOAT_st*)(ptrInstance->MixerStream[1].PrivateParams);
55     Mix_Private_FLOAT_st* pMixPrivInst[4] = {pInstance1, pInstance2, &Target_ctr, &Target_lfe};
56     Mix_Private_FLOAT_st* pInstance[NrChannels];
57 
58     if (audio_channel_mask_get_representation(ChMask) == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
59         int loopLimit = (NrChannels == FCC_1) ? NrChannels : FCC_2;
60         for (int i = 0; i < loopLimit; i++) {
61             pInstance[i] = pMixPrivInst[i];
62         }
63         for (int i = loopLimit; i < NrChannels; i++) {
64             pInstance[i] = pMixPrivInst[2];
65         }
66     } else {
67         // TODO: Combine with system/media/audio_utils/Balance.cpp
68         // Constants in system/media/audio/include/system/audio-base.h
69         // 'mixInstIdx' is used to map the appropriate mixer instance for each channel.
70         const int mixInstIdx[] = {
71                 0,  // AUDIO_CHANNEL_OUT_FRONT_LEFT            = 0x1u,
72                 1,  // AUDIO_CHANNEL_OUT_FRONT_RIGHT           = 0x2u,
73                 2,  // AUDIO_CHANNEL_OUT_FRONT_CENTER          = 0x4u,
74                 3,  // AUDIO_CHANNEL_OUT_LOW_FREQUENCY         = 0x8u,
75                 0,  // AUDIO_CHANNEL_OUT_BACK_LEFT             = 0x10u,
76                 1,  // AUDIO_CHANNEL_OUT_BACK_RIGHT            = 0x20u,
77                 0,  // AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER  = 0x40u,
78                 1,  // AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x80u,
79                 2,  // AUDIO_CHANNEL_OUT_BACK_CENTER           = 0x100u,
80                 0,  // AUDIO_CHANNEL_OUT_SIDE_LEFT             = 0x200u,
81                 1,  // AUDIO_CHANNEL_OUT_SIDE_RIGHT            = 0x400u,
82                 2,  // AUDIO_CHANNEL_OUT_TOP_CENTER            = 0x800u,
83                 0,  // AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT        = 0x1000u,
84                 2,  // AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER      = 0x2000u,
85                 1,  // AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT       = 0x4000u,
86                 0,  // AUDIO_CHANNEL_OUT_TOP_BACK_LEFT         = 0x8000u,
87                 2,  // AUDIO_CHANNEL_OUT_TOP_BACK_CENTER       = 0x10000u,
88                 1,  // AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT        = 0x20000u,
89                 0,  // AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT         = 0x40000u,
90                 1,  // AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT        = 0x80000u
91         };
92         if (pInstance1->Target <= LVM_VOL_BAL_THR || pInstance2->Target <= LVM_VOL_BAL_THR) {
93             Target_ctr.Target = 0.0f;
94             Target_ctr.Current = 0.0f;
95             Target_ctr.Delta = 0.0f;
96         }
97         const unsigned int idxArrSize = ARRAY_SIZE(mixInstIdx);
98         for (unsigned int i = 0, channel = ChMask; channel != 0; ++i) {
99             const unsigned int idx = __builtin_ctz(channel);
100             if (idx < idxArrSize) {
101                 pInstance[i] = pMixPrivInst[mixInstIdx[idx]];
102             } else {
103                 pInstance[i] = pMixPrivInst[2];
104             }
105             channel &= ~(1 << idx);
106         }
107     }
108 
109     if (NrFrames <= 0) return;
110 
111     /******************************************************************************
112        SOFT MIXING
113     *******************************************************************************/
114 
115     if ((pInstance1->Current != pInstance1->Target) ||
116         (pInstance2->Current != pInstance2->Target)) {
117         // TODO: combine similar checks below.
118         if (pInstance1->Delta == LVM_MAXFLOAT ||
119             Abs_Float(pInstance1->Current - pInstance1->Target) < pInstance1->Delta) {
120             /* Difference is not significant anymore. Make them equal. */
121             pInstance1->Current = pInstance1->Target;
122             TargetGain = pInstance1->Target;
123             LVC_Mixer_SetTarget(&(ptrInstance->MixerStream[0]), TargetGain);
124         } else {
125             /* Soft mixing has to be applied */
126             HardMixing = FALSE;
127         }
128 
129         if (HardMixing == TRUE) {
130             if (pInstance2->Delta == LVM_MAXFLOAT ||
131                 Abs_Float(pInstance2->Current - pInstance2->Target) < pInstance2->Delta) {
132                 /* Difference is not significant anymore. Make them equal. */
133                 pInstance2->Current = pInstance2->Target;
134                 TargetGain = pInstance2->Target;
135                 LVC_Mixer_SetTarget(&(ptrInstance->MixerStream[1]), TargetGain);
136             } else {
137                 /* Soft mixing has to be applied */
138                 HardMixing = FALSE;
139             }
140         }
141 
142         if (HardMixing == FALSE) {
143             LVC_Core_MixSoft_1St_MC_float_WRA(&pInstance[0], src, dst, NrFrames, NrChannels);
144         }
145     }
146 
147     /******************************************************************************
148        HARD MIXING
149     *******************************************************************************/
150 
151     if (HardMixing == TRUE) {
152         if ((pInstance1->Target == LVM_MAXFLOAT) && (pInstance2->Target == LVM_MAXFLOAT)) {
153             if (src != dst) {
154                 Copy_Float(src, dst, NrFrames * NrChannels);
155             }
156         } else {
157             LVC_Core_MixHard_1St_MC_float_SAT(&(pInstance[0]), src, dst, NrFrames, NrChannels);
158         }
159     }
160 
161     /******************************************************************************
162        CALL BACK
163     *******************************************************************************/
164 
165     if (ptrInstance->MixerStream[0].CallbackSet) {
166         if (Abs_Float(pInstance1->Current - pInstance1->Target) < pInstance1->Delta) {
167             pInstance1->Current = pInstance1->Target; /* Difference is not significant anymore. \
168                                                          Make them equal. */
169             TargetGain = pInstance1->Target;
170             LVC_Mixer_SetTarget(&ptrInstance->MixerStream[0], TargetGain);
171             ptrInstance->MixerStream[0].CallbackSet = FALSE;
172             if (ptrInstance->MixerStream[0].pCallBack != 0) {
173                 (*ptrInstance->MixerStream[0].pCallBack)(
174                         ptrInstance->MixerStream[0].pCallbackHandle,
175                         ptrInstance->MixerStream[0].pGeneralPurpose,
176                         ptrInstance->MixerStream[0].CallbackParam);
177             }
178         }
179     }
180     if (ptrInstance->MixerStream[1].CallbackSet) {
181         if (Abs_Float(pInstance2->Current - pInstance2->Target) < pInstance2->Delta) {
182             pInstance2->Current = pInstance2->Target; /* Difference is not significant anymore.
183                                                          Make them equal. */
184             TargetGain = pInstance2->Target;
185             LVC_Mixer_SetTarget(&ptrInstance->MixerStream[1], TargetGain);
186             ptrInstance->MixerStream[1].CallbackSet = FALSE;
187             if (ptrInstance->MixerStream[1].pCallBack != 0) {
188                 (*ptrInstance->MixerStream[1].pCallBack)(
189                         ptrInstance->MixerStream[1].pCallbackHandle,
190                         ptrInstance->MixerStream[1].pGeneralPurpose,
191                         ptrInstance->MixerStream[1].CallbackParam);
192             }
193         }
194     }
195 }
196