blob: f838892e55d85fb2e816b5eb5ec9cd7cc83aca0e [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
160 if (channelCount < 1 || channelCount > FCC_8) return -EINVAL;
161#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
Glenn Kastena189a682012-02-20 12:16:30 -0800168 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700169
170 Visualizer_reset(pContext);
171
172 return 0;
173}
174
175
176//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800177// Visualizer_getConfig()
178//----------------------------------------------------------------------------
179// Purpose: Get input and output audio configuration.
180//
181// Inputs:
182// pContext: effect engine context
183// pConfig: pointer to effect_config_t structure holding input and output
184// configuration parameters
185//
186// Outputs:
187//
188//----------------------------------------------------------------------------
189
190void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
191{
Glenn Kastena189a682012-02-20 12:16:30 -0800192 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800193}
194
195
196//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700197// Visualizer_init()
198//----------------------------------------------------------------------------
199// Purpose: Initialize engine with default configuration.
200//
201// Inputs:
202// pContext: effect engine context
203//
204// Outputs:
205//
206//----------------------------------------------------------------------------
207
208int Visualizer_init(VisualizerContext *pContext)
209{
210 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700211 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
Andy Hungfda70d32018-08-08 11:51:52 -0700212 pContext->mConfig.inputCfg.format = kProcessFormat;
Eric Laurentda7581b2010-07-02 08:12:41 -0700213 pContext->mConfig.inputCfg.samplingRate = 44100;
214 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
215 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
216 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
217 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
218 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700219 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
Andy Hungfda70d32018-08-08 11:51:52 -0700220 pContext->mConfig.outputCfg.format = kProcessFormat;
Eric Laurentda7581b2010-07-02 08:12:41 -0700221 pContext->mConfig.outputCfg.samplingRate = 44100;
222 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
223 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
224 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
225 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
226
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700227 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700228 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700229 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700230
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700231 // measurement initialization
Andy Hunge5412692014-05-16 11:25:07 -0700232 pContext->mChannelCount =
233 audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700234 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
235 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
236 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700237 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700238 pContext->mPastMeasurements[i].mIsValid = false;
239 pContext->mPastMeasurements[i].mPeakU16 = 0;
240 pContext->mPastMeasurements[i].mRmsSquared = 0;
241 }
242
Eric Laurent3d5188b2011-12-16 15:30:36 -0800243 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700244
245 return 0;
246}
247
248//
249//--- Effect Library Interface Implementation
250//
251
Glenn Kasten5e92a782012-01-30 07:40:52 -0800252int VisualizerLib_Create(const effect_uuid_t *uuid,
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700253 int32_t /*sessionId*/,
254 int32_t /*ioId*/,
Eric Laurente1315cf2011-05-17 19:16:02 -0700255 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700256 int ret;
Eric Laurentda7581b2010-07-02 08:12:41 -0700257
Eric Laurente1315cf2011-05-17 19:16:02 -0700258 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700259 return -EINVAL;
260 }
261
262 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
263 return -EINVAL;
264 }
265
266 VisualizerContext *pContext = new VisualizerContext;
267
268 pContext->mItfe = &gVisualizerInterface;
269 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
270
271 ret = Visualizer_init(pContext);
272 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000273 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700274 delete pContext;
275 return ret;
276 }
277
Eric Laurente1315cf2011-05-17 19:16:02 -0700278 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700279
280 pContext->mState = VISUALIZER_STATE_INITIALIZED;
281
Steve Block3856b092011-10-20 11:56:00 +0100282 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700283
284 return 0;
285
286}
287
Eric Laurente1315cf2011-05-17 19:16:02 -0700288int VisualizerLib_Release(effect_handle_t handle) {
289 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700290
Steve Block3856b092011-10-20 11:56:00 +0100291 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700292 if (pContext == NULL) {
293 return -EINVAL;
294 }
295 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
296 delete pContext;
297
298 return 0;
299}
300
Glenn Kasten5e92a782012-01-30 07:40:52 -0800301int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700302 effect_descriptor_t *pDescriptor) {
303
304 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100305 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700306 return -EINVAL;
307 }
308
309 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800310 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700311 return 0;
312 }
313
314 return -EINVAL;
315} /* end VisualizerLib_GetDescriptor */
316
Eric Laurentda7581b2010-07-02 08:12:41 -0700317//
318//--- Effect Control Interface Implementation
319//
320
Eric Laurente1315cf2011-05-17 19:16:02 -0700321int Visualizer_process(
Andy Hungfda70d32018-08-08 11:51:52 -0700322 effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700323{
Eric Laurente1315cf2011-05-17 19:16:02 -0700324 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700325
326 if (pContext == NULL) {
327 return -EINVAL;
328 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700329
330 if (inBuffer == NULL || inBuffer->raw == NULL ||
331 outBuffer == NULL || outBuffer->raw == NULL ||
332 inBuffer->frameCount != outBuffer->frameCount ||
333 inBuffer->frameCount == 0) {
334 return -EINVAL;
335 }
336
Andy Hungfda70d32018-08-08 11:51:52 -0700337 const size_t sampleLen = inBuffer->frameCount * pContext->mChannelCount;
338
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700339 // perform measurements if needed
340 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
341 // find the peak and RMS squared for the new buffer
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700342 float rmsSqAcc = 0;
Andy Hungfda70d32018-08-08 11:51:52 -0700343
344#ifdef BUILD_FLOAT
345 float maxSample = 0.f;
346 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
347 maxSample = fmax(maxSample, fabs(inBuffer->f32[inIdx]));
348 rmsSqAcc += inBuffer->f32[inIdx] * inBuffer->f32[inIdx];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700349 }
Andy Hungfda70d32018-08-08 11:51:52 -0700350 maxSample *= 1 << 15; // scale to int16_t, with exactly 1 << 15 representing positive num.
351 rmsSqAcc *= 1 << 30; // scale to int16_t * 2
352#else
353 int maxSample = 0;
354 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
355 maxSample = std::max(maxSample, std::abs(int32_t(inBuffer->s16[inIdx])));
356 rmsSqAcc += inBuffer->s16[inIdx] * inBuffer->s16[inIdx];
357 }
358#endif
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700359 // store the measurement
360 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
361 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700362 rmsSqAcc / sampleLen;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700363 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
364 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
365 pContext->mMeasurementBufferIdx = 0;
366 }
367 }
368
Andy Hungfda70d32018-08-08 11:51:52 -0700369#ifdef BUILD_FLOAT
370 float fscale; // multiplicative scale
371#else
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700372 int32_t shift;
Andy Hungfda70d32018-08-08 11:51:52 -0700373#endif // BUILD_FLOAT
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700374
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700375 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
376 // derive capture scaling factor from peak value in current buffer
377 // this gives more interesting captures for display.
Andy Hungfda70d32018-08-08 11:51:52 -0700378
379#ifdef BUILD_FLOAT
380 float maxSample = 0.f;
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700381 for (size_t inIdx = 0; inIdx < sampleLen; ) {
382 // we reconstruct the actual summed value to ensure proper normalization
383 // for multichannel outputs (channels > 2 may often be 0).
384 float smp = 0.f;
385 for (int i = 0; i < pContext->mChannelCount; ++i) {
386 smp += inBuffer->f32[inIdx++];
387 }
388 maxSample = fmax(maxSample, fabs(smp));
Andy Hungfda70d32018-08-08 11:51:52 -0700389 }
390 if (maxSample > 0.f) {
Andy Hungb0ffc5a2019-06-17 11:53:41 -0700391 fscale = 0.99f / maxSample;
Andy Hungfda70d32018-08-08 11:51:52 -0700392 int exp; // unused
393 const float significand = frexp(fscale, &exp);
394 if (significand == 0.5f) {
395 fscale *= 255.f / 256.f; // avoid returning unaltered PCM signal
396 }
397 } else {
398 // scale doesn't matter, the values are all 0.
399 fscale = 1.f;
400 }
401#else
402 int32_t orAccum = 0;
403 for (size_t i = 0; i < sampleLen; ++i) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700404 int32_t smp = inBuffer->s16[i];
405 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
Andy Hungfda70d32018-08-08 11:51:52 -0700406 orAccum |= smp;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700407 }
Andy Hungfda70d32018-08-08 11:51:52 -0700408
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700409 // A maximum amplitude signal will have 17 leading zeros, which we want to
410 // translate to a shift of 8 (for converting 16 bit to 8 bit)
Andy Hungfda70d32018-08-08 11:51:52 -0700411 shift = 25 - __builtin_clz(orAccum);
412
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700413 // Never scale by less than 8 to avoid returning unaltered PCM signal.
414 if (shift < 3) {
415 shift = 3;
416 }
417 // add one to combine the division by 2 needed after summing left and right channels below
418 shift++;
Andy Hungfda70d32018-08-08 11:51:52 -0700419#endif // BUILD_FLOAT
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700420 } else {
421 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
Andy Hungfda70d32018-08-08 11:51:52 -0700422#ifdef BUILD_FLOAT
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700423 // Note: if channels are uncorrelated, 1/sqrt(N) could be used at the risk of clipping.
424 fscale = 1.f / pContext->mChannelCount; // account for summing all the channels together.
Andy Hungfda70d32018-08-08 11:51:52 -0700425#else
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700426 shift = 9;
Andy Hungfda70d32018-08-08 11:51:52 -0700427#endif // BUILD_FLOAT
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700428 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700429
Eric Laurentda7581b2010-07-02 08:12:41 -0700430 uint32_t captIdx;
431 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700432 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700433 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700434 inIdx < sampleLen;
435 captIdx++) {
436 if (captIdx >= CAPTURE_BUF_SIZE) captIdx = 0; // wrap
437
Andy Hungfda70d32018-08-08 11:51:52 -0700438#ifdef BUILD_FLOAT
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700439 float smp = 0.f;
440 for (uint32_t i = 0; i < pContext->mChannelCount; ++i) {
441 smp += inBuffer->f32[inIdx++];
442 }
443 buf[captIdx] = clamp8_from_float(smp * fscale);
Andy Hungfda70d32018-08-08 11:51:52 -0700444#else
Andy Hung1f7ef9b2018-11-02 13:58:41 -0700445 const int32_t smp = (inBuffer->s16[inIdx] + inBuffer->s16[inIdx + 1]) >> shift;
446 inIdx += FCC_2; // integer supports stereo only.
Eric Laurentda7581b2010-07-02 08:12:41 -0700447 buf[captIdx] = ((uint8_t)smp)^0x80;
Andy Hungfda70d32018-08-08 11:51:52 -0700448#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -0700449 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700450
451 // XXX the following two should really be atomic, though it probably doesn't
452 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700453 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700454 // update last buffer update time stamp
455 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
456 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700457 }
458
459 if (inBuffer->raw != outBuffer->raw) {
Andy Hungfda70d32018-08-08 11:51:52 -0700460#ifdef BUILD_FLOAT
461 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
462 for (size_t i = 0; i < sampleLen; ++i) {
463 outBuffer->f32[i] += inBuffer->f32[i];
464 }
465 } else {
466 memcpy(outBuffer->raw, inBuffer->raw, sampleLen * sizeof(float));
467 }
468#else
Eric Laurentda7581b2010-07-02 08:12:41 -0700469 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
470 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
471 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
472 }
473 } else {
474 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
475 }
Andy Hungfda70d32018-08-08 11:51:52 -0700476#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -0700477 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700478 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
479 return -ENODATA;
480 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700481 return 0;
482} // end Visualizer_process
483
Eric Laurente1315cf2011-05-17 19:16:02 -0700484int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700485 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700486
Eric Laurente1315cf2011-05-17 19:16:02 -0700487 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700488
489 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
490 return -EINVAL;
491 }
492
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700493// ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700494
495 switch (cmdCode) {
496 case EFFECT_CMD_INIT:
Eric Laurent0f714a42015-06-19 15:33:57 -0700497 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700498 return -EINVAL;
499 }
500 *(int *) pReplyData = Visualizer_init(pContext);
501 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800502 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700503 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent0f714a42015-06-19 15:33:57 -0700504 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700505 return -EINVAL;
506 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800507 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700508 (effect_config_t *) pCmdData);
509 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800510 case EFFECT_CMD_GET_CONFIG:
Eric Laurent0f714a42015-06-19 15:33:57 -0700511 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800512 *replySize != sizeof(effect_config_t)) {
513 return -EINVAL;
514 }
515 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
516 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700517 case EFFECT_CMD_RESET:
518 Visualizer_reset(pContext);
519 break;
520 case EFFECT_CMD_ENABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700521 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700522 return -EINVAL;
523 }
524 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
525 return -ENOSYS;
526 }
527 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700529 *(int *)pReplyData = 0;
530 break;
531 case EFFECT_CMD_DISABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700532 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700533 return -EINVAL;
534 }
535 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
536 return -ENOSYS;
537 }
538 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100539 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700540 *(int *)pReplyData = 0;
541 break;
542 case EFFECT_CMD_GET_PARAM: {
543 if (pCmdData == NULL ||
544 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700545 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700546 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
547 return -EINVAL;
548 }
549 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
550 effect_param_t *p = (effect_param_t *)pReplyData;
551 p->status = 0;
552 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700553 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700554 p->status = -EINVAL;
555 break;
556 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700557 switch (*(uint32_t *)p->data) {
558 case VISUALIZER_PARAM_CAPTURE_SIZE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700559 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700560 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
561 p->vsize = sizeof(uint32_t);
562 *replySize += sizeof(uint32_t);
563 break;
564 case VISUALIZER_PARAM_SCALING_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700565 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700566 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
567 p->vsize = sizeof(uint32_t);
568 *replySize += sizeof(uint32_t);
569 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700570 case VISUALIZER_PARAM_MEASUREMENT_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700571 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700572 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
573 p->vsize = sizeof(uint32_t);
574 *replySize += sizeof(uint32_t);
575 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700576 default:
577 p->status = -EINVAL;
578 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700579 } break;
580 case EFFECT_CMD_SET_PARAM: {
581 if (pCmdData == NULL ||
582 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700583 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700584 return -EINVAL;
585 }
586 *(int32_t *)pReplyData = 0;
587 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700588 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700589 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700590 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700591 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700592 switch (*(uint32_t *)p->data) {
Andy Hung9a2732b2016-10-18 17:13:09 -0700593 case VISUALIZER_PARAM_CAPTURE_SIZE: {
594 const uint32_t captureSize = *((uint32_t *)p->data + 1);
595 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
596 android_errorWriteLog(0x534e4554, "31781965");
597 *(int32_t *)pReplyData = -EINVAL;
598 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
599 } else {
600 pContext->mCaptureSize = captureSize;
601 ALOGV("set mCaptureSize = %u", captureSize);
602 }
603 } break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700604 case VISUALIZER_PARAM_SCALING_MODE:
605 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700606 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700607 break;
Andy Hung9a2732b2016-10-18 17:13:09 -0700608 case VISUALIZER_PARAM_LATENCY: {
609 uint32_t latency = *((uint32_t *)p->data + 1);
610 if (latency > MAX_LATENCY_MS) {
611 latency = MAX_LATENCY_MS; // clamp latency b/31781965
612 }
613 pContext->mLatency = latency;
614 ALOGV("set mLatency = %u", latency);
615 } break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700616 case VISUALIZER_PARAM_MEASUREMENT_MODE:
617 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700618 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700619 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700620 default:
621 *(int32_t *)pReplyData = -EINVAL;
622 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700623 } break;
624 case EFFECT_CMD_SET_DEVICE:
625 case EFFECT_CMD_SET_VOLUME:
626 case EFFECT_CMD_SET_AUDIO_MODE:
627 break;
628
629
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100630 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700631 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent0f714a42015-06-19 15:33:57 -0700632 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700633 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100634 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700635 return -EINVAL;
636 }
637 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700638 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700639
640 // if audio framework has stopped playing audio although the effect is still
641 // active we must clear the capture buffer to return silence
642 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100643 (pContext->mBufferUpdateTime.tv_sec != 0) &&
644 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700645 ALOGV("capture going to idle");
646 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100647 memset(pReplyData, 0x80, captureSize);
648 } else {
649 int32_t latencyMs = pContext->mLatency;
650 latencyMs -= deltaMs;
651 if (latencyMs < 0) {
652 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700653 }
Andy Hung9a2732b2016-10-18 17:13:09 -0700654 uint32_t deltaSmpl = captureSize
655 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100656
Andy Hung9a2732b2016-10-18 17:13:09 -0700657 // large sample rate, latency, or capture size, could cause overflow.
658 // do not offset more than the size of buffer.
659 if (deltaSmpl > CAPTURE_BUF_SIZE) {
660 android_errorWriteLog(0x534e4554, "31781965");
661 deltaSmpl = CAPTURE_BUF_SIZE;
662 }
663
Ivan Lozano612d8fc2018-01-03 09:40:44 -0800664 int32_t capturePoint;
665 //capturePoint = (int32_t)pContext->mCaptureIdx - deltaSmpl;
666 __builtin_sub_overflow((int32_t)pContext->mCaptureIdx, deltaSmpl, &capturePoint);
Andy Hung9a2732b2016-10-18 17:13:09 -0700667 // a negative capturePoint means we wrap the buffer.
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100668 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700669 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100670 if (size > captureSize) {
671 size = captureSize;
672 }
673 memcpy(pReplyData,
674 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
675 size);
676 pReplyData = (char *)pReplyData + size;
677 captureSize -= size;
678 capturePoint = 0;
679 }
680 memcpy(pReplyData,
681 pContext->mCaptureBuf + capturePoint,
682 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700683 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100684
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700685 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700686 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100687 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700688 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800689
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100690 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700691
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700692 case VISUALIZER_CMD_MEASURE: {
rago099ab282016-08-22 17:20:26 -0700693 if (pReplyData == NULL || replySize == NULL ||
694 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
ragob66492c2016-10-07 18:16:09 -0700695 if (replySize == NULL) {
696 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
697 } else {
698 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
699 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
700 *replySize,
701 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
702 }
rago099ab282016-08-22 17:20:26 -0700703 android_errorWriteLog(0x534e4554, "30229821");
704 return -EINVAL;
705 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700706 uint16_t peakU16 = 0;
707 float sumRmsSquared = 0.0f;
708 uint8_t nbValidMeasurements = 0;
709 // reset measurements if last measurement was too long ago (which implies stored
710 // measurements aren't relevant anymore and shouldn't bias the new one)
711 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
712 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700713 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700714 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700715 pContext->mPastMeasurements[i].mIsValid = false;
716 pContext->mPastMeasurements[i].mPeakU16 = 0;
717 pContext->mPastMeasurements[i].mRmsSquared = 0;
718 }
719 pContext->mMeasurementBufferIdx = 0;
720 } else {
721 // only use actual measurements, otherwise the first RMS measure happening before
722 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
723 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700724 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700725 if (pContext->mPastMeasurements[i].mIsValid) {
726 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
727 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
728 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700729 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700730 nbValidMeasurements++;
731 }
732 }
733 }
734 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
735 int32_t* pIntReplyData = (int32_t*)pReplyData;
736 // convert from I16 sample values to mB and write results
737 if (rms < 0.000016f) {
738 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
739 } else {
740 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
741 }
742 if (peakU16 == 0) {
743 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
744 } else {
745 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
746 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700747 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700748 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
749 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
750 }
751 break;
752
Eric Laurentda7581b2010-07-02 08:12:41 -0700753 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700754 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700755 return -EINVAL;
756 }
757
758 return 0;
759}
760
Eric Laurente1315cf2011-05-17 19:16:02 -0700761/* Effect Control Interface Implementation: get_descriptor */
762int Visualizer_getDescriptor(effect_handle_t self,
763 effect_descriptor_t *pDescriptor)
764{
765 VisualizerContext * pContext = (VisualizerContext *) self;
766
767 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700769 return -EINVAL;
770 }
771
Glenn Kastena189a682012-02-20 12:16:30 -0800772 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700773
774 return 0;
775} /* end Visualizer_getDescriptor */
776
777// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700778const struct effect_interface_s gVisualizerInterface = {
779 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700780 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700781 Visualizer_getDescriptor,
782 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700783};
784
Marco Nelissen7f16b192012-10-25 16:05:57 -0700785// This is the only symbol that needs to be exported
786__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700787audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700788 .tag = AUDIO_EFFECT_LIBRARY_TAG,
789 .version = EFFECT_LIBRARY_API_VERSION,
790 .name = "Visualizer Library",
791 .implementor = "The Android Open Source Project",
792 .create_effect = VisualizerLib_Create,
793 .release_effect = VisualizerLib_Release,
794 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700795};
796
797}; // extern "C"