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         0, /* TODO */
36         1,
37         "MSM offload bassboost",
38         "The Android Open Source Project",
39 };
40 
41 /*
42  * Bassboost operations
43  */
44 
bassboost_get_strength(bassboost_context_t * context)45 int bassboost_get_strength(bassboost_context_t *context)
46 {
47     ALOGV("%s: strength: %d", __func__, context->strength);
48     return context->strength;
49 }
50 
bassboost_set_strength(bassboost_context_t * context,uint32_t strength)51 int bassboost_set_strength(bassboost_context_t *context, uint32_t strength)
52 {
53     ALOGV("%s: strength: %d", __func__, strength);
54     context->strength = strength;
55 
56     offload_bassboost_set_strength(&(context->offload_bass), strength);
57     if (context->ctl)
58         offload_bassboost_send_params(context->ctl, &context->offload_bass,
59                                       OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
60                                       OFFLOAD_SEND_BASSBOOST_STRENGTH);
61     return 0;
62 }
63 
bassboost_get_parameter(effect_context_t * context,effect_param_t * p,uint32_t * size)64 int bassboost_get_parameter(effect_context_t *context, effect_param_t *p,
65                             uint32_t *size)
66 {
67     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
68     int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
69     int32_t *param_tmp = (int32_t *)p->data;
70     int32_t param = *param_tmp++;
71     void *value = p->data + voffset;
72     int i;
73 
74     ALOGV("%s", __func__);
75 
76     p->status = 0;
77 
78     switch (param) {
79     case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
80         if (p->vsize < sizeof(uint32_t))
81            p->status = -EINVAL;
82         p->vsize = sizeof(uint32_t);
83         break;
84     case BASSBOOST_PARAM_STRENGTH:
85         if (p->vsize < sizeof(int16_t))
86            p->status = -EINVAL;
87         p->vsize = sizeof(int16_t);
88         break;
89     default:
90         p->status = -EINVAL;
91     }
92 
93     *size = sizeof(effect_param_t) + voffset + p->vsize;
94 
95     if (p->status != 0)
96         return 0;
97 
98     switch (param) {
99     case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
100         ALOGV("%s: BASSBOOST_PARAM_STRENGTH_SUPPORTED", __func__);
101         *(uint32_t *)value = 1;
102         break;
103 
104     case BASSBOOST_PARAM_STRENGTH:
105         ALOGV("%s: BASSBOOST_PARAM_STRENGTH", __func__);
106         *(int16_t *)value = bassboost_get_strength(bass_ctxt);
107         break;
108 
109     default:
110         p->status = -EINVAL;
111         break;
112     }
113 
114     return 0;
115 }
116 
bassboost_set_parameter(effect_context_t * context,effect_param_t * p,uint32_t size __unused)117 int bassboost_set_parameter(effect_context_t *context, effect_param_t *p,
118                             uint32_t size __unused)
119 {
120     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
121     int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
122     void *value = p->data + voffset;
123     int32_t *param_tmp = (int32_t *)p->data;
124     int32_t param = *param_tmp++;
125     uint32_t strength;
126 
127     ALOGV("%s", __func__);
128 
129     p->status = 0;
130 
131     switch (param) {
132     case BASSBOOST_PARAM_STRENGTH:
133         ALOGV("%s BASSBOOST_PARAM_STRENGTH", __func__);
134         strength = (uint32_t)(*(int16_t *)value);
135         bassboost_set_strength(bass_ctxt, strength);
136         break;
137     default:
138         p->status = -EINVAL;
139         break;
140     }
141 
142     return 0;
143 }
144 
bassboost_set_device(effect_context_t * context,uint32_t device)145 int bassboost_set_device(effect_context_t *context, uint32_t device)
146 {
147     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
148 
149     ALOGV("%s: device: %d", __func__, device);
150     bass_ctxt->device = device;
151     if ((device == AUDIO_DEVICE_OUT_SPEAKER) ||
152        (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) ||
153        (device == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER) ||
154        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
155        (device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET)) {
156         if (!bass_ctxt->temp_disabled) {
157             if (effect_is_active(&bass_ctxt->common)) {
158                 offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), false);
159                 if (bass_ctxt->ctl)
160                     offload_bassboost_send_params(bass_ctxt->ctl,
161                                                   &bass_ctxt->offload_bass,
162                                                   OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG);
163             }
164             bass_ctxt->temp_disabled = true;
165         }
166     } else {
167         if (bass_ctxt->temp_disabled) {
168             if (effect_is_active(&bass_ctxt->common)) {
169                 offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), true);
170                 if (bass_ctxt->ctl)
171                     offload_bassboost_send_params(bass_ctxt->ctl,
172                                                   &bass_ctxt->offload_bass,
173                                                   OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG);
174             }
175             bass_ctxt->temp_disabled = false;
176         }
177     }
178     offload_bassboost_set_device(&(bass_ctxt->offload_bass), device);
179     return 0;
180 }
181 
bassboost_reset(effect_context_t * context)182 int bassboost_reset(effect_context_t *context)
183 {
184     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
185 
186     return 0;
187 }
188 
bassboost_init(effect_context_t * context)189 int bassboost_init(effect_context_t *context)
190 {
191     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
192 
193     ALOGV("%s", __func__);
194     context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
195     context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
196     context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
197     context->config.inputCfg.samplingRate = 44100;
198     context->config.inputCfg.bufferProvider.getBuffer = NULL;
199     context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
200     context->config.inputCfg.bufferProvider.cookie = NULL;
201     context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
202     context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
203     context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
204     context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
205     context->config.outputCfg.samplingRate = 44100;
206     context->config.outputCfg.bufferProvider.getBuffer = NULL;
207     context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
208     context->config.outputCfg.bufferProvider.cookie = NULL;
209     context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
210 
211     set_config(context, &context->config);
212 
213     bass_ctxt->temp_disabled = false;
214     memset(&(bass_ctxt->offload_bass), 0, sizeof(struct bass_boost_params));
215 
216     return 0;
217 }
218 
bassboost_enable(effect_context_t * context)219 int bassboost_enable(effect_context_t *context)
220 {
221     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
222 
223     ALOGV("%s", __func__);
224 
225     if (!offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass)) &&
226         !(bass_ctxt->temp_disabled)) {
227         offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), true);
228         if (bass_ctxt->ctl && bass_ctxt->strength)
229             offload_bassboost_send_params(bass_ctxt->ctl,
230                                           &bass_ctxt->offload_bass,
231                                           OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
232                                           OFFLOAD_SEND_BASSBOOST_STRENGTH);
233     }
234     return 0;
235 }
236 
bassboost_disable(effect_context_t * context)237 int bassboost_disable(effect_context_t *context)
238 {
239     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
240 
241     ALOGV("%s", __func__);
242     if (offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass))) {
243         offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), false);
244         if (bass_ctxt->ctl)
245             offload_bassboost_send_params(bass_ctxt->ctl,
246                                           &bass_ctxt->offload_bass,
247                                           OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG);
248     }
249     return 0;
250 }
251 
bassboost_start(effect_context_t * context,output_context_t * output)252 int bassboost_start(effect_context_t *context, output_context_t *output)
253 {
254     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
255 
256     ALOGV("%s", __func__);
257     bass_ctxt->ctl = output->ctl;
258     ALOGV("output->ctl: %p", output->ctl);
259     if (offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass)))
260         if (bass_ctxt->ctl)
261             offload_bassboost_send_params(bass_ctxt->ctl, &bass_ctxt->offload_bass,
262                                           OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
263                                           OFFLOAD_SEND_BASSBOOST_STRENGTH);
264     return 0;
265 }
266 
bassboost_stop(effect_context_t * context,output_context_t * output __unused)267 int bassboost_stop(effect_context_t *context, output_context_t *output __unused)
268 {
269     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
270 
271     ALOGV("%s", __func__);
272     bass_ctxt->ctl = NULL;
273     return 0;
274 }
275