blob: e2ccfb798e726f3ffc5bd8964f6ddeb2e03e7502 [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
35#define BUILD_FLOAT
36
37#ifdef BUILD_FLOAT
38
39static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_FLOAT;
40
41#else
42
43static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_16_BIT;
44
45#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -070046
Eric Laurente1315cf2011-05-17 19:16:02 -070047extern "C" {
48
49// effect_handle_t interface implementation for visualizer effect
50extern const struct effect_interface_s gVisualizerInterface;
Eric Laurentda7581b2010-07-02 08:12:41 -070051
52// Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
53const effect_descriptor_t gVisualizerDescriptor = {
54 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
55 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
Eric Laurente1315cf2011-05-17 19:16:02 -070056 EFFECT_CONTROL_API_VERSION,
Eric Laurentda7581b2010-07-02 08:12:41 -070057 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
58 0, // TODO
59 1,
60 "Visualizer",
Eric Laurente1315cf2011-05-17 19:16:02 -070061 "The Android Open Source Project",
Eric Laurentda7581b2010-07-02 08:12:41 -070062};
63
64enum visualizer_state_e {
65 VISUALIZER_STATE_UNINITIALIZED,
66 VISUALIZER_STATE_INITIALIZED,
67 VISUALIZER_STATE_ACTIVE,
68};
69
Eric Laurent183dc772012-03-23 15:35:48 -070070// maximum time since last capture buffer update before resetting capture buffer. This means
Eric Laurent3df40a02011-11-10 10:02:18 -080071// that the framework has stopped playing audio and we must start returning silence
Eric Laurent183dc772012-03-23 15:35:48 -070072#define MAX_STALL_TIME_MS 1000
Eric Laurent3df40a02011-11-10 10:02:18 -080073
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070074#define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
75
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070076#define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
77
Andy Hung9a2732b2016-10-18 17:13:09 -070078#define MAX_LATENCY_MS 3000 // 3 seconds of latency for audio pipeline
79
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070080// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070081#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070082
83
84struct BufferStats {
85 bool mIsValid;
86 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
87 float mRmsSquared; // the average square of the samples in a buffer
88};
89
Eric Laurentda7581b2010-07-02 08:12:41 -070090struct VisualizerContext {
91 const struct effect_interface_s *mItfe;
92 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070093 uint32_t mCaptureIdx;
94 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070095 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080096 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070097 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070098 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070099 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700100 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700101 // for measurements
102 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
103 uint32_t mMeasurementMode;
104 uint8_t mMeasurementWindowSizeInBuffers;
105 uint8_t mMeasurementBufferIdx;
106 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -0700107};
108
Eric Laurentda7581b2010-07-02 08:12:41 -0700109//
110//--- Local functions
111//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700112uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
113 uint32_t deltaMs = 0;
114 if (pContext->mBufferUpdateTime.tv_sec != 0) {
115 struct timespec ts;
116 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
117 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
118 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
119 if (nsec < 0) {
120 --secs;
121 nsec += 1000000000;
122 }
123 deltaMs = secs * 1000 + nsec / 1000000;
124 }
125 }
126 return deltaMs;
127}
128
Eric Laurentda7581b2010-07-02 08:12:41 -0700129
130void Visualizer_reset(VisualizerContext *pContext)
131{
132 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700133 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700134 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700135 pContext->mLatency = 0;
136 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700137}
138
139//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800140// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700141//----------------------------------------------------------------------------
142// Purpose: Set input and output audio configuration.
143//
144// Inputs:
145// pContext: effect engine context
146// pConfig: pointer to effect_config_t structure holding input and output
147// configuration parameters
148//
149// Outputs:
150//
151//----------------------------------------------------------------------------
152
Eric Laurent3d5188b2011-12-16 15:30:36 -0800153int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700154{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800155 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700156
157 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
158 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
159 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700160 if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700161 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
162 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Andy Hungfda70d32018-08-08 11:51:52 -0700163 if (pConfig->inputCfg.format != kProcessFormat) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700164
Glenn Kastena189a682012-02-20 12:16:30 -0800165 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700166
167 Visualizer_reset(pContext);
168
169 return 0;
170}
171
172
173//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800174// Visualizer_getConfig()
175//----------------------------------------------------------------------------
176// Purpose: Get input and output audio configuration.
177//
178// Inputs:
179// pContext: effect engine context
180// pConfig: pointer to effect_config_t structure holding input and output
181// configuration parameters
182//
183// Outputs:
184//
185//----------------------------------------------------------------------------
186
187void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
188{
Glenn Kastena189a682012-02-20 12:16:30 -0800189 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800190}
191
192
193//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700194// Visualizer_init()
195//----------------------------------------------------------------------------
196// Purpose: Initialize engine with default configuration.
197//
198// Inputs:
199// pContext: effect engine context
200//
201// Outputs:
202//
203//----------------------------------------------------------------------------
204
205int Visualizer_init(VisualizerContext *pContext)
206{
207 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700208 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
Andy Hungfda70d32018-08-08 11:51:52 -0700209 pContext->mConfig.inputCfg.format = kProcessFormat;
Eric Laurentda7581b2010-07-02 08:12:41 -0700210 pContext->mConfig.inputCfg.samplingRate = 44100;
211 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
212 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
213 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
214 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
215 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700216 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
Andy Hungfda70d32018-08-08 11:51:52 -0700217 pContext->mConfig.outputCfg.format = kProcessFormat;
Eric Laurentda7581b2010-07-02 08:12:41 -0700218 pContext->mConfig.outputCfg.samplingRate = 44100;
219 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
220 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
221 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
222 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
223
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700224 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700225 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700226 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700227
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700228 // measurement initialization
Andy Hunge5412692014-05-16 11:25:07 -0700229 pContext->mChannelCount =
230 audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700231 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
232 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
233 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700234 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700235 pContext->mPastMeasurements[i].mIsValid = false;
236 pContext->mPastMeasurements[i].mPeakU16 = 0;
237 pContext->mPastMeasurements[i].mRmsSquared = 0;
238 }
239
Eric Laurent3d5188b2011-12-16 15:30:36 -0800240 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700241
242 return 0;
243}
244
245//
246//--- Effect Library Interface Implementation
247//
248
Glenn Kasten5e92a782012-01-30 07:40:52 -0800249int VisualizerLib_Create(const effect_uuid_t *uuid,
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700250 int32_t /*sessionId*/,
251 int32_t /*ioId*/,
Eric Laurente1315cf2011-05-17 19:16:02 -0700252 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700253 int ret;
Eric Laurentda7581b2010-07-02 08:12:41 -0700254
Eric Laurente1315cf2011-05-17 19:16:02 -0700255 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700256 return -EINVAL;
257 }
258
259 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
260 return -EINVAL;
261 }
262
263 VisualizerContext *pContext = new VisualizerContext;
264
265 pContext->mItfe = &gVisualizerInterface;
266 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
267
268 ret = Visualizer_init(pContext);
269 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000270 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700271 delete pContext;
272 return ret;
273 }
274
Eric Laurente1315cf2011-05-17 19:16:02 -0700275 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700276
277 pContext->mState = VISUALIZER_STATE_INITIALIZED;
278
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700280
281 return 0;
282
283}
284
Eric Laurente1315cf2011-05-17 19:16:02 -0700285int VisualizerLib_Release(effect_handle_t handle) {
286 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700287
Steve Block3856b092011-10-20 11:56:00 +0100288 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700289 if (pContext == NULL) {
290 return -EINVAL;
291 }
292 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
293 delete pContext;
294
295 return 0;
296}
297
Glenn Kasten5e92a782012-01-30 07:40:52 -0800298int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700299 effect_descriptor_t *pDescriptor) {
300
301 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100302 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700303 return -EINVAL;
304 }
305
306 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800307 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700308 return 0;
309 }
310
311 return -EINVAL;
312} /* end VisualizerLib_GetDescriptor */
313
Eric Laurentda7581b2010-07-02 08:12:41 -0700314//
315//--- Effect Control Interface Implementation
316//
317
Eric Laurente1315cf2011-05-17 19:16:02 -0700318int Visualizer_process(
Andy Hungfda70d32018-08-08 11:51:52 -0700319 effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700320{
Eric Laurente1315cf2011-05-17 19:16:02 -0700321 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700322
323 if (pContext == NULL) {
324 return -EINVAL;
325 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700326
327 if (inBuffer == NULL || inBuffer->raw == NULL ||
328 outBuffer == NULL || outBuffer->raw == NULL ||
329 inBuffer->frameCount != outBuffer->frameCount ||
330 inBuffer->frameCount == 0) {
331 return -EINVAL;
332 }
333
Andy Hungfda70d32018-08-08 11:51:52 -0700334 const size_t sampleLen = inBuffer->frameCount * pContext->mChannelCount;
335
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700336 // perform measurements if needed
337 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
338 // find the peak and RMS squared for the new buffer
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700339 float rmsSqAcc = 0;
Andy Hungfda70d32018-08-08 11:51:52 -0700340
341#ifdef BUILD_FLOAT
342 float maxSample = 0.f;
343 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
344 maxSample = fmax(maxSample, fabs(inBuffer->f32[inIdx]));
345 rmsSqAcc += inBuffer->f32[inIdx] * inBuffer->f32[inIdx];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700346 }
Andy Hungfda70d32018-08-08 11:51:52 -0700347 maxSample *= 1 << 15; // scale to int16_t, with exactly 1 << 15 representing positive num.
348 rmsSqAcc *= 1 << 30; // scale to int16_t * 2
349#else
350 int maxSample = 0;
351 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
352 maxSample = std::max(maxSample, std::abs(int32_t(inBuffer->s16[inIdx])));
353 rmsSqAcc += inBuffer->s16[inIdx] * inBuffer->s16[inIdx];
354 }
355#endif
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700356 // store the measurement
357 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
358 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
359 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
360 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
361 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
362 pContext->mMeasurementBufferIdx = 0;
363 }
364 }
365
Andy Hungfda70d32018-08-08 11:51:52 -0700366#ifdef BUILD_FLOAT
367 float fscale; // multiplicative scale
368#else
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700369 int32_t shift;
Andy Hungfda70d32018-08-08 11:51:52 -0700370#endif // BUILD_FLOAT
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700371
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700372 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
373 // derive capture scaling factor from peak value in current buffer
374 // this gives more interesting captures for display.
Andy Hungfda70d32018-08-08 11:51:52 -0700375
376#ifdef BUILD_FLOAT
377 float maxSample = 0.f;
378 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
379 maxSample = fmax(maxSample, fabs(inBuffer->f32[inIdx]));
380 }
381 if (maxSample > 0.f) {
382 constexpr float halfish = 127.f / 256.f;
383 fscale = halfish / maxSample;
384 int exp; // unused
385 const float significand = frexp(fscale, &exp);
386 if (significand == 0.5f) {
387 fscale *= 255.f / 256.f; // avoid returning unaltered PCM signal
388 }
389 } else {
390 // scale doesn't matter, the values are all 0.
391 fscale = 1.f;
392 }
393#else
394 int32_t orAccum = 0;
395 for (size_t i = 0; i < sampleLen; ++i) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700396 int32_t smp = inBuffer->s16[i];
397 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
Andy Hungfda70d32018-08-08 11:51:52 -0700398 orAccum |= smp;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700399 }
Andy Hungfda70d32018-08-08 11:51:52 -0700400
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700401 // A maximum amplitude signal will have 17 leading zeros, which we want to
402 // translate to a shift of 8 (for converting 16 bit to 8 bit)
Andy Hungfda70d32018-08-08 11:51:52 -0700403 shift = 25 - __builtin_clz(orAccum);
404
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700405 // Never scale by less than 8 to avoid returning unaltered PCM signal.
406 if (shift < 3) {
407 shift = 3;
408 }
409 // add one to combine the division by 2 needed after summing left and right channels below
410 shift++;
Andy Hungfda70d32018-08-08 11:51:52 -0700411#endif // BUILD_FLOAT
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700412 } else {
413 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
Andy Hungfda70d32018-08-08 11:51:52 -0700414#ifdef BUILD_FLOAT
415 fscale = 0.5f; // default divide by 2 to account for sum of L + R.
416#else
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700417 shift = 9;
Andy Hungfda70d32018-08-08 11:51:52 -0700418#endif // BUILD_FLOAT
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700419 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700420
Eric Laurentda7581b2010-07-02 08:12:41 -0700421 uint32_t captIdx;
422 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700423 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700424 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700425 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700426 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700427 if (captIdx >= CAPTURE_BUF_SIZE) {
428 // wrap around
429 captIdx = 0;
430 }
Andy Hungfda70d32018-08-08 11:51:52 -0700431#ifdef BUILD_FLOAT
432 const float smp = (inBuffer->f32[2 * inIdx] + inBuffer->f32[2 * inIdx + 1]) * fscale;
433 buf[captIdx] = clamp8_from_float(smp);
434#else
435 const int32_t smp = (inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1]) >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700436 buf[captIdx] = ((uint8_t)smp)^0x80;
Andy Hungfda70d32018-08-08 11:51:52 -0700437#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -0700438 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700439
440 // XXX the following two should really be atomic, though it probably doesn't
441 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700442 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700443 // update last buffer update time stamp
444 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
445 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700446 }
447
448 if (inBuffer->raw != outBuffer->raw) {
Andy Hungfda70d32018-08-08 11:51:52 -0700449#ifdef BUILD_FLOAT
450 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
451 for (size_t i = 0; i < sampleLen; ++i) {
452 outBuffer->f32[i] += inBuffer->f32[i];
453 }
454 } else {
455 memcpy(outBuffer->raw, inBuffer->raw, sampleLen * sizeof(float));
456 }
457#else
Eric Laurentda7581b2010-07-02 08:12:41 -0700458 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
459 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
460 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
461 }
462 } else {
463 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
464 }
Andy Hungfda70d32018-08-08 11:51:52 -0700465#endif // BUILD_FLOAT
Eric Laurentda7581b2010-07-02 08:12:41 -0700466 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700467 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
468 return -ENODATA;
469 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700470 return 0;
471} // end Visualizer_process
472
Eric Laurente1315cf2011-05-17 19:16:02 -0700473int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700474 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700475
Eric Laurente1315cf2011-05-17 19:16:02 -0700476 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700477
478 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
479 return -EINVAL;
480 }
481
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700482// ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700483
484 switch (cmdCode) {
485 case EFFECT_CMD_INIT:
Eric Laurent0f714a42015-06-19 15:33:57 -0700486 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700487 return -EINVAL;
488 }
489 *(int *) pReplyData = Visualizer_init(pContext);
490 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800491 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700492 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent0f714a42015-06-19 15:33:57 -0700493 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700494 return -EINVAL;
495 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800496 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700497 (effect_config_t *) pCmdData);
498 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800499 case EFFECT_CMD_GET_CONFIG:
Eric Laurent0f714a42015-06-19 15:33:57 -0700500 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800501 *replySize != sizeof(effect_config_t)) {
502 return -EINVAL;
503 }
504 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
505 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700506 case EFFECT_CMD_RESET:
507 Visualizer_reset(pContext);
508 break;
509 case EFFECT_CMD_ENABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700510 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700511 return -EINVAL;
512 }
513 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
514 return -ENOSYS;
515 }
516 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100517 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700518 *(int *)pReplyData = 0;
519 break;
520 case EFFECT_CMD_DISABLE:
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_ACTIVE) {
525 return -ENOSYS;
526 }
527 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700529 *(int *)pReplyData = 0;
530 break;
531 case EFFECT_CMD_GET_PARAM: {
532 if (pCmdData == NULL ||
533 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700534 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700535 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
536 return -EINVAL;
537 }
538 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
539 effect_param_t *p = (effect_param_t *)pReplyData;
540 p->status = 0;
541 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700542 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700543 p->status = -EINVAL;
544 break;
545 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700546 switch (*(uint32_t *)p->data) {
547 case VISUALIZER_PARAM_CAPTURE_SIZE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700548 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700549 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
550 p->vsize = sizeof(uint32_t);
551 *replySize += sizeof(uint32_t);
552 break;
553 case VISUALIZER_PARAM_SCALING_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700554 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700555 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
556 p->vsize = sizeof(uint32_t);
557 *replySize += sizeof(uint32_t);
558 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700559 case VISUALIZER_PARAM_MEASUREMENT_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700560 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700561 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
562 p->vsize = sizeof(uint32_t);
563 *replySize += sizeof(uint32_t);
564 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700565 default:
566 p->status = -EINVAL;
567 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700568 } break;
569 case EFFECT_CMD_SET_PARAM: {
570 if (pCmdData == NULL ||
571 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700572 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700573 return -EINVAL;
574 }
575 *(int32_t *)pReplyData = 0;
576 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700577 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700578 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700579 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700580 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700581 switch (*(uint32_t *)p->data) {
Andy Hung9a2732b2016-10-18 17:13:09 -0700582 case VISUALIZER_PARAM_CAPTURE_SIZE: {
583 const uint32_t captureSize = *((uint32_t *)p->data + 1);
584 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
585 android_errorWriteLog(0x534e4554, "31781965");
586 *(int32_t *)pReplyData = -EINVAL;
587 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
588 } else {
589 pContext->mCaptureSize = captureSize;
590 ALOGV("set mCaptureSize = %u", captureSize);
591 }
592 } break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700593 case VISUALIZER_PARAM_SCALING_MODE:
594 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700595 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700596 break;
Andy Hung9a2732b2016-10-18 17:13:09 -0700597 case VISUALIZER_PARAM_LATENCY: {
598 uint32_t latency = *((uint32_t *)p->data + 1);
599 if (latency > MAX_LATENCY_MS) {
600 latency = MAX_LATENCY_MS; // clamp latency b/31781965
601 }
602 pContext->mLatency = latency;
603 ALOGV("set mLatency = %u", latency);
604 } break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700605 case VISUALIZER_PARAM_MEASUREMENT_MODE:
606 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700607 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700608 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700609 default:
610 *(int32_t *)pReplyData = -EINVAL;
611 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700612 } break;
613 case EFFECT_CMD_SET_DEVICE:
614 case EFFECT_CMD_SET_VOLUME:
615 case EFFECT_CMD_SET_AUDIO_MODE:
616 break;
617
618
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100619 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700620 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent0f714a42015-06-19 15:33:57 -0700621 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700622 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100623 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700624 return -EINVAL;
625 }
626 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700627 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700628
629 // if audio framework has stopped playing audio although the effect is still
630 // active we must clear the capture buffer to return silence
631 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100632 (pContext->mBufferUpdateTime.tv_sec != 0) &&
633 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700634 ALOGV("capture going to idle");
635 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100636 memset(pReplyData, 0x80, captureSize);
637 } else {
638 int32_t latencyMs = pContext->mLatency;
639 latencyMs -= deltaMs;
640 if (latencyMs < 0) {
641 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700642 }
Andy Hung9a2732b2016-10-18 17:13:09 -0700643 uint32_t deltaSmpl = captureSize
644 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100645
Andy Hung9a2732b2016-10-18 17:13:09 -0700646 // large sample rate, latency, or capture size, could cause overflow.
647 // do not offset more than the size of buffer.
648 if (deltaSmpl > CAPTURE_BUF_SIZE) {
649 android_errorWriteLog(0x534e4554, "31781965");
650 deltaSmpl = CAPTURE_BUF_SIZE;
651 }
652
Ivan Lozano612d8fc2018-01-03 09:40:44 -0800653 int32_t capturePoint;
654 //capturePoint = (int32_t)pContext->mCaptureIdx - deltaSmpl;
655 __builtin_sub_overflow((int32_t)pContext->mCaptureIdx, deltaSmpl, &capturePoint);
Andy Hung9a2732b2016-10-18 17:13:09 -0700656 // a negative capturePoint means we wrap the buffer.
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100657 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700658 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100659 if (size > captureSize) {
660 size = captureSize;
661 }
662 memcpy(pReplyData,
663 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
664 size);
665 pReplyData = (char *)pReplyData + size;
666 captureSize -= size;
667 capturePoint = 0;
668 }
669 memcpy(pReplyData,
670 pContext->mCaptureBuf + capturePoint,
671 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700672 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100673
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700674 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700675 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100676 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700677 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800678
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100679 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700680
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700681 case VISUALIZER_CMD_MEASURE: {
rago099ab282016-08-22 17:20:26 -0700682 if (pReplyData == NULL || replySize == NULL ||
683 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
ragob66492c2016-10-07 18:16:09 -0700684 if (replySize == NULL) {
685 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
686 } else {
687 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
688 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
689 *replySize,
690 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
691 }
rago099ab282016-08-22 17:20:26 -0700692 android_errorWriteLog(0x534e4554, "30229821");
693 return -EINVAL;
694 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700695 uint16_t peakU16 = 0;
696 float sumRmsSquared = 0.0f;
697 uint8_t nbValidMeasurements = 0;
698 // reset measurements if last measurement was too long ago (which implies stored
699 // measurements aren't relevant anymore and shouldn't bias the new one)
700 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
701 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700702 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700703 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700704 pContext->mPastMeasurements[i].mIsValid = false;
705 pContext->mPastMeasurements[i].mPeakU16 = 0;
706 pContext->mPastMeasurements[i].mRmsSquared = 0;
707 }
708 pContext->mMeasurementBufferIdx = 0;
709 } else {
710 // only use actual measurements, otherwise the first RMS measure happening before
711 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
712 // low
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 if (pContext->mPastMeasurements[i].mIsValid) {
715 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
716 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
717 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700718 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700719 nbValidMeasurements++;
720 }
721 }
722 }
723 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
724 int32_t* pIntReplyData = (int32_t*)pReplyData;
725 // convert from I16 sample values to mB and write results
726 if (rms < 0.000016f) {
727 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
728 } else {
729 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
730 }
731 if (peakU16 == 0) {
732 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
733 } else {
734 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
735 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700736 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700737 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
738 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
739 }
740 break;
741
Eric Laurentda7581b2010-07-02 08:12:41 -0700742 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700743 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700744 return -EINVAL;
745 }
746
747 return 0;
748}
749
Eric Laurente1315cf2011-05-17 19:16:02 -0700750/* Effect Control Interface Implementation: get_descriptor */
751int Visualizer_getDescriptor(effect_handle_t self,
752 effect_descriptor_t *pDescriptor)
753{
754 VisualizerContext * pContext = (VisualizerContext *) self;
755
756 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700758 return -EINVAL;
759 }
760
Glenn Kastena189a682012-02-20 12:16:30 -0800761 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700762
763 return 0;
764} /* end Visualizer_getDescriptor */
765
766// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700767const struct effect_interface_s gVisualizerInterface = {
768 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700769 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700770 Visualizer_getDescriptor,
771 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700772};
773
Marco Nelissen7f16b192012-10-25 16:05:57 -0700774// This is the only symbol that needs to be exported
775__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700776audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700777 .tag = AUDIO_EFFECT_LIBRARY_TAG,
778 .version = EFFECT_LIBRARY_API_VERSION,
779 .name = "Visualizer Library",
780 .implementor = "The Android Open Source Project",
781 .create_effect = VisualizerLib_Create,
782 .release_effect = VisualizerLib_Release,
783 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700784};
785
786}; // extern "C"