1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "offload_effect_virtualizer"
18 //#define LOG_NDEBUG 0
19 
20 #include <cutils/list.h>
21 #include <cutils/log.h>
22 #include <tinyalsa/asoundlib.h>
23 #include <sound/audio_effects.h>
24 #include <audio_effects/effect_virtualizer.h>
25 
26 #include "effect_api.h"
27 #include "virtualizer.h"
28 
29 /* Offload Virtualizer UUID: 509a4498-561a-4bea-b3b1-0002a5d5c51b */
30 const effect_descriptor_t virtualizer_descriptor = {
31         {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
32         {0x509a4498, 0x561a, 0x4bea, 0xb3b1, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
33         EFFECT_CONTROL_API_VERSION,
34         (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_HW_ACC_TUNNEL |
35                 EFFECT_FLAG_VOLUME_CTRL),
36         0, /* TODO */
37         1,
38         "MSM offload virtualizer",
39         "The Android Open Source Project",
40 };
41 
42 /*
43  * Virtualizer operations
44  */
45 
virtualizer_get_strength(virtualizer_context_t * context)46 int virtualizer_get_strength(virtualizer_context_t *context)
47 {
48     ALOGV("%s: strength: %d", __func__, context->strength);
49     return context->strength;
50 }
51 
virtualizer_set_strength(virtualizer_context_t * context,uint32_t strength)52 int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength)
53 {
54     ALOGV("%s: strength: %d", __func__, strength);
55     context->strength = strength;
56 
57     offload_virtualizer_set_strength(&(context->offload_virt), strength);
58     if (context->ctl)
59         offload_virtualizer_send_params(context->ctl, &context->offload_virt,
60                                         OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
61                                         OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
62     return 0;
63 }
64 
virtualizer_get_parameter(effect_context_t * context,effect_param_t * p,uint32_t * size)65 int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p,
66                               uint32_t *size)
67 {
68     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
69     int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
70     int32_t *param_tmp = (int32_t *)p->data;
71     int32_t param = *param_tmp++;
72     void *value = p->data + voffset;
73     int i;
74 
75     ALOGV("%s", __func__);
76 
77     p->status = 0;
78 
79     switch (param) {
80     case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
81         if (p->vsize < sizeof(uint32_t))
82            p->status = -EINVAL;
83         p->vsize = sizeof(uint32_t);
84         break;
85     case VIRTUALIZER_PARAM_STRENGTH:
86         if (p->vsize < sizeof(int16_t))
87            p->status = -EINVAL;
88         p->vsize = sizeof(int16_t);
89         break;
90     default:
91         p->status = -EINVAL;
92     }
93 
94     *size = sizeof(effect_param_t) + voffset + p->vsize;
95 
96     if (p->status != 0)
97         return 0;
98 
99     switch (param) {
100     case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
101 	ALOGV("%s: VIRTUALIZER_PARAM_STRENGTH_SUPPORTED", __func__);
102         *(uint32_t *)value = 1;
103         break;
104 
105     case VIRTUALIZER_PARAM_STRENGTH:
106 	ALOGV("%s: VIRTUALIZER_PARAM_STRENGTH", __func__);
107         *(int16_t *)value = virtualizer_get_strength(virt_ctxt);
108         break;
109 
110     default:
111         p->status = -EINVAL;
112         break;
113     }
114 
115     return 0;
116 }
117 
virtualizer_set_parameter(effect_context_t * context,effect_param_t * p,uint32_t size __unused)118 int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p,
119                               uint32_t size __unused)
120 {
121     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
122     int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
123     void *value = p->data + voffset;
124     int32_t *param_tmp = (int32_t *)p->data;
125     int32_t param = *param_tmp++;
126     uint32_t strength;
127 
128     ALOGV("%s", __func__);
129 
130     p->status = 0;
131 
132     switch (param) {
133     case VIRTUALIZER_PARAM_STRENGTH:
134 	ALOGV("%s VIRTUALIZER_PARAM_STRENGTH", __func__);
135         strength = (uint32_t)(*(int16_t *)value);
136         virtualizer_set_strength(virt_ctxt, strength);
137         break;
138     default:
139         p->status = -EINVAL;
140         break;
141     }
142 
143     return 0;
144 }
145 
virtualizer_set_device(effect_context_t * context,uint32_t device)146 int virtualizer_set_device(effect_context_t *context, uint32_t device)
147 {
148     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
149 
150     ALOGV("%s: device: %d", __func__, device);
151     virt_ctxt->device = device;
152     if ((device == AUDIO_DEVICE_OUT_SPEAKER) ||
153        (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) ||
154        (device == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER) ||
155        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
156        (device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET)) {
157         if (!virt_ctxt->temp_disabled) {
158             if (effect_is_active(&virt_ctxt->common)) {
159                 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
160                 if (virt_ctxt->ctl)
161                     offload_virtualizer_send_params(virt_ctxt->ctl,
162                                                     &virt_ctxt->offload_virt,
163                                                     OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
164             }
165             virt_ctxt->temp_disabled = true;
166         }
167     } else {
168         if (virt_ctxt->temp_disabled) {
169             if (effect_is_active(&virt_ctxt->common)) {
170                 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
171                 if (virt_ctxt->ctl)
172                     offload_virtualizer_send_params(virt_ctxt->ctl,
173                                                     &virt_ctxt->offload_virt,
174                                                     OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
175             }
176             virt_ctxt->temp_disabled = false;
177         }
178     }
179     offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device);
180     return 0;
181 }
182 
virtualizer_reset(effect_context_t * context)183 int virtualizer_reset(effect_context_t *context)
184 {
185     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
186 
187     return 0;
188 }
189 
virtualizer_init(effect_context_t * context)190 int virtualizer_init(effect_context_t *context)
191 {
192     ALOGV("%s", __func__);
193     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
194 
195     context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
196     context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
197     context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
198     context->config.inputCfg.samplingRate = 44100;
199     context->config.inputCfg.bufferProvider.getBuffer = NULL;
200     context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
201     context->config.inputCfg.bufferProvider.cookie = NULL;
202     context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
203     context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
204     context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
205     context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
206     context->config.outputCfg.samplingRate = 44100;
207     context->config.outputCfg.bufferProvider.getBuffer = NULL;
208     context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
209     context->config.outputCfg.bufferProvider.cookie = NULL;
210     context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
211 
212     set_config(context, &context->config);
213 
214     virt_ctxt->temp_disabled = false;
215     memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params));
216 
217     return 0;
218 }
219 
virtualizer_enable(effect_context_t * context)220 int virtualizer_enable(effect_context_t *context)
221 {
222     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
223 
224     ALOGV("%s", __func__);
225 
226     if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) &&
227         !(virt_ctxt->temp_disabled)) {
228         offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
229         if (virt_ctxt->ctl && virt_ctxt->strength)
230             offload_virtualizer_send_params(virt_ctxt->ctl,
231                                           &virt_ctxt->offload_virt,
232                                           OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
233                                           OFFLOAD_SEND_BASSBOOST_STRENGTH);
234     }
235     return 0;
236 }
237 
virtualizer_disable(effect_context_t * context)238 int virtualizer_disable(effect_context_t *context)
239 {
240     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
241 
242     ALOGV("%s", __func__);
243     if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
244         offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
245         if (virt_ctxt->ctl)
246             offload_virtualizer_send_params(virt_ctxt->ctl,
247                                           &virt_ctxt->offload_virt,
248                                           OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
249     }
250     return 0;
251 }
252 
virtualizer_start(effect_context_t * context,output_context_t * output)253 int virtualizer_start(effect_context_t *context, output_context_t *output)
254 {
255     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
256 
257     ALOGV("%s", __func__);
258     virt_ctxt->ctl = output->ctl;
259     if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)))
260         if (virt_ctxt->ctl)
261             offload_virtualizer_send_params(virt_ctxt->ctl, &virt_ctxt->offload_virt,
262                                           OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
263                                           OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
264     return 0;
265 }
266 
virtualizer_stop(effect_context_t * context,output_context_t * output __unused)267 int virtualizer_stop(effect_context_t *context, output_context_t *output __unused)
268 {
269     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
270 
271     ALOGV("%s", __func__);
272     virt_ctxt->ctl = NULL;
273     return 0;
274 }
275