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