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