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