Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "Visualizer" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <cutils/log.h> |
| 20 | #include <assert.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <new> |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 24 | #include <time.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 25 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 26 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 27 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 28 | extern "C" { |
| 29 | |
| 30 | // effect_handle_t interface implementation for visualizer effect |
| 31 | extern const struct effect_interface_s gVisualizerInterface; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 32 | |
| 33 | // Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b |
| 34 | const effect_descriptor_t gVisualizerDescriptor = { |
| 35 | {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type |
| 36 | {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 37 | EFFECT_CONTROL_API_VERSION, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 38 | (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST), |
| 39 | 0, // TODO |
| 40 | 1, |
| 41 | "Visualizer", |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 42 | "The Android Open Source Project", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | enum visualizer_state_e { |
| 46 | VISUALIZER_STATE_UNINITIALIZED, |
| 47 | VISUALIZER_STATE_INITIALIZED, |
| 48 | VISUALIZER_STATE_ACTIVE, |
| 49 | }; |
| 50 | |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 51 | // maximum time since last capture buffer update before resetting capture buffer. This means |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 52 | // that the framework has stopped playing audio and we must start returning silence |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 53 | #define MAX_STALL_TIME_MS 1000 |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 54 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 55 | struct VisualizerContext { |
| 56 | const struct effect_interface_s *mItfe; |
| 57 | effect_config_t mConfig; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 58 | uint32_t mCaptureIdx; |
| 59 | uint32_t mCaptureSize; |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 60 | uint8_t mState; |
| 61 | uint8_t mCurrentBuf; |
| 62 | uint8_t mLastBuf; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 63 | struct timespec mBufferUpdateTime; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 64 | uint8_t mCaptureBuf[2][VISUALIZER_CAPTURE_SIZE_MAX]; |
| 65 | }; |
| 66 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 67 | // |
| 68 | //--- Local functions |
| 69 | // |
| 70 | |
| 71 | void Visualizer_reset(VisualizerContext *pContext) |
| 72 | { |
| 73 | pContext->mCaptureIdx = 0; |
| 74 | pContext->mCurrentBuf = 0; |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 75 | pContext->mLastBuf = 1; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 76 | pContext->mBufferUpdateTime.tv_sec = 0; |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 77 | memset(pContext->mCaptureBuf[0], 0x80, VISUALIZER_CAPTURE_SIZE_MAX); |
| 78 | memset(pContext->mCaptureBuf[1], 0x80, VISUALIZER_CAPTURE_SIZE_MAX); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | //---------------------------------------------------------------------------- |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 82 | // Visualizer_setConfig() |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 83 | //---------------------------------------------------------------------------- |
| 84 | // Purpose: Set input and output audio configuration. |
| 85 | // |
| 86 | // Inputs: |
| 87 | // pContext: effect engine context |
| 88 | // pConfig: pointer to effect_config_t structure holding input and output |
| 89 | // configuration parameters |
| 90 | // |
| 91 | // Outputs: |
| 92 | // |
| 93 | //---------------------------------------------------------------------------- |
| 94 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 95 | int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 96 | { |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 97 | ALOGV("Visualizer_setConfig start"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 98 | |
| 99 | if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL; |
| 100 | if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL; |
| 101 | if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 102 | if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 103 | if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE && |
| 104 | pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 105 | if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 106 | |
| 107 | memcpy(&pContext->mConfig, pConfig, sizeof(effect_config_t)); |
| 108 | |
| 109 | Visualizer_reset(pContext); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | //---------------------------------------------------------------------------- |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 116 | // Visualizer_getConfig() |
| 117 | //---------------------------------------------------------------------------- |
| 118 | // Purpose: Get input and output audio configuration. |
| 119 | // |
| 120 | // Inputs: |
| 121 | // pContext: effect engine context |
| 122 | // pConfig: pointer to effect_config_t structure holding input and output |
| 123 | // configuration parameters |
| 124 | // |
| 125 | // Outputs: |
| 126 | // |
| 127 | //---------------------------------------------------------------------------- |
| 128 | |
| 129 | void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig) |
| 130 | { |
| 131 | memcpy(pConfig, &pContext->mConfig, sizeof(effect_config_t)); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | //---------------------------------------------------------------------------- |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 136 | // Visualizer_init() |
| 137 | //---------------------------------------------------------------------------- |
| 138 | // Purpose: Initialize engine with default configuration. |
| 139 | // |
| 140 | // Inputs: |
| 141 | // pContext: effect engine context |
| 142 | // |
| 143 | // Outputs: |
| 144 | // |
| 145 | //---------------------------------------------------------------------------- |
| 146 | |
| 147 | int Visualizer_init(VisualizerContext *pContext) |
| 148 | { |
| 149 | pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 150 | pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 151 | pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 152 | pContext->mConfig.inputCfg.samplingRate = 44100; |
| 153 | pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 154 | pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 155 | pContext->mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 156 | pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 157 | pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 158 | pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 159 | pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 160 | pContext->mConfig.outputCfg.samplingRate = 44100; |
| 161 | pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 162 | pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 163 | pContext->mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 164 | pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 165 | |
| 166 | pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX; |
| 167 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 168 | Visualizer_setConfig(pContext, &pContext->mConfig); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | // |
| 174 | //--- Effect Library Interface Implementation |
| 175 | // |
| 176 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 177 | int VisualizerLib_QueryNumberEffects(uint32_t *pNumEffects) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 178 | *pNumEffects = 1; |
| 179 | return 0; |
| 180 | } |
| 181 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 182 | int VisualizerLib_QueryEffect(uint32_t index, |
| 183 | effect_descriptor_t *pDescriptor) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 184 | if (pDescriptor == NULL) { |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | if (index > 0) { |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | memcpy(pDescriptor, &gVisualizerDescriptor, sizeof(effect_descriptor_t)); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 194 | int VisualizerLib_Create(const effect_uuid_t *uuid, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 195 | int32_t sessionId, |
| 196 | int32_t ioId, |
| 197 | effect_handle_t *pHandle) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 198 | int ret; |
| 199 | int i; |
| 200 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 201 | if (pHandle == NULL || uuid == NULL) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) { |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | |
| 209 | VisualizerContext *pContext = new VisualizerContext; |
| 210 | |
| 211 | pContext->mItfe = &gVisualizerInterface; |
| 212 | pContext->mState = VISUALIZER_STATE_UNINITIALIZED; |
| 213 | |
| 214 | ret = Visualizer_init(pContext); |
| 215 | if (ret < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 216 | ALOGW("VisualizerLib_Create() init failed"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 217 | delete pContext; |
| 218 | return ret; |
| 219 | } |
| 220 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 221 | *pHandle = (effect_handle_t)pContext; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 222 | |
| 223 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
| 224 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 225 | ALOGV("VisualizerLib_Create %p", pContext); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | |
| 229 | } |
| 230 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 231 | int VisualizerLib_Release(effect_handle_t handle) { |
| 232 | VisualizerContext * pContext = (VisualizerContext *)handle; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 233 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 234 | ALOGV("VisualizerLib_Release %p", handle); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 235 | if (pContext == NULL) { |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | pContext->mState = VISUALIZER_STATE_UNINITIALIZED; |
| 239 | delete pContext; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 244 | int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 245 | effect_descriptor_t *pDescriptor) { |
| 246 | |
| 247 | if (pDescriptor == NULL || uuid == NULL){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 248 | ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) { |
| 253 | memcpy(pDescriptor, &gVisualizerDescriptor, sizeof(effect_descriptor_t)); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | return -EINVAL; |
| 258 | } /* end VisualizerLib_GetDescriptor */ |
| 259 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 260 | // |
| 261 | //--- Effect Control Interface Implementation |
| 262 | // |
| 263 | |
| 264 | static inline int16_t clamp16(int32_t sample) |
| 265 | { |
| 266 | if ((sample>>15) ^ (sample>>31)) |
| 267 | sample = 0x7FFF ^ (sample>>31); |
| 268 | return sample; |
| 269 | } |
| 270 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 271 | int Visualizer_process( |
| 272 | effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 273 | { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 274 | VisualizerContext * pContext = (VisualizerContext *)self; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 275 | |
| 276 | if (pContext == NULL) { |
| 277 | return -EINVAL; |
| 278 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 279 | |
| 280 | if (inBuffer == NULL || inBuffer->raw == NULL || |
| 281 | outBuffer == NULL || outBuffer->raw == NULL || |
| 282 | inBuffer->frameCount != outBuffer->frameCount || |
| 283 | inBuffer->frameCount == 0) { |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
| 287 | // all code below assumes stereo 16 bit PCM output and input |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 288 | |
| 289 | // derive capture scaling factor from peak value in current buffer |
| 290 | // this gives more interesting captures for display. |
| 291 | int32_t shift = 32; |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 292 | int len = inBuffer->frameCount * 2; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 293 | for (int i = 0; i < len; i++) { |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 294 | int32_t smp = inBuffer->s16[i]; |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 295 | if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 296 | int32_t clz = __builtin_clz(smp); |
| 297 | if (shift > clz) shift = clz; |
| 298 | } |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 299 | // A maximum amplitude signal will have 17 leading zeros, which we want to |
| 300 | // translate to a shift of 8 (for converting 16 bit to 8 bit) |
| 301 | shift = 25 - shift; |
| 302 | // Never scale by less than 8 to avoid returning unaltered PCM signal. |
| 303 | if (shift < 3) { |
| 304 | shift = 3; |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 305 | } |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 306 | // add one to combine the division by 2 needed after summing left and right channels below |
| 307 | shift++; |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 308 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 309 | uint32_t captIdx; |
| 310 | uint32_t inIdx; |
| 311 | uint8_t *buf = pContext->mCaptureBuf[pContext->mCurrentBuf]; |
| 312 | for (inIdx = 0, captIdx = pContext->mCaptureIdx; |
| 313 | inIdx < inBuffer->frameCount && captIdx < pContext->mCaptureSize; |
| 314 | inIdx++, captIdx++) { |
| 315 | int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1]; |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 316 | smp = smp >> shift; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 317 | buf[captIdx] = ((uint8_t)smp)^0x80; |
| 318 | } |
| 319 | pContext->mCaptureIdx = captIdx; |
| 320 | |
| 321 | // go to next buffer when buffer full |
| 322 | if (pContext->mCaptureIdx == pContext->mCaptureSize) { |
| 323 | pContext->mCurrentBuf ^= 1; |
| 324 | pContext->mCaptureIdx = 0; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 325 | |
| 326 | // update last buffer update time stamp |
| 327 | if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) { |
| 328 | pContext->mBufferUpdateTime.tv_sec = 0; |
| 329 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (inBuffer->raw != outBuffer->raw) { |
| 333 | if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 334 | for (size_t i = 0; i < outBuffer->frameCount*2; i++) { |
| 335 | outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]); |
| 336 | } |
| 337 | } else { |
| 338 | memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t)); |
| 339 | } |
| 340 | } |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 341 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 342 | return -ENODATA; |
| 343 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 344 | return 0; |
| 345 | } // end Visualizer_process |
| 346 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 347 | int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 348 | void *pCmdData, uint32_t *replySize, void *pReplyData) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 349 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 350 | VisualizerContext * pContext = (VisualizerContext *)self; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 351 | int retsize; |
| 352 | |
| 353 | if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) { |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 357 | // ALOGV("Visualizer_command command %d cmdSize %d",cmdCode, cmdSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 358 | |
| 359 | switch (cmdCode) { |
| 360 | case EFFECT_CMD_INIT: |
| 361 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | *(int *) pReplyData = Visualizer_init(pContext); |
| 365 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 366 | case EFFECT_CMD_SET_CONFIG: |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 367 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) |
| 368 | || pReplyData == NULL || *replySize != sizeof(int)) { |
| 369 | return -EINVAL; |
| 370 | } |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 371 | *(int *) pReplyData = Visualizer_setConfig(pContext, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 372 | (effect_config_t *) pCmdData); |
| 373 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 374 | case EFFECT_CMD_GET_CONFIG: |
| 375 | if (pReplyData == NULL || |
| 376 | *replySize != sizeof(effect_config_t)) { |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | Visualizer_getConfig(pContext, (effect_config_t *)pReplyData); |
| 380 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 381 | case EFFECT_CMD_RESET: |
| 382 | Visualizer_reset(pContext); |
| 383 | break; |
| 384 | case EFFECT_CMD_ENABLE: |
| 385 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | if (pContext->mState != VISUALIZER_STATE_INITIALIZED) { |
| 389 | return -ENOSYS; |
| 390 | } |
| 391 | pContext->mState = VISUALIZER_STATE_ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 392 | ALOGV("EFFECT_CMD_ENABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 393 | *(int *)pReplyData = 0; |
| 394 | break; |
| 395 | case EFFECT_CMD_DISABLE: |
| 396 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 400 | return -ENOSYS; |
| 401 | } |
| 402 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 403 | ALOGV("EFFECT_CMD_DISABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 404 | *(int *)pReplyData = 0; |
| 405 | break; |
| 406 | case EFFECT_CMD_GET_PARAM: { |
| 407 | if (pCmdData == NULL || |
| 408 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) || |
| 409 | pReplyData == NULL || |
| 410 | *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) { |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t)); |
| 414 | effect_param_t *p = (effect_param_t *)pReplyData; |
| 415 | p->status = 0; |
| 416 | *replySize = sizeof(effect_param_t) + sizeof(uint32_t); |
| 417 | if (p->psize != sizeof(uint32_t) || |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 418 | *(uint32_t *)p->data != VISUALIZER_PARAM_CAPTURE_SIZE) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 419 | p->status = -EINVAL; |
| 420 | break; |
| 421 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 422 | ALOGV("get mCaptureSize = %d", pContext->mCaptureSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 423 | *((uint32_t *)p->data + 1) = pContext->mCaptureSize; |
| 424 | p->vsize = sizeof(uint32_t); |
| 425 | *replySize += sizeof(uint32_t); |
| 426 | } break; |
| 427 | case EFFECT_CMD_SET_PARAM: { |
| 428 | if (pCmdData == NULL || |
| 429 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) || |
| 430 | pReplyData == NULL || *replySize != sizeof(int32_t)) { |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | *(int32_t *)pReplyData = 0; |
| 434 | effect_param_t *p = (effect_param_t *)pCmdData; |
| 435 | if (p->psize != sizeof(uint32_t) || |
| 436 | p->vsize != sizeof(uint32_t) || |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 437 | *(uint32_t *)p->data != VISUALIZER_PARAM_CAPTURE_SIZE) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 438 | *(int32_t *)pReplyData = -EINVAL; |
| 439 | break;; |
| 440 | } |
| 441 | pContext->mCaptureSize = *((uint32_t *)p->data + 1); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 442 | ALOGV("set mCaptureSize = %d", pContext->mCaptureSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 443 | } break; |
| 444 | case EFFECT_CMD_SET_DEVICE: |
| 445 | case EFFECT_CMD_SET_VOLUME: |
| 446 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 447 | break; |
| 448 | |
| 449 | |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 450 | case VISUALIZER_CMD_CAPTURE: |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 451 | if (pReplyData == NULL || *replySize != pContext->mCaptureSize) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 452 | ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %d pContext->mCaptureSize %d", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 453 | *replySize, pContext->mCaptureSize); |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | if (pContext->mState == VISUALIZER_STATE_ACTIVE) { |
| 457 | memcpy(pReplyData, |
| 458 | pContext->mCaptureBuf[pContext->mCurrentBuf ^ 1], |
| 459 | pContext->mCaptureSize); |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 460 | // if audio framework has stopped playing audio although the effect is still |
| 461 | // active we must clear the capture buffer to return silence |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 462 | if ((pContext->mLastBuf == pContext->mCurrentBuf) && |
| 463 | (pContext->mBufferUpdateTime.tv_sec != 0)) { |
| 464 | struct timespec ts; |
| 465 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 466 | time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec; |
| 467 | long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec; |
| 468 | if (nsec < 0) { |
| 469 | --secs; |
| 470 | nsec += 1000000000; |
| 471 | } |
| 472 | uint32_t deltaMs = secs * 1000 + nsec / 1000000; |
| 473 | if (deltaMs > MAX_STALL_TIME_MS) { |
| 474 | ALOGV("capture going to idle"); |
| 475 | pContext->mBufferUpdateTime.tv_sec = 0; |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 476 | memset(pContext->mCaptureBuf[pContext->mCurrentBuf ^ 1], |
| 477 | 0x80, |
| 478 | pContext->mCaptureSize); |
| 479 | } |
| 480 | } |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 481 | } |
| 482 | pContext->mLastBuf = pContext->mCurrentBuf; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 483 | } else { |
| 484 | memset(pReplyData, 0x80, pContext->mCaptureSize); |
| 485 | } |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 486 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 487 | break; |
| 488 | |
| 489 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 490 | ALOGW("Visualizer_command invalid command %d",cmdCode); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 491 | return -EINVAL; |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 497 | /* Effect Control Interface Implementation: get_descriptor */ |
| 498 | int Visualizer_getDescriptor(effect_handle_t self, |
| 499 | effect_descriptor_t *pDescriptor) |
| 500 | { |
| 501 | VisualizerContext * pContext = (VisualizerContext *) self; |
| 502 | |
| 503 | if (pContext == NULL || pDescriptor == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 504 | ALOGV("Visualizer_getDescriptor() invalid param"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | memcpy(pDescriptor, &gVisualizerDescriptor, sizeof(effect_descriptor_t)); |
| 509 | |
| 510 | return 0; |
| 511 | } /* end Visualizer_getDescriptor */ |
| 512 | |
| 513 | // effect_handle_t interface implementation for visualizer effect |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 514 | const struct effect_interface_s gVisualizerInterface = { |
| 515 | Visualizer_process, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 516 | Visualizer_command, |
Eric Laurent | ba7b8f8 | 2011-06-17 18:54:16 -0700 | [diff] [blame] | 517 | Visualizer_getDescriptor, |
| 518 | NULL, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 519 | }; |
| 520 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 521 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 522 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
| 523 | tag : AUDIO_EFFECT_LIBRARY_TAG, |
| 524 | version : EFFECT_LIBRARY_API_VERSION, |
| 525 | name : "Visualizer Library", |
| 526 | implementor : "The Android Open Source Project", |
| 527 | query_num_effects : VisualizerLib_QueryNumberEffects, |
| 528 | query_effect : VisualizerLib_QueryEffect, |
| 529 | create_effect : VisualizerLib_Create, |
| 530 | release_effect : VisualizerLib_Release, |
| 531 | get_descriptor : VisualizerLib_GetDescriptor, |
| 532 | }; |
| 533 | |
| 534 | }; // extern "C" |