blob: bd29cdc91af813954744d24c905b5ff9e05f2500 [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
19#include <cutils/log.h>
20#include <assert.h>
21#include <stdlib.h>
22#include <string.h>
23#include <new>
Eric Laurent183dc772012-03-23 15:35:48 -070024#include <time.h>
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070025#include <math.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070026#include <audio_effects/effect_visualizer.h>
rago46dc7142016-08-22 17:20:26 -070027#include <cutils/log.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070028
Eric Laurentda7581b2010-07-02 08:12:41 -070029
Eric Laurente1315cf2011-05-17 19:16:02 -070030extern "C" {
31
32// effect_handle_t interface implementation for visualizer effect
33extern const struct effect_interface_s gVisualizerInterface;
Eric Laurentda7581b2010-07-02 08:12:41 -070034
35// Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
36const effect_descriptor_t gVisualizerDescriptor = {
37 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
38 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
Eric Laurente1315cf2011-05-17 19:16:02 -070039 EFFECT_CONTROL_API_VERSION,
Eric Laurentda7581b2010-07-02 08:12:41 -070040 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
41 0, // TODO
42 1,
43 "Visualizer",
Eric Laurente1315cf2011-05-17 19:16:02 -070044 "The Android Open Source Project",
Eric Laurentda7581b2010-07-02 08:12:41 -070045};
46
47enum visualizer_state_e {
48 VISUALIZER_STATE_UNINITIALIZED,
49 VISUALIZER_STATE_INITIALIZED,
50 VISUALIZER_STATE_ACTIVE,
51};
52
Eric Laurent183dc772012-03-23 15:35:48 -070053// maximum time since last capture buffer update before resetting capture buffer. This means
Eric Laurent3df40a02011-11-10 10:02:18 -080054// that the framework has stopped playing audio and we must start returning silence
Eric Laurent183dc772012-03-23 15:35:48 -070055#define MAX_STALL_TIME_MS 1000
Eric Laurent3df40a02011-11-10 10:02:18 -080056
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070057#define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
58
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070059#define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
60
61// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070062#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070063
64
65struct BufferStats {
66 bool mIsValid;
67 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
68 float mRmsSquared; // the average square of the samples in a buffer
69};
70
Eric Laurentda7581b2010-07-02 08:12:41 -070071struct VisualizerContext {
72 const struct effect_interface_s *mItfe;
73 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070074 uint32_t mCaptureIdx;
75 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070076 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080077 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070078 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070079 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070080 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070081 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070082 // for measurements
83 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
84 uint32_t mMeasurementMode;
85 uint8_t mMeasurementWindowSizeInBuffers;
86 uint8_t mMeasurementBufferIdx;
87 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -070088};
89
Eric Laurentda7581b2010-07-02 08:12:41 -070090//
91//--- Local functions
92//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070093uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
94 uint32_t deltaMs = 0;
95 if (pContext->mBufferUpdateTime.tv_sec != 0) {
96 struct timespec ts;
97 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
98 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
99 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
100 if (nsec < 0) {
101 --secs;
102 nsec += 1000000000;
103 }
104 deltaMs = secs * 1000 + nsec / 1000000;
105 }
106 }
107 return deltaMs;
108}
109
Eric Laurentda7581b2010-07-02 08:12:41 -0700110
111void Visualizer_reset(VisualizerContext *pContext)
112{
113 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700114 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700115 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700116 pContext->mLatency = 0;
117 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700118}
119
120//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800121// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700122//----------------------------------------------------------------------------
123// Purpose: Set input and output audio configuration.
124//
125// Inputs:
126// pContext: effect engine context
127// pConfig: pointer to effect_config_t structure holding input and output
128// configuration parameters
129//
130// Outputs:
131//
132//----------------------------------------------------------------------------
133
Eric Laurent3d5188b2011-12-16 15:30:36 -0800134int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700135{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800136 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700137
138 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
139 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
140 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700141 if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700142 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
143 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700144 if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700145
Glenn Kastena189a682012-02-20 12:16:30 -0800146 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700147
148 Visualizer_reset(pContext);
149
150 return 0;
151}
152
153
154//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800155// Visualizer_getConfig()
156//----------------------------------------------------------------------------
157// Purpose: Get input and output audio configuration.
158//
159// Inputs:
160// pContext: effect engine context
161// pConfig: pointer to effect_config_t structure holding input and output
162// configuration parameters
163//
164// Outputs:
165//
166//----------------------------------------------------------------------------
167
168void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
169{
Glenn Kastena189a682012-02-20 12:16:30 -0800170 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800171}
172
173
174//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700175// Visualizer_init()
176//----------------------------------------------------------------------------
177// Purpose: Initialize engine with default configuration.
178//
179// Inputs:
180// pContext: effect engine context
181//
182// Outputs:
183//
184//----------------------------------------------------------------------------
185
186int Visualizer_init(VisualizerContext *pContext)
187{
188 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700189 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
190 pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700191 pContext->mConfig.inputCfg.samplingRate = 44100;
192 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
193 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
194 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
195 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
196 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700197 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
198 pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700199 pContext->mConfig.outputCfg.samplingRate = 44100;
200 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
201 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
202 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
203 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
204
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700205 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700206 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700207 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700208
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700209 // measurement initialization
210 pContext->mChannelCount = popcount(pContext->mConfig.inputCfg.channels);
211 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
212 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
213 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700214 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700215 pContext->mPastMeasurements[i].mIsValid = false;
216 pContext->mPastMeasurements[i].mPeakU16 = 0;
217 pContext->mPastMeasurements[i].mRmsSquared = 0;
218 }
219
Eric Laurent3d5188b2011-12-16 15:30:36 -0800220 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700221
222 return 0;
223}
224
225//
226//--- Effect Library Interface Implementation
227//
228
Glenn Kasten5e92a782012-01-30 07:40:52 -0800229int VisualizerLib_Create(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700230 int32_t sessionId,
231 int32_t ioId,
232 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700233 int ret;
234 int i;
235
Eric Laurente1315cf2011-05-17 19:16:02 -0700236 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700237 return -EINVAL;
238 }
239
240 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
241 return -EINVAL;
242 }
243
244 VisualizerContext *pContext = new VisualizerContext;
245
246 pContext->mItfe = &gVisualizerInterface;
247 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
248
249 ret = Visualizer_init(pContext);
250 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000251 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700252 delete pContext;
253 return ret;
254 }
255
Eric Laurente1315cf2011-05-17 19:16:02 -0700256 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700257
258 pContext->mState = VISUALIZER_STATE_INITIALIZED;
259
Steve Block3856b092011-10-20 11:56:00 +0100260 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700261
262 return 0;
263
264}
265
Eric Laurente1315cf2011-05-17 19:16:02 -0700266int VisualizerLib_Release(effect_handle_t handle) {
267 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700268
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700270 if (pContext == NULL) {
271 return -EINVAL;
272 }
273 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
274 delete pContext;
275
276 return 0;
277}
278
Glenn Kasten5e92a782012-01-30 07:40:52 -0800279int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700280 effect_descriptor_t *pDescriptor) {
281
282 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100283 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700284 return -EINVAL;
285 }
286
287 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800288 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700289 return 0;
290 }
291
292 return -EINVAL;
293} /* end VisualizerLib_GetDescriptor */
294
Eric Laurentda7581b2010-07-02 08:12:41 -0700295//
296//--- Effect Control Interface Implementation
297//
298
299static inline int16_t clamp16(int32_t sample)
300{
301 if ((sample>>15) ^ (sample>>31))
302 sample = 0x7FFF ^ (sample>>31);
303 return sample;
304}
305
Eric Laurente1315cf2011-05-17 19:16:02 -0700306int Visualizer_process(
307 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700308{
Eric Laurente1315cf2011-05-17 19:16:02 -0700309 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700310
311 if (pContext == NULL) {
312 return -EINVAL;
313 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700314
315 if (inBuffer == NULL || inBuffer->raw == NULL ||
316 outBuffer == NULL || outBuffer->raw == NULL ||
317 inBuffer->frameCount != outBuffer->frameCount ||
318 inBuffer->frameCount == 0) {
319 return -EINVAL;
320 }
321
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700322 // perform measurements if needed
323 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
324 // find the peak and RMS squared for the new buffer
325 uint32_t inIdx;
326 int16_t maxSample = 0;
327 float rmsSqAcc = 0;
328 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
329 if (inBuffer->s16[inIdx] > maxSample) {
330 maxSample = inBuffer->s16[inIdx];
331 } else if (-inBuffer->s16[inIdx] > maxSample) {
332 maxSample = -inBuffer->s16[inIdx];
333 }
334 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
335 }
336 // store the measurement
337 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
338 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
339 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
340 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
341 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
342 pContext->mMeasurementBufferIdx = 0;
343 }
344 }
345
Eric Laurentda7581b2010-07-02 08:12:41 -0700346 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700347 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700348
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700349 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
350 // derive capture scaling factor from peak value in current buffer
351 // this gives more interesting captures for display.
352 shift = 32;
353 int len = inBuffer->frameCount * 2;
354 for (int i = 0; i < len; i++) {
355 int32_t smp = inBuffer->s16[i];
356 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
357 int32_t clz = __builtin_clz(smp);
358 if (shift > clz) shift = clz;
359 }
360 // A maximum amplitude signal will have 17 leading zeros, which we want to
361 // translate to a shift of 8 (for converting 16 bit to 8 bit)
362 shift = 25 - shift;
363 // Never scale by less than 8 to avoid returning unaltered PCM signal.
364 if (shift < 3) {
365 shift = 3;
366 }
367 // add one to combine the division by 2 needed after summing left and right channels below
368 shift++;
369 } else {
370 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
371 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700372 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700373
Eric Laurentda7581b2010-07-02 08:12:41 -0700374 uint32_t captIdx;
375 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700376 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700377 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700378 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700379 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700380 if (captIdx >= CAPTURE_BUF_SIZE) {
381 // wrap around
382 captIdx = 0;
383 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700384 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700385 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700386 buf[captIdx] = ((uint8_t)smp)^0x80;
387 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700388
389 // XXX the following two should really be atomic, though it probably doesn't
390 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700391 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700392 // update last buffer update time stamp
393 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
394 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700395 }
396
397 if (inBuffer->raw != outBuffer->raw) {
398 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
399 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
400 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
401 }
402 } else {
403 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
404 }
405 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700406 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
407 return -ENODATA;
408 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700409 return 0;
410} // end Visualizer_process
411
Eric Laurente1315cf2011-05-17 19:16:02 -0700412int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700413 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700414
Eric Laurente1315cf2011-05-17 19:16:02 -0700415 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700416 int retsize;
417
418 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
419 return -EINVAL;
420 }
421
Steve Block3856b092011-10-20 11:56:00 +0100422// ALOGV("Visualizer_command command %d cmdSize %d",cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700423
424 switch (cmdCode) {
425 case EFFECT_CMD_INIT:
Eric Laurent29b83cb2015-06-19 15:33:57 -0700426 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700427 return -EINVAL;
428 }
429 *(int *) pReplyData = Visualizer_init(pContext);
430 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800431 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700432 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent29b83cb2015-06-19 15:33:57 -0700433 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700434 return -EINVAL;
435 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800436 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700437 (effect_config_t *) pCmdData);
438 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800439 case EFFECT_CMD_GET_CONFIG:
Eric Laurent29b83cb2015-06-19 15:33:57 -0700440 if (pReplyData == NULL || replySize == NULL ||
Eric Laurent3d5188b2011-12-16 15:30:36 -0800441 *replySize != sizeof(effect_config_t)) {
442 return -EINVAL;
443 }
444 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
445 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700446 case EFFECT_CMD_RESET:
447 Visualizer_reset(pContext);
448 break;
449 case EFFECT_CMD_ENABLE:
Eric Laurent29b83cb2015-06-19 15:33:57 -0700450 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700451 return -EINVAL;
452 }
453 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
454 return -ENOSYS;
455 }
456 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700458 *(int *)pReplyData = 0;
459 break;
460 case EFFECT_CMD_DISABLE:
Eric Laurent29b83cb2015-06-19 15:33:57 -0700461 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700462 return -EINVAL;
463 }
464 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
465 return -ENOSYS;
466 }
467 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100468 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700469 *(int *)pReplyData = 0;
470 break;
471 case EFFECT_CMD_GET_PARAM: {
472 if (pCmdData == NULL ||
473 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
Eric Laurent29b83cb2015-06-19 15:33:57 -0700474 pReplyData == NULL || replySize == NULL ||
Eric Laurentda7581b2010-07-02 08:12:41 -0700475 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
476 return -EINVAL;
477 }
478 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
479 effect_param_t *p = (effect_param_t *)pReplyData;
480 p->status = 0;
481 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700482 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700483 p->status = -EINVAL;
484 break;
485 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700486 switch (*(uint32_t *)p->data) {
487 case VISUALIZER_PARAM_CAPTURE_SIZE:
488 ALOGV("get mCaptureSize = %d", pContext->mCaptureSize);
489 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
490 p->vsize = sizeof(uint32_t);
491 *replySize += sizeof(uint32_t);
492 break;
493 case VISUALIZER_PARAM_SCALING_MODE:
494 ALOGV("get mScalingMode = %d", pContext->mScalingMode);
495 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
496 p->vsize = sizeof(uint32_t);
497 *replySize += sizeof(uint32_t);
498 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700499 case VISUALIZER_PARAM_MEASUREMENT_MODE:
500 ALOGV("get mMeasurementMode = %d", pContext->mMeasurementMode);
501 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
502 p->vsize = sizeof(uint32_t);
503 *replySize += sizeof(uint32_t);
504 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700505 default:
506 p->status = -EINVAL;
507 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700508 } break;
509 case EFFECT_CMD_SET_PARAM: {
510 if (pCmdData == NULL ||
511 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
Eric Laurent29b83cb2015-06-19 15:33:57 -0700512 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700513 return -EINVAL;
514 }
515 *(int32_t *)pReplyData = 0;
516 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700517 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700518 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700519 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700520 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700521 switch (*(uint32_t *)p->data) {
522 case VISUALIZER_PARAM_CAPTURE_SIZE:
523 pContext->mCaptureSize = *((uint32_t *)p->data + 1);
524 ALOGV("set mCaptureSize = %d", pContext->mCaptureSize);
525 break;
526 case VISUALIZER_PARAM_SCALING_MODE:
527 pContext->mScalingMode = *((uint32_t *)p->data + 1);
528 ALOGV("set mScalingMode = %d", pContext->mScalingMode);
529 break;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700530 case VISUALIZER_PARAM_LATENCY:
531 pContext->mLatency = *((uint32_t *)p->data + 1);
532 ALOGV("set mLatency = %d", pContext->mLatency);
533 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700534 case VISUALIZER_PARAM_MEASUREMENT_MODE:
535 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
536 ALOGV("set mMeasurementMode = %d", pContext->mMeasurementMode);
537 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700538 default:
539 *(int32_t *)pReplyData = -EINVAL;
540 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700541 } break;
542 case EFFECT_CMD_SET_DEVICE:
543 case EFFECT_CMD_SET_VOLUME:
544 case EFFECT_CMD_SET_AUDIO_MODE:
545 break;
546
547
Eric Laurent29b83cb2015-06-19 15:33:57 -0700548 case VISUALIZER_CMD_CAPTURE: {
549 uint32_t captureSize = pContext->mCaptureSize;
550 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
551 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
552 *replySize, captureSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700553 return -EINVAL;
554 }
555 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700556 int32_t latencyMs = pContext->mLatency;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700557 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
558 latencyMs -= deltaMs;
559 if (latencyMs < 0) {
560 latencyMs = 0;
Eric Laurent3df40a02011-11-10 10:02:18 -0800561 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700562 const uint32_t deltaSmpl = pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700563
564 int32_t capturePoint = pContext->mCaptureIdx - pContext->mCaptureSize - deltaSmpl;
565 int32_t captureSize = pContext->mCaptureSize;
566 if (capturePoint < 0) {
567 int32_t size = -capturePoint;
568 if (size > captureSize) {
569 size = captureSize;
570 }
571 memcpy(pReplyData,
572 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
573 size);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700574 pReplyData = (char *)pReplyData + size;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700575 captureSize -= size;
576 capturePoint = 0;
577 }
578 memcpy(pReplyData,
579 pContext->mCaptureBuf + capturePoint,
580 captureSize);
581
582
583 // if audio framework has stopped playing audio although the effect is still
584 // active we must clear the capture buffer to return silence
585 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
586 (pContext->mBufferUpdateTime.tv_sec != 0)) {
587 if (deltaMs > MAX_STALL_TIME_MS) {
588 ALOGV("capture going to idle");
589 pContext->mBufferUpdateTime.tv_sec = 0;
590 memset(pReplyData, 0x80, pContext->mCaptureSize);
591 }
592 }
593 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700594 } else {
595 memset(pReplyData, 0x80, pContext->mCaptureSize);
596 }
Eric Laurent29b83cb2015-06-19 15:33:57 -0700597 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700598 break;
599
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700600 case VISUALIZER_CMD_MEASURE: {
rago46dc7142016-08-22 17:20:26 -0700601 if (pReplyData == NULL || replySize == NULL ||
602 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
603 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
604 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32, *replySize,
605 sizeof(int32_t) * MEASUREMENT_COUNT);
606 android_errorWriteLog(0x534e4554, "30229821");
607 return -EINVAL;
608 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700609 uint16_t peakU16 = 0;
610 float sumRmsSquared = 0.0f;
611 uint8_t nbValidMeasurements = 0;
612 // reset measurements if last measurement was too long ago (which implies stored
613 // measurements aren't relevant anymore and shouldn't bias the new one)
614 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
615 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700616 ALOGV("Discarding measurements, last measurement is %dms old", delayMs);
617 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700618 pContext->mPastMeasurements[i].mIsValid = false;
619 pContext->mPastMeasurements[i].mPeakU16 = 0;
620 pContext->mPastMeasurements[i].mRmsSquared = 0;
621 }
622 pContext->mMeasurementBufferIdx = 0;
623 } else {
624 // only use actual measurements, otherwise the first RMS measure happening before
625 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
626 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700627 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700628 if (pContext->mPastMeasurements[i].mIsValid) {
629 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
630 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
631 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700632 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700633 nbValidMeasurements++;
634 }
635 }
636 }
637 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
638 int32_t* pIntReplyData = (int32_t*)pReplyData;
639 // convert from I16 sample values to mB and write results
640 if (rms < 0.000016f) {
641 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
642 } else {
643 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
644 }
645 if (peakU16 == 0) {
646 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
647 } else {
648 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
649 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700650 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700651 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
652 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
653 }
654 break;
655
Eric Laurentda7581b2010-07-02 08:12:41 -0700656 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000657 ALOGW("Visualizer_command invalid command %d",cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700658 return -EINVAL;
659 }
660
661 return 0;
662}
663
Eric Laurente1315cf2011-05-17 19:16:02 -0700664/* Effect Control Interface Implementation: get_descriptor */
665int Visualizer_getDescriptor(effect_handle_t self,
666 effect_descriptor_t *pDescriptor)
667{
668 VisualizerContext * pContext = (VisualizerContext *) self;
669
670 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700672 return -EINVAL;
673 }
674
Glenn Kastena189a682012-02-20 12:16:30 -0800675 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700676
677 return 0;
678} /* end Visualizer_getDescriptor */
679
680// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700681const struct effect_interface_s gVisualizerInterface = {
682 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700683 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700684 Visualizer_getDescriptor,
685 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700686};
687
Marco Nelissen655604a2012-10-25 16:05:57 -0700688// This is the only symbol that needs to be exported
689__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700690audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700691 .tag = AUDIO_EFFECT_LIBRARY_TAG,
692 .version = EFFECT_LIBRARY_API_VERSION,
693 .name = "Visualizer Library",
694 .implementor = "The Android Open Source Project",
695 .create_effect = VisualizerLib_Create,
696 .release_effect = VisualizerLib_Release,
697 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700698};
699
700}; // extern "C"