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