blob: 0e823392587324164a2f80da257324ad55b79543 [file] [log] [blame]
Eric Laurentda7581b2010-07-02 08:12:41 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070017#define LOG_TAG "EffectVisualizer"
Eric Laurentda7581b2010-07-02 08:12:41 -070018//#define LOG_NDEBUG 0
Mark Salyzyn60d02072016-09-29 08:48:48 -070019
Eric Laurentda7581b2010-07-02 08:12:41 -070020#include <assert.h>
Mark Salyzyn7cb0e732014-04-18 13:48:25 -070021#include <inttypes.h>
Mark Salyzyn60d02072016-09-29 08:48:48 -070022#include <math.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070023#include <stdlib.h>
24#include <string.h>
Eric Laurent183dc772012-03-23 15:35:48 -070025#include <time.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070026
Mark Salyzyn60d02072016-09-29 08:48:48 -070027#include <new>
28
29#include <log/log.h>
30
31#include <audio_effects/effect_visualizer.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070032
Eric Laurente1315cf2011-05-17 19:16:02 -070033extern "C" {
34
35// effect_handle_t interface implementation for visualizer effect
36extern const struct effect_interface_s gVisualizerInterface;
Eric Laurentda7581b2010-07-02 08:12:41 -070037
38// Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
39const effect_descriptor_t gVisualizerDescriptor = {
40 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
41 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
Eric Laurente1315cf2011-05-17 19:16:02 -070042 EFFECT_CONTROL_API_VERSION,
Eric Laurentda7581b2010-07-02 08:12:41 -070043 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
44 0, // TODO
45 1,
46 "Visualizer",
Eric Laurente1315cf2011-05-17 19:16:02 -070047 "The Android Open Source Project",
Eric Laurentda7581b2010-07-02 08:12:41 -070048};
49
50enum visualizer_state_e {
51 VISUALIZER_STATE_UNINITIALIZED,
52 VISUALIZER_STATE_INITIALIZED,
53 VISUALIZER_STATE_ACTIVE,
54};
55
Eric Laurent183dc772012-03-23 15:35:48 -070056// maximum time since last capture buffer update before resetting capture buffer. This means
Eric Laurent3df40a02011-11-10 10:02:18 -080057// that the framework has stopped playing audio and we must start returning silence
Eric Laurent183dc772012-03-23 15:35:48 -070058#define MAX_STALL_TIME_MS 1000
Eric Laurent3df40a02011-11-10 10:02:18 -080059
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070060#define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
61
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070062#define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
63
Andy Hung9a2732b2016-10-18 17:13:09 -070064#define MAX_LATENCY_MS 3000 // 3 seconds of latency for audio pipeline
65
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070066// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070067#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070068
69
70struct BufferStats {
71 bool mIsValid;
72 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
73 float mRmsSquared; // the average square of the samples in a buffer
74};
75
Eric Laurentda7581b2010-07-02 08:12:41 -070076struct VisualizerContext {
77 const struct effect_interface_s *mItfe;
78 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070079 uint32_t mCaptureIdx;
80 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070081 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080082 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070083 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070084 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070085 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070086 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070087 // for measurements
88 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
89 uint32_t mMeasurementMode;
90 uint8_t mMeasurementWindowSizeInBuffers;
91 uint8_t mMeasurementBufferIdx;
92 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -070093};
94
Eric Laurentda7581b2010-07-02 08:12:41 -070095//
96//--- Local functions
97//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070098uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
99 uint32_t deltaMs = 0;
100 if (pContext->mBufferUpdateTime.tv_sec != 0) {
101 struct timespec ts;
102 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
103 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
104 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
105 if (nsec < 0) {
106 --secs;
107 nsec += 1000000000;
108 }
109 deltaMs = secs * 1000 + nsec / 1000000;
110 }
111 }
112 return deltaMs;
113}
114
Eric Laurentda7581b2010-07-02 08:12:41 -0700115
116void Visualizer_reset(VisualizerContext *pContext)
117{
118 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700119 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700120 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700121 pContext->mLatency = 0;
122 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700123}
124
125//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800126// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700127//----------------------------------------------------------------------------
128// Purpose: Set input and output audio configuration.
129//
130// Inputs:
131// pContext: effect engine context
132// pConfig: pointer to effect_config_t structure holding input and output
133// configuration parameters
134//
135// Outputs:
136//
137//----------------------------------------------------------------------------
138
Eric Laurent3d5188b2011-12-16 15:30:36 -0800139int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700140{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800141 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700142
143 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
144 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
145 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700146 if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700147 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
148 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700149 if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700150
Glenn Kastena189a682012-02-20 12:16:30 -0800151 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700152
153 Visualizer_reset(pContext);
154
155 return 0;
156}
157
158
159//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800160// Visualizer_getConfig()
161//----------------------------------------------------------------------------
162// Purpose: Get input and output audio configuration.
163//
164// Inputs:
165// pContext: effect engine context
166// pConfig: pointer to effect_config_t structure holding input and output
167// configuration parameters
168//
169// Outputs:
170//
171//----------------------------------------------------------------------------
172
173void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
174{
Glenn Kastena189a682012-02-20 12:16:30 -0800175 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800176}
177
178
179//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700180// Visualizer_init()
181//----------------------------------------------------------------------------
182// Purpose: Initialize engine with default configuration.
183//
184// Inputs:
185// pContext: effect engine context
186//
187// Outputs:
188//
189//----------------------------------------------------------------------------
190
191int Visualizer_init(VisualizerContext *pContext)
192{
193 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700194 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
195 pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700196 pContext->mConfig.inputCfg.samplingRate = 44100;
197 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
198 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
199 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
200 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
201 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700202 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
203 pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700204 pContext->mConfig.outputCfg.samplingRate = 44100;
205 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
206 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
207 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
208 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
209
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700210 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700211 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700212 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700213
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700214 // measurement initialization
Andy Hunge5412692014-05-16 11:25:07 -0700215 pContext->mChannelCount =
216 audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700217 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
218 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
219 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700220 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700221 pContext->mPastMeasurements[i].mIsValid = false;
222 pContext->mPastMeasurements[i].mPeakU16 = 0;
223 pContext->mPastMeasurements[i].mRmsSquared = 0;
224 }
225
Eric Laurent3d5188b2011-12-16 15:30:36 -0800226 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700227
228 return 0;
229}
230
231//
232//--- Effect Library Interface Implementation
233//
234
Glenn Kasten5e92a782012-01-30 07:40:52 -0800235int VisualizerLib_Create(const effect_uuid_t *uuid,
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700236 int32_t /*sessionId*/,
237 int32_t /*ioId*/,
Eric Laurente1315cf2011-05-17 19:16:02 -0700238 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700239 int ret;
Eric Laurentda7581b2010-07-02 08:12:41 -0700240
Eric Laurente1315cf2011-05-17 19:16:02 -0700241 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700242 return -EINVAL;
243 }
244
245 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
246 return -EINVAL;
247 }
248
249 VisualizerContext *pContext = new VisualizerContext;
250
251 pContext->mItfe = &gVisualizerInterface;
252 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
253
254 ret = Visualizer_init(pContext);
255 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000256 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700257 delete pContext;
258 return ret;
259 }
260
Eric Laurente1315cf2011-05-17 19:16:02 -0700261 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700262
263 pContext->mState = VISUALIZER_STATE_INITIALIZED;
264
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700266
267 return 0;
268
269}
270
Eric Laurente1315cf2011-05-17 19:16:02 -0700271int VisualizerLib_Release(effect_handle_t handle) {
272 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700273
Steve Block3856b092011-10-20 11:56:00 +0100274 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700275 if (pContext == NULL) {
276 return -EINVAL;
277 }
278 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
279 delete pContext;
280
281 return 0;
282}
283
Glenn Kasten5e92a782012-01-30 07:40:52 -0800284int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700285 effect_descriptor_t *pDescriptor) {
286
287 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100288 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700289 return -EINVAL;
290 }
291
292 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800293 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700294 return 0;
295 }
296
297 return -EINVAL;
298} /* end VisualizerLib_GetDescriptor */
299
Eric Laurentda7581b2010-07-02 08:12:41 -0700300//
301//--- Effect Control Interface Implementation
302//
303
304static inline int16_t clamp16(int32_t sample)
305{
306 if ((sample>>15) ^ (sample>>31))
307 sample = 0x7FFF ^ (sample>>31);
308 return sample;
309}
310
Eric Laurente1315cf2011-05-17 19:16:02 -0700311int Visualizer_process(
312 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700313{
Eric Laurente1315cf2011-05-17 19:16:02 -0700314 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700315
316 if (pContext == NULL) {
317 return -EINVAL;
318 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700319
320 if (inBuffer == NULL || inBuffer->raw == NULL ||
321 outBuffer == NULL || outBuffer->raw == NULL ||
322 inBuffer->frameCount != outBuffer->frameCount ||
323 inBuffer->frameCount == 0) {
324 return -EINVAL;
325 }
326
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700327 // perform measurements if needed
328 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
329 // find the peak and RMS squared for the new buffer
330 uint32_t inIdx;
331 int16_t maxSample = 0;
332 float rmsSqAcc = 0;
333 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
334 if (inBuffer->s16[inIdx] > maxSample) {
335 maxSample = inBuffer->s16[inIdx];
336 } else if (-inBuffer->s16[inIdx] > maxSample) {
337 maxSample = -inBuffer->s16[inIdx];
338 }
339 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
340 }
341 // store the measurement
342 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
343 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
344 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
345 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
346 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
347 pContext->mMeasurementBufferIdx = 0;
348 }
349 }
350
Eric Laurentda7581b2010-07-02 08:12:41 -0700351 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700352 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700353
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700354 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
355 // derive capture scaling factor from peak value in current buffer
356 // this gives more interesting captures for display.
357 shift = 32;
358 int len = inBuffer->frameCount * 2;
359 for (int i = 0; i < len; i++) {
360 int32_t smp = inBuffer->s16[i];
361 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
362 int32_t clz = __builtin_clz(smp);
363 if (shift > clz) shift = clz;
364 }
365 // A maximum amplitude signal will have 17 leading zeros, which we want to
366 // translate to a shift of 8 (for converting 16 bit to 8 bit)
367 shift = 25 - shift;
368 // Never scale by less than 8 to avoid returning unaltered PCM signal.
369 if (shift < 3) {
370 shift = 3;
371 }
372 // add one to combine the division by 2 needed after summing left and right channels below
373 shift++;
374 } else {
375 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
376 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700377 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700378
Eric Laurentda7581b2010-07-02 08:12:41 -0700379 uint32_t captIdx;
380 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700381 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700382 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700383 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700384 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700385 if (captIdx >= CAPTURE_BUF_SIZE) {
386 // wrap around
387 captIdx = 0;
388 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700389 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700390 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700391 buf[captIdx] = ((uint8_t)smp)^0x80;
392 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700393
394 // XXX the following two should really be atomic, though it probably doesn't
395 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700396 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700397 // update last buffer update time stamp
398 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
399 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700400 }
401
402 if (inBuffer->raw != outBuffer->raw) {
403 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
404 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
405 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
406 }
407 } else {
408 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
409 }
410 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700411 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
412 return -ENODATA;
413 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700414 return 0;
415} // end Visualizer_process
416
Eric Laurente1315cf2011-05-17 19:16:02 -0700417int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700418 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700419
Eric Laurente1315cf2011-05-17 19:16:02 -0700420 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700421
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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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 Laurent0f714a42015-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: {
rago099ab282016-08-22 17:20:26 -0700624 if (pReplyData == NULL || replySize == NULL ||
625 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
ragob66492c2016-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 }
rago099ab282016-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"