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> |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 25 | #include <math.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 26 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 27 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 28 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 29 | extern "C" { |
| 30 | |
| 31 | // effect_handle_t interface implementation for visualizer effect |
| 32 | extern const struct effect_interface_s gVisualizerInterface; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 33 | |
| 34 | // Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b |
| 35 | const effect_descriptor_t gVisualizerDescriptor = { |
| 36 | {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type |
| 37 | {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 38 | EFFECT_CONTROL_API_VERSION, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 39 | (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST), |
| 40 | 0, // TODO |
| 41 | 1, |
| 42 | "Visualizer", |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 43 | "The Android Open Source Project", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | enum visualizer_state_e { |
| 47 | VISUALIZER_STATE_UNINITIALIZED, |
| 48 | VISUALIZER_STATE_INITIALIZED, |
| 49 | VISUALIZER_STATE_ACTIVE, |
| 50 | }; |
| 51 | |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 52 | // 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] | 53 | // 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] | 54 | #define MAX_STALL_TIME_MS 1000 |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 55 | |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 56 | #define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone" |
| 57 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 58 | #define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms |
| 59 | |
| 60 | // maximum number of buffers for which we keep track of the measurements |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 61 | #define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 62 | |
| 63 | |
| 64 | struct BufferStats { |
| 65 | bool mIsValid; |
| 66 | uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer |
| 67 | float mRmsSquared; // the average square of the samples in a buffer |
| 68 | }; |
| 69 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 70 | struct VisualizerContext { |
| 71 | const struct effect_interface_s *mItfe; |
| 72 | effect_config_t mConfig; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 73 | uint32_t mCaptureIdx; |
| 74 | uint32_t mCaptureSize; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 75 | uint32_t mScalingMode; |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 76 | uint8_t mState; |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 77 | uint32_t mLastCaptureIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 78 | uint32_t mLatency; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 79 | struct timespec mBufferUpdateTime; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 80 | uint8_t mCaptureBuf[CAPTURE_BUF_SIZE]; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 81 | // for measurements |
| 82 | uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed |
| 83 | uint32_t mMeasurementMode; |
| 84 | uint8_t mMeasurementWindowSizeInBuffers; |
| 85 | uint8_t mMeasurementBufferIdx; |
| 86 | BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS]; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 89 | // |
| 90 | //--- Local functions |
| 91 | // |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 92 | uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) { |
| 93 | uint32_t deltaMs = 0; |
| 94 | if (pContext->mBufferUpdateTime.tv_sec != 0) { |
| 95 | struct timespec ts; |
| 96 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 97 | time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec; |
| 98 | long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec; |
| 99 | if (nsec < 0) { |
| 100 | --secs; |
| 101 | nsec += 1000000000; |
| 102 | } |
| 103 | deltaMs = secs * 1000 + nsec / 1000000; |
| 104 | } |
| 105 | } |
| 106 | return deltaMs; |
| 107 | } |
| 108 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 109 | |
| 110 | void Visualizer_reset(VisualizerContext *pContext) |
| 111 | { |
| 112 | pContext->mCaptureIdx = 0; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 113 | pContext->mLastCaptureIdx = 0; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 114 | pContext->mBufferUpdateTime.tv_sec = 0; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 115 | pContext->mLatency = 0; |
| 116 | memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | //---------------------------------------------------------------------------- |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 120 | // Visualizer_setConfig() |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 121 | //---------------------------------------------------------------------------- |
| 122 | // Purpose: Set input and output audio configuration. |
| 123 | // |
| 124 | // Inputs: |
| 125 | // pContext: effect engine context |
| 126 | // pConfig: pointer to effect_config_t structure holding input and output |
| 127 | // configuration parameters |
| 128 | // |
| 129 | // Outputs: |
| 130 | // |
| 131 | //---------------------------------------------------------------------------- |
| 132 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 133 | int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 134 | { |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 135 | ALOGV("Visualizer_setConfig start"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 136 | |
| 137 | if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL; |
| 138 | if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL; |
| 139 | if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 140 | if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 141 | if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE && |
| 142 | pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 143 | if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 144 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 145 | pContext->mConfig = *pConfig; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 146 | |
| 147 | Visualizer_reset(pContext); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | //---------------------------------------------------------------------------- |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 154 | // Visualizer_getConfig() |
| 155 | //---------------------------------------------------------------------------- |
| 156 | // Purpose: Get input and output audio configuration. |
| 157 | // |
| 158 | // Inputs: |
| 159 | // pContext: effect engine context |
| 160 | // pConfig: pointer to effect_config_t structure holding input and output |
| 161 | // configuration parameters |
| 162 | // |
| 163 | // Outputs: |
| 164 | // |
| 165 | //---------------------------------------------------------------------------- |
| 166 | |
| 167 | void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig) |
| 168 | { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 169 | *pConfig = pContext->mConfig; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | //---------------------------------------------------------------------------- |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 174 | // Visualizer_init() |
| 175 | //---------------------------------------------------------------------------- |
| 176 | // Purpose: Initialize engine with default configuration. |
| 177 | // |
| 178 | // Inputs: |
| 179 | // pContext: effect engine context |
| 180 | // |
| 181 | // Outputs: |
| 182 | // |
| 183 | //---------------------------------------------------------------------------- |
| 184 | |
| 185 | int Visualizer_init(VisualizerContext *pContext) |
| 186 | { |
| 187 | pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 188 | pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 189 | pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 190 | pContext->mConfig.inputCfg.samplingRate = 44100; |
| 191 | pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 192 | pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 193 | pContext->mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 194 | pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 195 | pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 196 | pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 197 | pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 198 | pContext->mConfig.outputCfg.samplingRate = 44100; |
| 199 | pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 200 | pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 201 | pContext->mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 202 | pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 203 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 204 | // visualization initialization |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 205 | pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 206 | pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 207 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 208 | // measurement initialization |
| 209 | pContext->mChannelCount = popcount(pContext->mConfig.inputCfg.channels); |
| 210 | pContext->mMeasurementMode = MEASUREMENT_MODE_NONE; |
| 211 | pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS; |
| 212 | pContext->mMeasurementBufferIdx = 0; |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 213 | for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 214 | pContext->mPastMeasurements[i].mIsValid = false; |
| 215 | pContext->mPastMeasurements[i].mPeakU16 = 0; |
| 216 | pContext->mPastMeasurements[i].mRmsSquared = 0; |
| 217 | } |
| 218 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 219 | Visualizer_setConfig(pContext, &pContext->mConfig); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | // |
| 225 | //--- Effect Library Interface Implementation |
| 226 | // |
| 227 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 228 | int VisualizerLib_Create(const effect_uuid_t *uuid, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 229 | int32_t sessionId, |
| 230 | int32_t ioId, |
| 231 | effect_handle_t *pHandle) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 232 | int ret; |
| 233 | int i; |
| 234 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 235 | if (pHandle == NULL || uuid == NULL) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) { |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | VisualizerContext *pContext = new VisualizerContext; |
| 244 | |
| 245 | pContext->mItfe = &gVisualizerInterface; |
| 246 | pContext->mState = VISUALIZER_STATE_UNINITIALIZED; |
| 247 | |
| 248 | ret = Visualizer_init(pContext); |
| 249 | if (ret < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 250 | ALOGW("VisualizerLib_Create() init failed"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 251 | delete pContext; |
| 252 | return ret; |
| 253 | } |
| 254 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 255 | *pHandle = (effect_handle_t)pContext; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 256 | |
| 257 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
| 258 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 259 | ALOGV("VisualizerLib_Create %p", pContext); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | |
| 263 | } |
| 264 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 265 | int VisualizerLib_Release(effect_handle_t handle) { |
| 266 | VisualizerContext * pContext = (VisualizerContext *)handle; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 267 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 268 | ALOGV("VisualizerLib_Release %p", handle); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 269 | if (pContext == NULL) { |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | pContext->mState = VISUALIZER_STATE_UNINITIALIZED; |
| 273 | delete pContext; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 278 | int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 279 | effect_descriptor_t *pDescriptor) { |
| 280 | |
| 281 | if (pDescriptor == NULL || uuid == NULL){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 282 | ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 283 | return -EINVAL; |
| 284 | } |
| 285 | |
| 286 | if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 287 | *pDescriptor = gVisualizerDescriptor; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | return -EINVAL; |
| 292 | } /* end VisualizerLib_GetDescriptor */ |
| 293 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 294 | // |
| 295 | //--- Effect Control Interface Implementation |
| 296 | // |
| 297 | |
| 298 | static inline int16_t clamp16(int32_t sample) |
| 299 | { |
| 300 | if ((sample>>15) ^ (sample>>31)) |
| 301 | sample = 0x7FFF ^ (sample>>31); |
| 302 | return sample; |
| 303 | } |
| 304 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 305 | int Visualizer_process( |
| 306 | effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 307 | { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 308 | VisualizerContext * pContext = (VisualizerContext *)self; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 309 | |
| 310 | if (pContext == NULL) { |
| 311 | return -EINVAL; |
| 312 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 313 | |
| 314 | if (inBuffer == NULL || inBuffer->raw == NULL || |
| 315 | outBuffer == NULL || outBuffer->raw == NULL || |
| 316 | inBuffer->frameCount != outBuffer->frameCount || |
| 317 | inBuffer->frameCount == 0) { |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 321 | // perform measurements if needed |
| 322 | if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) { |
| 323 | // find the peak and RMS squared for the new buffer |
| 324 | uint32_t inIdx; |
| 325 | int16_t maxSample = 0; |
| 326 | float rmsSqAcc = 0; |
| 327 | for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) { |
| 328 | if (inBuffer->s16[inIdx] > maxSample) { |
| 329 | maxSample = inBuffer->s16[inIdx]; |
| 330 | } else if (-inBuffer->s16[inIdx] > maxSample) { |
| 331 | maxSample = -inBuffer->s16[inIdx]; |
| 332 | } |
| 333 | rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]); |
| 334 | } |
| 335 | // store the measurement |
| 336 | pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample; |
| 337 | pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared = |
| 338 | rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount); |
| 339 | pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true; |
| 340 | if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) { |
| 341 | pContext->mMeasurementBufferIdx = 0; |
| 342 | } |
| 343 | } |
| 344 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 345 | // all code below assumes stereo 16 bit PCM output and input |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 346 | int32_t shift; |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 347 | |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 348 | if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) { |
| 349 | // derive capture scaling factor from peak value in current buffer |
| 350 | // this gives more interesting captures for display. |
| 351 | shift = 32; |
| 352 | int len = inBuffer->frameCount * 2; |
| 353 | for (int i = 0; i < len; i++) { |
| 354 | int32_t smp = inBuffer->s16[i]; |
| 355 | if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range |
| 356 | int32_t clz = __builtin_clz(smp); |
| 357 | if (shift > clz) shift = clz; |
| 358 | } |
| 359 | // A maximum amplitude signal will have 17 leading zeros, which we want to |
| 360 | // translate to a shift of 8 (for converting 16 bit to 8 bit) |
| 361 | shift = 25 - shift; |
| 362 | // Never scale by less than 8 to avoid returning unaltered PCM signal. |
| 363 | if (shift < 3) { |
| 364 | shift = 3; |
| 365 | } |
| 366 | // add one to combine the division by 2 needed after summing left and right channels below |
| 367 | shift++; |
| 368 | } else { |
| 369 | assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED); |
| 370 | shift = 9; |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 371 | } |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 372 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 373 | uint32_t captIdx; |
| 374 | uint32_t inIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 375 | uint8_t *buf = pContext->mCaptureBuf; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 376 | for (inIdx = 0, captIdx = pContext->mCaptureIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 377 | inIdx < inBuffer->frameCount; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 378 | inIdx++, captIdx++) { |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 379 | if (captIdx >= CAPTURE_BUF_SIZE) { |
| 380 | // wrap around |
| 381 | captIdx = 0; |
| 382 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 383 | int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1]; |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 384 | smp = smp >> shift; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 385 | buf[captIdx] = ((uint8_t)smp)^0x80; |
| 386 | } |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 387 | |
| 388 | // XXX the following two should really be atomic, though it probably doesn't |
| 389 | // matter much for visualization purposes |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 390 | pContext->mCaptureIdx = captIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 391 | // update last buffer update time stamp |
| 392 | if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) { |
| 393 | pContext->mBufferUpdateTime.tv_sec = 0; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | if (inBuffer->raw != outBuffer->raw) { |
| 397 | if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 398 | for (size_t i = 0; i < outBuffer->frameCount*2; i++) { |
| 399 | outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]); |
| 400 | } |
| 401 | } else { |
| 402 | memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t)); |
| 403 | } |
| 404 | } |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 405 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 406 | return -ENODATA; |
| 407 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 408 | return 0; |
| 409 | } // end Visualizer_process |
| 410 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 411 | 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] | 412 | void *pCmdData, uint32_t *replySize, void *pReplyData) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 413 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 414 | VisualizerContext * pContext = (VisualizerContext *)self; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 415 | int retsize; |
| 416 | |
| 417 | if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) { |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 421 | // ALOGV("Visualizer_command command %d cmdSize %d",cmdCode, cmdSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 422 | |
| 423 | switch (cmdCode) { |
| 424 | case EFFECT_CMD_INIT: |
| 425 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | *(int *) pReplyData = Visualizer_init(pContext); |
| 429 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 430 | case EFFECT_CMD_SET_CONFIG: |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 431 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) |
| 432 | || pReplyData == NULL || *replySize != sizeof(int)) { |
| 433 | return -EINVAL; |
| 434 | } |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 435 | *(int *) pReplyData = Visualizer_setConfig(pContext, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 436 | (effect_config_t *) pCmdData); |
| 437 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 438 | case EFFECT_CMD_GET_CONFIG: |
| 439 | if (pReplyData == NULL || |
| 440 | *replySize != sizeof(effect_config_t)) { |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | Visualizer_getConfig(pContext, (effect_config_t *)pReplyData); |
| 444 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 445 | case EFFECT_CMD_RESET: |
| 446 | Visualizer_reset(pContext); |
| 447 | break; |
| 448 | case EFFECT_CMD_ENABLE: |
| 449 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | if (pContext->mState != VISUALIZER_STATE_INITIALIZED) { |
| 453 | return -ENOSYS; |
| 454 | } |
| 455 | pContext->mState = VISUALIZER_STATE_ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 456 | ALOGV("EFFECT_CMD_ENABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 457 | *(int *)pReplyData = 0; |
| 458 | break; |
| 459 | case EFFECT_CMD_DISABLE: |
| 460 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 464 | return -ENOSYS; |
| 465 | } |
| 466 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 467 | ALOGV("EFFECT_CMD_DISABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 468 | *(int *)pReplyData = 0; |
| 469 | break; |
| 470 | case EFFECT_CMD_GET_PARAM: { |
| 471 | if (pCmdData == NULL || |
| 472 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) || |
| 473 | pReplyData == NULL || |
| 474 | *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) { |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t)); |
| 478 | effect_param_t *p = (effect_param_t *)pReplyData; |
| 479 | p->status = 0; |
| 480 | *replySize = sizeof(effect_param_t) + sizeof(uint32_t); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 481 | if (p->psize != sizeof(uint32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 482 | p->status = -EINVAL; |
| 483 | break; |
| 484 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 485 | switch (*(uint32_t *)p->data) { |
| 486 | case VISUALIZER_PARAM_CAPTURE_SIZE: |
| 487 | ALOGV("get mCaptureSize = %d", pContext->mCaptureSize); |
| 488 | *((uint32_t *)p->data + 1) = pContext->mCaptureSize; |
| 489 | p->vsize = sizeof(uint32_t); |
| 490 | *replySize += sizeof(uint32_t); |
| 491 | break; |
| 492 | case VISUALIZER_PARAM_SCALING_MODE: |
| 493 | ALOGV("get mScalingMode = %d", pContext->mScalingMode); |
| 494 | *((uint32_t *)p->data + 1) = pContext->mScalingMode; |
| 495 | p->vsize = sizeof(uint32_t); |
| 496 | *replySize += sizeof(uint32_t); |
| 497 | break; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 498 | case VISUALIZER_PARAM_MEASUREMENT_MODE: |
| 499 | ALOGV("get mMeasurementMode = %d", pContext->mMeasurementMode); |
| 500 | *((uint32_t *)p->data + 1) = pContext->mMeasurementMode; |
| 501 | p->vsize = sizeof(uint32_t); |
| 502 | *replySize += sizeof(uint32_t); |
| 503 | break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 504 | default: |
| 505 | p->status = -EINVAL; |
| 506 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 507 | } break; |
| 508 | case EFFECT_CMD_SET_PARAM: { |
| 509 | if (pCmdData == NULL || |
| 510 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) || |
| 511 | pReplyData == NULL || *replySize != sizeof(int32_t)) { |
| 512 | return -EINVAL; |
| 513 | } |
| 514 | *(int32_t *)pReplyData = 0; |
| 515 | effect_param_t *p = (effect_param_t *)pCmdData; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 516 | if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 517 | *(int32_t *)pReplyData = -EINVAL; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 518 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 519 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 520 | switch (*(uint32_t *)p->data) { |
| 521 | case VISUALIZER_PARAM_CAPTURE_SIZE: |
| 522 | pContext->mCaptureSize = *((uint32_t *)p->data + 1); |
| 523 | ALOGV("set mCaptureSize = %d", pContext->mCaptureSize); |
| 524 | break; |
| 525 | case VISUALIZER_PARAM_SCALING_MODE: |
| 526 | pContext->mScalingMode = *((uint32_t *)p->data + 1); |
| 527 | ALOGV("set mScalingMode = %d", pContext->mScalingMode); |
| 528 | break; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 529 | case VISUALIZER_PARAM_LATENCY: |
| 530 | pContext->mLatency = *((uint32_t *)p->data + 1); |
| 531 | ALOGV("set mLatency = %d", pContext->mLatency); |
| 532 | break; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 533 | case VISUALIZER_PARAM_MEASUREMENT_MODE: |
| 534 | pContext->mMeasurementMode = *((uint32_t *)p->data + 1); |
| 535 | ALOGV("set mMeasurementMode = %d", pContext->mMeasurementMode); |
| 536 | break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 537 | default: |
| 538 | *(int32_t *)pReplyData = -EINVAL; |
| 539 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 540 | } break; |
| 541 | case EFFECT_CMD_SET_DEVICE: |
| 542 | case EFFECT_CMD_SET_VOLUME: |
| 543 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 544 | break; |
| 545 | |
| 546 | |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 547 | case VISUALIZER_CMD_CAPTURE: |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 548 | if (pReplyData == NULL || *replySize != pContext->mCaptureSize) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 549 | ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %d pContext->mCaptureSize %d", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 550 | *replySize, pContext->mCaptureSize); |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | if (pContext->mState == VISUALIZER_STATE_ACTIVE) { |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 554 | int32_t latencyMs = pContext->mLatency; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 555 | const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext); |
| 556 | latencyMs -= deltaMs; |
| 557 | if (latencyMs < 0) { |
| 558 | latencyMs = 0; |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 559 | } |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 560 | const uint32_t deltaSmpl = pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 561 | |
| 562 | int32_t capturePoint = pContext->mCaptureIdx - pContext->mCaptureSize - deltaSmpl; |
| 563 | int32_t captureSize = pContext->mCaptureSize; |
| 564 | if (capturePoint < 0) { |
| 565 | int32_t size = -capturePoint; |
| 566 | if (size > captureSize) { |
| 567 | size = captureSize; |
| 568 | } |
| 569 | memcpy(pReplyData, |
| 570 | pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint, |
| 571 | size); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 572 | pReplyData = (char *)pReplyData + size; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 573 | captureSize -= size; |
| 574 | capturePoint = 0; |
| 575 | } |
| 576 | memcpy(pReplyData, |
| 577 | pContext->mCaptureBuf + capturePoint, |
| 578 | captureSize); |
| 579 | |
| 580 | |
| 581 | // if audio framework has stopped playing audio although the effect is still |
| 582 | // active we must clear the capture buffer to return silence |
| 583 | if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) && |
| 584 | (pContext->mBufferUpdateTime.tv_sec != 0)) { |
| 585 | if (deltaMs > MAX_STALL_TIME_MS) { |
| 586 | ALOGV("capture going to idle"); |
| 587 | pContext->mBufferUpdateTime.tv_sec = 0; |
| 588 | memset(pReplyData, 0x80, pContext->mCaptureSize); |
| 589 | } |
| 590 | } |
| 591 | pContext->mLastCaptureIdx = pContext->mCaptureIdx; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 592 | } else { |
| 593 | memset(pReplyData, 0x80, pContext->mCaptureSize); |
| 594 | } |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 595 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 596 | break; |
| 597 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 598 | case VISUALIZER_CMD_MEASURE: { |
| 599 | uint16_t peakU16 = 0; |
| 600 | float sumRmsSquared = 0.0f; |
| 601 | uint8_t nbValidMeasurements = 0; |
| 602 | // reset measurements if last measurement was too long ago (which implies stored |
| 603 | // measurements aren't relevant anymore and shouldn't bias the new one) |
| 604 | const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext); |
| 605 | if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) { |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 606 | ALOGV("Discarding measurements, last measurement is %dms old", delayMs); |
| 607 | for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 608 | pContext->mPastMeasurements[i].mIsValid = false; |
| 609 | pContext->mPastMeasurements[i].mPeakU16 = 0; |
| 610 | pContext->mPastMeasurements[i].mRmsSquared = 0; |
| 611 | } |
| 612 | pContext->mMeasurementBufferIdx = 0; |
| 613 | } else { |
| 614 | // only use actual measurements, otherwise the first RMS measure happening before |
| 615 | // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially |
| 616 | // low |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 617 | for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 618 | if (pContext->mPastMeasurements[i].mIsValid) { |
| 619 | if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) { |
| 620 | peakU16 = pContext->mPastMeasurements[i].mPeakU16; |
| 621 | } |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 622 | sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 623 | nbValidMeasurements++; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements); |
| 628 | int32_t* pIntReplyData = (int32_t*)pReplyData; |
| 629 | // convert from I16 sample values to mB and write results |
| 630 | if (rms < 0.000016f) { |
| 631 | pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB |
| 632 | } else { |
| 633 | pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f)); |
| 634 | } |
| 635 | if (peakU16 == 0) { |
| 636 | pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB |
| 637 | } else { |
| 638 | pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f)); |
| 639 | } |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 640 | ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)", |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 641 | peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK], |
| 642 | rms, pIntReplyData[MEASUREMENT_IDX_RMS]); |
| 643 | } |
| 644 | break; |
| 645 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 646 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 647 | ALOGW("Visualizer_command invalid command %d",cmdCode); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 648 | return -EINVAL; |
| 649 | } |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 654 | /* Effect Control Interface Implementation: get_descriptor */ |
| 655 | int Visualizer_getDescriptor(effect_handle_t self, |
| 656 | effect_descriptor_t *pDescriptor) |
| 657 | { |
| 658 | VisualizerContext * pContext = (VisualizerContext *) self; |
| 659 | |
| 660 | if (pContext == NULL || pDescriptor == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 661 | ALOGV("Visualizer_getDescriptor() invalid param"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 662 | return -EINVAL; |
| 663 | } |
| 664 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 665 | *pDescriptor = gVisualizerDescriptor; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 666 | |
| 667 | return 0; |
| 668 | } /* end Visualizer_getDescriptor */ |
| 669 | |
| 670 | // effect_handle_t interface implementation for visualizer effect |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 671 | const struct effect_interface_s gVisualizerInterface = { |
| 672 | Visualizer_process, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 673 | Visualizer_command, |
Eric Laurent | ba7b8f8 | 2011-06-17 18:54:16 -0700 | [diff] [blame] | 674 | Visualizer_getDescriptor, |
| 675 | NULL, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 676 | }; |
| 677 | |
Marco Nelissen | 7f16b19 | 2012-10-25 16:05:57 -0700 | [diff] [blame] | 678 | // This is the only symbol that needs to be exported |
| 679 | __attribute__ ((visibility ("default"))) |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 680 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
synergydev | c9d8ea7 | 2013-10-19 22:51:33 -0700 | [diff] [blame] | 681 | .tag = AUDIO_EFFECT_LIBRARY_TAG, |
| 682 | .version = EFFECT_LIBRARY_API_VERSION, |
| 683 | .name = "Visualizer Library", |
| 684 | .implementor = "The Android Open Source Project", |
| 685 | .create_effect = VisualizerLib_Create, |
| 686 | .release_effect = VisualizerLib_Release, |
| 687 | .get_descriptor = VisualizerLib_GetDescriptor, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | }; // extern "C" |