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 0, /* TODO */ 36 1, 37 "MSM offload virtualizer", 38 "The Android Open Source Project", 39 }; 40 41 /* 42 * Virtualizer operations 43 */ 44 45 int virtualizer_get_strength(virtualizer_context_t *context) 46 { 47 ALOGV("%s: strength: %d", __func__, context->strength); 48 return context->strength; 49 } 50 51 int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength) 52 { 53 ALOGV("%s: strength: %d", __func__, strength); 54 context->strength = strength; 55 56 offload_virtualizer_set_strength(&(context->offload_virt), strength); 57 if (context->ctl) 58 offload_virtualizer_send_params(context->ctl, &context->offload_virt, 59 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | 60 OFFLOAD_SEND_VIRTUALIZER_STRENGTH); 61 return 0; 62 } 63 64 int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p, 65 uint32_t *size) 66 { 67 virtualizer_context_t *virt_ctxt = (virtualizer_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 VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: 80 if (p->vsize < sizeof(uint32_t)) 81 p->status = -EINVAL; 82 p->vsize = sizeof(uint32_t); 83 break; 84 case VIRTUALIZER_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 VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: 100 ALOGV("%s: VIRTUALIZER_PARAM_STRENGTH_SUPPORTED", __func__); 101 *(uint32_t *)value = 1; 102 break; 103 104 case VIRTUALIZER_PARAM_STRENGTH: 105 ALOGV("%s: VIRTUALIZER_PARAM_STRENGTH", __func__); 106 *(int16_t *)value = virtualizer_get_strength(virt_ctxt); 107 break; 108 109 default: 110 p->status = -EINVAL; 111 break; 112 } 113 114 return 0; 115 } 116 117 int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p, 118 uint32_t size __unused) 119 { 120 virtualizer_context_t *virt_ctxt = (virtualizer_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 VIRTUALIZER_PARAM_STRENGTH: 133 ALOGV("%s VIRTUALIZER_PARAM_STRENGTH", __func__); 134 strength = (uint32_t)(*(int16_t *)value); 135 virtualizer_set_strength(virt_ctxt, strength); 136 break; 137 default: 138 p->status = -EINVAL; 139 break; 140 } 141 142 return 0; 143 } 144 145 int virtualizer_set_device(effect_context_t *context, uint32_t device) 146 { 147 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 148 149 ALOGV("%s: device: %d", __func__, device); 150 virt_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 (!virt_ctxt->temp_disabled) { 157 if (effect_is_active(&virt_ctxt->common)) { 158 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false); 159 if (virt_ctxt->ctl) 160 offload_virtualizer_send_params(virt_ctxt->ctl, 161 &virt_ctxt->offload_virt, 162 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); 163 } 164 virt_ctxt->temp_disabled = true; 165 } 166 } else { 167 if (virt_ctxt->temp_disabled) { 168 if (effect_is_active(&virt_ctxt->common)) { 169 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); 170 if (virt_ctxt->ctl) 171 offload_virtualizer_send_params(virt_ctxt->ctl, 172 &virt_ctxt->offload_virt, 173 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); 174 } 175 virt_ctxt->temp_disabled = false; 176 } 177 } 178 offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device); 179 return 0; 180 } 181 182 int virtualizer_reset(effect_context_t *context) 183 { 184 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 185 186 return 0; 187 } 188 189 int virtualizer_init(effect_context_t *context) 190 { 191 ALOGV("%s", __func__); 192 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 193 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 virt_ctxt->temp_disabled = false; 214 memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params)); 215 216 return 0; 217 } 218 219 int virtualizer_enable(effect_context_t *context) 220 { 221 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 222 223 ALOGV("%s", __func__); 224 225 if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) && 226 !(virt_ctxt->temp_disabled)) { 227 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); 228 if (virt_ctxt->ctl && virt_ctxt->strength) 229 offload_virtualizer_send_params(virt_ctxt->ctl, 230 &virt_ctxt->offload_virt, 231 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | 232 OFFLOAD_SEND_BASSBOOST_STRENGTH); 233 } 234 return 0; 235 } 236 237 int virtualizer_disable(effect_context_t *context) 238 { 239 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 240 241 ALOGV("%s", __func__); 242 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) { 243 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false); 244 if (virt_ctxt->ctl) 245 offload_virtualizer_send_params(virt_ctxt->ctl, 246 &virt_ctxt->offload_virt, 247 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); 248 } 249 return 0; 250 } 251 252 int virtualizer_start(effect_context_t *context, output_context_t *output) 253 { 254 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 255 256 ALOGV("%s", __func__); 257 virt_ctxt->ctl = output->ctl; 258 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) 259 if (virt_ctxt->ctl) 260 offload_virtualizer_send_params(virt_ctxt->ctl, &virt_ctxt->offload_virt, 261 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | 262 OFFLOAD_SEND_VIRTUALIZER_STRENGTH); 263 return 0; 264 } 265 266 int virtualizer_stop(effect_context_t *context, output_context_t *output __unused) 267 { 268 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; 269 270 ALOGV("%s", __func__); 271 virt_ctxt->ctl = NULL; 272 return 0; 273 } 274