blob: 6a126ef7bb8e8f08cb7797453f9685160da517ff [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;
240 int i;
241
Eric Laurente1315cf2011-05-17 19:16:02 -0700242 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700243 return -EINVAL;
244 }
245
246 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
247 return -EINVAL;
248 }
249
250 VisualizerContext *pContext = new VisualizerContext;
251
252 pContext->mItfe = &gVisualizerInterface;
253 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
254
255 ret = Visualizer_init(pContext);
256 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000257 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700258 delete pContext;
259 return ret;
260 }
261
Eric Laurente1315cf2011-05-17 19:16:02 -0700262 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700263
264 pContext->mState = VISUALIZER_STATE_INITIALIZED;
265
Steve Block3856b092011-10-20 11:56:00 +0100266 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700267
268 return 0;
269
270}
271
Eric Laurente1315cf2011-05-17 19:16:02 -0700272int VisualizerLib_Release(effect_handle_t handle) {
273 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700274
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700276 if (pContext == NULL) {
277 return -EINVAL;
278 }
279 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
280 delete pContext;
281
282 return 0;
283}
284
Glenn Kasten5e92a782012-01-30 07:40:52 -0800285int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700286 effect_descriptor_t *pDescriptor) {
287
288 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100289 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700290 return -EINVAL;
291 }
292
293 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800294 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700295 return 0;
296 }
297
298 return -EINVAL;
299} /* end VisualizerLib_GetDescriptor */
300
Eric Laurentda7581b2010-07-02 08:12:41 -0700301//
302//--- Effect Control Interface Implementation
303//
304
305static inline int16_t clamp16(int32_t sample)
306{
307 if ((sample>>15) ^ (sample>>31))
308 sample = 0x7FFF ^ (sample>>31);
309 return sample;
310}
311
Eric Laurente1315cf2011-05-17 19:16:02 -0700312int Visualizer_process(
313 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700314{
Eric Laurente1315cf2011-05-17 19:16:02 -0700315 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700316
317 if (pContext == NULL) {
318 return -EINVAL;
319 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700320
321 if (inBuffer == NULL || inBuffer->raw == NULL ||
322 outBuffer == NULL || outBuffer->raw == NULL ||
323 inBuffer->frameCount != outBuffer->frameCount ||
324 inBuffer->frameCount == 0) {
325 return -EINVAL;
326 }
327
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700328 // perform measurements if needed
329 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
330 // find the peak and RMS squared for the new buffer
331 uint32_t inIdx;
332 int16_t maxSample = 0;
333 float rmsSqAcc = 0;
334 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
335 if (inBuffer->s16[inIdx] > maxSample) {
336 maxSample = inBuffer->s16[inIdx];
337 } else if (-inBuffer->s16[inIdx] > maxSample) {
338 maxSample = -inBuffer->s16[inIdx];
339 }
340 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
341 }
342 // store the measurement
343 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
344 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
345 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
346 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
347 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
348 pContext->mMeasurementBufferIdx = 0;
349 }
350 }
351
Eric Laurentda7581b2010-07-02 08:12:41 -0700352 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700353 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700354
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700355 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
356 // derive capture scaling factor from peak value in current buffer
357 // this gives more interesting captures for display.
358 shift = 32;
359 int len = inBuffer->frameCount * 2;
360 for (int i = 0; i < len; i++) {
361 int32_t smp = inBuffer->s16[i];
362 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
363 int32_t clz = __builtin_clz(smp);
364 if (shift > clz) shift = clz;
365 }
366 // A maximum amplitude signal will have 17 leading zeros, which we want to
367 // translate to a shift of 8 (for converting 16 bit to 8 bit)
368 shift = 25 - shift;
369 // Never scale by less than 8 to avoid returning unaltered PCM signal.
370 if (shift < 3) {
371 shift = 3;
372 }
373 // add one to combine the division by 2 needed after summing left and right channels below
374 shift++;
375 } else {
376 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
377 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700378 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700379
Eric Laurentda7581b2010-07-02 08:12:41 -0700380 uint32_t captIdx;
381 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700382 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700383 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700384 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700385 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700386 if (captIdx >= CAPTURE_BUF_SIZE) {
387 // wrap around
388 captIdx = 0;
389 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700390 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700391 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700392 buf[captIdx] = ((uint8_t)smp)^0x80;
393 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700394
395 // XXX the following two should really be atomic, though it probably doesn't
396 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700397 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700398 // update last buffer update time stamp
399 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
400 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700401 }
402
403 if (inBuffer->raw != outBuffer->raw) {
404 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
405 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
406 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
407 }
408 } else {
409 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
410 }
411 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700412 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
413 return -ENODATA;
414 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700415 return 0;
416} // end Visualizer_process
417
Eric Laurente1315cf2011-05-17 19:16:02 -0700418int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700419 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700420
Eric Laurente1315cf2011-05-17 19:16:02 -0700421 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700422 int retsize;
423
424 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
425 return -EINVAL;
426 }
427
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700428// ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700429
430 switch (cmdCode) {
431 case EFFECT_CMD_INIT:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700432 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700433 return -EINVAL;
434 }
435 *(int *) pReplyData = Visualizer_init(pContext);
436 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800437 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700438 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent6368e6d2015-06-19 15:33:57 -0700439 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700440 return -EINVAL;
441 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800442 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700443 (effect_config_t *) pCmdData);
444 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800445 case EFFECT_CMD_GET_CONFIG:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700446 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800447 *replySize != sizeof(effect_config_t)) {
448 return -EINVAL;
449 }
450 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
451 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700452 case EFFECT_CMD_RESET:
453 Visualizer_reset(pContext);
454 break;
455 case EFFECT_CMD_ENABLE:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700456 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700457 return -EINVAL;
458 }
459 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
460 return -ENOSYS;
461 }
462 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700464 *(int *)pReplyData = 0;
465 break;
466 case EFFECT_CMD_DISABLE:
Eric Laurent6368e6d2015-06-19 15:33:57 -0700467 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700468 return -EINVAL;
469 }
470 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
471 return -ENOSYS;
472 }
473 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100474 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700475 *(int *)pReplyData = 0;
476 break;
477 case EFFECT_CMD_GET_PARAM: {
478 if (pCmdData == NULL ||
479 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent6368e6d2015-06-19 15:33:57 -0700480 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700481 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
482 return -EINVAL;
483 }
484 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
485 effect_param_t *p = (effect_param_t *)pReplyData;
486 p->status = 0;
487 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700488 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700489 p->status = -EINVAL;
490 break;
491 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700492 switch (*(uint32_t *)p->data) {
493 case VISUALIZER_PARAM_CAPTURE_SIZE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700494 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700495 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
496 p->vsize = sizeof(uint32_t);
497 *replySize += sizeof(uint32_t);
498 break;
499 case VISUALIZER_PARAM_SCALING_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700500 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700501 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
502 p->vsize = sizeof(uint32_t);
503 *replySize += sizeof(uint32_t);
504 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700505 case VISUALIZER_PARAM_MEASUREMENT_MODE:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700506 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700507 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
508 p->vsize = sizeof(uint32_t);
509 *replySize += sizeof(uint32_t);
510 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700511 default:
512 p->status = -EINVAL;
513 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700514 } break;
515 case EFFECT_CMD_SET_PARAM: {
516 if (pCmdData == NULL ||
517 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent6368e6d2015-06-19 15:33:57 -0700518 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700519 return -EINVAL;
520 }
521 *(int32_t *)pReplyData = 0;
522 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700523 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700524 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700525 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700526 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700527 switch (*(uint32_t *)p->data) {
Andy Hung9a2732b2016-10-18 17:13:09 -0700528 case VISUALIZER_PARAM_CAPTURE_SIZE: {
529 const uint32_t captureSize = *((uint32_t *)p->data + 1);
530 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
531 android_errorWriteLog(0x534e4554, "31781965");
532 *(int32_t *)pReplyData = -EINVAL;
533 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
534 } else {
535 pContext->mCaptureSize = captureSize;
536 ALOGV("set mCaptureSize = %u", captureSize);
537 }
538 } break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700539 case VISUALIZER_PARAM_SCALING_MODE:
540 pContext->mScalingMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700541 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700542 break;
Andy Hung9a2732b2016-10-18 17:13:09 -0700543 case VISUALIZER_PARAM_LATENCY: {
544 uint32_t latency = *((uint32_t *)p->data + 1);
545 if (latency > MAX_LATENCY_MS) {
546 latency = MAX_LATENCY_MS; // clamp latency b/31781965
547 }
548 pContext->mLatency = latency;
549 ALOGV("set mLatency = %u", latency);
550 } break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700551 case VISUALIZER_PARAM_MEASUREMENT_MODE:
552 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700553 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700554 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700555 default:
556 *(int32_t *)pReplyData = -EINVAL;
557 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700558 } break;
559 case EFFECT_CMD_SET_DEVICE:
560 case EFFECT_CMD_SET_VOLUME:
561 case EFFECT_CMD_SET_AUDIO_MODE:
562 break;
563
564
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100565 case VISUALIZER_CMD_CAPTURE: {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700566 uint32_t captureSize = pContext->mCaptureSize;
Eric Laurent6368e6d2015-06-19 15:33:57 -0700567 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700568 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100569 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700570 return -EINVAL;
571 }
572 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700573 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700574
575 // if audio framework has stopped playing audio although the effect is still
576 // active we must clear the capture buffer to return silence
577 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100578 (pContext->mBufferUpdateTime.tv_sec != 0) &&
579 (deltaMs > MAX_STALL_TIME_MS)) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700580 ALOGV("capture going to idle");
581 pContext->mBufferUpdateTime.tv_sec = 0;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100582 memset(pReplyData, 0x80, captureSize);
583 } else {
584 int32_t latencyMs = pContext->mLatency;
585 latencyMs -= deltaMs;
586 if (latencyMs < 0) {
587 latencyMs = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700588 }
Andy Hung9a2732b2016-10-18 17:13:09 -0700589 uint32_t deltaSmpl = captureSize
590 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100591
Andy Hung9a2732b2016-10-18 17:13:09 -0700592 // large sample rate, latency, or capture size, could cause overflow.
593 // do not offset more than the size of buffer.
594 if (deltaSmpl > CAPTURE_BUF_SIZE) {
595 android_errorWriteLog(0x534e4554, "31781965");
596 deltaSmpl = CAPTURE_BUF_SIZE;
597 }
598
599 int32_t capturePoint = pContext->mCaptureIdx - deltaSmpl;
600 // a negative capturePoint means we wrap the buffer.
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100601 if (capturePoint < 0) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700602 uint32_t size = -capturePoint;
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100603 if (size > captureSize) {
604 size = captureSize;
605 }
606 memcpy(pReplyData,
607 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
608 size);
609 pReplyData = (char *)pReplyData + size;
610 captureSize -= size;
611 capturePoint = 0;
612 }
613 memcpy(pReplyData,
614 pContext->mCaptureBuf + capturePoint,
615 captureSize);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700616 }
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100617
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700618 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700619 } else {
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100620 memset(pReplyData, 0x80, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700621 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800622
Ryszard Grzesicaabb7b172014-01-17 11:40:16 +0100623 } break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700624
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700625 case VISUALIZER_CMD_MEASURE: {
rago46dc7142016-08-22 17:20:26 -0700626 if (pReplyData == NULL || replySize == NULL ||
627 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
rago874f9e02016-10-07 18:16:09 -0700628 if (replySize == NULL) {
629 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
630 } else {
631 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
632 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
633 *replySize,
634 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
635 }
rago46dc7142016-08-22 17:20:26 -0700636 android_errorWriteLog(0x534e4554, "30229821");
637 return -EINVAL;
638 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700639 uint16_t peakU16 = 0;
640 float sumRmsSquared = 0.0f;
641 uint8_t nbValidMeasurements = 0;
642 // reset measurements if last measurement was too long ago (which implies stored
643 // measurements aren't relevant anymore and shouldn't bias the new one)
644 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
645 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700646 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700647 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700648 pContext->mPastMeasurements[i].mIsValid = false;
649 pContext->mPastMeasurements[i].mPeakU16 = 0;
650 pContext->mPastMeasurements[i].mRmsSquared = 0;
651 }
652 pContext->mMeasurementBufferIdx = 0;
653 } else {
654 // only use actual measurements, otherwise the first RMS measure happening before
655 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
656 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700657 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700658 if (pContext->mPastMeasurements[i].mIsValid) {
659 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
660 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
661 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700662 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700663 nbValidMeasurements++;
664 }
665 }
666 }
667 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
668 int32_t* pIntReplyData = (int32_t*)pReplyData;
669 // convert from I16 sample values to mB and write results
670 if (rms < 0.000016f) {
671 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
672 } else {
673 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
674 }
675 if (peakU16 == 0) {
676 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
677 } else {
678 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
679 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700680 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700681 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
682 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
683 }
684 break;
685
Eric Laurentda7581b2010-07-02 08:12:41 -0700686 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700687 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700688 return -EINVAL;
689 }
690
691 return 0;
692}
693
Eric Laurente1315cf2011-05-17 19:16:02 -0700694/* Effect Control Interface Implementation: get_descriptor */
695int Visualizer_getDescriptor(effect_handle_t self,
696 effect_descriptor_t *pDescriptor)
697{
698 VisualizerContext * pContext = (VisualizerContext *) self;
699
700 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700702 return -EINVAL;
703 }
704
Glenn Kastena189a682012-02-20 12:16:30 -0800705 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700706
707 return 0;
708} /* end Visualizer_getDescriptor */
709
710// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700711const struct effect_interface_s gVisualizerInterface = {
712 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700713 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700714 Visualizer_getDescriptor,
715 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700716};
717
Marco Nelissen7f16b192012-10-25 16:05:57 -0700718// This is the only symbol that needs to be exported
719__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700720audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700721 .tag = AUDIO_EFFECT_LIBRARY_TAG,
722 .version = EFFECT_LIBRARY_API_VERSION,
723 .name = "Visualizer Library",
724 .implementor = "The Android Open Source Project",
725 .create_effect = VisualizerLib_Create,
726 .release_effect = VisualizerLib_Release,
727 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700728};
729
730}; // extern "C"