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