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_bass_boost"
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_bassboost.h>
25 
26 #include "effect_api.h"
27 #include "bass_boost.h"
28 
29 /* Offload bassboost UUID: 2c4a8c24-1581-487f-94f6-0002a5d5c51b */
30 const effect_descriptor_t bassboost_descriptor = {
31         {0x0634f220, 0xddd4, 0x11db, 0xa0fc, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }},
32         {0x2c4a8c24, 0x1581, 0x487f, 0x94f6, { 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 bassboost",
39         "The Android Open Source Project",
40 };
41 
42 /*
43  * Bassboost operations
44  */
45 
bassboost_get_strength(bassboost_context_t * context)46 int bassboost_get_strength(bassboost_context_t *context)
47 {
48     ALOGV("%s: strength: %d", __func__, context->strength);
49     return context->strength;
50 }
51 
bassboost_set_strength(bassboost_context_t * context,uint32_t strength)52 int bassboost_set_strength(bassboost_context_t *context, uint32_t strength)
53 {
54     ALOGV("%s: strength: %d", __func__, strength);
55     context->strength = strength;
56 
57     offload_bassboost_set_strength(&(context->offload_bass), strength);
58     if (context->ctl)
59         offload_bassboost_send_params(context->ctl, &context->offload_bass,
60                                       OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
61                                       OFFLOAD_SEND_BASSBOOST_STRENGTH);
62     return 0;
63 }
64 
bassboost_get_parameter(effect_context_t * context,effect_param_t * p,uint32_t * size)65 int bassboost_get_parameter(effect_context_t *context, effect_param_t *p,
66                             uint32_t *size)
67 {
68     bassboost_context_t *bass_ctxt = (bassboost_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 BASSBOOST_PARAM_STRENGTH_SUPPORTED:
81         if (p->vsize < sizeof(uint32_t))
82            p->status = -EINVAL;
83         p->vsize = sizeof(uint32_t);
84         break;
85     case BASSBOOST_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 BASSBOOST_PARAM_STRENGTH_SUPPORTED:
101         ALOGV("%s: BASSBOOST_PARAM_STRENGTH_SUPPORTED", __func__);
102         *(uint32_t *)value = 1;
103         break;
104 
105     case BASSBOOST_PARAM_STRENGTH:
106         ALOGV("%s: BASSBOOST_PARAM_STRENGTH", __func__);
107         *(int16_t *)value = bassboost_get_strength(bass_ctxt);
108         break;
109 
110     default:
111         p->status = -EINVAL;
112         break;
113     }
114 
115     return 0;
116 }
117 
bassboost_set_parameter(effect_context_t * context,effect_param_t * p,uint32_t size __unused)118 int bassboost_set_parameter(effect_context_t *context, effect_param_t *p,
119                             uint32_t size __unused)
120 {
121     bassboost_context_t *bass_ctxt = (bassboost_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 BASSBOOST_PARAM_STRENGTH:
134         ALOGV("%s BASSBOOST_PARAM_STRENGTH", __func__);
135         strength = (uint32_t)(*(int16_t *)value);
136         bassboost_set_strength(bass_ctxt, strength);
137         break;
138     default:
139         p->status = -EINVAL;
140         break;
141     }
142 
143     return 0;
144 }
145 
bassboost_set_device(effect_context_t * context,uint32_t device)146 int bassboost_set_device(effect_context_t *context, uint32_t device)
147 {
148     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
149 
150     ALOGV("%s: device: %d", __func__, device);
151     bass_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 (!bass_ctxt->temp_disabled) {
158             if (effect_is_active(&bass_ctxt->common)) {
159                 offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), false);
160                 if (bass_ctxt->ctl)
161                     offload_bassboost_send_params(bass_ctxt->ctl,
162                                                   &bass_ctxt->offload_bass,
163                                                   OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG);
164             }
165             bass_ctxt->temp_disabled = true;
166         }
167     } else {
168         if (bass_ctxt->temp_disabled) {
169             if (effect_is_active(&bass_ctxt->common)) {
170                 offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), true);
171                 if (bass_ctxt->ctl)
172                     offload_bassboost_send_params(bass_ctxt->ctl,
173                                                   &bass_ctxt->offload_bass,
174                                                   OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG);
175             }
176             bass_ctxt->temp_disabled = false;
177         }
178     }
179     offload_bassboost_set_device(&(bass_ctxt->offload_bass), device);
180     return 0;
181 }
182 
bassboost_reset(effect_context_t * context)183 int bassboost_reset(effect_context_t *context)
184 {
185     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
186 
187     return 0;
188 }
189 
bassboost_init(effect_context_t * context)190 int bassboost_init(effect_context_t *context)
191 {
192     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
193 
194     ALOGV("%s", __func__);
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     bass_ctxt->temp_disabled = false;
215     memset(&(bass_ctxt->offload_bass), 0, sizeof(struct bass_boost_params));
216 
217     return 0;
218 }
219 
bassboost_enable(effect_context_t * context)220 int bassboost_enable(effect_context_t *context)
221 {
222     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
223 
224     ALOGV("%s", __func__);
225 
226     if (!offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass)) &&
227         !(bass_ctxt->temp_disabled)) {
228         offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), true);
229         if (bass_ctxt->ctl && bass_ctxt->strength)
230             offload_bassboost_send_params(bass_ctxt->ctl,
231                                           &bass_ctxt->offload_bass,
232                                           OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
233                                           OFFLOAD_SEND_BASSBOOST_STRENGTH);
234     }
235     return 0;
236 }
237 
bassboost_disable(effect_context_t * context)238 int bassboost_disable(effect_context_t *context)
239 {
240     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
241 
242     ALOGV("%s", __func__);
243     if (offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass))) {
244         offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), false);
245         if (bass_ctxt->ctl)
246             offload_bassboost_send_params(bass_ctxt->ctl,
247                                           &bass_ctxt->offload_bass,
248                                           OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG);
249     }
250     return 0;
251 }
252 
bassboost_start(effect_context_t * context,output_context_t * output)253 int bassboost_start(effect_context_t *context, output_context_t *output)
254 {
255     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
256 
257     ALOGV("%s", __func__);
258     bass_ctxt->ctl = output->ctl;
259     ALOGV("output->ctl: %p", output->ctl);
260     if (offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass)))
261         if (bass_ctxt->ctl)
262             offload_bassboost_send_params(bass_ctxt->ctl, &bass_ctxt->offload_bass,
263                                           OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
264                                           OFFLOAD_SEND_BASSBOOST_STRENGTH);
265     return 0;
266 }
267 
bassboost_stop(effect_context_t * context,output_context_t * output __unused)268 int bassboost_stop(effect_context_t *context, output_context_t *output __unused)
269 {
270     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
271 
272     ALOGV("%s", __func__);
273     bass_ctxt->ctl = NULL;
274     return 0;
275 }
276