blob: b7d27d6cc1d59038633be5306857845d1459c5ba [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
Andy Hung9a2732b2016-10-18 17:13:09 -070062#define MAX_LATENCY_MS 3000 // 3 seconds of latency for audio pipeline
63
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070064// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070065#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070066
67
68struct BufferStats {
69 bool mIsValid;
70 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
71 float mRmsSquared; // the average square of the samples in a buffer
72};
73
Eric Laurentda7581b2010-07-02 08:12:41 -070074struct VisualizerContext {
75 const struct effect_interface_s *mItfe;
76 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070077 uint32_t mCaptureIdx;
78 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070079 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080080 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070081 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070082 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070083 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070084 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070085 // for measurements
86 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
87 uint32_t mMeasurementMode;
88 uint8_t mMeasurementWindowSizeInBuffers;
89 uint8_t mMeasurementBufferIdx;
90 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -070091};
92
Eric Laurentda7581b2010-07-02 08:12:41 -070093//
94//--- Local functions
95//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070096uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
97 uint32_t deltaMs = 0;
98 if (pContext->mBufferUpdateTime.tv_sec != 0) {
99 struct timespec ts;
100 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
101 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
102 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
103 if (nsec < 0) {
104 --secs;
105 nsec += 1000000000;
106 }
107 deltaMs = secs * 1000 + nsec / 1000000;
108 }
109 }
110 return deltaMs;
111}
112
Eric Laurentda7581b2010-07-02 08:12:41 -0700113
114void Visualizer_reset(VisualizerContext *pContext)
115{
116 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700117 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700118 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700119 pContext->mLatency = 0;
120 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700121}
122
123//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800124// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700125//----------------------------------------------------------------------------
126// Purpose: Set input and output audio configuration.
127//
128// Inputs:
129// pContext: effect engine context
130// pConfig: pointer to effect_config_t structure holding input and output
131// configuration parameters
132//
133// Outputs:
134//
135//----------------------------------------------------------------------------
136
Eric Laurent3d5188b2011-12-16 15:30:36 -0800137int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700138{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800139 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700140
141 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
142 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
143 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700144 if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700145 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
146 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700147 if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700148
Glenn Kastena189a682012-02-20 12:16:30 -0800149 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700150
151 Visualizer_reset(pContext);
152
153 return 0;
154}
155
156
157//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800158// Visualizer_getConfig()
159//----------------------------------------------------------------------------
160// Purpose: Get input and output audio configuration.
161//
162// Inputs:
163// pContext: effect engine context
164// pConfig: pointer to effect_config_t structure holding input and output
165// configuration parameters
166//
167// Outputs:
168//
169//----------------------------------------------------------------------------
170
171void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
172{
Glenn Kastena189a682012-02-20 12:16:30 -0800173 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800174}
175
176
177//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700178// Visualizer_init()
179//----------------------------------------------------------------------------
180// Purpose: Initialize engine with default configuration.
181//
182// Inputs:
183// pContext: effect engine context
184//
185// Outputs:
186//
187//----------------------------------------------------------------------------
188
189int Visualizer_init(VisualizerContext *pContext)
190{
191 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700192 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
193 pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700194 pContext->mConfig.inputCfg.samplingRate = 44100;
195 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
196 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
197 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
198 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
199 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700200 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
201 pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700202 pContext->mConfig.outputCfg.samplingRate = 44100;
203 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
204 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
205 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
206 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
207
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700208 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700209 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700210 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700211
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700212 // measurement initialization
Andy Hunge5412692014-05-16 11:25:07 -0700213 pContext->mChannelCount =
214 audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700215 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
216 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
217 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700218 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700219 pContext->mPastMeasurements[i].mIsValid = false;
220 pContext->mPastMeasurements[i].mPeakU16 = 0;
221 pContext->mPastMeasurements[i].mRmsSquared = 0;
222 }
223
Eric Laurent3d5188b2011-12-16 15:30:36 -0800224 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700225
226 return 0;
227}
228
229//
230//--- Effect Library Interface Implementation
231//
232
Glenn Kasten5e92a782012-01-30 07:40:52 -0800233int VisualizerLib_Create(const effect_uuid_t *uuid,
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700234 int32_t /*sessionId*/,
235 int32_t /*ioId*/,
Eric Laurente1315cf2011-05-17 19:16:02 -0700236 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700237 int ret;
238 int i;
239
Eric Laurente1315cf2011-05-17 19:16:02 -0700240 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700241 return -EINVAL;
242 }
243
244 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
245 return -EINVAL;
246 }
247
248 VisualizerContext *pContext = new VisualizerContext;
249
250 pContext->mItfe = &gVisualizerInterface;
251 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
252
253 ret = Visualizer_init(pContext);
254 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000255 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700256 delete pContext;
257 return ret;
258 }
259
Eric Laurente1315cf2011-05-17 19:16:02 -0700260 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700261
262 pContext->mState = VISUALIZER_STATE_INITIALIZED;
263
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700265
266 return 0;
267
268}
269
Eric Laurente1315cf2011-05-17 19:16:02 -0700270int VisualizerLib_Release(effect_handle_t handle) {
271 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700272
Steve Block3856b092011-10-20 11:56:00 +0100273 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700274 if (pContext == NULL) {
275 return -EINVAL;
276 }
277 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
278 delete pContext;
279
280 return 0;
281}
282
Glenn Kasten5e92a782012-01-30 07:40:52 -0800283int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700284 effect_descriptor_t *pDescriptor) {
285
286 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100287 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700288 return -EINVAL;
289 }
290
291 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800292 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700293 return 0;
294 }
295
296 return -EINVAL;
297} /* end VisualizerLib_GetDescriptor */
298
Eric Laurentda7581b2010-07-02 08:12:41 -0700299//
300//--- Effect Control Interface Implementation
301//
302
303static inline int16_t clamp16(int32_t sample)
304{
305 if ((sample>>15) ^ (sample>>31))
306 sample = 0x7FFF ^ (sample>>31);
307 return sample;
308}
309
Eric Laurente1315cf2011-05-17 19:16:02 -0700310int Visualizer_process(
311 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700312{
Eric Laurente1315cf2011-05-17 19:16:02 -0700313 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700314
315 if (pContext == NULL) {
316 return -EINVAL;
317 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700318
319 if (inBuffer == NULL || inBuffer->raw == NULL ||
320 outBuffer == NULL || outBuffer->raw == NULL ||
321 inBuffer->frameCount != outBuffer->frameCount ||
322 inBuffer->frameCount == 0) {
323 return -EINVAL;
324 }
325
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700326 // perform measurements if needed
327 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
328 // find the peak and RMS squared for the new buffer
329 uint32_t inIdx;
330 int16_t maxSample = 0;
331 float rmsSqAcc = 0;
332 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
333 if (inBuffer->s16[inIdx] > maxSample) {
334 maxSample = inBuffer->s16[inIdx];
335 } else if (-inBuffer->s16[inIdx] > maxSample) {
336 maxSample = -inBuffer->s16[inIdx];
337 }
338 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
339 }
340 // store the measurement
341 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
342 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
343 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
344 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
345 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
346 pContext->mMeasurementBufferIdx = 0;
347 }
348 }
349
Eric Laurentda7581b2010-07-02 08:12:41 -0700350 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700351 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700352
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700353 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
354 // derive capture scaling factor from peak value in current buffer
355 // this gives more interesting captures for display.
356 shift = 32;
357 int len = inBuffer->frameCount * 2;
358 for (int i = 0; i < len; i++) {
359 int32_t smp = inBuffer->s16[i];
360 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
361 int32_t clz = __builtin_clz(smp);
362 if (shift > clz) shift = clz;
363 }
364 // A maximum amplitude signal will have 17 leading zeros, which we want to
365 // translate to a shift of 8 (for converting 16 bit to 8 bit)
366 shift = 25 - shift;
367 // Never scale by less than 8 to avoid returning unaltered PCM signal.
368 if (shift < 3) {
369 shift = 3;
370 }
371 // add one to combine the division by 2 needed after summing left and right channels below
372 shift++;
373 } else {
374 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
375 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700376 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700377
Eric Laurentda7581b2010-07-02 08:12:41 -0700378 uint32_t captIdx;
379 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700380 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700381 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700382 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700383 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700384 if (captIdx >= CAPTURE_BUF_SIZE) {
385 // wrap around
386 captIdx = 0;
387 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700388 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700389 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700390 buf[captIdx] = ((uint8_t)smp)^0x80;
391 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700392
393 // XXX the following two should really be atomic, though it probably doesn't
394 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700395 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700396 // update last buffer update time stamp
397 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
398 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700399 }
400
401 if (inBuffer->raw != outBuffer->raw) {
402 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
403 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
404 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
405 }
406 } else {
407 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
408 }
409 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700410 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
411 return -ENODATA;
412 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700413 return 0;
414} // end Visualizer_process
415
Eric Laurente1315cf2011-05-17 19:16:02 -0700416int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700417 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700418
Eric Laurente1315cf2011-05-17 19:16:02 -0700419 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700420 int retsize;
421
422 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
423 return -EINVAL;
424 }
425
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700426// ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700427
428 switch (cmdCode) {
429 case EFFECT_CMD_INIT:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700430 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700431 return -EINVAL;
432 }
433 *(int *) pReplyData = Visualizer_init(pContext);
434 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800435 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700436 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent6368e6d2015-06-19 15:33:57 -0700437 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700438 return -EINVAL;
439 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800440 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700441 (effect_config_t *) pCmdData);
442 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800443 case EFFECT_CMD_GET_CONFIG:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700444 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800445 *replySize != sizeof(effect_config_t)) {
446 return -EINVAL;
447 }
448 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
449 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700450 case EFFECT_CMD_RESET:
451 Visualizer_reset(pContext);
452 break;
453 case EFFECT_CMD_ENABLE:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700454 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700455 return -EINVAL;
456 }
457 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
458 return -ENOSYS;
459 }
460 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100461 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700462 *(int *)pReplyData = 0;
463 break;
464 case EFFECT_CMD_DISABLE:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700465 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700466 return -EINVAL;
467 }
468 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
469 return -ENOSYS;
470 }
471 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100472 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700473 *(int *)pReplyData = 0;
474 break;
475 case EFFECT_CMD_GET_PARAM: {
476 if (pCmdData == NULL ||
477 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent6368e6d2015-06-19 15:33:57 -0700478 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700479 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
480 return -EINVAL;
481 }
482 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
483 effect_param_t *p = (effect_param_t *)pReplyData;
484 p->status = 0;
485 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700486 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700487 p->status = -EINVAL;
488 break;
489 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700490 switch (*(uint32_t *)p->data) {
491 case VISUALIZER_PARAM_CAPTURE_SIZE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700492 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700493 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
494 p->vsize = sizeof(uint32_t);
495 *replySize += sizeof(uint32_t);
496 break;
497 case VISUALIZER_PARAM_SCALING_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700498 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700499 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
500 p->vsize = sizeof(uint32_t);
501 *replySize += sizeof(uint32_t);
502 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700503 case VISUALIZER_PARAM_MEASUREMENT_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700504 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700505 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
506 p->vsize = sizeof(uint32_t);
507 *replySize += sizeof(uint32_t);
508 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700509 default:
510 p->status = -EINVAL;
511 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700512 } break;
513 case EFFECT_CMD_SET_PARAM: {
514 if (pCmdData == NULL ||
515 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent6368e6d2015-06-19 15:33:57 -0700516 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700517 return -EINVAL;
518 }
519 *(int32_t *)pReplyData = 0;
520 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700521 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700522 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700523 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700524 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700525 switch (*(uint32_t *)p->data) {
Andy Hung9a2732b2016-10-18 17:13:09 -0700526 case VISUALIZER_PARAM_CAPTURE_SIZE: {
527 const uint32_t captureSize = *((uint32_t *)p->data + 1);
528 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
529 android_errorWriteLog(0x534e4554, "31781965");
530 *(int32_t *)pReplyData = -EINVAL;
531 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
532 } else {
533 pContext->mCaptureSize = captureSize;
534 ALOGV("set mCaptureSize = %u", captureSize);
535 }
536 } break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700537 case VISUALIZER_PARAM_SCALING_MODE:
538 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700539 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700540 break;
Andy Hung9a2732b2016-10-18 17:13:09 -0700541 case VISUALIZER_PARAM_LATENCY: {
542 uint32_t latency = *((uint32_t *)p->data + 1);
543 if (latency > MAX_LATENCY_MS) {
544 latency = MAX_LATENCY_MS; // clamp latency b/31781965
545 }
546 pContext->mLatency = latency;
547 ALOGV("set mLatency = %u", latency);
548 } break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700549 case VISUALIZER_PARAM_MEASUREMENT_MODE:
550 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700551 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700552 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700553 default:
554 *(int32_t *)pReplyData = -EINVAL;
555 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700556 } break;
557 case EFFECT_CMD_SET_DEVICE:
558 case EFFECT_CMD_SET_VOLUME:
559 case EFFECT_CMD_SET_AUDIO_MODE:
560 break;
561
562
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100563 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700564 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent6368e6d2015-06-19 15:33:57 -0700565 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700566 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100567 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700568 return -EINVAL;
569 }
570 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700571 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700572
573 // if audio framework has stopped playing audio although the effect is still
574 // active we must clear the capture buffer to return silence
575 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100576 (pContext->mBufferUpdateTime.tv_sec != 0) &&
577 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700578 ALOGV("capture going to idle");
579 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100580 memset(pReplyData, 0x80, captureSize);
581 } else {
582 int32_t latencyMs = pContext->mLatency;
583 latencyMs -= deltaMs;
584 if (latencyMs < 0) {
585 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700586 }
Andy Hung9a2732b2016-10-18 17:13:09 -0700587 uint32_t deltaSmpl = captureSize
588 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100589
Andy Hung9a2732b2016-10-18 17:13:09 -0700590 // large sample rate, latency, or capture size, could cause overflow.
591 // do not offset more than the size of buffer.
592 if (deltaSmpl > CAPTURE_BUF_SIZE) {
593 android_errorWriteLog(0x534e4554, "31781965");
594 deltaSmpl = CAPTURE_BUF_SIZE;
595 }
596
597 int32_t capturePoint = pContext->mCaptureIdx - deltaSmpl;
598 // a negative capturePoint means we wrap the buffer.
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100599 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700600 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100601 if (size > captureSize) {
602 size = captureSize;
603 }
604 memcpy(pReplyData,
605 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
606 size);
607 pReplyData = (char *)pReplyData + size;
608 captureSize -= size;
609 capturePoint = 0;
610 }
611 memcpy(pReplyData,
612 pContext->mCaptureBuf + capturePoint,
613 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700614 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100615
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700616 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700617 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100618 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700619 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800620
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100621 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700622
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700623 case VISUALIZER_CMD_MEASURE: {
rago46dc7142016-08-22 17:20:26 -0700624 if (pReplyData == NULL || replySize == NULL ||
625 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
rago874f9e02016-10-07 18:16:09 -0700626 if (replySize == NULL) {
627 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
628 } else {
629 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
630 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
631 *replySize,
632 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
633 }
rago46dc7142016-08-22 17:20:26 -0700634 android_errorWriteLog(0x534e4554, "30229821");
635 return -EINVAL;
636 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700637 uint16_t peakU16 = 0;
638 float sumRmsSquared = 0.0f;
639 uint8_t nbValidMeasurements = 0;
640 // reset measurements if last measurement was too long ago (which implies stored
641 // measurements aren't relevant anymore and shouldn't bias the new one)
642 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
643 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700644 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700645 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700646 pContext->mPastMeasurements[i].mIsValid = false;
647 pContext->mPastMeasurements[i].mPeakU16 = 0;
648 pContext->mPastMeasurements[i].mRmsSquared = 0;
649 }
650 pContext->mMeasurementBufferIdx = 0;
651 } else {
652 // only use actual measurements, otherwise the first RMS measure happening before
653 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
654 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700655 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700656 if (pContext->mPastMeasurements[i].mIsValid) {
657 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
658 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
659 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700660 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700661 nbValidMeasurements++;
662 }
663 }
664 }
665 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
666 int32_t* pIntReplyData = (int32_t*)pReplyData;
667 // convert from I16 sample values to mB and write results
668 if (rms < 0.000016f) {
669 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
670 } else {
671 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
672 }
673 if (peakU16 == 0) {
674 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
675 } else {
676 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
677 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700678 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700679 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
680 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
681 }
682 break;
683
Eric Laurentda7581b2010-07-02 08:12:41 -0700684 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700685 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700686 return -EINVAL;
687 }
688
689 return 0;
690}
691
Eric Laurente1315cf2011-05-17 19:16:02 -0700692/* Effect Control Interface Implementation: get_descriptor */
693int Visualizer_getDescriptor(effect_handle_t self,
694 effect_descriptor_t *pDescriptor)
695{
696 VisualizerContext * pContext = (VisualizerContext *) self;
697
698 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700700 return -EINVAL;
701 }
702
Glenn Kastena189a682012-02-20 12:16:30 -0800703 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700704
705 return 0;
706} /* end Visualizer_getDescriptor */
707
708// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700709const struct effect_interface_s gVisualizerInterface = {
710 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700711 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700712 Visualizer_getDescriptor,
713 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700714};
715
Marco Nelissen7f16b192012-10-25 16:05:57 -0700716// This is the only symbol that needs to be exported
717__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700718audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700719 .tag = AUDIO_EFFECT_LIBRARY_TAG,
720 .version = EFFECT_LIBRARY_API_VERSION,
721 .name = "Visualizer Library",
722 .implementor = "The Android Open Source Project",
723 .create_effect = VisualizerLib_Create,
724 .release_effect = VisualizerLib_Release,
725 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700726};
727
728}; // extern "C"