blob: e519f1319316bf568889c2e03328a9b55691f95d [file] [log] [blame]
Eric Laurentda7581b2010-07-02 08:12:41 -07001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "Visualizer"
21#include <utils/Log.h>
22
23#include <stdint.h>
24#include <sys/types.h>
25#include <limits.h>
26
Dima Zavinfce7a472011-04-19 22:30:36 -070027#include <cutils/bitops.h>
28
Eric Laurentda7581b2010-07-02 08:12:41 -070029#include <media/Visualizer.h>
Glenn Kasten3f6448e2012-01-16 13:11:50 -080030#include <audio_utils/fixedfft.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070031#include <utils/Thread.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070032
33namespace android {
34
35// ---------------------------------------------------------------------------
36
37Visualizer::Visualizer (int32_t priority,
38 effect_callback_t cbf,
39 void* user,
40 int sessionId)
41 : AudioEffect(SL_IID_VISUALIZATION, NULL, priority, cbf, user, sessionId),
42 mCaptureRate(CAPTURE_RATE_DEF),
43 mCaptureSize(CAPTURE_SIZE_DEF),
44 mSampleRate(44100000),
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070045 mScalingMode(VISUALIZER_SCALING_MODE_NORMALIZED),
Eric Laurentda7581b2010-07-02 08:12:41 -070046 mCaptureCallBack(NULL),
47 mCaptureCbkUser(NULL)
48{
49 initCaptureSize();
Eric Laurentda7581b2010-07-02 08:12:41 -070050}
51
52Visualizer::~Visualizer()
53{
Eric Laurentda7581b2010-07-02 08:12:41 -070054}
55
56status_t Visualizer::setEnabled(bool enabled)
57{
Glenn Kastena9b21c52012-01-17 10:06:38 -080058 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070059
60 sp<CaptureThread> t = mCaptureThread;
61 if (t != 0) {
62 if (enabled) {
63 if (t->exitPending()) {
64 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +000065 ALOGE("Visualizer::enable() called from thread");
Eric Laurentda7581b2010-07-02 08:12:41 -070066 return INVALID_OPERATION;
67 }
68 }
69 }
70 t->mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070071 }
Eric Laurentda7581b2010-07-02 08:12:41 -070072
73 status_t status = AudioEffect::setEnabled(enabled);
74
75 if (status == NO_ERROR) {
76 if (t != 0) {
77 if (enabled) {
Glenn Kasten9096f342012-01-19 08:14:08 -080078 t->run("Visualizer");
Eric Laurentda7581b2010-07-02 08:12:41 -070079 } else {
80 t->requestExit();
81 }
82 }
83 }
84
85 if (t != 0) {
86 t->mLock.unlock();
87 }
88
89 return status;
90}
91
Glenn Kasten85ab62c2012-11-01 11:11:38 -070092status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags,
93 uint32_t rate)
Eric Laurentda7581b2010-07-02 08:12:41 -070094{
95 if (rate > CAPTURE_RATE_MAX) {
96 return BAD_VALUE;
97 }
Glenn Kastena9b21c52012-01-17 10:06:38 -080098 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070099
100 if (mEnabled) {
101 return INVALID_OPERATION;
102 }
103
104 sp<CaptureThread> t = mCaptureThread;
105 if (t != 0) {
106 t->mLock.lock();
107 }
108 mCaptureThread.clear();
109 mCaptureCallBack = cbk;
110 mCaptureCbkUser = user;
111 mCaptureFlags = flags;
112 mCaptureRate = rate;
113
114 if (t != 0) {
115 t->mLock.unlock();
116 }
117
118 if (cbk != NULL) {
119 mCaptureThread = new CaptureThread(*this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentda7581b2010-07-02 08:12:41 -0700120 }
Steve Block3856b092011-10-20 11:56:00 +0100121 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700122 rate, mCaptureThread.get(), mCaptureFlags);
123 return NO_ERROR;
124}
125
126status_t Visualizer::setCaptureSize(uint32_t size)
127{
128 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
129 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700130 popcount(size) != 1) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700131 return BAD_VALUE;
132 }
133
Glenn Kastena9b21c52012-01-17 10:06:38 -0800134 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700135 if (mEnabled) {
136 return INVALID_OPERATION;
137 }
138
139 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
140 effect_param_t *p = (effect_param_t *)buf32;
141
142 p->psize = sizeof(uint32_t);
143 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700144 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700145 *((int32_t *)p->data + 1)= size;
146 status_t status = setParameter(p);
147
Steve Block3856b092011-10-20 11:56:00 +0100148 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700149
150 if (status == NO_ERROR) {
151 status = p->status;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700152 if (status == NO_ERROR) {
153 mCaptureSize = size;
154 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700155 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700156
157 return status;
158}
159
160status_t Visualizer::setScalingMode(uint32_t mode) {
161 if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED)
162 && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) {
163 return BAD_VALUE;
164 }
165
166 Mutex::Autolock _l(mCaptureLock);
167
168 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
169 effect_param_t *p = (effect_param_t *)buf32;
170
171 p->psize = sizeof(uint32_t);
172 p->vsize = sizeof(uint32_t);
173 *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE;
174 *((int32_t *)p->data + 1)= mode;
175 status_t status = setParameter(p);
176
177 ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status);
178
Eric Laurentda7581b2010-07-02 08:12:41 -0700179 if (status == NO_ERROR) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700180 status = p->status;
181 if (status == NO_ERROR) {
182 mScalingMode = mode;
183 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700184 }
185
186 return status;
187}
188
189status_t Visualizer::getWaveForm(uint8_t *waveform)
190{
191 if (waveform == NULL) {
192 return BAD_VALUE;
193 }
194 if (mCaptureSize == 0) {
195 return NO_INIT;
196 }
197
198 status_t status = NO_ERROR;
199 if (mEnabled) {
Eric Laurent25f43952010-07-28 05:40:18 -0700200 uint32_t replySize = mCaptureSize;
Eric Laurent6d8b6942011-06-24 07:01:31 -0700201 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block3856b092011-10-20 11:56:00 +0100202 ALOGV("getWaveForm() command returned %d", status);
John Grossmanaf7d8182012-01-11 12:23:42 -0800203 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700204 status = NOT_ENOUGH_DATA;
205 }
206 } else {
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("getWaveForm() disabled");
Eric Laurentda7581b2010-07-02 08:12:41 -0700208 memset(waveform, 0x80, mCaptureSize);
209 }
210 return status;
211}
212
213status_t Visualizer::getFft(uint8_t *fft)
214{
215 if (fft == NULL) {
216 return BAD_VALUE;
217 }
218 if (mCaptureSize == 0) {
219 return NO_INIT;
220 }
221
222 status_t status = NO_ERROR;
223 if (mEnabled) {
224 uint8_t buf[mCaptureSize];
Eric Laurent0fa449c2010-09-24 11:52:04 -0700225 status = getWaveForm(buf);
Eric Laurentda7581b2010-07-02 08:12:41 -0700226 if (status == NO_ERROR) {
227 status = doFft(fft, buf);
228 }
229 } else {
230 memset(fft, 0, mCaptureSize);
231 }
232 return status;
233}
234
235status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
236{
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800237 int32_t workspace[mCaptureSize >> 1];
238 int32_t nonzero = 0;
239
240 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh6b6a7362010-11-01 10:56:45 +0800241 workspace[i >> 1] =
242 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800243 nonzero |= workspace[i >> 1];
Eric Laurentda7581b2010-07-02 08:12:41 -0700244 }
245
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800246 if (nonzero) {
247 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentda7581b2010-07-02 08:12:41 -0700248 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800249
250 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissen209821c2011-01-18 16:44:28 -0800251 short tmp = workspace[i >> 1] >> 21;
252 while (tmp > 127 || tmp < -128) tmp >>= 1;
253 fft[i] = tmp;
254 tmp = workspace[i >> 1];
255 tmp >>= 5;
256 while (tmp > 127 || tmp < -128) tmp >>= 1;
257 fft[i + 1] = tmp;
Eric Laurentda7581b2010-07-02 08:12:41 -0700258 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800259
Eric Laurentda7581b2010-07-02 08:12:41 -0700260 return NO_ERROR;
261}
262
263void Visualizer::periodicCapture()
264{
Glenn Kastena9b21c52012-01-17 10:06:38 -0800265 Mutex::Autolock _l(mCaptureLock);
Steve Block3856b092011-10-20 11:56:00 +0100266 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700267 this, mCaptureCallBack, mCaptureFlags);
268 if (mCaptureCallBack != NULL &&
269 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
270 mCaptureSize != 0) {
271 uint8_t waveform[mCaptureSize];
272 status_t status = getWaveForm(waveform);
273 if (status != NO_ERROR) {
274 return;
275 }
276 uint8_t fft[mCaptureSize];
277 if (mCaptureFlags & CAPTURE_FFT) {
278 status = doFft(fft, waveform);
279 }
280 if (status != NO_ERROR) {
281 return;
282 }
283 uint8_t *wavePtr = NULL;
284 uint8_t *fftPtr = NULL;
285 uint32_t waveSize = 0;
286 uint32_t fftSize = 0;
287 if (mCaptureFlags & CAPTURE_WAVEFORM) {
288 wavePtr = waveform;
289 waveSize = mCaptureSize;
290 }
291 if (mCaptureFlags & CAPTURE_FFT) {
292 fftPtr = fft;
293 fftSize = mCaptureSize;
294 }
295 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
296 }
297}
298
299uint32_t Visualizer::initCaptureSize()
300{
301 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
302 effect_param_t *p = (effect_param_t *)buf32;
303
304 p->psize = sizeof(uint32_t);
305 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700306 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700307 status_t status = getParameter(p);
308
309 if (status == NO_ERROR) {
310 status = p->status;
311 }
312
313 uint32_t size = 0;
314 if (status == NO_ERROR) {
315 size = *((int32_t *)p->data + 1);
316 }
317 mCaptureSize = size;
318
Steve Block3856b092011-10-20 11:56:00 +0100319 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700320
321 return size;
322}
323
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700324void Visualizer::controlStatusChanged(bool controlGranted) {
325 if (controlGranted) {
326 // this Visualizer instance regained control of the effect, reset the scaling mode
327 // and capture size as has been cached through it.
328 ALOGV("controlStatusChanged(true) causes effect parameter reset:");
329 ALOGV(" scaling mode reset to %d", mScalingMode);
330 setScalingMode(mScalingMode);
331 ALOGV(" capture size reset to %d", mCaptureSize);
332 setCaptureSize(mCaptureSize);
333 }
334 AudioEffect::controlStatusChanged(controlGranted);
335}
336
Eric Laurentda7581b2010-07-02 08:12:41 -0700337//-------------------------------------------------------------------------
338
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700339Visualizer::CaptureThread::CaptureThread(Visualizer& receiver, uint32_t captureRate,
340 bool bCanCallJava)
Eric Laurentda7581b2010-07-02 08:12:41 -0700341 : Thread(bCanCallJava), mReceiver(receiver)
342{
343 mSleepTimeUs = 1000000000 / captureRate;
Steve Block3856b092011-10-20 11:56:00 +0100344 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700345}
346
347bool Visualizer::CaptureThread::threadLoop()
348{
Steve Block3856b092011-10-20 11:56:00 +0100349 ALOGV("CaptureThread %p enter", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700350 while (!exitPending())
351 {
352 usleep(mSleepTimeUs);
353 mReceiver.periodicCapture();
354 }
Steve Block3856b092011-10-20 11:56:00 +0100355 ALOGV("CaptureThread %p exiting", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700356 return false;
357}
358
Eric Laurentda7581b2010-07-02 08:12:41 -0700359}; // namespace android