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