1 /*
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
17 #define LOG_TAG "EffectVisualizer"
18 //#define LOG_NDEBUG 0
19
20 #include <assert.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include <algorithm> // max
28 #include <new>
29
30 #include <log/log.h>
31
32 #include <audio_effects/effect_visualizer.h>
33 #include <audio_utils/primitives.h>
34
35 #ifdef BUILD_FLOAT
36
37 static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_FLOAT;
38
39 #else
40
41 static constexpr audio_format_t kProcessFormat = AUDIO_FORMAT_PCM_16_BIT;
42
43 #endif // BUILD_FLOAT
44
45 extern "C" {
46
47 // effect_handle_t interface implementation for visualizer effect
48 extern const struct effect_interface_s gVisualizerInterface;
49
50 // Google Visualizer UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
51 const effect_descriptor_t gVisualizerDescriptor = {
52 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
53 {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
54 EFFECT_CONTROL_API_VERSION,
55 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST),
56 0, // TODO
57 1,
58 "Visualizer",
59 "The Android Open Source Project",
60 };
61
62 enum visualizer_state_e {
63 VISUALIZER_STATE_UNINITIALIZED,
64 VISUALIZER_STATE_INITIALIZED,
65 VISUALIZER_STATE_ACTIVE,
66 };
67
68 // maximum time since last capture buffer update before resetting capture buffer. This means
69 // that the framework has stopped playing audio and we must start returning silence
70 #define MAX_STALL_TIME_MS 1000
71
72 #define CAPTURE_BUF_SIZE 65536 // "64k should be enough for everyone"
73
74 #define DISCARD_MEASUREMENTS_TIME_MS 2000 // discard measurements older than this number of ms
75
76 #define MAX_LATENCY_MS 3000 // 3 seconds of latency for audio pipeline
77
78 // maximum number of buffers for which we keep track of the measurements
79 #define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 // note: buffer index is stored in uint8_t
80
81
82 struct BufferStats {
83 bool mIsValid;
84 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
85 float mRmsSquared; // the average square of the samples in a buffer
86 };
87
88 struct VisualizerContext {
89 const struct effect_interface_s *mItfe;
90 effect_config_t mConfig;
91 uint32_t mCaptureIdx;
92 uint32_t mCaptureSize;
93 uint32_t mScalingMode;
94 uint8_t mState;
95 uint32_t mLastCaptureIdx;
96 uint32_t mLatency;
97 struct timespec mBufferUpdateTime;
98 uint8_t mCaptureBuf[CAPTURE_BUF_SIZE];
99 // for measurements
100 uint8_t mChannelCount; // to avoid recomputing it every time a buffer is processed
101 uint32_t mMeasurementMode;
102 uint8_t mMeasurementWindowSizeInBuffers;
103 uint8_t mMeasurementBufferIdx;
104 BufferStats mPastMeasurements[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
105 };
106
107 //
108 //--- Local functions
109 //
Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext * pContext)110 uint32_t Visualizer_getDeltaTimeMsFromUpdatedTime(VisualizerContext* pContext) {
111 uint32_t deltaMs = 0;
112 if (pContext->mBufferUpdateTime.tv_sec != 0) {
113 struct timespec ts;
114 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
115 time_t secs = ts.tv_sec - pContext->mBufferUpdateTime.tv_sec;
116 long nsec = ts.tv_nsec - pContext->mBufferUpdateTime.tv_nsec;
117 if (nsec < 0) {
118 --secs;
119 nsec += 1000000000;
120 }
121 deltaMs = secs * 1000 + nsec / 1000000;
122 }
123 }
124 return deltaMs;
125 }
126
127
Visualizer_reset(VisualizerContext * pContext)128 void Visualizer_reset(VisualizerContext *pContext)
129 {
130 pContext->mCaptureIdx = 0;
131 pContext->mLastCaptureIdx = 0;
132 pContext->mBufferUpdateTime.tv_sec = 0;
133 pContext->mLatency = 0;
134 memset(pContext->mCaptureBuf, 0x80, CAPTURE_BUF_SIZE);
135 }
136
137 //----------------------------------------------------------------------------
138 // Visualizer_setConfig()
139 //----------------------------------------------------------------------------
140 // Purpose: Set input and output audio configuration.
141 //
142 // Inputs:
143 // pContext: effect engine context
144 // pConfig: pointer to effect_config_t structure holding input and output
145 // configuration parameters
146 //
147 // Outputs:
148 //
149 //----------------------------------------------------------------------------
150
Visualizer_setConfig(VisualizerContext * pContext,effect_config_t * pConfig)151 int Visualizer_setConfig(VisualizerContext *pContext, effect_config_t *pConfig)
152 {
153 ALOGV("Visualizer_setConfig start");
154
155 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate) return -EINVAL;
156 if (pConfig->inputCfg.channels != pConfig->outputCfg.channels) return -EINVAL;
157 if (pConfig->inputCfg.format != pConfig->outputCfg.format) return -EINVAL;
158 const uint32_t channelCount = audio_channel_count_from_out_mask(pConfig->inputCfg.channels);
159 #ifdef SUPPORT_MC
160 if (channelCount < 1 || channelCount > FCC_8) return -EINVAL;
161 #else
162 if (channelCount != FCC_2) return -EINVAL;
163 #endif
164 if (pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
165 pConfig->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
166 if (pConfig->inputCfg.format != kProcessFormat) return -EINVAL;
167
168 pContext->mConfig = *pConfig;
169
170 Visualizer_reset(pContext);
171
172 return 0;
173 }
174
175
176 //----------------------------------------------------------------------------
177 // Visualizer_getConfig()
178 //----------------------------------------------------------------------------
179 // Purpose: Get input and output audio configuration.
180 //
181 // Inputs:
182 // pContext: effect engine context
183 // pConfig: pointer to effect_config_t structure holding input and output
184 // configuration parameters
185 //
186 // Outputs:
187 //
188 //----------------------------------------------------------------------------
189
Visualizer_getConfig(VisualizerContext * pContext,effect_config_t * pConfig)190 void Visualizer_getConfig(VisualizerContext *pContext, effect_config_t *pConfig)
191 {
192 *pConfig = pContext->mConfig;
193 }
194
195
196 //----------------------------------------------------------------------------
197 // Visualizer_init()
198 //----------------------------------------------------------------------------
199 // Purpose: Initialize engine with default configuration.
200 //
201 // Inputs:
202 // pContext: effect engine context
203 //
204 // Outputs:
205 //
206 //----------------------------------------------------------------------------
207
Visualizer_init(VisualizerContext * pContext)208 int Visualizer_init(VisualizerContext *pContext)
209 {
210 pContext->mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
211 pContext->mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
212 pContext->mConfig.inputCfg.format = kProcessFormat;
213 pContext->mConfig.inputCfg.samplingRate = 44100;
214 pContext->mConfig.inputCfg.bufferProvider.getBuffer = NULL;
215 pContext->mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
216 pContext->mConfig.inputCfg.bufferProvider.cookie = NULL;
217 pContext->mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
218 pContext->mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
219 pContext->mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
220 pContext->mConfig.outputCfg.format = kProcessFormat;
221 pContext->mConfig.outputCfg.samplingRate = 44100;
222 pContext->mConfig.outputCfg.bufferProvider.getBuffer = NULL;
223 pContext->mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
224 pContext->mConfig.outputCfg.bufferProvider.cookie = NULL;
225 pContext->mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
226
227 // visualization initialization
228 pContext->mCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
229 pContext->mScalingMode = VISUALIZER_SCALING_MODE_NORMALIZED;
230
231 // measurement initialization
232 pContext->mChannelCount =
233 audio_channel_count_from_out_mask(pContext->mConfig.inputCfg.channels);
234 pContext->mMeasurementMode = MEASUREMENT_MODE_NONE;
235 pContext->mMeasurementWindowSizeInBuffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
236 pContext->mMeasurementBufferIdx = 0;
237 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
238 pContext->mPastMeasurements[i].mIsValid = false;
239 pContext->mPastMeasurements[i].mPeakU16 = 0;
240 pContext->mPastMeasurements[i].mRmsSquared = 0;
241 }
242
243 Visualizer_setConfig(pContext, &pContext->mConfig);
244
245 return 0;
246 }
247
248 //
249 //--- Effect Library Interface Implementation
250 //
251
VisualizerLib_Create(const effect_uuid_t * uuid,int32_t,int32_t,effect_handle_t * pHandle)252 int VisualizerLib_Create(const effect_uuid_t *uuid,
253 int32_t /*sessionId*/,
254 int32_t /*ioId*/,
255 effect_handle_t *pHandle) {
256 int ret;
257
258 if (pHandle == NULL || uuid == NULL) {
259 return -EINVAL;
260 }
261
262 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
263 return -EINVAL;
264 }
265
266 VisualizerContext *pContext = new VisualizerContext;
267
268 pContext->mItfe = &gVisualizerInterface;
269 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
270
271 ret = Visualizer_init(pContext);
272 if (ret < 0) {
273 ALOGW("VisualizerLib_Create() init failed");
274 delete pContext;
275 return ret;
276 }
277
278 *pHandle = (effect_handle_t)pContext;
279
280 pContext->mState = VISUALIZER_STATE_INITIALIZED;
281
282 ALOGV("VisualizerLib_Create %p", pContext);
283
284 return 0;
285
286 }
287
VisualizerLib_Release(effect_handle_t handle)288 int VisualizerLib_Release(effect_handle_t handle) {
289 VisualizerContext * pContext = (VisualizerContext *)handle;
290
291 ALOGV("VisualizerLib_Release %p", handle);
292 if (pContext == NULL) {
293 return -EINVAL;
294 }
295 pContext->mState = VISUALIZER_STATE_UNINITIALIZED;
296 delete pContext;
297
298 return 0;
299 }
300
VisualizerLib_GetDescriptor(const effect_uuid_t * uuid,effect_descriptor_t * pDescriptor)301 int VisualizerLib_GetDescriptor(const effect_uuid_t *uuid,
302 effect_descriptor_t *pDescriptor) {
303
304 if (pDescriptor == NULL || uuid == NULL){
305 ALOGV("VisualizerLib_GetDescriptor() called with NULL pointer");
306 return -EINVAL;
307 }
308
309 if (memcmp(uuid, &gVisualizerDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
310 *pDescriptor = gVisualizerDescriptor;
311 return 0;
312 }
313
314 return -EINVAL;
315 } /* end VisualizerLib_GetDescriptor */
316
317 //
318 //--- Effect Control Interface Implementation
319 //
320
Visualizer_process(effect_handle_t self,audio_buffer_t * inBuffer,audio_buffer_t * outBuffer)321 int Visualizer_process(
322 effect_handle_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
323 {
324 VisualizerContext * pContext = (VisualizerContext *)self;
325
326 if (pContext == NULL) {
327 return -EINVAL;
328 }
329
330 if (inBuffer == NULL || inBuffer->raw == NULL ||
331 outBuffer == NULL || outBuffer->raw == NULL ||
332 inBuffer->frameCount != outBuffer->frameCount ||
333 inBuffer->frameCount == 0) {
334 return -EINVAL;
335 }
336
337 const size_t sampleLen = inBuffer->frameCount * pContext->mChannelCount;
338
339 // perform measurements if needed
340 if (pContext->mMeasurementMode & MEASUREMENT_MODE_PEAK_RMS) {
341 // find the peak and RMS squared for the new buffer
342 float rmsSqAcc = 0;
343
344 #ifdef BUILD_FLOAT
345 float maxSample = 0.f;
346 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
347 maxSample = fmax(maxSample, fabs(inBuffer->f32[inIdx]));
348 rmsSqAcc += inBuffer->f32[inIdx] * inBuffer->f32[inIdx];
349 }
350 maxSample *= 1 << 15; // scale to int16_t, with exactly 1 << 15 representing positive num.
351 rmsSqAcc *= 1 << 30; // scale to int16_t * 2
352 #else
353 int maxSample = 0;
354 for (size_t inIdx = 0; inIdx < sampleLen; ++inIdx) {
355 maxSample = std::max(maxSample, std::abs(int32_t(inBuffer->s16[inIdx])));
356 rmsSqAcc += inBuffer->s16[inIdx] * inBuffer->s16[inIdx];
357 }
358 #endif
359 // store the measurement
360 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mPeakU16 = (uint16_t)maxSample;
361 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mRmsSquared =
362 rmsSqAcc / sampleLen;
363 pContext->mPastMeasurements[pContext->mMeasurementBufferIdx].mIsValid = true;
364 if (++pContext->mMeasurementBufferIdx >= pContext->mMeasurementWindowSizeInBuffers) {
365 pContext->mMeasurementBufferIdx = 0;
366 }
367 }
368
369 #ifdef BUILD_FLOAT
370 float fscale; // multiplicative scale
371 #else
372 int32_t shift;
373 #endif // BUILD_FLOAT
374
375 if (pContext->mScalingMode == VISUALIZER_SCALING_MODE_NORMALIZED) {
376 // derive capture scaling factor from peak value in current buffer
377 // this gives more interesting captures for display.
378
379 #ifdef BUILD_FLOAT
380 float maxSample = 0.f;
381 for (size_t inIdx = 0; inIdx < sampleLen; ) {
382 // we reconstruct the actual summed value to ensure proper normalization
383 // for multichannel outputs (channels > 2 may often be 0).
384 float smp = 0.f;
385 for (int i = 0; i < pContext->mChannelCount; ++i) {
386 smp += inBuffer->f32[inIdx++];
387 }
388 maxSample = fmax(maxSample, fabs(smp));
389 }
390 if (maxSample > 0.f) {
391 fscale = 0.99f / maxSample;
392 int exp; // unused
393 const float significand = frexp(fscale, &exp);
394 if (significand == 0.5f) {
395 fscale *= 255.f / 256.f; // avoid returning unaltered PCM signal
396 }
397 } else {
398 // scale doesn't matter, the values are all 0.
399 fscale = 1.f;
400 }
401 #else
402 int32_t orAccum = 0;
403 for (size_t i = 0; i < sampleLen; ++i) {
404 int32_t smp = inBuffer->s16[i];
405 if (smp < 0) smp = -smp - 1; // take care to keep the max negative in range
406 orAccum |= smp;
407 }
408
409 // A maximum amplitude signal will have 17 leading zeros, which we want to
410 // translate to a shift of 8 (for converting 16 bit to 8 bit)
411 shift = 25 - __builtin_clz(orAccum);
412
413 // Never scale by less than 8 to avoid returning unaltered PCM signal.
414 if (shift < 3) {
415 shift = 3;
416 }
417 // add one to combine the division by 2 needed after summing left and right channels below
418 shift++;
419 #endif // BUILD_FLOAT
420 } else {
421 assert(pContext->mScalingMode == VISUALIZER_SCALING_MODE_AS_PLAYED);
422 #ifdef BUILD_FLOAT
423 // Note: if channels are uncorrelated, 1/sqrt(N) could be used at the risk of clipping.
424 fscale = 1.f / pContext->mChannelCount; // account for summing all the channels together.
425 #else
426 shift = 9;
427 #endif // BUILD_FLOAT
428 }
429
430 uint32_t captIdx;
431 uint32_t inIdx;
432 uint8_t *buf = pContext->mCaptureBuf;
433 for (inIdx = 0, captIdx = pContext->mCaptureIdx;
434 inIdx < sampleLen;
435 captIdx++) {
436 if (captIdx >= CAPTURE_BUF_SIZE) captIdx = 0; // wrap
437
438 #ifdef BUILD_FLOAT
439 float smp = 0.f;
440 for (uint32_t i = 0; i < pContext->mChannelCount; ++i) {
441 smp += inBuffer->f32[inIdx++];
442 }
443 buf[captIdx] = clamp8_from_float(smp * fscale);
444 #else
445 const int32_t smp = (inBuffer->s16[inIdx] + inBuffer->s16[inIdx + 1]) >> shift;
446 inIdx += FCC_2; // integer supports stereo only.
447 buf[captIdx] = ((uint8_t)smp)^0x80;
448 #endif // BUILD_FLOAT
449 }
450
451 // XXX the following two should really be atomic, though it probably doesn't
452 // matter much for visualization purposes
453 pContext->mCaptureIdx = captIdx;
454 // update last buffer update time stamp
455 if (clock_gettime(CLOCK_MONOTONIC, &pContext->mBufferUpdateTime) < 0) {
456 pContext->mBufferUpdateTime.tv_sec = 0;
457 }
458
459 if (inBuffer->raw != outBuffer->raw) {
460 #ifdef BUILD_FLOAT
461 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
462 for (size_t i = 0; i < sampleLen; ++i) {
463 outBuffer->f32[i] += inBuffer->f32[i];
464 }
465 } else {
466 memcpy(outBuffer->raw, inBuffer->raw, sampleLen * sizeof(float));
467 }
468 #else
469 if (pContext->mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
470 for (size_t i = 0; i < outBuffer->frameCount*2; i++) {
471 outBuffer->s16[i] = clamp16(outBuffer->s16[i] + inBuffer->s16[i]);
472 }
473 } else {
474 memcpy(outBuffer->raw, inBuffer->raw, outBuffer->frameCount * 2 * sizeof(int16_t));
475 }
476 #endif // BUILD_FLOAT
477 }
478 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
479 return -ENODATA;
480 }
481 return 0;
482 } // end Visualizer_process
483
Visualizer_command(effect_handle_t self,uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)484 int Visualizer_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
485 void *pCmdData, uint32_t *replySize, void *pReplyData) {
486
487 VisualizerContext * pContext = (VisualizerContext *)self;
488
489 if (pContext == NULL || pContext->mState == VISUALIZER_STATE_UNINITIALIZED) {
490 return -EINVAL;
491 }
492
493 // ALOGV("Visualizer_command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
494
495 switch (cmdCode) {
496 case EFFECT_CMD_INIT:
497 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
498 return -EINVAL;
499 }
500 *(int *) pReplyData = Visualizer_init(pContext);
501 break;
502 case EFFECT_CMD_SET_CONFIG:
503 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
504 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
505 return -EINVAL;
506 }
507 *(int *) pReplyData = Visualizer_setConfig(pContext,
508 (effect_config_t *) pCmdData);
509 break;
510 case EFFECT_CMD_GET_CONFIG:
511 if (pReplyData == NULL || replySize == NULL ||
512 *replySize != sizeof(effect_config_t)) {
513 return -EINVAL;
514 }
515 Visualizer_getConfig(pContext, (effect_config_t *)pReplyData);
516 break;
517 case EFFECT_CMD_RESET:
518 Visualizer_reset(pContext);
519 break;
520 case EFFECT_CMD_ENABLE:
521 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
522 return -EINVAL;
523 }
524 if (pContext->mState != VISUALIZER_STATE_INITIALIZED) {
525 return -ENOSYS;
526 }
527 pContext->mState = VISUALIZER_STATE_ACTIVE;
528 ALOGV("EFFECT_CMD_ENABLE() OK");
529 *(int *)pReplyData = 0;
530 break;
531 case EFFECT_CMD_DISABLE:
532 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
533 return -EINVAL;
534 }
535 if (pContext->mState != VISUALIZER_STATE_ACTIVE) {
536 return -ENOSYS;
537 }
538 pContext->mState = VISUALIZER_STATE_INITIALIZED;
539 ALOGV("EFFECT_CMD_DISABLE() OK");
540 *(int *)pReplyData = 0;
541 break;
542 case EFFECT_CMD_GET_PARAM: {
543 if (pCmdData == NULL ||
544 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
545 pReplyData == NULL || replySize == NULL ||
546 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
547 return -EINVAL;
548 }
549 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
550 effect_param_t *p = (effect_param_t *)pReplyData;
551 p->status = 0;
552 *replySize = sizeof(effect_param_t) + sizeof(uint32_t);
553 if (p->psize != sizeof(uint32_t)) {
554 p->status = -EINVAL;
555 break;
556 }
557 switch (*(uint32_t *)p->data) {
558 case VISUALIZER_PARAM_CAPTURE_SIZE:
559 ALOGV("get mCaptureSize = %" PRIu32, pContext->mCaptureSize);
560 *((uint32_t *)p->data + 1) = pContext->mCaptureSize;
561 p->vsize = sizeof(uint32_t);
562 *replySize += sizeof(uint32_t);
563 break;
564 case VISUALIZER_PARAM_SCALING_MODE:
565 ALOGV("get mScalingMode = %" PRIu32, pContext->mScalingMode);
566 *((uint32_t *)p->data + 1) = pContext->mScalingMode;
567 p->vsize = sizeof(uint32_t);
568 *replySize += sizeof(uint32_t);
569 break;
570 case VISUALIZER_PARAM_MEASUREMENT_MODE:
571 ALOGV("get mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
572 *((uint32_t *)p->data + 1) = pContext->mMeasurementMode;
573 p->vsize = sizeof(uint32_t);
574 *replySize += sizeof(uint32_t);
575 break;
576 default:
577 p->status = -EINVAL;
578 }
579 } break;
580 case EFFECT_CMD_SET_PARAM: {
581 if (pCmdData == NULL ||
582 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
583 pReplyData == NULL || replySize == NULL || *replySize != sizeof(int32_t)) {
584 return -EINVAL;
585 }
586 *(int32_t *)pReplyData = 0;
587 effect_param_t *p = (effect_param_t *)pCmdData;
588 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t)) {
589 *(int32_t *)pReplyData = -EINVAL;
590 break;
591 }
592 switch (*(uint32_t *)p->data) {
593 case VISUALIZER_PARAM_CAPTURE_SIZE: {
594 const uint32_t captureSize = *((uint32_t *)p->data + 1);
595 if (captureSize > VISUALIZER_CAPTURE_SIZE_MAX) {
596 android_errorWriteLog(0x534e4554, "31781965");
597 *(int32_t *)pReplyData = -EINVAL;
598 ALOGW("set mCaptureSize = %u > %u", captureSize, VISUALIZER_CAPTURE_SIZE_MAX);
599 } else {
600 pContext->mCaptureSize = captureSize;
601 ALOGV("set mCaptureSize = %u", captureSize);
602 }
603 } break;
604 case VISUALIZER_PARAM_SCALING_MODE:
605 pContext->mScalingMode = *((uint32_t *)p->data + 1);
606 ALOGV("set mScalingMode = %" PRIu32, pContext->mScalingMode);
607 break;
608 case VISUALIZER_PARAM_LATENCY: {
609 uint32_t latency = *((uint32_t *)p->data + 1);
610 if (latency > MAX_LATENCY_MS) {
611 latency = MAX_LATENCY_MS; // clamp latency b/31781965
612 }
613 pContext->mLatency = latency;
614 ALOGV("set mLatency = %u", latency);
615 } break;
616 case VISUALIZER_PARAM_MEASUREMENT_MODE:
617 pContext->mMeasurementMode = *((uint32_t *)p->data + 1);
618 ALOGV("set mMeasurementMode = %" PRIu32, pContext->mMeasurementMode);
619 break;
620 default:
621 *(int32_t *)pReplyData = -EINVAL;
622 }
623 } break;
624 case EFFECT_CMD_SET_DEVICE:
625 case EFFECT_CMD_SET_VOLUME:
626 case EFFECT_CMD_SET_AUDIO_MODE:
627 break;
628
629
630 case VISUALIZER_CMD_CAPTURE: {
631 uint32_t captureSize = pContext->mCaptureSize;
632 if (pReplyData == NULL || replySize == NULL || *replySize != captureSize) {
633 ALOGV("VISUALIZER_CMD_CAPTURE() error *replySize %" PRIu32 " captureSize %" PRIu32,
634 *replySize, captureSize);
635 return -EINVAL;
636 }
637 if (pContext->mState == VISUALIZER_STATE_ACTIVE) {
638 const uint32_t deltaMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
639
640 // if audio framework has stopped playing audio although the effect is still
641 // active we must clear the capture buffer to return silence
642 if ((pContext->mLastCaptureIdx == pContext->mCaptureIdx) &&
643 (pContext->mBufferUpdateTime.tv_sec != 0) &&
644 (deltaMs > MAX_STALL_TIME_MS)) {
645 ALOGV("capture going to idle");
646 pContext->mBufferUpdateTime.tv_sec = 0;
647 memset(pReplyData, 0x80, captureSize);
648 } else {
649 int32_t latencyMs = pContext->mLatency;
650 latencyMs -= deltaMs;
651 if (latencyMs < 0) {
652 latencyMs = 0;
653 }
654 uint32_t deltaSmpl = captureSize
655 + pContext->mConfig.inputCfg.samplingRate * latencyMs / 1000;
656
657 // large sample rate, latency, or capture size, could cause overflow.
658 // do not offset more than the size of buffer.
659 if (deltaSmpl > CAPTURE_BUF_SIZE) {
660 android_errorWriteLog(0x534e4554, "31781965");
661 deltaSmpl = CAPTURE_BUF_SIZE;
662 }
663
664 int32_t capturePoint;
665 //capturePoint = (int32_t)pContext->mCaptureIdx - deltaSmpl;
666 __builtin_sub_overflow((int32_t)pContext->mCaptureIdx, deltaSmpl, &capturePoint);
667 // a negative capturePoint means we wrap the buffer.
668 if (capturePoint < 0) {
669 uint32_t size = -capturePoint;
670 if (size > captureSize) {
671 size = captureSize;
672 }
673 memcpy(pReplyData,
674 pContext->mCaptureBuf + CAPTURE_BUF_SIZE + capturePoint,
675 size);
676 pReplyData = (char *)pReplyData + size;
677 captureSize -= size;
678 capturePoint = 0;
679 }
680 memcpy(pReplyData,
681 pContext->mCaptureBuf + capturePoint,
682 captureSize);
683 }
684
685 pContext->mLastCaptureIdx = pContext->mCaptureIdx;
686 } else {
687 memset(pReplyData, 0x80, captureSize);
688 }
689
690 } break;
691
692 case VISUALIZER_CMD_MEASURE: {
693 if (pReplyData == NULL || replySize == NULL ||
694 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
695 if (replySize == NULL) {
696 ALOGV("VISUALIZER_CMD_MEASURE() error replySize NULL");
697 } else {
698 ALOGV("VISUALIZER_CMD_MEASURE() error *replySize %" PRIu32
699 " < (sizeof(int32_t) * MEASUREMENT_COUNT) %" PRIu32,
700 *replySize,
701 uint32_t(sizeof(int32_t)) * MEASUREMENT_COUNT);
702 }
703 android_errorWriteLog(0x534e4554, "30229821");
704 return -EINVAL;
705 }
706 uint16_t peakU16 = 0;
707 float sumRmsSquared = 0.0f;
708 uint8_t nbValidMeasurements = 0;
709 // reset measurements if last measurement was too long ago (which implies stored
710 // measurements aren't relevant anymore and shouldn't bias the new one)
711 const int32_t delayMs = Visualizer_getDeltaTimeMsFromUpdatedTime(pContext);
712 if (delayMs > DISCARD_MEASUREMENTS_TIME_MS) {
713 ALOGV("Discarding measurements, last measurement is %" PRId32 "ms old", delayMs);
714 for (uint32_t i=0 ; i<pContext->mMeasurementWindowSizeInBuffers ; i++) {
715 pContext->mPastMeasurements[i].mIsValid = false;
716 pContext->mPastMeasurements[i].mPeakU16 = 0;
717 pContext->mPastMeasurements[i].mRmsSquared = 0;
718 }
719 pContext->mMeasurementBufferIdx = 0;
720 } else {
721 // only use actual measurements, otherwise the first RMS measure happening before
722 // MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
723 // low
724 for (uint32_t i=0 ; i < pContext->mMeasurementWindowSizeInBuffers ; i++) {
725 if (pContext->mPastMeasurements[i].mIsValid) {
726 if (pContext->mPastMeasurements[i].mPeakU16 > peakU16) {
727 peakU16 = pContext->mPastMeasurements[i].mPeakU16;
728 }
729 sumRmsSquared += pContext->mPastMeasurements[i].mRmsSquared;
730 nbValidMeasurements++;
731 }
732 }
733 }
734 float rms = nbValidMeasurements == 0 ? 0.0f : sqrtf(sumRmsSquared / nbValidMeasurements);
735 int32_t* pIntReplyData = (int32_t*)pReplyData;
736 // convert from I16 sample values to mB and write results
737 if (rms < 0.000016f) {
738 pIntReplyData[MEASUREMENT_IDX_RMS] = -9600; //-96dB
739 } else {
740 pIntReplyData[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
741 }
742 if (peakU16 == 0) {
743 pIntReplyData[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
744 } else {
745 pIntReplyData[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peakU16 / 32767.0f));
746 }
747 ALOGV("VISUALIZER_CMD_MEASURE peak=%" PRIu16 " (%" PRId32 "mB), rms=%.1f (%" PRId32 "mB)",
748 peakU16, pIntReplyData[MEASUREMENT_IDX_PEAK],
749 rms, pIntReplyData[MEASUREMENT_IDX_RMS]);
750 }
751 break;
752
753 default:
754 ALOGW("Visualizer_command invalid command %" PRIu32, cmdCode);
755 return -EINVAL;
756 }
757
758 return 0;
759 }
760
761 /* Effect Control Interface Implementation: get_descriptor */
Visualizer_getDescriptor(effect_handle_t self,effect_descriptor_t * pDescriptor)762 int Visualizer_getDescriptor(effect_handle_t self,
763 effect_descriptor_t *pDescriptor)
764 {
765 VisualizerContext * pContext = (VisualizerContext *) self;
766
767 if (pContext == NULL || pDescriptor == NULL) {
768 ALOGV("Visualizer_getDescriptor() invalid param");
769 return -EINVAL;
770 }
771
772 *pDescriptor = gVisualizerDescriptor;
773
774 return 0;
775 } /* end Visualizer_getDescriptor */
776
777 // effect_handle_t interface implementation for visualizer effect
778 const struct effect_interface_s gVisualizerInterface = {
779 Visualizer_process,
780 Visualizer_command,
781 Visualizer_getDescriptor,
782 NULL,
783 };
784
785 // This is the only symbol that needs to be exported
786 __attribute__ ((visibility ("default")))
787 audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
788 .tag = AUDIO_EFFECT_LIBRARY_TAG,
789 .version = EFFECT_LIBRARY_API_VERSION,
790 .name = "Visualizer Library",
791 .implementor = "The Android Open Source Project",
792 .create_effect = VisualizerLib_Create,
793 .release_effect = VisualizerLib_Release,
794 .get_descriptor = VisualizerLib_GetDescriptor,
795 };
796
797 }; // extern "C"
798