blob: 91f9fc7845a9dd914cf5d7a296a75a69608414fc [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 Salyzyn7cb0e732014-04-18 13:48:25 -070019#include <log/log.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070020#include <assert.h>
Mark Salyzyn7cb0e732014-04-18 13:48:25 -070021#include <inttypes.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070022#include <stdlib.h>
23#include <string.h>
24#include <new>
Eric Laurent183dc772012-03-23 15:35:48 -070025#include <time.h>
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070026#include <math.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070027#include <audio_effects/effect_visualizer.h>
rago46dc7142016-08-22 17:20:26 -070028#include <cutils/log.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070029
Eric Laurentda7581b2010-07-02 08:12:41 -070030
Eric Laurente1315cf2011-05-17 19:16:02 -070031extern "C" {
32
33// effect_handle_t interface implementation for visualizer effect
34extern const struct effect_interface_s gVisualizerInterface;
Eric Laurentda7581b2010-07-02 08:12:41 -070035
36// Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
37const effect_descriptor_t gVisualizerDescriptor = {
38 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
39 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
Eric Laurente1315cf2011-05-17 19:16:02 -070040 EFFECT_CONTROL_API_VERSION,
Eric Laurentda7581b2010-07-02 08:12:41 -070041 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
42 0, // TODO
43 1,
44 "Visualizer",
Eric Laurente1315cf2011-05-17 19:16:02 -070045 "The Android Open Source Project",
Eric Laurentda7581b2010-07-02 08:12:41 -070046};
47
48enum visualizer_state_e {
49 VISUALIZER_STATE_UNINITIALIZED,
50 VISUALIZER_STATE_INITIALIZED,
51 VISUALIZER_STATE_ACTIVE,
52};
53
Eric Laurent183dc772012-03-23 15:35:48 -070054// maximum time since last capture buffer update before resetting capture buffer. This means
Eric Laurent3df40a02011-11-10 10:02:18 -080055// that the framework has stopped playing audio and we must start returning silence
Eric Laurent183dc772012-03-23 15:35:48 -070056#define MAX_STALL_TIME_MS 1000
Eric Laurent3df40a02011-11-10 10:02:18 -080057
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070058#define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
59
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070060#define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
61
62// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070063#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070064
65
66struct BufferStats {
67 bool mIsValid;
68 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
69 float mRmsSquared; // the average square of the samples in a buffer
70};
71
Eric Laurentda7581b2010-07-02 08:12:41 -070072struct VisualizerContext {
73 const struct effect_interface_s *mItfe;
74 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070075 uint32_t mCaptureIdx;
76 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070077 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080078 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070079 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070080 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070081 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070082 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070083 // for measurements
84 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
85 uint32_t mMeasurementMode;
86 uint8_t mMeasurementWindowSizeInBuffers;
87 uint8_t mMeasurementBufferIdx;
88 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -070089};
90
Eric Laurentda7581b2010-07-02 08:12:41 -070091//
92//--- Local functions
93//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070094uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
95 uint32_t deltaMs = 0;
96 if (pContext->mBufferUpdateTime.tv_sec != 0) {
97 struct timespec ts;
98 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
99 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
100 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
101 if (nsec < 0) {
102 --secs;
103 nsec += 1000000000;
104 }
105 deltaMs = secs * 1000 + nsec / 1000000;
106 }
107 }
108 return deltaMs;
109}
110
Eric Laurentda7581b2010-07-02 08:12:41 -0700111
112void Visualizer_reset(VisualizerContext *pContext)
113{
114 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700115 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700116 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700117 pContext->mLatency = 0;
118 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700119}
120
121//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800122// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700123//----------------------------------------------------------------------------
124// Purpose: Set input and output audio configuration.
125//
126// Inputs:
127// pContext: effect engine context
128// pConfig: pointer to effect_config_t structure holding input and output
129// configuration parameters
130//
131// Outputs:
132//
133//----------------------------------------------------------------------------
134
Eric Laurent3d5188b2011-12-16 15:30:36 -0800135int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700136{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800137 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700138
139 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
140 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
141 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700142 if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700143 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
144 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700145 if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700146
Glenn Kastena189a682012-02-20 12:16:30 -0800147 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700148
149 Visualizer_reset(pContext);
150
151 return 0;
152}
153
154
155//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800156// Visualizer_getConfig()
157//----------------------------------------------------------------------------
158// Purpose: Get input and output audio configuration.
159//
160// Inputs:
161// pContext: effect engine context
162// pConfig: pointer to effect_config_t structure holding input and output
163// configuration parameters
164//
165// Outputs:
166//
167//----------------------------------------------------------------------------
168
169void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
170{
Glenn Kastena189a682012-02-20 12:16:30 -0800171 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800172}
173
174
175//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700176// Visualizer_init()
177//----------------------------------------------------------------------------
178// Purpose: Initialize engine with default configuration.
179//
180// Inputs:
181// pContext: effect engine context
182//
183// Outputs:
184//
185//----------------------------------------------------------------------------
186
187int Visualizer_init(VisualizerContext *pContext)
188{
189 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700190 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
191 pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700192 pContext->mConfig.inputCfg.samplingRate = 44100;
193 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
194 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
195 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
196 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
197 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700198 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
199 pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700200 pContext->mConfig.outputCfg.samplingRate = 44100;
201 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
202 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
203 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
204 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
205
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700206 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700207 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700208 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700209
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700210 // measurement initialization
Andy Hunge5412692014-05-16 11:25:07 -0700211 pContext->mChannelCount =
212 audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700213 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
214 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
215 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700216 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700217 pContext->mPastMeasurements[i].mIsValid = false;
218 pContext->mPastMeasurements[i].mPeakU16 = 0;
219 pContext->mPastMeasurements[i].mRmsSquared = 0;
220 }
221
Eric Laurent3d5188b2011-12-16 15:30:36 -0800222 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700223
224 return 0;
225}
226
227//
228//--- Effect Library Interface Implementation
229//
230
Glenn Kasten5e92a782012-01-30 07:40:52 -0800231int VisualizerLib_Create(const effect_uuid_t *uuid,
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700232 int32_t /*sessionId*/,
233 int32_t /*ioId*/,
Eric Laurente1315cf2011-05-17 19:16:02 -0700234 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700235 int ret;
236 int i;
237
Eric Laurente1315cf2011-05-17 19:16:02 -0700238 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700239 return -EINVAL;
240 }
241
242 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
243 return -EINVAL;
244 }
245
246 VisualizerContext *pContext = new VisualizerContext;
247
248 pContext->mItfe = &gVisualizerInterface;
249 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
250
251 ret = Visualizer_init(pContext);
252 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000253 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700254 delete pContext;
255 return ret;
256 }
257
Eric Laurente1315cf2011-05-17 19:16:02 -0700258 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700259
260 pContext->mState = VISUALIZER_STATE_INITIALIZED;
261
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700263
264 return 0;
265
266}
267
Eric Laurente1315cf2011-05-17 19:16:02 -0700268int VisualizerLib_Release(effect_handle_t handle) {
269 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700270
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700272 if (pContext == NULL) {
273 return -EINVAL;
274 }
275 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
276 delete pContext;
277
278 return 0;
279}
280
Glenn Kasten5e92a782012-01-30 07:40:52 -0800281int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700282 effect_descriptor_t *pDescriptor) {
283
284 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100285 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700286 return -EINVAL;
287 }
288
289 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800290 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700291 return 0;
292 }
293
294 return -EINVAL;
295} /* end VisualizerLib_GetDescriptor */
296
Eric Laurentda7581b2010-07-02 08:12:41 -0700297//
298//--- Effect Control Interface Implementation
299//
300
301static inline int16_t clamp16(int32_t sample)
302{
303 if ((sample>>15) ^ (sample>>31))
304 sample = 0x7FFF ^ (sample>>31);
305 return sample;
306}
307
Eric Laurente1315cf2011-05-17 19:16:02 -0700308int Visualizer_process(
309 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700310{
Eric Laurente1315cf2011-05-17 19:16:02 -0700311 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700312
313 if (pContext == NULL) {
314 return -EINVAL;
315 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700316
317 if (inBuffer == NULL || inBuffer->raw == NULL ||
318 outBuffer == NULL || outBuffer->raw == NULL ||
319 inBuffer->frameCount != outBuffer->frameCount ||
320 inBuffer->frameCount == 0) {
321 return -EINVAL;
322 }
323
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700324 // perform measurements if needed
325 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
326 // find the peak and RMS squared for the new buffer
327 uint32_t inIdx;
328 int16_t maxSample = 0;
329 float rmsSqAcc = 0;
330 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
331 if (inBuffer->s16[inIdx] > maxSample) {
332 maxSample = inBuffer->s16[inIdx];
333 } else if (-inBuffer->s16[inIdx] > maxSample) {
334 maxSample = -inBuffer->s16[inIdx];
335 }
336 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
337 }
338 // store the measurement
339 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
340 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
341 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
342 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
343 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
344 pContext->mMeasurementBufferIdx = 0;
345 }
346 }
347
Eric Laurentda7581b2010-07-02 08:12:41 -0700348 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700349 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700350
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700351 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
352 // derive capture scaling factor from peak value in current buffer
353 // this gives more interesting captures for display.
354 shift = 32;
355 int len = inBuffer->frameCount * 2;
356 for (int i = 0; i < len; i++) {
357 int32_t smp = inBuffer->s16[i];
358 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
359 int32_t clz = __builtin_clz(smp);
360 if (shift > clz) shift = clz;
361 }
362 // A maximum amplitude signal will have 17 leading zeros, which we want to
363 // translate to a shift of 8 (for converting 16 bit to 8 bit)
364 shift = 25 - shift;
365 // Never scale by less than 8 to avoid returning unaltered PCM signal.
366 if (shift < 3) {
367 shift = 3;
368 }
369 // add one to combine the division by 2 needed after summing left and right channels below
370 shift++;
371 } else {
372 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
373 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700374 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700375
Eric Laurentda7581b2010-07-02 08:12:41 -0700376 uint32_t captIdx;
377 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700378 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700379 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700380 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700381 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700382 if (captIdx >= CAPTURE_BUF_SIZE) {
383 // wrap around
384 captIdx = 0;
385 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700386 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700387 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700388 buf[captIdx] = ((uint8_t)smp)^0x80;
389 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700390
391 // XXX the following two should really be atomic, though it probably doesn't
392 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700393 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700394 // update last buffer update time stamp
395 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
396 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700397 }
398
399 if (inBuffer->raw != outBuffer->raw) {
400 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
401 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
402 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
403 }
404 } else {
405 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
406 }
407 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700408 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
409 return -ENODATA;
410 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700411 return 0;
412} // end Visualizer_process
413
Eric Laurente1315cf2011-05-17 19:16:02 -0700414int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700415 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700416
Eric Laurente1315cf2011-05-17 19:16:02 -0700417 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700418 int retsize;
419
420 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
421 return -EINVAL;
422 }
423
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700424// ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700425
426 switch (cmdCode) {
427 case EFFECT_CMD_INIT:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700428 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700429 return -EINVAL;
430 }
431 *(int *) pReplyData = Visualizer_init(pContext);
432 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800433 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700434 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent6368e6d2015-06-19 15:33:57 -0700435 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700436 return -EINVAL;
437 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800438 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700439 (effect_config_t *) pCmdData);
440 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800441 case EFFECT_CMD_GET_CONFIG:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700442 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800443 *replySize != sizeof(effect_config_t)) {
444 return -EINVAL;
445 }
446 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
447 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700448 case EFFECT_CMD_RESET:
449 Visualizer_reset(pContext);
450 break;
451 case EFFECT_CMD_ENABLE:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700452 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700453 return -EINVAL;
454 }
455 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
456 return -ENOSYS;
457 }
458 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100459 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700460 *(int *)pReplyData = 0;
461 break;
462 case EFFECT_CMD_DISABLE:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700463 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700464 return -EINVAL;
465 }
466 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
467 return -ENOSYS;
468 }
469 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100470 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700471 *(int *)pReplyData = 0;
472 break;
473 case EFFECT_CMD_GET_PARAM: {
474 if (pCmdData == NULL ||
475 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent6368e6d2015-06-19 15:33:57 -0700476 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700477 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
478 return -EINVAL;
479 }
480 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
481 effect_param_t *p = (effect_param_t *)pReplyData;
482 p->status = 0;
483 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700484 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700485 p->status = -EINVAL;
486 break;
487 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700488 switch (*(uint32_t *)p->data) {
489 case VISUALIZER_PARAM_CAPTURE_SIZE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700490 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700491 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
492 p->vsize = sizeof(uint32_t);
493 *replySize += sizeof(uint32_t);
494 break;
495 case VISUALIZER_PARAM_SCALING_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700496 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700497 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
498 p->vsize = sizeof(uint32_t);
499 *replySize += sizeof(uint32_t);
500 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700501 case VISUALIZER_PARAM_MEASUREMENT_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700502 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700503 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
504 p->vsize = sizeof(uint32_t);
505 *replySize += sizeof(uint32_t);
506 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700507 default:
508 p->status = -EINVAL;
509 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700510 } break;
511 case EFFECT_CMD_SET_PARAM: {
512 if (pCmdData == NULL ||
513 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent6368e6d2015-06-19 15:33:57 -0700514 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700515 return -EINVAL;
516 }
517 *(int32_t *)pReplyData = 0;
518 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700519 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700520 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700521 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700522 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700523 switch (*(uint32_t *)p->data) {
524 case VISUALIZER_PARAM_CAPTURE_SIZE:
525 pContext->mCaptureSize = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700526 ALOGV("set mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700527 break;
528 case VISUALIZER_PARAM_SCALING_MODE:
529 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700530 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700531 break;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700532 case VISUALIZER_PARAM_LATENCY:
533 pContext->mLatency = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700534 ALOGV("set mLatency = %" PRIu32, pContext->mLatency);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700535 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700536 case VISUALIZER_PARAM_MEASUREMENT_MODE:
537 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700538 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700539 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700540 default:
541 *(int32_t *)pReplyData = -EINVAL;
542 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700543 } break;
544 case EFFECT_CMD_SET_DEVICE:
545 case EFFECT_CMD_SET_VOLUME:
546 case EFFECT_CMD_SET_AUDIO_MODE:
547 break;
548
549
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100550 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700551 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent6368e6d2015-06-19 15:33:57 -0700552 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700553 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100554 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700555 return -EINVAL;
556 }
557 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700558 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700559
560 // if audio framework has stopped playing audio although the effect is still
561 // active we must clear the capture buffer to return silence
562 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100563 (pContext->mBufferUpdateTime.tv_sec != 0) &&
564 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700565 ALOGV("capture going to idle");
566 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100567 memset(pReplyData, 0x80, captureSize);
568 } else {
569 int32_t latencyMs = pContext->mLatency;
570 latencyMs -= deltaMs;
571 if (latencyMs < 0) {
572 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700573 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100574 const uint32_t deltaSmpl =
575 pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
576 int32_t capturePoint = pContext->mCaptureIdx - captureSize - deltaSmpl;
577
578 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700579 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100580 if (size > captureSize) {
581 size = captureSize;
582 }
583 memcpy(pReplyData,
584 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
585 size);
586 pReplyData = (char *)pReplyData + size;
587 captureSize -= size;
588 capturePoint = 0;
589 }
590 memcpy(pReplyData,
591 pContext->mCaptureBuf + capturePoint,
592 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700593 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100594
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700595 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700596 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100597 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700598 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800599
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100600 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700601
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700602 case VISUALIZER_CMD_MEASURE: {
rago46dc7142016-08-22 17:20:26 -0700603 if (pReplyData == NULL || replySize == NULL ||
604 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
605 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
606 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32, *replySize,
607 sizeof(int32_t) * MEASUREMENT_COUNT);
608 android_errorWriteLog(0x534e4554, "30229821");
609 return -EINVAL;
610 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700611 uint16_t peakU16 = 0;
612 float sumRmsSquared = 0.0f;
613 uint8_t nbValidMeasurements = 0;
614 // reset measurements if last measurement was too long ago (which implies stored
615 // measurements aren't relevant anymore and shouldn't bias the new one)
616 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
617 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700618 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700619 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700620 pContext->mPastMeasurements[i].mIsValid = false;
621 pContext->mPastMeasurements[i].mPeakU16 = 0;
622 pContext->mPastMeasurements[i].mRmsSquared = 0;
623 }
624 pContext->mMeasurementBufferIdx = 0;
625 } else {
626 // only use actual measurements, otherwise the first RMS measure happening before
627 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
628 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700629 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700630 if (pContext->mPastMeasurements[i].mIsValid) {
631 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
632 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
633 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700634 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700635 nbValidMeasurements++;
636 }
637 }
638 }
639 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
640 int32_t* pIntReplyData = (int32_t*)pReplyData;
641 // convert from I16 sample values to mB and write results
642 if (rms < 0.000016f) {
643 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
644 } else {
645 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
646 }
647 if (peakU16 == 0) {
648 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
649 } else {
650 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
651 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700652 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700653 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
654 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
655 }
656 break;
657
Eric Laurentda7581b2010-07-02 08:12:41 -0700658 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700659 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700660 return -EINVAL;
661 }
662
663 return 0;
664}
665
Eric Laurente1315cf2011-05-17 19:16:02 -0700666/* Effect Control Interface Implementation: get_descriptor */
667int Visualizer_getDescriptor(effect_handle_t self,
668 effect_descriptor_t *pDescriptor)
669{
670 VisualizerContext * pContext = (VisualizerContext *) self;
671
672 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700674 return -EINVAL;
675 }
676
Glenn Kastena189a682012-02-20 12:16:30 -0800677 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700678
679 return 0;
680} /* end Visualizer_getDescriptor */
681
682// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700683const struct effect_interface_s gVisualizerInterface = {
684 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700685 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700686 Visualizer_getDescriptor,
687 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700688};
689
Marco Nelissen7f16b192012-10-25 16:05:57 -0700690// This is the only symbol that needs to be exported
691__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700692audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700693 .tag = AUDIO_EFFECT_LIBRARY_TAG,
694 .version = EFFECT_LIBRARY_API_VERSION,
695 .name = "Visualizer Library",
696 .implementor = "The Android Open Source Project",
697 .create_effect = VisualizerLib_Create,
698 .release_effect = VisualizerLib_Release,
699 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700700};
701
702}; // extern "C"