Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 1 | /* |
wjiang | ee2ff96 | 2014-03-18 06:49:46 +0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved. |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 3 | * Not a Contribution. |
| 4 | * |
| 5 | * Copyright (C) 2013 The Android Open Source Project |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #define LOG_TAG "offload_effect_virtualizer" |
| 21 | #define LOG_NDEBUG 0 |
| 22 | |
| 23 | #include <cutils/list.h> |
| 24 | #include <cutils/log.h> |
| 25 | #include <tinyalsa/asoundlib.h> |
Subhash Chandra Bose Naripeddy | 090a2aa | 2014-01-30 14:03:12 -0800 | [diff] [blame] | 26 | #include <sound/audio_effects.h> |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 27 | #include <audio_effects/effect_virtualizer.h> |
| 28 | |
| 29 | #include "effect_api.h" |
| 30 | #include "virtualizer.h" |
| 31 | |
| 32 | /* Offload Virtualizer UUID: 509a4498-561a-4bea-b3b1-0002a5d5c51b */ |
| 33 | const effect_descriptor_t virtualizer_descriptor = { |
| 34 | {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, |
| 35 | {0x509a4498, 0x561a, 0x4bea, 0xb3b1, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid |
| 36 | EFFECT_CONTROL_API_VERSION, |
| 37 | (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_HW_ACC_TUNNEL), |
| 38 | 0, /* TODO */ |
| 39 | 1, |
| 40 | "MSM offload virtualizer", |
| 41 | "The Android Open Source Project", |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * Virtualizer operations |
| 46 | */ |
| 47 | |
| 48 | int virtualizer_get_strength(virtualizer_context_t *context) |
| 49 | { |
| 50 | ALOGV("%s: strength: %d", __func__, context->strength); |
| 51 | return context->strength; |
| 52 | } |
| 53 | |
| 54 | int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength) |
| 55 | { |
| 56 | ALOGV("%s: strength: %d", __func__, strength); |
| 57 | context->strength = strength; |
| 58 | |
| 59 | offload_virtualizer_set_strength(&(context->offload_virt), strength); |
| 60 | if (context->ctl) |
| 61 | offload_virtualizer_send_params(context->ctl, context->offload_virt, |
| 62 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 63 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p, |
| 68 | uint32_t *size) |
| 69 | { |
| 70 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 71 | int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t); |
| 72 | int32_t *param_tmp = (int32_t *)p->data; |
| 73 | int32_t param = *param_tmp++; |
| 74 | void *value = p->data + voffset; |
| 75 | int i; |
| 76 | |
| 77 | ALOGV("%s", __func__); |
| 78 | |
| 79 | p->status = 0; |
| 80 | |
| 81 | switch (param) { |
| 82 | case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: |
| 83 | if (p->vsize < sizeof(uint32_t)) |
| 84 | p->status = -EINVAL; |
| 85 | p->vsize = sizeof(uint32_t); |
| 86 | break; |
| 87 | case VIRTUALIZER_PARAM_STRENGTH: |
| 88 | if (p->vsize < sizeof(int16_t)) |
| 89 | p->status = -EINVAL; |
| 90 | p->vsize = sizeof(int16_t); |
| 91 | break; |
| 92 | default: |
| 93 | p->status = -EINVAL; |
| 94 | } |
| 95 | |
| 96 | *size = sizeof(effect_param_t) + voffset + p->vsize; |
| 97 | |
| 98 | if (p->status != 0) |
| 99 | return 0; |
| 100 | |
| 101 | switch (param) { |
| 102 | case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: |
| 103 | ALOGV("%s: VIRTUALIZER_PARAM_STRENGTH_SUPPORTED", __func__); |
| 104 | *(uint32_t *)value = 1; |
| 105 | break; |
| 106 | |
| 107 | case VIRTUALIZER_PARAM_STRENGTH: |
| 108 | ALOGV("%s: VIRTUALIZER_PARAM_STRENGTH", __func__); |
| 109 | *(int16_t *)value = virtualizer_get_strength(virt_ctxt); |
| 110 | break; |
| 111 | |
| 112 | default: |
| 113 | p->status = -EINVAL; |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p, |
| 121 | uint32_t size) |
| 122 | { |
| 123 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 124 | int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t); |
| 125 | void *value = p->data + voffset; |
| 126 | int32_t *param_tmp = (int32_t *)p->data; |
| 127 | int32_t param = *param_tmp++; |
| 128 | uint32_t strength; |
| 129 | |
| 130 | ALOGV("%s", __func__); |
| 131 | |
| 132 | p->status = 0; |
| 133 | |
| 134 | switch (param) { |
| 135 | case VIRTUALIZER_PARAM_STRENGTH: |
| 136 | ALOGV("%s VIRTUALIZER_PARAM_STRENGTH", __func__); |
| 137 | strength = (uint32_t)(*(int16_t *)value); |
| 138 | virtualizer_set_strength(virt_ctxt, strength); |
| 139 | break; |
| 140 | default: |
| 141 | p->status = -EINVAL; |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int virtualizer_set_device(effect_context_t *context, uint32_t device) |
| 149 | { |
| 150 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 151 | |
| 152 | ALOGV("%s: device: %d", __func__, device); |
| 153 | virt_ctxt->device = device; |
| 154 | if((device == AUDIO_DEVICE_OUT_SPEAKER) || |
| 155 | (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) || |
wjiang | ee2ff96 | 2014-03-18 06:49:46 +0800 | [diff] [blame] | 156 | (device == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER) || |
| 157 | (device == AUDIO_DEVICE_OUT_PROXY) || |
| 158 | (device == AUDIO_DEVICE_OUT_AUX_DIGITAL) || |
| 159 | (device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET)) { |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 160 | if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) { |
| 161 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false); |
| 162 | virt_ctxt->temp_disabled = true; |
wjiang | 95d74c2 | 2014-03-28 12:29:58 +0800 | [diff] [blame] | 163 | if (virt_ctxt->ctl) |
| 164 | offload_virtualizer_send_params(virt_ctxt->ctl, |
| 165 | virt_ctxt->offload_virt, |
| 166 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 167 | } |
| 168 | } else { |
| 169 | if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) && |
| 170 | virt_ctxt->temp_disabled) { |
| 171 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); |
| 172 | virt_ctxt->temp_disabled = false; |
wjiang | 95d74c2 | 2014-03-28 12:29:58 +0800 | [diff] [blame] | 173 | if (virt_ctxt->ctl) |
| 174 | offload_virtualizer_send_params(virt_ctxt->ctl, |
| 175 | virt_ctxt->offload_virt, |
| 176 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 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 | |
| 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 | 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__); |
wjiang | 95d74c2 | 2014-03-28 12:29:58 +0800 | [diff] [blame] | 224 | |
| 225 | if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) && |
| 226 | !(virt_ctxt->temp_disabled)) { |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 227 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); |
wjiang | d45948e | 2014-02-24 22:19:43 +0800 | [diff] [blame] | 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 | } |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 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; |
Amit Shekhar | d02f2cd | 2014-01-16 16:51:43 -0800 | [diff] [blame] | 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); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | int virtualizer_stop(effect_context_t *context, output_context_t *output) |
| 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 | } |