blob: 2d66eefad5c7ac4c1364b78c2da5c75136eee208 [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>
Eric Laurentda7581b2010-07-02 08:12:41 -070027
Eric Laurentda7581b2010-07-02 08:12:41 -070028
Eric Laurente1315cf2011-05-17 19:16:02 -070029extern "C" {
30
31// effect_handle_t interface implementation for visualizer effect
32extern const struct effect_interface_s gVisualizerInterface;
Eric Laurentda7581b2010-07-02 08:12:41 -070033
34// Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
35const effect_descriptor_t gVisualizerDescriptor = {
36 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
37 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
Eric Laurente1315cf2011-05-17 19:16:02 -070038 EFFECT_CONTROL_API_VERSION,
Eric Laurentda7581b2010-07-02 08:12:41 -070039 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
40 0, // TODO
41 1,
42 "Visualizer",
Eric Laurente1315cf2011-05-17 19:16:02 -070043 "The Android Open Source Project",
Eric Laurentda7581b2010-07-02 08:12:41 -070044};
45
46enum visualizer_state_e {
47 VISUALIZER_STATE_UNINITIALIZED,
48 VISUALIZER_STATE_INITIALIZED,
49 VISUALIZER_STATE_ACTIVE,
50};
51
Eric Laurent183dc772012-03-23 15:35:48 -070052// maximum time since last capture buffer update before resetting capture buffer. This means
Eric Laurent3df40a02011-11-10 10:02:18 -080053// that the framework has stopped playing audio and we must start returning silence
Eric Laurent183dc772012-03-23 15:35:48 -070054#define MAX_STALL_TIME_MS 1000
Eric Laurent3df40a02011-11-10 10:02:18 -080055
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070056#define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
57
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070058#define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
59
60// maximum number of buffers for which we keep track of the measurements
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -070061#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070062
63
64struct BufferStats {
65 bool mIsValid;
66 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
67 float mRmsSquared; // the average square of the samples in a buffer
68};
69
Eric Laurentda7581b2010-07-02 08:12:41 -070070struct VisualizerContext {
71 const struct effect_interface_s *mItfe;
72 effect_config_t mConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -070073 uint32_t mCaptureIdx;
74 uint32_t mCaptureSize;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070075 uint32_t mScalingMode;
Eric Laurent3df40a02011-11-10 10:02:18 -080076 uint8_t mState;
Eric Laurent5baf2af2013-09-12 17:37:00 -070077 uint32_t mLastCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070078 uint32_t mLatency;
Eric Laurent183dc772012-03-23 15:35:48 -070079 struct timespec mBufferUpdateTime;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -070080 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070081 // for measurements
82 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
83 uint32_t mMeasurementMode;
84 uint8_t mMeasurementWindowSizeInBuffers;
85 uint8_t mMeasurementBufferIdx;
86 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentda7581b2010-07-02 08:12:41 -070087};
88
Eric Laurentda7581b2010-07-02 08:12:41 -070089//
90//--- Local functions
91//
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070092uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
93 uint32_t deltaMs = 0;
94 if (pContext->mBufferUpdateTime.tv_sec != 0) {
95 struct timespec ts;
96 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
97 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
98 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
99 if (nsec < 0) {
100 --secs;
101 nsec += 1000000000;
102 }
103 deltaMs = secs * 1000 + nsec / 1000000;
104 }
105 }
106 return deltaMs;
107}
108
Eric Laurentda7581b2010-07-02 08:12:41 -0700109
110void Visualizer_reset(VisualizerContext *pContext)
111{
112 pContext->mCaptureIdx = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700113 pContext->mLastCaptureIdx = 0;
Eric Laurent183dc772012-03-23 15:35:48 -0700114 pContext->mBufferUpdateTime.tv_sec = 0;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700115 pContext->mLatency = 0;
116 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
Eric Laurentda7581b2010-07-02 08:12:41 -0700117}
118
119//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800120// Visualizer_setConfig()
Eric Laurentda7581b2010-07-02 08:12:41 -0700121//----------------------------------------------------------------------------
122// Purpose: Set input and output audio configuration.
123//
124// Inputs:
125// pContext: effect engine context
126// pConfig: pointer to effect_config_t structure holding input and output
127// configuration parameters
128//
129// Outputs:
130//
131//----------------------------------------------------------------------------
132
Eric Laurent3d5188b2011-12-16 15:30:36 -0800133int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
Eric Laurentda7581b2010-07-02 08:12:41 -0700134{
Eric Laurent3d5188b2011-12-16 15:30:36 -0800135 ALOGV("Visualizer_setConfig start");
Eric Laurentda7581b2010-07-02 08:12:41 -0700136
137 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
138 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
139 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700140 if (pConfig->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700141 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
142 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
Eric Laurente1315cf2011-05-17 19:16:02 -0700143 if (pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
Eric Laurentda7581b2010-07-02 08:12:41 -0700144
Glenn Kastena189a682012-02-20 12:16:30 -0800145 pContext->mConfig = *pConfig;
Eric Laurentda7581b2010-07-02 08:12:41 -0700146
147 Visualizer_reset(pContext);
148
149 return 0;
150}
151
152
153//----------------------------------------------------------------------------
Eric Laurent3d5188b2011-12-16 15:30:36 -0800154// Visualizer_getConfig()
155//----------------------------------------------------------------------------
156// Purpose: Get input and output audio configuration.
157//
158// Inputs:
159// pContext: effect engine context
160// pConfig: pointer to effect_config_t structure holding input and output
161// configuration parameters
162//
163// Outputs:
164//
165//----------------------------------------------------------------------------
166
167void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
168{
Glenn Kastena189a682012-02-20 12:16:30 -0800169 *pConfig = pContext->mConfig;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800170}
171
172
173//----------------------------------------------------------------------------
Eric Laurentda7581b2010-07-02 08:12:41 -0700174// Visualizer_init()
175//----------------------------------------------------------------------------
176// Purpose: Initialize engine with default configuration.
177//
178// Inputs:
179// pContext: effect engine context
180//
181// Outputs:
182//
183//----------------------------------------------------------------------------
184
185int Visualizer_init(VisualizerContext *pContext)
186{
187 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
Eric Laurente1315cf2011-05-17 19:16:02 -0700188 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
189 pContext->mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700190 pContext->mConfig.inputCfg.samplingRate = 44100;
191 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
192 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
193 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
194 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
195 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
Eric Laurente1315cf2011-05-17 19:16:02 -0700196 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
197 pContext->mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentda7581b2010-07-02 08:12:41 -0700198 pContext->mConfig.outputCfg.samplingRate = 44100;
199 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
200 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
201 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
202 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
203
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700204 // visualization initialization
Eric Laurentda7581b2010-07-02 08:12:41 -0700205 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700206 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
Eric Laurentda7581b2010-07-02 08:12:41 -0700207
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700208 // measurement initialization
209 pContext->mChannelCount = popcount(pContext->mConfig.inputCfg.channels);
210 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
211 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
212 pContext->mMeasurementBufferIdx = 0;
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700213 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700214 pContext->mPastMeasurements[i].mIsValid = false;
215 pContext->mPastMeasurements[i].mPeakU16 = 0;
216 pContext->mPastMeasurements[i].mRmsSquared = 0;
217 }
218
Eric Laurent3d5188b2011-12-16 15:30:36 -0800219 Visualizer_setConfig(pContext, &pContext->mConfig);
Eric Laurentda7581b2010-07-02 08:12:41 -0700220
221 return 0;
222}
223
224//
225//--- Effect Library Interface Implementation
226//
227
Glenn Kasten5e92a782012-01-30 07:40:52 -0800228int VisualizerLib_Create(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700229 int32_t sessionId,
230 int32_t ioId,
231 effect_handle_t *pHandle) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700232 int ret;
233 int i;
234
Eric Laurente1315cf2011-05-17 19:16:02 -0700235 if (pHandle == NULL || uuid == NULL) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700236 return -EINVAL;
237 }
238
239 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
240 return -EINVAL;
241 }
242
243 VisualizerContext *pContext = new VisualizerContext;
244
245 pContext->mItfe = &gVisualizerInterface;
246 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
247
248 ret = Visualizer_init(pContext);
249 if (ret < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000250 ALOGW("VisualizerLib_Create() init failed");
Eric Laurentda7581b2010-07-02 08:12:41 -0700251 delete pContext;
252 return ret;
253 }
254
Eric Laurente1315cf2011-05-17 19:16:02 -0700255 *pHandle = (effect_handle_t)pContext;
Eric Laurentda7581b2010-07-02 08:12:41 -0700256
257 pContext->mState = VISUALIZER_STATE_INITIALIZED;
258
Steve Block3856b092011-10-20 11:56:00 +0100259 ALOGV("VisualizerLib_Create %p", pContext);
Eric Laurentda7581b2010-07-02 08:12:41 -0700260
261 return 0;
262
263}
264
Eric Laurente1315cf2011-05-17 19:16:02 -0700265int VisualizerLib_Release(effect_handle_t handle) {
266 VisualizerContext * pContext = (VisualizerContext *)handle;
Eric Laurentda7581b2010-07-02 08:12:41 -0700267
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("VisualizerLib_Release %p", handle);
Eric Laurentda7581b2010-07-02 08:12:41 -0700269 if (pContext == NULL) {
270 return -EINVAL;
271 }
272 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
273 delete pContext;
274
275 return 0;
276}
277
Glenn Kasten5e92a782012-01-30 07:40:52 -0800278int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
Eric Laurente1315cf2011-05-17 19:16:02 -0700279 effect_descriptor_t *pDescriptor) {
280
281 if (pDescriptor == NULL || uuid == NULL){
Steve Block3856b092011-10-20 11:56:00 +0100282 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
Eric Laurente1315cf2011-05-17 19:16:02 -0700283 return -EINVAL;
284 }
285
286 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastena189a682012-02-20 12:16:30 -0800287 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700288 return 0;
289 }
290
291 return -EINVAL;
292} /* end VisualizerLib_GetDescriptor */
293
Eric Laurentda7581b2010-07-02 08:12:41 -0700294//
295//--- Effect Control Interface Implementation
296//
297
298static inline int16_t clamp16(int32_t sample)
299{
300 if ((sample>>15) ^ (sample>>31))
301 sample = 0x7FFF ^ (sample>>31);
302 return sample;
303}
304
Eric Laurente1315cf2011-05-17 19:16:02 -0700305int Visualizer_process(
306 effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
Eric Laurentda7581b2010-07-02 08:12:41 -0700307{
Eric Laurente1315cf2011-05-17 19:16:02 -0700308 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700309
310 if (pContext == NULL) {
311 return -EINVAL;
312 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700313
314 if (inBuffer == NULL || inBuffer->raw == NULL ||
315 outBuffer == NULL || outBuffer->raw == NULL ||
316 inBuffer->frameCount != outBuffer->frameCount ||
317 inBuffer->frameCount == 0) {
318 return -EINVAL;
319 }
320
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700321 // perform measurements if needed
322 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
323 // find the peak and RMS squared for the new buffer
324 uint32_t inIdx;
325 int16_t maxSample = 0;
326 float rmsSqAcc = 0;
327 for (inIdx = 0 ; inIdx < inBuffer->frameCount * pContext->mChannelCount ; inIdx++) {
328 if (inBuffer->s16[inIdx] > maxSample) {
329 maxSample = inBuffer->s16[inIdx];
330 } else if (-inBuffer->s16[inIdx] > maxSample) {
331 maxSample = -inBuffer->s16[inIdx];
332 }
333 rmsSqAcc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
334 }
335 // store the measurement
336 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
337 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
338 rmsSqAcc / (inBuffer->frameCount * pContext->mChannelCount);
339 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
340 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
341 pContext->mMeasurementBufferIdx = 0;
342 }
343 }
344
Eric Laurentda7581b2010-07-02 08:12:41 -0700345 // all code below assumes stereo 16 bit PCM output and input
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700346 int32_t shift;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700347
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700348 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
349 // derive capture scaling factor from peak value in current buffer
350 // this gives more interesting captures for display.
351 shift = 32;
352 int len = inBuffer->frameCount * 2;
353 for (int i = 0; i < len; i++) {
354 int32_t smp = inBuffer->s16[i];
355 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
356 int32_t clz = __builtin_clz(smp);
357 if (shift > clz) shift = clz;
358 }
359 // A maximum amplitude signal will have 17 leading zeros, which we want to
360 // translate to a shift of 8 (for converting 16 bit to 8 bit)
361 shift = 25 - shift;
362 // Never scale by less than 8 to avoid returning unaltered PCM signal.
363 if (shift < 3) {
364 shift = 3;
365 }
366 // add one to combine the division by 2 needed after summing left and right channels below
367 shift++;
368 } else {
369 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
370 shift = 9;
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700371 }
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700372
Eric Laurentda7581b2010-07-02 08:12:41 -0700373 uint32_t captIdx;
374 uint32_t inIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700375 uint8_t *buf = pContext->mCaptureBuf;
Eric Laurentda7581b2010-07-02 08:12:41 -0700376 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700377 inIdx < inBuffer->frameCount;
Eric Laurentda7581b2010-07-02 08:12:41 -0700378 inIdx++, captIdx++) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700379 if (captIdx >= CAPTURE_BUF_SIZE) {
380 // wrap around
381 captIdx = 0;
382 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700383 int32_t smp = inBuffer->s16[2 * inIdx] + inBuffer->s16[2 * inIdx + 1];
Marco Nelissen64c3bde2010-10-27 09:06:01 -0700384 smp = smp >> shift;
Eric Laurentda7581b2010-07-02 08:12:41 -0700385 buf[captIdx] = ((uint8_t)smp)^0x80;
386 }
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700387
388 // XXX the following two should really be atomic, though it probably doesn't
389 // matter much for visualization purposes
Eric Laurentda7581b2010-07-02 08:12:41 -0700390 pContext->mCaptureIdx = captIdx;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700391 // update last buffer update time stamp
392 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
393 pContext->mBufferUpdateTime.tv_sec = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -0700394 }
395
396 if (inBuffer->raw != outBuffer->raw) {
397 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
398 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
399 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
400 }
401 } else {
402 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
403 }
404 }
Eric Laurentf997cab2010-07-19 06:24:46 -0700405 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
406 return -ENODATA;
407 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700408 return 0;
409} // end Visualizer_process
410
Eric Laurente1315cf2011-05-17 19:16:02 -0700411int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
Eric Laurent25f43952010-07-28 05:40:18 -0700412 void *pCmdData, uint32_t *replySize, void *pReplyData) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700413
Eric Laurente1315cf2011-05-17 19:16:02 -0700414 VisualizerContext * pContext = (VisualizerContext *)self;
Eric Laurentda7581b2010-07-02 08:12:41 -0700415 int retsize;
416
417 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
418 return -EINVAL;
419 }
420
Steve Block3856b092011-10-20 11:56:00 +0100421// ALOGV("Visualizer_command command %d cmdSize %d",cmdCode, cmdSize);
Eric Laurentda7581b2010-07-02 08:12:41 -0700422
423 switch (cmdCode) {
424 case EFFECT_CMD_INIT:
425 if (pReplyData == NULL || *replySize != sizeof(int)) {
426 return -EINVAL;
427 }
428 *(int *) pReplyData = Visualizer_init(pContext);
429 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800430 case EFFECT_CMD_SET_CONFIG:
Eric Laurentda7581b2010-07-02 08:12:41 -0700431 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
432 || pReplyData == NULL || *replySize != sizeof(int)) {
433 return -EINVAL;
434 }
Eric Laurent3d5188b2011-12-16 15:30:36 -0800435 *(int *) pReplyData = Visualizer_setConfig(pContext,
Eric Laurentda7581b2010-07-02 08:12:41 -0700436 (effect_config_t *) pCmdData);
437 break;
Eric Laurent3d5188b2011-12-16 15:30:36 -0800438 case EFFECT_CMD_GET_CONFIG:
439 if (pReplyData == NULL ||
440 *replySize != sizeof(effect_config_t)) {
441 return -EINVAL;
442 }
443 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
444 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700445 case EFFECT_CMD_RESET:
446 Visualizer_reset(pContext);
447 break;
448 case EFFECT_CMD_ENABLE:
449 if (pReplyData == NULL || *replySize != sizeof(int)) {
450 return -EINVAL;
451 }
452 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
453 return -ENOSYS;
454 }
455 pContext->mState = VISUALIZER_STATE_ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +0100456 ALOGV("EFFECT_CMD_ENABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700457 *(int *)pReplyData = 0;
458 break;
459 case EFFECT_CMD_DISABLE:
460 if (pReplyData == NULL || *replySize != sizeof(int)) {
461 return -EINVAL;
462 }
463 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
464 return -ENOSYS;
465 }
466 pContext->mState = VISUALIZER_STATE_INITIALIZED;
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("EFFECT_CMD_DISABLE() OK");
Eric Laurentda7581b2010-07-02 08:12:41 -0700468 *(int *)pReplyData = 0;
469 break;
470 case EFFECT_CMD_GET_PARAM: {
471 if (pCmdData == NULL ||
472 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
473 pReplyData == NULL ||
474 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
475 return -EINVAL;
476 }
477 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
478 effect_param_t *p = (effect_param_t *)pReplyData;
479 p->status = 0;
480 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700481 if (p->psize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700482 p->status = -EINVAL;
483 break;
484 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700485 switch (*(uint32_t *)p->data) {
486 case VISUALIZER_PARAM_CAPTURE_SIZE:
487 ALOGV("get mCaptureSize = %d", pContext->mCaptureSize);
488 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
489 p->vsize = sizeof(uint32_t);
490 *replySize += sizeof(uint32_t);
491 break;
492 case VISUALIZER_PARAM_SCALING_MODE:
493 ALOGV("get mScalingMode = %d", pContext->mScalingMode);
494 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
495 p->vsize = sizeof(uint32_t);
496 *replySize += sizeof(uint32_t);
497 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700498 case VISUALIZER_PARAM_MEASUREMENT_MODE:
499 ALOGV("get mMeasurementMode = %d", pContext->mMeasurementMode);
500 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
501 p->vsize = sizeof(uint32_t);
502 *replySize += sizeof(uint32_t);
503 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700504 default:
505 p->status = -EINVAL;
506 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700507 } break;
508 case EFFECT_CMD_SET_PARAM: {
509 if (pCmdData == NULL ||
510 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
511 pReplyData == NULL || *replySize != sizeof(int32_t)) {
512 return -EINVAL;
513 }
514 *(int32_t *)pReplyData = 0;
515 effect_param_t *p = (effect_param_t *)pCmdData;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700516 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700517 *(int32_t *)pReplyData = -EINVAL;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700518 break;
Eric Laurentda7581b2010-07-02 08:12:41 -0700519 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700520 switch (*(uint32_t *)p->data) {
521 case VISUALIZER_PARAM_CAPTURE_SIZE:
522 pContext->mCaptureSize = *((uint32_t *)p->data + 1);
523 ALOGV("set mCaptureSize = %d", pContext->mCaptureSize);
524 break;
525 case VISUALIZER_PARAM_SCALING_MODE:
526 pContext->mScalingMode = *((uint32_t *)p->data + 1);
527 ALOGV("set mScalingMode = %d", pContext->mScalingMode);
528 break;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700529 case VISUALIZER_PARAM_LATENCY:
530 pContext->mLatency = *((uint32_t *)p->data + 1);
531 ALOGV("set mLatency = %d", pContext->mLatency);
532 break;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700533 case VISUALIZER_PARAM_MEASUREMENT_MODE:
534 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
535 ALOGV("set mMeasurementMode = %d", pContext->mMeasurementMode);
536 break;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700537 default:
538 *(int32_t *)pReplyData = -EINVAL;
539 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700540 } break;
541 case EFFECT_CMD_SET_DEVICE:
542 case EFFECT_CMD_SET_VOLUME:
543 case EFFECT_CMD_SET_AUDIO_MODE:
544 break;
545
546
Eric Laurent6d8b6942011-06-24 07:01:31 -0700547 case VISUALIZER_CMD_CAPTURE:
Eric Laurent0e75f0f2010-09-21 14:52:01 -0700548 if (pReplyData == NULL || *replySize != pContext->mCaptureSize) {
Steve Block3856b092011-10-20 11:56:00 +0100549 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %d pContext->mCaptureSize %d",
Eric Laurentda7581b2010-07-02 08:12:41 -0700550 *replySize, pContext->mCaptureSize);
551 return -EINVAL;
552 }
553 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700554 int32_t latencyMs = pContext->mLatency;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700555 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
556 latencyMs -= deltaMs;
557 if (latencyMs < 0) {
558 latencyMs = 0;
Eric Laurent3df40a02011-11-10 10:02:18 -0800559 }
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700560 const uint32_t deltaSmpl = pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700561
562 int32_t capturePoint = pContext->mCaptureIdx - pContext->mCaptureSize - deltaSmpl;
563 int32_t captureSize = pContext->mCaptureSize;
564 if (capturePoint < 0) {
565 int32_t size = -capturePoint;
566 if (size > captureSize) {
567 size = captureSize;
568 }
569 memcpy(pReplyData,
570 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
571 size);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700572 pReplyData = (char *)pReplyData + size;
Marco Nelissenf06c2ed2012-06-06 09:52:31 -0700573 captureSize -= size;
574 capturePoint = 0;
575 }
576 memcpy(pReplyData,
577 pContext->mCaptureBuf + capturePoint,
578 captureSize);
579
580
581 // if audio framework has stopped playing audio although the effect is still
582 // active we must clear the capture buffer to return silence
583 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
584 (pContext->mBufferUpdateTime.tv_sec != 0)) {
585 if (deltaMs > MAX_STALL_TIME_MS) {
586 ALOGV("capture going to idle");
587 pContext->mBufferUpdateTime.tv_sec = 0;
588 memset(pReplyData, 0x80, pContext->mCaptureSize);
589 }
590 }
591 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
Eric Laurentda7581b2010-07-02 08:12:41 -0700592 } else {
593 memset(pReplyData, 0x80, pContext->mCaptureSize);
594 }
Eric Laurent3df40a02011-11-10 10:02:18 -0800595
Eric Laurentda7581b2010-07-02 08:12:41 -0700596 break;
597
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700598 case VISUALIZER_CMD_MEASURE: {
599 uint16_t peakU16 = 0;
600 float sumRmsSquared = 0.0f;
601 uint8_t nbValidMeasurements = 0;
602 // reset measurements if last measurement was too long ago (which implies stored
603 // measurements aren't relevant anymore and shouldn't bias the new one)
604 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
605 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700606 ALOGV("Discarding measurements, last measurement is %dms old", delayMs);
607 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700608 pContext->mPastMeasurements[i].mIsValid = false;
609 pContext->mPastMeasurements[i].mPeakU16 = 0;
610 pContext->mPastMeasurements[i].mRmsSquared = 0;
611 }
612 pContext->mMeasurementBufferIdx = 0;
613 } else {
614 // only use actual measurements, otherwise the first RMS measure happening before
615 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
616 // low
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700617 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700618 if (pContext->mPastMeasurements[i].mIsValid) {
619 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
620 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
621 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700622 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700623 nbValidMeasurements++;
624 }
625 }
626 }
627 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
628 int32_t* pIntReplyData = (int32_t*)pReplyData;
629 // convert from I16 sample values to mB and write results
630 if (rms < 0.000016f) {
631 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
632 } else {
633 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
634 }
635 if (peakU16 == 0) {
636 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
637 } else {
638 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
639 }
Jean-Michel Trivi6fbc9ef2013-09-24 15:31:13 -0700640 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700641 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
642 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
643 }
644 break;
645
Eric Laurentda7581b2010-07-02 08:12:41 -0700646 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000647 ALOGW("Visualizer_command invalid command %d",cmdCode);
Eric Laurentda7581b2010-07-02 08:12:41 -0700648 return -EINVAL;
649 }
650
651 return 0;
652}
653
Eric Laurente1315cf2011-05-17 19:16:02 -0700654/* Effect Control Interface Implementation: get_descriptor */
655int Visualizer_getDescriptor(effect_handle_t self,
656 effect_descriptor_t *pDescriptor)
657{
658 VisualizerContext * pContext = (VisualizerContext *) self;
659
660 if (pContext == NULL || pDescriptor == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("Visualizer_getDescriptor() invalid param");
Eric Laurente1315cf2011-05-17 19:16:02 -0700662 return -EINVAL;
663 }
664
Glenn Kastena189a682012-02-20 12:16:30 -0800665 *pDescriptor = gVisualizerDescriptor;
Eric Laurente1315cf2011-05-17 19:16:02 -0700666
667 return 0;
668} /* end Visualizer_getDescriptor */
669
670// effect_handle_t interface implementation for visualizer effect
Eric Laurentda7581b2010-07-02 08:12:41 -0700671const struct effect_interface_s gVisualizerInterface = {
672 Visualizer_process,
Eric Laurente1315cf2011-05-17 19:16:02 -0700673 Visualizer_command,
Eric Laurentba7b8f82011-06-17 18:54:16 -0700674 Visualizer_getDescriptor,
675 NULL,
Eric Laurentda7581b2010-07-02 08:12:41 -0700676};
677
Marco Nelissen7f16b192012-10-25 16:05:57 -0700678// This is the only symbol that needs to be exported
679__attribute__ ((visibility ("default")))
Eric Laurente1315cf2011-05-17 19:16:02 -0700680audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -0700681 .tag = AUDIO_EFFECT_LIBRARY_TAG,
682 .version = EFFECT_LIBRARY_API_VERSION,
683 .name = "Visualizer Library",
684 .implementor = "The Android Open Source Project",
685 .create_effect = VisualizerLib_Create,
686 .release_effect = VisualizerLib_Release,
687 .get_descriptor = VisualizerLib_GetDescriptor,
Eric Laurente1315cf2011-05-17 19:16:02 -0700688};
689
690}; // extern "C"