blob: 1d9801f4061b2940bbe8948fcbe6896022695e04 [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>
rago099ab282016-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;
Eric Laurentda7581b2010-07-02 08:12:41 -0700238
Eric Laurente1315cf2011-05-17 19:16:02 -0700239 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700240 return -EINVAL;
241 }
242
243 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
244 return -EINVAL;
245 }
246
247 VisualizerContext *pContext = new VisualizerContext;
248
249 pContext->mItfe = &gVisualizerInterface;
250 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
251
252 ret = Visualizer_init(pContext);
253 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000254 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700255 delete pContext;
256 return ret;
257 }
258
Eric Laurente1315cf2011-05-17 19:16:02 -0700259 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700260
261 pContext->mState = VISUALIZER_STATE_INITIALIZED;
262
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700264
265 return 0;
266
267}
268
Eric Laurente1315cf2011-05-17 19:16:02 -0700269int VisualizerLib_Release(effect_handle_t handle) {
270 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700271
Steve Block3856b092011-10-20 11:56:00 +0100272 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700273 if (pContext == NULL) {
274 return -EINVAL;
275 }
276 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
277 delete pContext;
278
279 return 0;
280}
281
Glenn Kasten5e92a782012-01-30 07:40:52 -0800282int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700283 effect_descriptor_t *pDescriptor) {
284
285 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100286 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700287 return -EINVAL;
288 }
289
290 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800291 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700292 return 0;
293 }
294
295 return -EINVAL;
296} /* end VisualizerLib_GetDescriptor */
297
Eric Laurentda7581b2010-07-02 08:12:41 -0700298//
299//--- Effect Control Interface Implementation
300//
301
302static inline int16_t clamp16(int32_t sample)
303{
304 if ((sample>>15) ^ (sample>>31))
305 sample = 0x7FFF ^ (sample>>31);
306 return sample;
307}
308
Eric Laurente1315cf2011-05-17 19:16:02 -0700309int Visualizer_process(
310 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700311{
Eric Laurente1315cf2011-05-17 19:16:02 -0700312 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700313
314 if (pContext == NULL) {
315 return -EINVAL;
316 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700317
318 if (inBuffer == NULL || inBuffer->raw == NULL ||
319 outBuffer == NULL || outBuffer->raw == NULL ||
320 inBuffer->frameCount != outBuffer->frameCount ||
321 inBuffer->frameCount == 0) {
322 return -EINVAL;
323 }
324
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700325 // perform measurements if needed
326 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
327 // find the peak and RMS squared for the new buffer
328 uint32_t inIdx;
329 int16_t maxSample = 0;
330 float rmsSqAcc = 0;
331 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
332 if (inBuffer->s16[inIdx] > maxSample) {
333 maxSample = inBuffer->s16[inIdx];
334 } else if (-inBuffer->s16[inIdx] > maxSample) {
335 maxSample = -inBuffer->s16[inIdx];
336 }
337 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
338 }
339 // store the measurement
340 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
341 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
342 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
343 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
344 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
345 pContext->mMeasurementBufferIdx = 0;
346 }
347 }
348
Eric Laurentda7581b2010-07-02 08:12:41 -0700349 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700350 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700351
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700352 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
353 // derive capture scaling factor from peak value in current buffer
354 // this gives more interesting captures for display.
355 shift = 32;
356 int len = inBuffer->frameCount * 2;
357 for (int i = 0; i < len; i++) {
358 int32_t smp = inBuffer->s16[i];
359 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
360 int32_t clz = __builtin_clz(smp);
361 if (shift > clz) shift = clz;
362 }
363 // A maximum amplitude signal will have 17 leading zeros, which we want to
364 // translate to a shift of 8 (for converting 16 bit to 8 bit)
365 shift = 25 - shift;
366 // Never scale by less than 8 to avoid returning unaltered PCM signal.
367 if (shift < 3) {
368 shift = 3;
369 }
370 // add one to combine the division by 2 needed after summing left and right channels below
371 shift++;
372 } else {
373 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
374 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700375 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700376
Eric Laurentda7581b2010-07-02 08:12:41 -0700377 uint32_t captIdx;
378 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700379 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700380 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700381 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700382 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700383 if (captIdx >= CAPTURE_BUF_SIZE) {
384 // wrap around
385 captIdx = 0;
386 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700387 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700388 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700389 buf[captIdx] = ((uint8_t)smp)^0x80;
390 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700391
392 // XXX the following two should really be atomic, though it probably doesn't
393 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700394 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700395 // update last buffer update time stamp
396 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
397 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700398 }
399
400 if (inBuffer->raw != outBuffer->raw) {
401 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
402 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
403 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
404 }
405 } else {
406 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
407 }
408 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700409 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
410 return -ENODATA;
411 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700412 return 0;
413} // end Visualizer_process
414
Eric Laurente1315cf2011-05-17 19:16:02 -0700415int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700416 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700417
Eric Laurente1315cf2011-05-17 19:16:02 -0700418 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700419
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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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) {
Andy Hung9a2732b2016-10-18 17:13:09 -0700524 case VISUALIZER_PARAM_CAPTURE_SIZE: {
525 const uint32_t captureSize = *((uint32_t *)p->data + 1);
526 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
527 android_errorWriteLog(0x534e4554, "31781965");
528 *(int32_t *)pReplyData = -EINVAL;
529 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
530 } else {
531 pContext->mCaptureSize = captureSize;
532 ALOGV("set mCaptureSize = %u", captureSize);
533 }
534 } break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700535 case VISUALIZER_PARAM_SCALING_MODE:
536 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700537 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700538 break;
Andy Hung9a2732b2016-10-18 17:13:09 -0700539 case VISUALIZER_PARAM_LATENCY: {
540 uint32_t latency = *((uint32_t *)p->data + 1);
541 if (latency > MAX_LATENCY_MS) {
542 latency = MAX_LATENCY_MS; // clamp latency b/31781965
543 }
544 pContext->mLatency = latency;
545 ALOGV("set mLatency = %u", latency);
546 } break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700547 case VISUALIZER_PARAM_MEASUREMENT_MODE:
548 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700549 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700550 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700551 default:
552 *(int32_t *)pReplyData = -EINVAL;
553 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700554 } break;
555 case EFFECT_CMD_SET_DEVICE:
556 case EFFECT_CMD_SET_VOLUME:
557 case EFFECT_CMD_SET_AUDIO_MODE:
558 break;
559
560
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100561 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700562 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent0f714a42015-06-19 15:33:57 -0700563 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700564 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100565 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700566 return -EINVAL;
567 }
568 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700569 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700570
571 // if audio framework has stopped playing audio although the effect is still
572 // active we must clear the capture buffer to return silence
573 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100574 (pContext->mBufferUpdateTime.tv_sec != 0) &&
575 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700576 ALOGV("capture going to idle");
577 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100578 memset(pReplyData, 0x80, captureSize);
579 } else {
580 int32_t latencyMs = pContext->mLatency;
581 latencyMs -= deltaMs;
582 if (latencyMs < 0) {
583 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700584 }
Andy Hung9a2732b2016-10-18 17:13:09 -0700585 uint32_t deltaSmpl = captureSize
586 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100587
Andy Hung9a2732b2016-10-18 17:13:09 -0700588 // large sample rate, latency, or capture size, could cause overflow.
589 // do not offset more than the size of buffer.
590 if (deltaSmpl > CAPTURE_BUF_SIZE) {
591 android_errorWriteLog(0x534e4554, "31781965");
592 deltaSmpl = CAPTURE_BUF_SIZE;
593 }
594
595 int32_t capturePoint = pContext->mCaptureIdx - deltaSmpl;
596 // a negative capturePoint means we wrap the buffer.
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100597 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700598 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100599 if (size > captureSize) {
600 size = captureSize;
601 }
602 memcpy(pReplyData,
603 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
604 size);
605 pReplyData = (char *)pReplyData + size;
606 captureSize -= size;
607 capturePoint = 0;
608 }
609 memcpy(pReplyData,
610 pContext->mCaptureBuf + capturePoint,
611 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700612 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100613
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700614 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700615 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100616 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700617 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800618
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100619 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700620
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700621 case VISUALIZER_CMD_MEASURE: {
rago099ab282016-08-22 17:20:26 -0700622 if (pReplyData == NULL || replySize == NULL ||
623 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
ragob66492c2016-10-07 18:16:09 -0700624 if (replySize == NULL) {
625 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
626 } else {
627 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
628 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
629 *replySize,
630 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
631 }
rago099ab282016-08-22 17:20:26 -0700632 android_errorWriteLog(0x534e4554, "30229821");
633 return -EINVAL;
634 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700635 uint16_t peakU16 = 0;
636 float sumRmsSquared = 0.0f;
637 uint8_t nbValidMeasurements = 0;
638 // reset measurements if last measurement was too long ago (which implies stored
639 // measurements aren't relevant anymore and shouldn't bias the new one)
640 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
641 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700642 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700643 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700644 pContext->mPastMeasurements[i].mIsValid = false;
645 pContext->mPastMeasurements[i].mPeakU16 = 0;
646 pContext->mPastMeasurements[i].mRmsSquared = 0;
647 }
648 pContext->mMeasurementBufferIdx = 0;
649 } else {
650 // only use actual measurements, otherwise the first RMS measure happening before
651 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
652 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700653 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700654 if (pContext->mPastMeasurements[i].mIsValid) {
655 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
656 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
657 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700658 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700659 nbValidMeasurements++;
660 }
661 }
662 }
663 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
664 int32_t* pIntReplyData = (int32_t*)pReplyData;
665 // convert from I16 sample values to mB and write results
666 if (rms < 0.000016f) {
667 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
668 } else {
669 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
670 }
671 if (peakU16 == 0) {
672 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
673 } else {
674 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
675 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700676 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700677 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
678 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
679 }
680 break;
681
Eric Laurentda7581b2010-07-02 08:12:41 -0700682 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700683 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700684 return -EINVAL;
685 }
686
687 return 0;
688}
689
Eric Laurente1315cf2011-05-17 19:16:02 -0700690/* Effect Control Interface Implementation: get_descriptor */
691int Visualizer_getDescriptor(effect_handle_t self,
692 effect_descriptor_t *pDescriptor)
693{
694 VisualizerContext * pContext = (VisualizerContext *) self;
695
696 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700698 return -EINVAL;
699 }
700
Glenn Kastena189a682012-02-20 12:16:30 -0800701 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700702
703 return 0;
704} /* end Visualizer_getDescriptor */
705
706// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700707const struct effect_interface_s gVisualizerInterface = {
708 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700709 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700710 Visualizer_getDescriptor,
711 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700712};
713
Marco Nelissen7f16b192012-10-25 16:05:57 -0700714// This is the only symbol that needs to be exported
715__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700716audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700717 .tag = AUDIO_EFFECT_LIBRARY_TAG,
718 .version = EFFECT_LIBRARY_API_VERSION,
719 .name = "Visualizer Library",
720 .implementor = "The Android Open Source Project",
721 .create_effect = VisualizerLib_Create,
722 .release_effect = VisualizerLib_Release,
723 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700724};
725
726}; // extern "C"