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> |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 28 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 29 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 30 | extern "C" { |
| 31 | |
| 32 | // effect_handle_t interface implementation for visualizer effect |
| 33 | extern const struct effect_interface_s gVisualizerInterface; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 34 | |
| 35 | // Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b |
| 36 | const effect_descriptor_t gVisualizerDescriptor = { |
| 37 | {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type |
| 38 | {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 39 | EFFECT_CONTROL_API_VERSION, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 40 | (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST), |
| 41 | 0, // TODO |
| 42 | 1, |
| 43 | "Visualizer", |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 44 | "The Android Open Source Project", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | enum visualizer_state_e { |
| 48 | VISUALIZER_STATE_UNINITIALIZED, |
| 49 | VISUALIZER_STATE_INITIALIZED, |
| 50 | VISUALIZER_STATE_ACTIVE, |
| 51 | }; |
| 52 | |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 53 | // 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] | 54 | // 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] | 55 | #define MAX_STALL_TIME_MS 1000 |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 56 | |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 57 | #define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone" |
| 58 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 59 | #define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms |
| 60 | |
| 61 | // 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] | 62 | #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] | 63 | |
| 64 | |
| 65 | struct BufferStats { |
| 66 | bool mIsValid; |
| 67 | uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer |
| 68 | float mRmsSquared; // the average square of the samples in a buffer |
| 69 | }; |
| 70 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 71 | struct VisualizerContext { |
| 72 | const struct effect_interface_s *mItfe; |
| 73 | effect_config_t mConfig; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 74 | uint32_t mCaptureIdx; |
| 75 | uint32_t mCaptureSize; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 76 | uint32_t mScalingMode; |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 77 | uint8_t mState; |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 78 | uint32_t mLastCaptureIdx; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 79 | uint32_t mLatency; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 80 | struct timespec mBufferUpdateTime; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 81 | uint8_t mCaptureBuf[CAPTURE_BUF_SIZE]; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 82 | // for measurements |
| 83 | uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed |
| 84 | uint32_t mMeasurementMode; |
| 85 | uint8_t mMeasurementWindowSizeInBuffers; |
| 86 | uint8_t mMeasurementBufferIdx; |
| 87 | BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS]; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 90 | // |
| 91 | //--- Local functions |
| 92 | // |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 93 | uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) { |
| 94 | uint32_t deltaMs = 0; |
| 95 | if (pContext->mBufferUpdateTime.tv_sec != 0) { |
| 96 | struct timespec ts; |
| 97 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 98 | time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec; |
| 99 | long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec; |
| 100 | if (nsec < 0) { |
| 101 | --secs; |
| 102 | nsec += 1000000000; |
| 103 | } |
| 104 | deltaMs = secs * 1000 + nsec / 1000000; |
| 105 | } |
| 106 | } |
| 107 | return deltaMs; |
| 108 | } |
| 109 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 110 | |
| 111 | void Visualizer_reset(VisualizerContext *pContext) |
| 112 | { |
| 113 | pContext->mCaptureIdx = 0; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 114 | pContext->mLastCaptureIdx = 0; |
Eric Laurent | 183dc77 | 2012-03-23 15:35:48 -0700 | [diff] [blame] | 115 | pContext->mBufferUpdateTime.tv_sec = 0; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 116 | pContext->mLatency = 0; |
| 117 | memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | //---------------------------------------------------------------------------- |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 121 | // Visualizer_setConfig() |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 122 | //---------------------------------------------------------------------------- |
| 123 | // Purpose: Set input and output audio configuration. |
| 124 | // |
| 125 | // Inputs: |
| 126 | // pContext: effect engine context |
| 127 | // pConfig: pointer to effect_config_t structure holding input and output |
| 128 | // configuration parameters |
| 129 | // |
| 130 | // Outputs: |
| 131 | // |
| 132 | //---------------------------------------------------------------------------- |
| 133 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 134 | int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 135 | { |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 136 | ALOGV("Visualizer_setConfig start"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 137 | |
| 138 | if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL; |
| 139 | if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL; |
| 140 | if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 141 | if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 142 | if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE && |
| 143 | pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 144 | if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 145 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 146 | pContext->mConfig = *pConfig; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 147 | |
| 148 | Visualizer_reset(pContext); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | //---------------------------------------------------------------------------- |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 155 | // Visualizer_getConfig() |
| 156 | //---------------------------------------------------------------------------- |
| 157 | // Purpose: Get input and output audio configuration. |
| 158 | // |
| 159 | // Inputs: |
| 160 | // pContext: effect engine context |
| 161 | // pConfig: pointer to effect_config_t structure holding input and output |
| 162 | // configuration parameters |
| 163 | // |
| 164 | // Outputs: |
| 165 | // |
| 166 | //---------------------------------------------------------------------------- |
| 167 | |
| 168 | void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig) |
| 169 | { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 170 | *pConfig = pContext->mConfig; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | //---------------------------------------------------------------------------- |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 175 | // Visualizer_init() |
| 176 | //---------------------------------------------------------------------------- |
| 177 | // Purpose: Initialize engine with default configuration. |
| 178 | // |
| 179 | // Inputs: |
| 180 | // pContext: effect engine context |
| 181 | // |
| 182 | // Outputs: |
| 183 | // |
| 184 | //---------------------------------------------------------------------------- |
| 185 | |
| 186 | int Visualizer_init(VisualizerContext *pContext) |
| 187 | { |
| 188 | pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 189 | pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 190 | pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 191 | pContext->mConfig.inputCfg.samplingRate = 44100; |
| 192 | pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 193 | pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 194 | pContext->mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 195 | pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 196 | pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 197 | pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 198 | pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 199 | pContext->mConfig.outputCfg.samplingRate = 44100; |
| 200 | pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 201 | pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 202 | pContext->mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 203 | pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 204 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 205 | // visualization initialization |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 206 | pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 207 | pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 208 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 209 | // measurement initialization |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 210 | pContext->mChannelCount = |
| 211 | audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels); |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 212 | pContext->mMeasurementMode = MEASUREMENT_MODE_NONE; |
| 213 | pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS; |
| 214 | pContext->mMeasurementBufferIdx = 0; |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 215 | for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 216 | pContext->mPastMeasurements[i].mIsValid = false; |
| 217 | pContext->mPastMeasurements[i].mPeakU16 = 0; |
| 218 | pContext->mPastMeasurements[i].mRmsSquared = 0; |
| 219 | } |
| 220 | |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 221 | Visualizer_setConfig(pContext, &pContext->mConfig); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | // |
| 227 | //--- Effect Library Interface Implementation |
| 228 | // |
| 229 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 230 | int VisualizerLib_Create(const effect_uuid_t *uuid, |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 231 | int32_t /*sessionId*/, |
| 232 | int32_t /*ioId*/, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 233 | effect_handle_t *pHandle) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 234 | int ret; |
| 235 | int i; |
| 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 | int retsize; |
| 418 | |
| 419 | if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) { |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 423 | // ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 424 | |
| 425 | switch (cmdCode) { |
| 426 | case EFFECT_CMD_INIT: |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 427 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 428 | return -EINVAL; |
| 429 | } |
| 430 | *(int *) pReplyData = Visualizer_init(pContext); |
| 431 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 432 | case EFFECT_CMD_SET_CONFIG: |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 433 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 434 | || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 435 | return -EINVAL; |
| 436 | } |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 437 | *(int *) pReplyData = Visualizer_setConfig(pContext, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 438 | (effect_config_t *) pCmdData); |
| 439 | break; |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 440 | case EFFECT_CMD_GET_CONFIG: |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 441 | if (pReplyData == NULL || replySize == NULL || |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 442 | *replySize != sizeof(effect_config_t)) { |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | Visualizer_getConfig(pContext, (effect_config_t *)pReplyData); |
| 446 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 447 | case EFFECT_CMD_RESET: |
| 448 | Visualizer_reset(pContext); |
| 449 | break; |
| 450 | case EFFECT_CMD_ENABLE: |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 451 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 452 | return -EINVAL; |
| 453 | } |
| 454 | if (pContext->mState != VISUALIZER_STATE_INITIALIZED) { |
| 455 | return -ENOSYS; |
| 456 | } |
| 457 | pContext->mState = VISUALIZER_STATE_ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 458 | ALOGV("EFFECT_CMD_ENABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 459 | *(int *)pReplyData = 0; |
| 460 | break; |
| 461 | case EFFECT_CMD_DISABLE: |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 462 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 463 | return -EINVAL; |
| 464 | } |
| 465 | if (pContext->mState != VISUALIZER_STATE_ACTIVE) { |
| 466 | return -ENOSYS; |
| 467 | } |
| 468 | pContext->mState = VISUALIZER_STATE_INITIALIZED; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 469 | ALOGV("EFFECT_CMD_DISABLE() OK"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 470 | *(int *)pReplyData = 0; |
| 471 | break; |
| 472 | case EFFECT_CMD_GET_PARAM: { |
| 473 | if (pCmdData == NULL || |
| 474 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) || |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 475 | pReplyData == NULL || replySize == NULL || |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 476 | *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) { |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t)); |
| 480 | effect_param_t *p = (effect_param_t *)pReplyData; |
| 481 | p->status = 0; |
| 482 | *replySize = sizeof(effect_param_t) + sizeof(uint32_t); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 483 | if (p->psize != sizeof(uint32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 484 | p->status = -EINVAL; |
| 485 | break; |
| 486 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 487 | switch (*(uint32_t *)p->data) { |
| 488 | case VISUALIZER_PARAM_CAPTURE_SIZE: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 489 | ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 490 | *((uint32_t *)p->data + 1) = pContext->mCaptureSize; |
| 491 | p->vsize = sizeof(uint32_t); |
| 492 | *replySize += sizeof(uint32_t); |
| 493 | break; |
| 494 | case VISUALIZER_PARAM_SCALING_MODE: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 495 | ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 496 | *((uint32_t *)p->data + 1) = pContext->mScalingMode; |
| 497 | p->vsize = sizeof(uint32_t); |
| 498 | *replySize += sizeof(uint32_t); |
| 499 | break; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 500 | case VISUALIZER_PARAM_MEASUREMENT_MODE: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 501 | ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode); |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 502 | *((uint32_t *)p->data + 1) = pContext->mMeasurementMode; |
| 503 | p->vsize = sizeof(uint32_t); |
| 504 | *replySize += sizeof(uint32_t); |
| 505 | break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 506 | default: |
| 507 | p->status = -EINVAL; |
| 508 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 509 | } break; |
| 510 | case EFFECT_CMD_SET_PARAM: { |
| 511 | if (pCmdData == NULL || |
| 512 | cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) || |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 513 | pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 514 | return -EINVAL; |
| 515 | } |
| 516 | *(int32_t *)pReplyData = 0; |
| 517 | effect_param_t *p = (effect_param_t *)pCmdData; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 518 | if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 519 | *(int32_t *)pReplyData = -EINVAL; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 520 | break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 521 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 522 | switch (*(uint32_t *)p->data) { |
| 523 | case VISUALIZER_PARAM_CAPTURE_SIZE: |
| 524 | pContext->mCaptureSize = *((uint32_t *)p->data + 1); |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 525 | ALOGV("set mCaptureSize = %" PRIu32, pContext->mCaptureSize); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 526 | break; |
| 527 | case VISUALIZER_PARAM_SCALING_MODE: |
| 528 | pContext->mScalingMode = *((uint32_t *)p->data + 1); |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 529 | ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode); |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 530 | break; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 531 | case VISUALIZER_PARAM_LATENCY: |
| 532 | pContext->mLatency = *((uint32_t *)p->data + 1); |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 533 | ALOGV("set mLatency = %" PRIu32, pContext->mLatency); |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 534 | break; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 535 | case VISUALIZER_PARAM_MEASUREMENT_MODE: |
| 536 | pContext->mMeasurementMode = *((uint32_t *)p->data + 1); |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 537 | ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode); |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 538 | break; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 539 | default: |
| 540 | *(int32_t *)pReplyData = -EINVAL; |
| 541 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 542 | } break; |
| 543 | case EFFECT_CMD_SET_DEVICE: |
| 544 | case EFFECT_CMD_SET_VOLUME: |
| 545 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 546 | break; |
| 547 | |
| 548 | |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 549 | case VISUALIZER_CMD_CAPTURE: { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 550 | uint32_t captureSize = pContext->mCaptureSize; |
Eric Laurent | 6368e6d | 2015-06-19 15:33:57 -0700 | [diff] [blame^] | 551 | if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 552 | ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32, |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 553 | *replySize, captureSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 554 | return -EINVAL; |
| 555 | } |
| 556 | if (pContext->mState == VISUALIZER_STATE_ACTIVE) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 557 | const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext); |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 558 | |
| 559 | // if audio framework has stopped playing audio although the effect is still |
| 560 | // active we must clear the capture buffer to return silence |
| 561 | if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) && |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 562 | (pContext->mBufferUpdateTime.tv_sec != 0) && |
| 563 | (deltaMs > MAX_STALL_TIME_MS)) { |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 564 | ALOGV("capture going to idle"); |
| 565 | pContext->mBufferUpdateTime.tv_sec = 0; |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 566 | memset(pReplyData, 0x80, captureSize); |
| 567 | } else { |
| 568 | int32_t latencyMs = pContext->mLatency; |
| 569 | latencyMs -= deltaMs; |
| 570 | if (latencyMs < 0) { |
| 571 | latencyMs = 0; |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 572 | } |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 573 | const uint32_t deltaSmpl = |
| 574 | pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000; |
| 575 | int32_t capturePoint = pContext->mCaptureIdx - captureSize - deltaSmpl; |
| 576 | |
| 577 | if (capturePoint < 0) { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 578 | uint32_t size = -capturePoint; |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 579 | if (size > captureSize) { |
| 580 | size = captureSize; |
| 581 | } |
| 582 | memcpy(pReplyData, |
| 583 | pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint, |
| 584 | size); |
| 585 | pReplyData = (char *)pReplyData + size; |
| 586 | captureSize -= size; |
| 587 | capturePoint = 0; |
| 588 | } |
| 589 | memcpy(pReplyData, |
| 590 | pContext->mCaptureBuf + capturePoint, |
| 591 | captureSize); |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 592 | } |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 593 | |
Marco Nelissen | f06c2ed | 2012-06-06 09:52:31 -0700 | [diff] [blame] | 594 | pContext->mLastCaptureIdx = pContext->mCaptureIdx; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 595 | } else { |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 596 | memset(pReplyData, 0x80, captureSize); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 597 | } |
Eric Laurent | 3df40a0 | 2011-11-10 10:02:18 -0800 | [diff] [blame] | 598 | |
Ryszard Grzesica | abb7b17 | 2014-01-17 11:40:16 +0100 | [diff] [blame] | 599 | } break; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 600 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 601 | case VISUALIZER_CMD_MEASURE: { |
| 602 | uint16_t peakU16 = 0; |
| 603 | float sumRmsSquared = 0.0f; |
| 604 | uint8_t nbValidMeasurements = 0; |
| 605 | // reset measurements if last measurement was too long ago (which implies stored |
| 606 | // measurements aren't relevant anymore and shouldn't bias the new one) |
| 607 | const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext); |
| 608 | if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) { |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 609 | ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs); |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 610 | for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 611 | pContext->mPastMeasurements[i].mIsValid = false; |
| 612 | pContext->mPastMeasurements[i].mPeakU16 = 0; |
| 613 | pContext->mPastMeasurements[i].mRmsSquared = 0; |
| 614 | } |
| 615 | pContext->mMeasurementBufferIdx = 0; |
| 616 | } else { |
| 617 | // only use actual measurements, otherwise the first RMS measure happening before |
| 618 | // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially |
| 619 | // low |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 620 | for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) { |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 621 | if (pContext->mPastMeasurements[i].mIsValid) { |
| 622 | if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) { |
| 623 | peakU16 = pContext->mPastMeasurements[i].mPeakU16; |
| 624 | } |
Jean-Michel Trivi | 6fbc9ef | 2013-09-24 15:31:13 -0700 | [diff] [blame] | 625 | sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared; |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 626 | nbValidMeasurements++; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements); |
| 631 | int32_t* pIntReplyData = (int32_t*)pReplyData; |
| 632 | // convert from I16 sample values to mB and write results |
| 633 | if (rms < 0.000016f) { |
| 634 | pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB |
| 635 | } else { |
| 636 | pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f)); |
| 637 | } |
| 638 | if (peakU16 == 0) { |
| 639 | pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB |
| 640 | } else { |
| 641 | pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f)); |
| 642 | } |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 643 | 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] | 644 | peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK], |
| 645 | rms, pIntReplyData[MEASUREMENT_IDX_RMS]); |
| 646 | } |
| 647 | break; |
| 648 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 649 | default: |
Mark Salyzyn | 7cb0e73 | 2014-04-18 13:48:25 -0700 | [diff] [blame] | 650 | ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 651 | return -EINVAL; |
| 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 657 | /* Effect Control Interface Implementation: get_descriptor */ |
| 658 | int Visualizer_getDescriptor(effect_handle_t self, |
| 659 | effect_descriptor_t *pDescriptor) |
| 660 | { |
| 661 | VisualizerContext * pContext = (VisualizerContext *) self; |
| 662 | |
| 663 | if (pContext == NULL || pDescriptor == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 664 | ALOGV("Visualizer_getDescriptor() invalid param"); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 665 | return -EINVAL; |
| 666 | } |
| 667 | |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 668 | *pDescriptor = gVisualizerDescriptor; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 669 | |
| 670 | return 0; |
| 671 | } /* end Visualizer_getDescriptor */ |
| 672 | |
| 673 | // effect_handle_t interface implementation for visualizer effect |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 674 | const struct effect_interface_s gVisualizerInterface = { |
| 675 | Visualizer_process, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 676 | Visualizer_command, |
Eric Laurent | ba7b8f8 | 2011-06-17 18:54:16 -0700 | [diff] [blame] | 677 | Visualizer_getDescriptor, |
| 678 | NULL, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 679 | }; |
| 680 | |
Marco Nelissen | 7f16b19 | 2012-10-25 16:05:57 -0700 | [diff] [blame] | 681 | // This is the only symbol that needs to be exported |
| 682 | __attribute__ ((visibility ("default"))) |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 683 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
synergydev | c9d8ea7 | 2013-10-19 22:51:33 -0700 | [diff] [blame] | 684 | .tag = AUDIO_EFFECT_LIBRARY_TAG, |
| 685 | .version = EFFECT_LIBRARY_API_VERSION, |
| 686 | .name = "Visualizer Library", |
| 687 | .implementor = "The Android Open Source Project", |
| 688 | .create_effect = VisualizerLib_Create, |
| 689 | .release_effect = VisualizerLib_Release, |
| 690 | .get_descriptor = VisualizerLib_GetDescriptor, |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | }; // extern "C" |