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