blob: 8d408dde698bf2aee3da6f518e85c29042cc6361 [file] [log] [blame]
Eric Laurentda7581b2010-07-02 08:12:41 -07001/*
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 Trivi3476de62012-04-15 17:15:07 -070017#define LOG_TAG "EffectVisualizer"
Eric Laurentda7581b2010-07-02 08:12:41 -070018//#define LOG_NDEBUG 0
Mark Salyzyn60d02072016-09-29 08:48:48 -070019
Eric Laurentda7581b2010-07-02 08:12:41 -070020#include <assert.h>
Mark Salyzyn7cb0e732014-04-18 13:48:25 -070021#include <inttypes.h>
Mark Salyzyn60d02072016-09-29 08:48:48 -070022#include <math.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070023#include <stdlib.h>
24#include <string.h>
Eric Laurent183dc772012-03-23 15:35:48 -070025#include <time.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070026
Andy Hungfda70d32018-08-08 11:51:52 -070027#include <algorithm> // max
Mark Salyzyn60d02072016-09-29 08:48:48 -070028#include <new>
29
30#include <log/log.h>
31
32#include <audio_effects/effect_visualizer.h>
Andy Hungfda70d32018-08-08 11:51:52 -070033#include <audio_utils/primitives.h>
34
Andy Hungfda70d32018-08-08 11:51:52 -070035#ifdef BUILD_FLOAT
36
37static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_FLOAT;
38
39#else
40
41static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_16_BIT;
42
43#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -070044
Eric Laurente1315cf2011-05-17 19:16:02 -070045extern "C" {
46
47// effect_handle_t interface implementation for visualizer effect
48extern const struct effect_interface_s gVisualizerInterface;
Eric Laurentda7581b2010-07-02 08:12:41 -070049
50// Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
51const effect_descriptor_t gVisualizerDescriptor = {
52 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
53 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
Eric Laurente1315cf2011-05-17 19:16:02 -070054 EFFECT_CONTROL_API_VERSION,
Eric Laurentda7581b2010-07-02 08:12:41 -070055 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
56 0, // TODO
57 1,
58 "Visualizer",
Eric Laurente1315cf2011-05-17 19:16:02 -070059 "The Android Open Source Project",
Eric Laurentda7581b2010-07-02 08:12:41 -070060};
61
62enum visualizer_state_e {
63 VISUALIZER_STATE_UNINITIALIZED,
64 VISUALIZER_STATE_INITIALIZED,
65 VISUALIZER_STATE_ACTIVE,
66};
67
Eric Laurent183dc772012-03-23 15:35:48 -070068// maximum time since last capture buffer update before resetting capture buffer. This means
Eric Laurent3df40a02011-11-10 10:02:18 -080069// that the framework has stopped playing audio and we must start returning silence
Eric Laurent183dc772012-03-23 15:35:48 -070070#define MAX_STALL_TIME_MS 1000
Eric Laurent3df40a02011-11-10 10:02:18 -080071
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070072#define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
73
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070074#define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
75
Andy Hung9a2732b2016-10-18 17:13:09 -070076#define MAX_LATENCY_MS 3000 // 3 seconds of latency for audio pipeline
77
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070078// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070079#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070080
81
82struct BufferStats {
83 bool mIsValid;
84 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
85 float mRmsSquared; // the average square of the samples in a buffer
86};
87
Eric Laurentda7581b2010-07-02 08:12:41 -070088struct VisualizerContext {
89 const struct effect_interface_s *mItfe;
90 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070091 uint32_t mCaptureIdx;
92 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070093 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080094 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070095 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070096 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070097 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070098 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070099 // for measurements
100 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
101 uint32_t mMeasurementMode;
102 uint8_t mMeasurementWindowSizeInBuffers;
103 uint8_t mMeasurementBufferIdx;
104 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -0700105};
106
Eric Laurentda7581b2010-07-02 08:12:41 -0700107//
108//--- Local functions
109//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700110uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
111 uint32_t deltaMs = 0;
112 if (pContext->mBufferUpdateTime.tv_sec != 0) {
113 struct timespec ts;
114 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
115 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
116 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
117 if (nsec < 0) {
118 --secs;
119 nsec += 1000000000;
120 }
121 deltaMs = secs * 1000 + nsec / 1000000;
122 }
123 }
124 return deltaMs;
125}
126
Eric Laurentda7581b2010-07-02 08:12:41 -0700127
128void Visualizer_reset(VisualizerContext *pContext)
129{
130 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700131 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700132 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700133 pContext->mLatency = 0;
134 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700135}
136
137//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800138// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700139//----------------------------------------------------------------------------
140// Purpose: Set input and output audio configuration.
141//
142// Inputs:
143// pContext: effect engine context
144// pConfig: pointer to effect_config_t structure holding input and output
145// configuration parameters
146//
147// Outputs:
148//
149//----------------------------------------------------------------------------
150
Eric Laurent3d5188b2011-12-16 15:30:36 -0800151int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700152{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800153 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700154
155 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
156 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
157 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700158 const uint32_t channelCount = audio_channel_count_from_out_mask(pConfig->inputCfg.channels);
159#ifdef SUPPORT_MC
Andy Hung936845a2021-06-08 00:09:06 -0700160 if (channelCount < 1 || channelCount > FCC_LIMIT) return -EINVAL;
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700161#else
162 if (channelCount != FCC_2) return -EINVAL;
163#endif
Eric Laurentda7581b2010-07-02 08:12:41 -0700164 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
165 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Andy Hungfda70d32018-08-08 11:51:52 -0700166 if (pConfig->inputCfg.format != kProcessFormat) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700167
Andy Hungc0b05da2023-06-27 20:33:19 -0700168 pContext->mChannelCount = channelCount;
Glenn Kastena189a682012-02-20 12:16:30 -0800169 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700170
171 Visualizer_reset(pContext);
172
173 return 0;
174}
175
176
177//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800178// Visualizer_getConfig()
179//----------------------------------------------------------------------------
180// Purpose: Get input and output audio configuration.
181//
182// Inputs:
183// pContext: effect engine context
184// pConfig: pointer to effect_config_t structure holding input and output
185// configuration parameters
186//
187// Outputs:
188//
189//----------------------------------------------------------------------------
190
191void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
192{
Glenn Kastena189a682012-02-20 12:16:30 -0800193 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800194}
195
196
197//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700198// Visualizer_init()
199//----------------------------------------------------------------------------
200// Purpose: Initialize engine with default configuration.
201//
202// Inputs:
203// pContext: effect engine context
204//
205// Outputs:
206//
207//----------------------------------------------------------------------------
208
209int Visualizer_init(VisualizerContext *pContext)
210{
211 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700212 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
Andy Hungfda70d32018-08-08 11:51:52 -0700213 pContext->mConfig.inputCfg.format = kProcessFormat;
Eric Laurentda7581b2010-07-02 08:12:41 -0700214 pContext->mConfig.inputCfg.samplingRate = 44100;
215 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
216 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
217 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
218 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
219 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700220 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
Andy Hungfda70d32018-08-08 11:51:52 -0700221 pContext->mConfig.outputCfg.format = kProcessFormat;
Eric Laurentda7581b2010-07-02 08:12:41 -0700222 pContext->mConfig.outputCfg.samplingRate = 44100;
223 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
224 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
225 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
226 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
227
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700228 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700229 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700230 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700231
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700232 // measurement initialization
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700233 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
234 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
235 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700236 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700237 pContext->mPastMeasurements[i].mIsValid = false;
238 pContext->mPastMeasurements[i].mPeakU16 = 0;
239 pContext->mPastMeasurements[i].mRmsSquared = 0;
240 }
241
Eric Laurent3d5188b2011-12-16 15:30:36 -0800242 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700243
244 return 0;
245}
246
247//
248//--- Effect Library Interface Implementation
249//
250
Glenn Kasten5e92a782012-01-30 07:40:52 -0800251int VisualizerLib_Create(const effect_uuid_t *uuid,
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700252 int32_t /*sessionId*/,
253 int32_t /*ioId*/,
Eric Laurente1315cf2011-05-17 19:16:02 -0700254 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700255 int ret;
Eric Laurentda7581b2010-07-02 08:12:41 -0700256
Eric Laurente1315cf2011-05-17 19:16:02 -0700257 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700258 return -EINVAL;
259 }
260
261 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
262 return -EINVAL;
263 }
264
265 VisualizerContext *pContext = new VisualizerContext;
266
267 pContext->mItfe = &gVisualizerInterface;
268 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
269
270 ret = Visualizer_init(pContext);
271 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000272 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700273 delete pContext;
274 return ret;
275 }
276
Eric Laurente1315cf2011-05-17 19:16:02 -0700277 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700278
279 pContext->mState = VISUALIZER_STATE_INITIALIZED;
280
Steve Block3856b092011-10-20 11:56:00 +0100281 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700282
283 return 0;
284
285}
286
Eric Laurente1315cf2011-05-17 19:16:02 -0700287int VisualizerLib_Release(effect_handle_t handle) {
288 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700289
Steve Block3856b092011-10-20 11:56:00 +0100290 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700291 if (pContext == NULL) {
292 return -EINVAL;
293 }
294 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
295 delete pContext;
296
297 return 0;
298}
299
Glenn Kasten5e92a782012-01-30 07:40:52 -0800300int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700301 effect_descriptor_t *pDescriptor) {
302
303 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100304 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700305 return -EINVAL;
306 }
307
308 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800309 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700310 return 0;
311 }
312
313 return -EINVAL;
314} /* end VisualizerLib_GetDescriptor */
315
Eric Laurentda7581b2010-07-02 08:12:41 -0700316//
317//--- Effect Control Interface Implementation
318//
319
Eric Laurente1315cf2011-05-17 19:16:02 -0700320int Visualizer_process(
Andy Hungfda70d32018-08-08 11:51:52 -0700321 effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700322{
Eric Laurente1315cf2011-05-17 19:16:02 -0700323 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700324
325 if (pContext == NULL) {
326 return -EINVAL;
327 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700328
329 if (inBuffer == NULL || inBuffer->raw == NULL ||
330 outBuffer == NULL || outBuffer->raw == NULL ||
331 inBuffer->frameCount != outBuffer->frameCount ||
332 inBuffer->frameCount == 0) {
333 return -EINVAL;
334 }
335
Andy Hungfda70d32018-08-08 11:51:52 -0700336 const size_t sampleLen = inBuffer->frameCount * pContext->mChannelCount;
337
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700338 // perform measurements if needed
339 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
340 // find the peak and RMS squared for the new buffer
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700341 float rmsSqAcc = 0;
Andy Hungfda70d32018-08-08 11:51:52 -0700342
343#ifdef BUILD_FLOAT
344 float maxSample = 0.f;
345 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
346 maxSample = fmax(maxSample, fabs(inBuffer->f32[inIdx]));
347 rmsSqAcc += inBuffer->f32[inIdx] * inBuffer->f32[inIdx];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700348 }
Andy Hungfda70d32018-08-08 11:51:52 -0700349 maxSample *= 1 << 15; // scale to int16_t, with exactly 1 << 15 representing positive num.
350 rmsSqAcc *= 1 << 30; // scale to int16_t * 2
351#else
352 int maxSample = 0;
353 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
354 maxSample = std::max(maxSample, std::abs(int32_t(inBuffer->s16[inIdx])));
355 rmsSqAcc += inBuffer->s16[inIdx] * inBuffer->s16[inIdx];
356 }
357#endif
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700358 // store the measurement
359 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
360 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700361 rmsSqAcc / sampleLen;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700362 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
363 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
364 pContext->mMeasurementBufferIdx = 0;
365 }
366 }
367
Andy Hungfda70d32018-08-08 11:51:52 -0700368#ifdef BUILD_FLOAT
369 float fscale; // multiplicative scale
370#else
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700371 int32_t shift;
Andy Hungfda70d32018-08-08 11:51:52 -0700372#endif // BUILD_FLOAT
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700373
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700374 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
375 // derive capture scaling factor from peak value in current buffer
376 // this gives more interesting captures for display.
Andy Hungfda70d32018-08-08 11:51:52 -0700377
378#ifdef BUILD_FLOAT
379 float maxSample = 0.f;
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700380 for (size_t inIdx = 0; inIdx < sampleLen; ) {
381 // we reconstruct the actual summed value to ensure proper normalization
382 // for multichannel outputs (channels > 2 may often be 0).
383 float smp = 0.f;
384 for (int i = 0; i < pContext->mChannelCount; ++i) {
385 smp += inBuffer->f32[inIdx++];
386 }
387 maxSample = fmax(maxSample, fabs(smp));
Andy Hungfda70d32018-08-08 11:51:52 -0700388 }
389 if (maxSample > 0.f) {
Andy Hungb0ffc5a2019-06-17 11:53:41 -0700390 fscale = 0.99f / maxSample;
Andy Hungfda70d32018-08-08 11:51:52 -0700391 int exp; // unused
392 const float significand = frexp(fscale, &exp);
393 if (significand == 0.5f) {
394 fscale *= 255.f / 256.f; // avoid returning unaltered PCM signal
395 }
396 } else {
397 // scale doesn't matter, the values are all 0.
398 fscale = 1.f;
399 }
400#else
401 int32_t orAccum = 0;
402 for (size_t i = 0; i < sampleLen; ++i) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700403 int32_t smp = inBuffer->s16[i];
404 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
Andy Hungfda70d32018-08-08 11:51:52 -0700405 orAccum |= smp;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700406 }
Andy Hungfda70d32018-08-08 11:51:52 -0700407
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700408 // A maximum amplitude signal will have 17 leading zeros, which we want to
409 // translate to a shift of 8 (for converting 16 bit to 8 bit)
Andy Hungfda70d32018-08-08 11:51:52 -0700410 shift = 25 - __builtin_clz(orAccum);
411
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700412 // Never scale by less than 8 to avoid returning unaltered PCM signal.
413 if (shift < 3) {
414 shift = 3;
415 }
416 // add one to combine the division by 2 needed after summing left and right channels below
417 shift++;
Andy Hungfda70d32018-08-08 11:51:52 -0700418#endif // BUILD_FLOAT
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700419 } else {
420 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
Andy Hungfda70d32018-08-08 11:51:52 -0700421#ifdef BUILD_FLOAT
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700422 // Note: if channels are uncorrelated, 1/sqrt(N) could be used at the risk of clipping.
423 fscale = 1.f / pContext->mChannelCount; // account for summing all the channels together.
Andy Hungfda70d32018-08-08 11:51:52 -0700424#else
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700425 shift = 9;
Andy Hungfda70d32018-08-08 11:51:52 -0700426#endif // BUILD_FLOAT
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700427 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700428
Eric Laurentda7581b2010-07-02 08:12:41 -0700429 uint32_t captIdx;
430 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700431 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700432 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700433 inIdx < sampleLen;
434 captIdx++) {
435 if (captIdx >= CAPTURE_BUF_SIZE) captIdx = 0; // wrap
436
Andy Hungfda70d32018-08-08 11:51:52 -0700437#ifdef BUILD_FLOAT
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700438 float smp = 0.f;
439 for (uint32_t i = 0; i < pContext->mChannelCount; ++i) {
440 smp += inBuffer->f32[inIdx++];
441 }
442 buf[captIdx] = clamp8_from_float(smp * fscale);
Andy Hungfda70d32018-08-08 11:51:52 -0700443#else
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700444 const int32_t smp = (inBuffer->s16[inIdx] + inBuffer->s16[inIdx + 1]) >> shift;
445 inIdx += FCC_2; // integer supports stereo only.
Eric Laurentda7581b2010-07-02 08:12:41 -0700446 buf[captIdx] = ((uint8_t)smp)^0x80;
Andy Hungfda70d32018-08-08 11:51:52 -0700447#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -0700448 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700449
450 // XXX the following two should really be atomic, though it probably doesn't
451 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700452 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700453 // update last buffer update time stamp
454 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
455 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700456 }
457
458 if (inBuffer->raw != outBuffer->raw) {
Andy Hungfda70d32018-08-08 11:51:52 -0700459#ifdef BUILD_FLOAT
460 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
461 for (size_t i = 0; i < sampleLen; ++i) {
462 outBuffer->f32[i] += inBuffer->f32[i];
463 }
464 } else {
465 memcpy(outBuffer->raw, inBuffer->raw, sampleLen * sizeof(float));
466 }
467#else
Eric Laurentda7581b2010-07-02 08:12:41 -0700468 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
469 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
470 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
471 }
472 } else {
473 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
474 }
Andy Hungfda70d32018-08-08 11:51:52 -0700475#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -0700476 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700477 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
478 return -ENODATA;
479 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700480 return 0;
481} // end Visualizer_process
482
Eric Laurente1315cf2011-05-17 19:16:02 -0700483int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700484 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700485
Eric Laurente1315cf2011-05-17 19:16:02 -0700486 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700487
488 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
489 return -EINVAL;
490 }
491
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700492// ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700493
494 switch (cmdCode) {
495 case EFFECT_CMD_INIT:
Eric Laurent0f714a42015-06-19 15:33:57 -0700496 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700497 return -EINVAL;
498 }
499 *(int *) pReplyData = Visualizer_init(pContext);
500 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800501 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700502 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent0f714a42015-06-19 15:33:57 -0700503 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700504 return -EINVAL;
505 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800506 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700507 (effect_config_t *) pCmdData);
508 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800509 case EFFECT_CMD_GET_CONFIG:
Eric Laurent0f714a42015-06-19 15:33:57 -0700510 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800511 *replySize != sizeof(effect_config_t)) {
512 return -EINVAL;
513 }
514 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
515 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700516 case EFFECT_CMD_RESET:
517 Visualizer_reset(pContext);
518 break;
519 case EFFECT_CMD_ENABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700520 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700521 return -EINVAL;
522 }
523 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
524 return -ENOSYS;
525 }
526 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700528 *(int *)pReplyData = 0;
529 break;
530 case EFFECT_CMD_DISABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700531 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700532 return -EINVAL;
533 }
534 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
535 return -ENOSYS;
536 }
537 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100538 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700539 *(int *)pReplyData = 0;
540 break;
541 case EFFECT_CMD_GET_PARAM: {
542 if (pCmdData == NULL ||
543 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700544 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700545 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
546 return -EINVAL;
547 }
548 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
549 effect_param_t *p = (effect_param_t *)pReplyData;
550 p->status = 0;
551 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700552 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700553 p->status = -EINVAL;
554 break;
555 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700556 switch (*(uint32_t *)p->data) {
557 case VISUALIZER_PARAM_CAPTURE_SIZE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700558 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700559 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
560 p->vsize = sizeof(uint32_t);
561 *replySize += sizeof(uint32_t);
562 break;
563 case VISUALIZER_PARAM_SCALING_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700564 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700565 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
566 p->vsize = sizeof(uint32_t);
567 *replySize += sizeof(uint32_t);
568 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700569 case VISUALIZER_PARAM_MEASUREMENT_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700570 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700571 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
572 p->vsize = sizeof(uint32_t);
573 *replySize += sizeof(uint32_t);
574 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700575 default:
576 p->status = -EINVAL;
577 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700578 } break;
579 case EFFECT_CMD_SET_PARAM: {
580 if (pCmdData == NULL ||
581 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700582 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700583 return -EINVAL;
584 }
585 *(int32_t *)pReplyData = 0;
586 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700587 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700588 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700589 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700590 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700591 switch (*(uint32_t *)p->data) {
Andy Hung9a2732b2016-10-18 17:13:09 -0700592 case VISUALIZER_PARAM_CAPTURE_SIZE: {
593 const uint32_t captureSize = *((uint32_t *)p->data + 1);
594 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
595 android_errorWriteLog(0x534e4554, "31781965");
596 *(int32_t *)pReplyData = -EINVAL;
597 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
598 } else {
599 pContext->mCaptureSize = captureSize;
600 ALOGV("set mCaptureSize = %u", captureSize);
601 }
602 } break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700603 case VISUALIZER_PARAM_SCALING_MODE:
604 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700605 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700606 break;
Andy Hung9a2732b2016-10-18 17:13:09 -0700607 case VISUALIZER_PARAM_LATENCY: {
608 uint32_t latency = *((uint32_t *)p->data + 1);
609 if (latency > MAX_LATENCY_MS) {
610 latency = MAX_LATENCY_MS; // clamp latency b/31781965
611 }
612 pContext->mLatency = latency;
613 ALOGV("set mLatency = %u", latency);
614 } break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700615 case VISUALIZER_PARAM_MEASUREMENT_MODE:
616 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700617 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700618 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700619 default:
620 *(int32_t *)pReplyData = -EINVAL;
621 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700622 } break;
623 case EFFECT_CMD_SET_DEVICE:
624 case EFFECT_CMD_SET_VOLUME:
625 case EFFECT_CMD_SET_AUDIO_MODE:
626 break;
627
628
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100629 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700630 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent0f714a42015-06-19 15:33:57 -0700631 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700632 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100633 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700634 return -EINVAL;
635 }
636 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700637 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700638
639 // if audio framework has stopped playing audio although the effect is still
640 // active we must clear the capture buffer to return silence
641 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100642 (pContext->mBufferUpdateTime.tv_sec != 0) &&
643 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700644 ALOGV("capture going to idle");
645 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100646 memset(pReplyData, 0x80, captureSize);
647 } else {
648 int32_t latencyMs = pContext->mLatency;
649 latencyMs -= deltaMs;
650 if (latencyMs < 0) {
651 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700652 }
Andy Hung9a2732b2016-10-18 17:13:09 -0700653 uint32_t deltaSmpl = captureSize
654 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100655
Andy Hung9a2732b2016-10-18 17:13:09 -0700656 // large sample rate, latency, or capture size, could cause overflow.
657 // do not offset more than the size of buffer.
658 if (deltaSmpl > CAPTURE_BUF_SIZE) {
659 android_errorWriteLog(0x534e4554, "31781965");
660 deltaSmpl = CAPTURE_BUF_SIZE;
661 }
662
Ivan Lozano612d8fc2018-01-03 09:40:44 -0800663 int32_t capturePoint;
664 //capturePoint = (int32_t)pContext->mCaptureIdx - deltaSmpl;
665 __builtin_sub_overflow((int32_t)pContext->mCaptureIdx, deltaSmpl, &capturePoint);
Andy Hung9a2732b2016-10-18 17:13:09 -0700666 // a negative capturePoint means we wrap the buffer.
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100667 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700668 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100669 if (size > captureSize) {
670 size = captureSize;
671 }
672 memcpy(pReplyData,
673 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
674 size);
675 pReplyData = (char *)pReplyData + size;
676 captureSize -= size;
677 capturePoint = 0;
678 }
679 memcpy(pReplyData,
680 pContext->mCaptureBuf + capturePoint,
681 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700682 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100683
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700684 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700685 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100686 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700687 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800688
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100689 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700690
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700691 case VISUALIZER_CMD_MEASURE: {
rago099ab282016-08-22 17:20:26 -0700692 if (pReplyData == NULL || replySize == NULL ||
693 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
ragob66492c2016-10-07 18:16:09 -0700694 if (replySize == NULL) {
695 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
696 } else {
697 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
698 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
699 *replySize,
700 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
701 }
rago099ab282016-08-22 17:20:26 -0700702 android_errorWriteLog(0x534e4554, "30229821");
703 return -EINVAL;
704 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700705 uint16_t peakU16 = 0;
706 float sumRmsSquared = 0.0f;
707 uint8_t nbValidMeasurements = 0;
708 // reset measurements if last measurement was too long ago (which implies stored
709 // measurements aren't relevant anymore and shouldn't bias the new one)
710 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
711 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700712 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700713 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700714 pContext->mPastMeasurements[i].mIsValid = false;
715 pContext->mPastMeasurements[i].mPeakU16 = 0;
716 pContext->mPastMeasurements[i].mRmsSquared = 0;
717 }
718 pContext->mMeasurementBufferIdx = 0;
719 } else {
720 // only use actual measurements, otherwise the first RMS measure happening before
721 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
722 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700723 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700724 if (pContext->mPastMeasurements[i].mIsValid) {
725 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
726 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
727 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700728 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700729 nbValidMeasurements++;
730 }
731 }
732 }
733 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
734 int32_t* pIntReplyData = (int32_t*)pReplyData;
735 // convert from I16 sample values to mB and write results
736 if (rms < 0.000016f) {
737 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
738 } else {
739 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
740 }
741 if (peakU16 == 0) {
742 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
743 } else {
744 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
745 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700746 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700747 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
748 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
749 }
750 break;
751
Eric Laurentda7581b2010-07-02 08:12:41 -0700752 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700753 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700754 return -EINVAL;
755 }
756
757 return 0;
758}
759
Eric Laurente1315cf2011-05-17 19:16:02 -0700760/* Effect Control Interface Implementation: get_descriptor */
761int Visualizer_getDescriptor(effect_handle_t self,
762 effect_descriptor_t *pDescriptor)
763{
764 VisualizerContext * pContext = (VisualizerContext *) self;
765
766 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100767 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700768 return -EINVAL;
769 }
770
Glenn Kastena189a682012-02-20 12:16:30 -0800771 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700772
773 return 0;
774} /* end Visualizer_getDescriptor */
775
776// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700777const struct effect_interface_s gVisualizerInterface = {
778 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700779 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700780 Visualizer_getDescriptor,
781 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700782};
783
Marco Nelissen7f16b192012-10-25 16:05:57 -0700784// This is the only symbol that needs to be exported
785__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700786audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700787 .tag = AUDIO_EFFECT_LIBRARY_TAG,
788 .version = EFFECT_LIBRARY_API_VERSION,
789 .name = "Visualizer Library",
790 .implementor = "The Android Open Source Project",
791 .create_effect = VisualizerLib_Create,
792 .release_effect = VisualizerLib_Release,
793 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700794};
795
796}; // extern "C"