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