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; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 240 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 241 | if (pHandle == NULL || uuid == NULL) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) { |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | VisualizerContext *pContext = new VisualizerContext; |
| 250 | |
| 251 | pContext->mItfe = &gVisualizerInterface; |
| 252 | pContext->mState = VISUALIZER_STATE_UNINITIALIZED; |
| 253 | |
| 254 | ret = Visualizer_init(pContext); |
| 255 | if (ret < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 256 | ALOGW("VisualizerLib_Create() init failed"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 257 | delete pContext; |
| 258 | return ret; |
| 259 | } |
| 260 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 261 | *pHandle = (effect_handle_t)pContext; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 262 | |
| 263 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
| 264 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 265 | ALOGV("VisualizerLib_Create %p", pContext); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | |
| 269 | } |
| 270 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 271 | int VisualizerLib_Release(effect_handle_t handle) { |
| 272 | VisualizerContext * pContext = (VisualizerContext *)handle; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 273 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 274 | ALOGV("VisualizerLib_Release %p", handle); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 275 | if (pContext == NULL) { |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | pContext->mState = VISUALIZER_STATE_UNINITIALIZED; |
| 279 | delete pContext; |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 284 | int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 285 | effect_descriptor_t *pDescriptor) { |
| 286 | |
| 287 | if (pDescriptor == NULL || uuid == NULL){ |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 288 | ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 289 | return -EINVAL; |
| 290 | } |
| 291 | |
| 292 | if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 293 | *pDescriptor = gVisualizerDescriptor; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | return -EINVAL; |
| 298 | } /* end VisualizerLib_GetDescriptor */ |
| 299 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 300 | // |
| 301 | //--- Effect Control Interface Implementation |
| 302 | // |
| 303 | |
| 304 | static inline int16_t clamp16(int32_t sample) |
| 305 | { |
| 306 | if ((sample>>15) ^ (sample>>31)) |
| 307 | sample = 0x7FFF ^ (sample>>31); |
| 308 | return sample; |
| 309 | } |
| 310 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 311 | int Visualizer_process( |
| 312 | effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 313 | { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 314 | VisualizerContext * pContext = (VisualizerContext *)self; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 315 | |
| 316 | if (pContext == NULL) { |
| 317 | return -EINVAL; |
| 318 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 319 | |
| 320 | if (inBuffer == NULL || inBuffer->raw == NULL || |
| 321 | outBuffer == NULL || outBuffer->raw == NULL || |
| 322 | inBuffer->frameCount != outBuffer->frameCount || |
| 323 | inBuffer->frameCount == 0) { |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 327 | // perform measurements if needed |
| 328 | if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) { |
| 329 | // find the peak and RMS squared for the new buffer |
| 330 | uint32_t inIdx; |
| 331 | int16_t maxSample = 0; |
| 332 | float rmsSqAcc = 0; |
| 333 | for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) { |
| 334 | if (inBuffer->s16[inIdx] > maxSample) { |
| 335 | maxSample = inBuffer->s16[inIdx]; |
| 336 | } else if (-inBuffer->s16[inIdx] > maxSample) { |
| 337 | maxSample = -inBuffer->s16[inIdx]; |
| 338 | } |
| 339 | rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]); |
| 340 | } |
| 341 | // store the measurement |
| 342 | pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample; |
| 343 | pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared = |
| 344 | rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount); |
| 345 | pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true; |
| 346 | if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) { |
| 347 | pContext->mMeasurementBufferIdx = 0; |
| 348 | } |
| 349 | } |
| 350 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 351 | // all code below assumes stereo 16 bit PCM output and input |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 352 | int32_t shift; |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 353 | |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 354 | if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) { |
| 355 | // derive capture scaling factor from peak value in current buffer |
| 356 | // this gives more interesting captures for display. |
| 357 | shift = 32; |
| 358 | int len = inBuffer->frameCount * 2; |
| 359 | for (int i = 0; i < len; i++) { |
| 360 | int32_t smp = inBuffer->s16[i]; |
| 361 | if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range |
| 362 | int32_t clz = __builtin_clz(smp); |
| 363 | if (shift > clz) shift = clz; |
| 364 | } |
| 365 | // A maximum amplitude signal will have 17 leading zeros, which we want to |
| 366 | // translate to a shift of 8 (for converting 16 bit to 8 bit) |
| 367 | shift = 25 - shift; |
| 368 | // Never scale by less than 8 to avoid returning unaltered PCM signal. |
| 369 | if (shift < 3) { |
| 370 | shift = 3; |
| 371 | } |
| 372 | // add one to combine the division by 2 needed after summing left and right channels below |
| 373 | shift++; |
| 374 | } else { |
| 375 | assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED); |
| 376 | shift = 9; |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 377 | } |
Eric Laurent | 0e75f0f | 2010-09-21 14:52:01 -0700 | [diff] [blame] | 378 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 379 | uint32_t captIdx; |
| 380 | uint32_t inIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 381 | uint8_t *buf = pContext->mCaptureBuf; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 382 | for (inIdx = 0, captIdx = pContext->mCaptureIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 383 | inIdx < inBuffer->frameCount; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 384 | inIdx++, captIdx++) { |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 385 | if (captIdx >= CAPTURE_BUF_SIZE) { |
| 386 | // wrap around |
| 387 | captIdx = 0; |
| 388 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 389 | int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1]; |
Marco Nelissen | 64c3bde | 2010-10-27 09:06:01 -0700 | [diff] [blame] | 390 | smp = smp >> shift; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 391 | buf[captIdx] = ((uint8_t)smp)^0x80; |
| 392 | } |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 393 | |
| 394 | // XXX the following two should really be atomic, though it probably doesn't |
| 395 | // matter much for visualization purposes |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 396 | pContext->mCaptureIdx = captIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 397 | // update last buffer update time stamp |
| 398 | if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) { |
| 399 | pContext->mBufferUpdateTime.tv_sec = 0; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | if (inBuffer->raw != outBuffer->raw) { |
| 403 | if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 404 | for (size_t i = 0; i < outBuffer->frameCount*2; i++) { |
| 405 | outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]); |
| 406 | } |
| 407 | } else { |
| 408 | memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t)); |
| 409 | } |
| 410 | } |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 411 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 412 | return -ENODATA; |
| 413 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 414 | return 0; |
| 415 | } // end Visualizer_process |
| 416 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 417 | 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] | 418 | void *pCmdData, uint32_t *replySize, void *pReplyData) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 419 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 420 | VisualizerContext * pContext = (VisualizerContext *)self; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 421 | |
| 422 | if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) { |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 426 | // ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 427 | |
| 428 | switch (cmdCode) { |
| 429 | case EFFECT_CMD_INIT: |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 430 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 431 | return -EINVAL; |
| 432 | } |
| 433 | *(int *) pReplyData = Visualizer_init(pContext); |
| 434 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 435 | case EFFECT_CMD_SET_CONFIG: |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 436 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 437 | || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | } |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 440 | *(int *) pReplyData = Visualizer_setConfig(pContext, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 441 | (effect_config_t *) pCmdData); |
| 442 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 443 | case EFFECT_CMD_GET_CONFIG: |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 444 | if (pReplyData == NULL || replySize == NULL || |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 445 | *replySize != sizeof(effect_config_t)) { |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | Visualizer_getConfig(pContext, (effect_config_t *)pReplyData); |
| 449 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 450 | case EFFECT_CMD_RESET: |
| 451 | Visualizer_reset(pContext); |
| 452 | break; |
| 453 | case EFFECT_CMD_ENABLE: |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 454 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 455 | return -EINVAL; |
| 456 | } |
| 457 | if (pContext->mState != VISUALIZER_STATE_INITIALIZED) { |
| 458 | return -ENOSYS; |
| 459 | } |
| 460 | pContext->mState = VISUALIZER_STATE_ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 461 | ALOGV("EFFECT_CMD_ENABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 462 | *(int *)pReplyData = 0; |
| 463 | break; |
| 464 | case EFFECT_CMD_DISABLE: |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 465 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 466 | return -EINVAL; |
| 467 | } |
| 468 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 469 | return -ENOSYS; |
| 470 | } |
| 471 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 472 | ALOGV("EFFECT_CMD_DISABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 473 | *(int *)pReplyData = 0; |
| 474 | break; |
| 475 | case EFFECT_CMD_GET_PARAM: { |
| 476 | if (pCmdData == NULL || |
| 477 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) || |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 478 | pReplyData == NULL || replySize == NULL || |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 479 | *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) { |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t)); |
| 483 | effect_param_t *p = (effect_param_t *)pReplyData; |
| 484 | p->status = 0; |
| 485 | *replySize = sizeof(effect_param_t) + sizeof(uint32_t); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 486 | if (p->psize != sizeof(uint32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 487 | p->status = -EINVAL; |
| 488 | break; |
| 489 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 490 | switch (*(uint32_t *)p->data) { |
| 491 | case VISUALIZER_PARAM_CAPTURE_SIZE: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 492 | ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 493 | *((uint32_t *)p->data + 1) = pContext->mCaptureSize; |
| 494 | p->vsize = sizeof(uint32_t); |
| 495 | *replySize += sizeof(uint32_t); |
| 496 | break; |
| 497 | case VISUALIZER_PARAM_SCALING_MODE: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 498 | ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 499 | *((uint32_t *)p->data + 1) = pContext->mScalingMode; |
| 500 | p->vsize = sizeof(uint32_t); |
| 501 | *replySize += sizeof(uint32_t); |
| 502 | break; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 503 | case VISUALIZER_PARAM_MEASUREMENT_MODE: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 504 | ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode); |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 505 | *((uint32_t *)p->data + 1) = pContext->mMeasurementMode; |
| 506 | p->vsize = sizeof(uint32_t); |
| 507 | *replySize += sizeof(uint32_t); |
| 508 | break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 509 | default: |
| 510 | p->status = -EINVAL; |
| 511 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 512 | } break; |
| 513 | case EFFECT_CMD_SET_PARAM: { |
| 514 | if (pCmdData == NULL || |
| 515 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) || |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 516 | pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 517 | return -EINVAL; |
| 518 | } |
| 519 | *(int32_t *)pReplyData = 0; |
| 520 | effect_param_t *p = (effect_param_t *)pCmdData; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 521 | if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 522 | *(int32_t *)pReplyData = -EINVAL; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 523 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 524 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 525 | switch (*(uint32_t *)p->data) { |
Andy Hung | 9a2732b | 2016-10-18 17:13:09 -0700 | [diff] [blame] | 526 | case VISUALIZER_PARAM_CAPTURE_SIZE: { |
| 527 | const uint32_t captureSize = *((uint32_t *)p->data + 1); |
| 528 | if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) { |
| 529 | android_errorWriteLog(0x534e4554, "31781965"); |
| 530 | *(int32_t *)pReplyData = -EINVAL; |
| 531 | ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX); |
| 532 | } else { |
| 533 | pContext->mCaptureSize = captureSize; |
| 534 | ALOGV("set mCaptureSize = %u", captureSize); |
| 535 | } |
| 536 | } break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 537 | case VISUALIZER_PARAM_SCALING_MODE: |
| 538 | pContext->mScalingMode = *((uint32_t *)p->data + 1); |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 539 | ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 540 | break; |
Andy Hung | 9a2732b | 2016-10-18 17:13:09 -0700 | [diff] [blame] | 541 | case VISUALIZER_PARAM_LATENCY: { |
| 542 | uint32_t latency = *((uint32_t *)p->data + 1); |
| 543 | if (latency > MAX_LATENCY_MS) { |
| 544 | latency = MAX_LATENCY_MS; // clamp latency b/31781965 |
| 545 | } |
| 546 | pContext->mLatency = latency; |
| 547 | ALOGV("set mLatency = %u", latency); |
| 548 | } break; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 549 | case VISUALIZER_PARAM_MEASUREMENT_MODE: |
| 550 | pContext->mMeasurementMode = *((uint32_t *)p->data + 1); |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 551 | ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode); |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 552 | break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 553 | default: |
| 554 | *(int32_t *)pReplyData = -EINVAL; |
| 555 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 556 | } break; |
| 557 | case EFFECT_CMD_SET_DEVICE: |
| 558 | case EFFECT_CMD_SET_VOLUME: |
| 559 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 560 | break; |
| 561 | |
| 562 | |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 563 | case VISUALIZER_CMD_CAPTURE: { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 564 | uint32_t captureSize = pContext->mCaptureSize; |
Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 565 | if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 566 | ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32, |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 567 | *replySize, captureSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 568 | return -EINVAL; |
| 569 | } |
| 570 | if (pContext->mState == VISUALIZER_STATE_ACTIVE) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 571 | const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext); |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 572 | |
| 573 | // if audio framework has stopped playing audio although the effect is still |
| 574 | // active we must clear the capture buffer to return silence |
| 575 | if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) && |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 576 | (pContext->mBufferUpdateTime.tv_sec != 0) && |
| 577 | (deltaMs > MAX_STALL_TIME_MS)) { |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 578 | ALOGV("capture going to idle"); |
| 579 | pContext->mBufferUpdateTime.tv_sec = 0; |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 580 | memset(pReplyData, 0x80, captureSize); |
| 581 | } else { |
| 582 | int32_t latencyMs = pContext->mLatency; |
| 583 | latencyMs -= deltaMs; |
| 584 | if (latencyMs < 0) { |
| 585 | latencyMs = 0; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 586 | } |
Andy Hung | 9a2732b | 2016-10-18 17:13:09 -0700 | [diff] [blame] | 587 | uint32_t deltaSmpl = captureSize |
| 588 | + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000; |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 589 | |
Andy Hung | 9a2732b | 2016-10-18 17:13:09 -0700 | [diff] [blame] | 590 | // large sample rate, latency, or capture size, could cause overflow. |
| 591 | // do not offset more than the size of buffer. |
| 592 | if (deltaSmpl > CAPTURE_BUF_SIZE) { |
| 593 | android_errorWriteLog(0x534e4554, "31781965"); |
| 594 | deltaSmpl = CAPTURE_BUF_SIZE; |
| 595 | } |
| 596 | |
| 597 | int32_t capturePoint = pContext->mCaptureIdx - deltaSmpl; |
| 598 | // a negative capturePoint means we wrap the buffer. |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 599 | if (capturePoint < 0) { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 600 | uint32_t size = -capturePoint; |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 601 | if (size > captureSize) { |
| 602 | size = captureSize; |
| 603 | } |
| 604 | memcpy(pReplyData, |
| 605 | pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint, |
| 606 | size); |
| 607 | pReplyData = (char *)pReplyData + size; |
| 608 | captureSize -= size; |
| 609 | capturePoint = 0; |
| 610 | } |
| 611 | memcpy(pReplyData, |
| 612 | pContext->mCaptureBuf + capturePoint, |
| 613 | captureSize); |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 614 | } |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 615 | |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 616 | pContext->mLastCaptureIdx = pContext->mCaptureIdx; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 617 | } else { |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 618 | memset(pReplyData, 0x80, captureSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 619 | } |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 620 | |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 621 | } break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 622 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 623 | case VISUALIZER_CMD_MEASURE: { |
rago | 099ab28 | 2016-08-22 17:20:26 -0700 | [diff] [blame] | 624 | if (pReplyData == NULL || replySize == NULL || |
| 625 | *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) { |
rago | b66492c | 2016-10-07 18:16:09 -0700 | [diff] [blame] | 626 | if (replySize == NULL) { |
| 627 | ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL"); |
| 628 | } else { |
| 629 | ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32 |
| 630 | " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32, |
| 631 | *replySize, |
| 632 | uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT); |
| 633 | } |
rago | 099ab28 | 2016-08-22 17:20:26 -0700 | [diff] [blame] | 634 | android_errorWriteLog(0x534e4554, "30229821"); |
| 635 | return -EINVAL; |
| 636 | } |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 637 | uint16_t peakU16 = 0; |
| 638 | float sumRmsSquared = 0.0f; |
| 639 | uint8_t nbValidMeasurements = 0; |
| 640 | // reset measurements if last measurement was too long ago (which implies stored |
| 641 | // measurements aren't relevant anymore and shouldn't bias the new one) |
| 642 | const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext); |
| 643 | if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 644 | ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs); |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 645 | for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 646 | pContext->mPastMeasurements[i].mIsValid = false; |
| 647 | pContext->mPastMeasurements[i].mPeakU16 = 0; |
| 648 | pContext->mPastMeasurements[i].mRmsSquared = 0; |
| 649 | } |
| 650 | pContext->mMeasurementBufferIdx = 0; |
| 651 | } else { |
| 652 | // only use actual measurements, otherwise the first RMS measure happening before |
| 653 | // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially |
| 654 | // low |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 655 | for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 656 | if (pContext->mPastMeasurements[i].mIsValid) { |
| 657 | if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) { |
| 658 | peakU16 = pContext->mPastMeasurements[i].mPeakU16; |
| 659 | } |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 660 | sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 661 | nbValidMeasurements++; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements); |
| 666 | int32_t* pIntReplyData = (int32_t*)pReplyData; |
| 667 | // convert from I16 sample values to mB and write results |
| 668 | if (rms < 0.000016f) { |
| 669 | pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB |
| 670 | } else { |
| 671 | pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f)); |
| 672 | } |
| 673 | if (peakU16 == 0) { |
| 674 | pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB |
| 675 | } else { |
| 676 | pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f)); |
| 677 | } |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 678 | 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] | 679 | peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK], |
| 680 | rms, pIntReplyData[MEASUREMENT_IDX_RMS]); |
| 681 | } |
| 682 | break; |
| 683 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 684 | default: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 685 | ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 686 | return -EINVAL; |
| 687 | } |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 692 | /* Effect Control Interface Implementation: get_descriptor */ |
| 693 | int Visualizer_getDescriptor(effect_handle_t self, |
| 694 | effect_descriptor_t *pDescriptor) |
| 695 | { |
| 696 | VisualizerContext * pContext = (VisualizerContext *) self; |
| 697 | |
| 698 | if (pContext == NULL || pDescriptor == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 699 | ALOGV("Visualizer_getDescriptor() invalid param"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 700 | return -EINVAL; |
| 701 | } |
| 702 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 703 | *pDescriptor = gVisualizerDescriptor; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | } /* end Visualizer_getDescriptor */ |
| 707 | |
| 708 | // effect_handle_t interface implementation for visualizer effect |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 709 | const struct effect_interface_s gVisualizerInterface = { |
| 710 | Visualizer_process, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 711 | Visualizer_command, |
Eric Laurent | ba7b8f8 | 2011-06-17 18:54:16 -0700 | [diff] [blame] | 712 | Visualizer_getDescriptor, |
| 713 | NULL, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 714 | }; |
| 715 | |
Marco Nelissen | 7f16b19 | 2012-10-25 16:05:57 -0700 | [diff] [blame] | 716 | // This is the only symbol that needs to be exported |
| 717 | __attribute__ ((visibility ("default"))) |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 718 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
synergydev | c9d8ea7 | 2013-10-19 22:51:33 -0700 | [diff] [blame] | 719 | .tag = AUDIO_EFFECT_LIBRARY_TAG, |
| 720 | .version = EFFECT_LIBRARY_API_VERSION, |
| 721 | .name = "Visualizer Library", |
| 722 | .implementor = "The Android Open Source Project", |
| 723 | .create_effect = VisualizerLib_Create, |
| 724 | .release_effect = VisualizerLib_Release, |
| 725 | .get_descriptor = VisualizerLib_GetDescriptor, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | }; // extern "C" |