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