blob: 5b4071be582c9b5dba90ba28c76af924e3c814c7 [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>
Eric Laurentda7581b2010-07-02 08:12:41 -070031
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36Visualizer::Visualizer (int32_t priority,
37 effect_callback_t cbf,
38 void* user,
39 int sessionId)
40 : AudioEffect(SL_IID_VISUALIZATION, NULL, priority, cbf, user, sessionId),
41 mCaptureRate(CAPTURE_RATE_DEF),
42 mCaptureSize(CAPTURE_SIZE_DEF),
43 mSampleRate(44100000),
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070044 mScalingMode(VISUALIZER_SCALING_MODE_NORMALIZED),
Eric Laurentda7581b2010-07-02 08:12:41 -070045 mCaptureCallBack(NULL),
46 mCaptureCbkUser(NULL)
47{
48 initCaptureSize();
Eric Laurentda7581b2010-07-02 08:12:41 -070049}
50
51Visualizer::~Visualizer()
52{
Eric Laurentda7581b2010-07-02 08:12:41 -070053}
54
55status_t Visualizer::setEnabled(bool enabled)
56{
Glenn Kastena9b21c52012-01-17 10:06:38 -080057 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070058
59 sp<CaptureThread> t = mCaptureThread;
60 if (t != 0) {
61 if (enabled) {
62 if (t->exitPending()) {
63 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +000064 ALOGE("Visualizer::enable() called from thread");
Eric Laurentda7581b2010-07-02 08:12:41 -070065 return INVALID_OPERATION;
66 }
67 }
68 }
69 t->mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070070 }
Eric Laurentda7581b2010-07-02 08:12:41 -070071
72 status_t status = AudioEffect::setEnabled(enabled);
73
74 if (status == NO_ERROR) {
75 if (t != 0) {
76 if (enabled) {
Glenn Kasten9096f342012-01-19 08:14:08 -080077 t->run("Visualizer");
Eric Laurentda7581b2010-07-02 08:12:41 -070078 } else {
79 t->requestExit();
80 }
81 }
82 }
83
84 if (t != 0) {
85 t->mLock.unlock();
86 }
87
88 return status;
89}
90
Glenn Kasten8af901c2012-11-01 11:11:38 -070091status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags,
92 uint32_t rate)
Eric Laurentda7581b2010-07-02 08:12:41 -070093{
94 if (rate > CAPTURE_RATE_MAX) {
95 return BAD_VALUE;
96 }
Glenn Kastena9b21c52012-01-17 10:06:38 -080097 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070098
99 if (mEnabled) {
100 return INVALID_OPERATION;
101 }
102
103 sp<CaptureThread> t = mCaptureThread;
104 if (t != 0) {
105 t->mLock.lock();
106 }
107 mCaptureThread.clear();
108 mCaptureCallBack = cbk;
109 mCaptureCbkUser = user;
110 mCaptureFlags = flags;
111 mCaptureRate = rate;
112
113 if (t != 0) {
114 t->mLock.unlock();
115 }
116
117 if (cbk != NULL) {
118 mCaptureThread = new CaptureThread(*this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentda7581b2010-07-02 08:12:41 -0700119 }
Steve Block3856b092011-10-20 11:56:00 +0100120 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700121 rate, mCaptureThread.get(), mCaptureFlags);
122 return NO_ERROR;
123}
124
125status_t Visualizer::setCaptureSize(uint32_t size)
126{
127 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
128 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 popcount(size) != 1) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700130 return BAD_VALUE;
131 }
132
Glenn Kastena9b21c52012-01-17 10:06:38 -0800133 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700134 if (mEnabled) {
135 return INVALID_OPERATION;
136 }
137
138 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
139 effect_param_t *p = (effect_param_t *)buf32;
140
141 p->psize = sizeof(uint32_t);
142 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700143 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700144 *((int32_t *)p->data + 1)= size;
145 status_t status = setParameter(p);
146
Steve Block3856b092011-10-20 11:56:00 +0100147 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700148
149 if (status == NO_ERROR) {
150 status = p->status;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700151 if (status == NO_ERROR) {
152 mCaptureSize = size;
153 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700154 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700155
156 return status;
157}
158
159status_t Visualizer::setScalingMode(uint32_t mode) {
160 if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED)
161 && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) {
162 return BAD_VALUE;
163 }
164
165 Mutex::Autolock _l(mCaptureLock);
166
167 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
168 effect_param_t *p = (effect_param_t *)buf32;
169
170 p->psize = sizeof(uint32_t);
171 p->vsize = sizeof(uint32_t);
172 *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE;
173 *((int32_t *)p->data + 1)= mode;
174 status_t status = setParameter(p);
175
176 ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status);
177
Eric Laurentda7581b2010-07-02 08:12:41 -0700178 if (status == NO_ERROR) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700179 status = p->status;
180 if (status == NO_ERROR) {
181 mScalingMode = mode;
182 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700183 }
184
185 return status;
186}
187
188status_t Visualizer::getWaveForm(uint8_t *waveform)
189{
190 if (waveform == NULL) {
191 return BAD_VALUE;
192 }
193 if (mCaptureSize == 0) {
194 return NO_INIT;
195 }
196
197 status_t status = NO_ERROR;
198 if (mEnabled) {
Eric Laurent25f43952010-07-28 05:40:18 -0700199 uint32_t replySize = mCaptureSize;
Eric Laurent6d8b6942011-06-24 07:01:31 -0700200 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV("getWaveForm() command returned %d", status);
John Grossmanaf7d8182012-01-11 12:23:42 -0800202 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700203 status = NOT_ENOUGH_DATA;
204 }
205 } else {
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("getWaveForm() disabled");
Eric Laurentda7581b2010-07-02 08:12:41 -0700207 memset(waveform, 0x80, mCaptureSize);
208 }
209 return status;
210}
211
212status_t Visualizer::getFft(uint8_t *fft)
213{
214 if (fft == NULL) {
215 return BAD_VALUE;
216 }
217 if (mCaptureSize == 0) {
218 return NO_INIT;
219 }
220
221 status_t status = NO_ERROR;
222 if (mEnabled) {
223 uint8_t buf[mCaptureSize];
Eric Laurent0fa449c2010-09-24 11:52:04 -0700224 status = getWaveForm(buf);
Eric Laurentda7581b2010-07-02 08:12:41 -0700225 if (status == NO_ERROR) {
226 status = doFft(fft, buf);
227 }
228 } else {
229 memset(fft, 0, mCaptureSize);
230 }
231 return status;
232}
233
234status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
235{
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800236 int32_t workspace[mCaptureSize >> 1];
237 int32_t nonzero = 0;
238
239 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh6b6a7362010-11-01 10:56:45 +0800240 workspace[i >> 1] =
241 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800242 nonzero |= workspace[i >> 1];
Eric Laurentda7581b2010-07-02 08:12:41 -0700243 }
244
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800245 if (nonzero) {
246 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentda7581b2010-07-02 08:12:41 -0700247 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800248
249 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissen209821c2011-01-18 16:44:28 -0800250 short tmp = workspace[i >> 1] >> 21;
251 while (tmp > 127 || tmp < -128) tmp >>= 1;
252 fft[i] = tmp;
253 tmp = workspace[i >> 1];
254 tmp >>= 5;
255 while (tmp > 127 || tmp < -128) tmp >>= 1;
256 fft[i + 1] = tmp;
Eric Laurentda7581b2010-07-02 08:12:41 -0700257 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800258
Eric Laurentda7581b2010-07-02 08:12:41 -0700259 return NO_ERROR;
260}
261
262void Visualizer::periodicCapture()
263{
Glenn Kastena9b21c52012-01-17 10:06:38 -0800264 Mutex::Autolock _l(mCaptureLock);
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700266 this, mCaptureCallBack, mCaptureFlags);
267 if (mCaptureCallBack != NULL &&
268 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
269 mCaptureSize != 0) {
270 uint8_t waveform[mCaptureSize];
271 status_t status = getWaveForm(waveform);
272 if (status != NO_ERROR) {
273 return;
274 }
275 uint8_t fft[mCaptureSize];
276 if (mCaptureFlags & CAPTURE_FFT) {
277 status = doFft(fft, waveform);
278 }
279 if (status != NO_ERROR) {
280 return;
281 }
282 uint8_t *wavePtr = NULL;
283 uint8_t *fftPtr = NULL;
284 uint32_t waveSize = 0;
285 uint32_t fftSize = 0;
286 if (mCaptureFlags & CAPTURE_WAVEFORM) {
287 wavePtr = waveform;
288 waveSize = mCaptureSize;
289 }
290 if (mCaptureFlags & CAPTURE_FFT) {
291 fftPtr = fft;
292 fftSize = mCaptureSize;
293 }
294 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
295 }
296}
297
298uint32_t Visualizer::initCaptureSize()
299{
300 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
301 effect_param_t *p = (effect_param_t *)buf32;
302
303 p->psize = sizeof(uint32_t);
304 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700305 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700306 status_t status = getParameter(p);
307
308 if (status == NO_ERROR) {
309 status = p->status;
310 }
311
312 uint32_t size = 0;
313 if (status == NO_ERROR) {
314 size = *((int32_t *)p->data + 1);
315 }
316 mCaptureSize = size;
317
Steve Block3856b092011-10-20 11:56:00 +0100318 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700319
320 return size;
321}
322
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700323void Visualizer::controlStatusChanged(bool controlGranted) {
324 if (controlGranted) {
325 // this Visualizer instance regained control of the effect, reset the scaling mode
326 // and capture size as has been cached through it.
327 ALOGV("controlStatusChanged(true) causes effect parameter reset:");
328 ALOGV(" scaling mode reset to %d", mScalingMode);
329 setScalingMode(mScalingMode);
330 ALOGV(" capture size reset to %d", mCaptureSize);
331 setCaptureSize(mCaptureSize);
332 }
333 AudioEffect::controlStatusChanged(controlGranted);
334}
335
Eric Laurentda7581b2010-07-02 08:12:41 -0700336//-------------------------------------------------------------------------
337
Glenn Kasten8af901c2012-11-01 11:11:38 -0700338Visualizer::CaptureThread::CaptureThread(Visualizer& receiver, uint32_t captureRate,
339 bool bCanCallJava)
Eric Laurentda7581b2010-07-02 08:12:41 -0700340 : Thread(bCanCallJava), mReceiver(receiver)
341{
342 mSleepTimeUs = 1000000000 / captureRate;
Steve Block3856b092011-10-20 11:56:00 +0100343 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700344}
345
346bool Visualizer::CaptureThread::threadLoop()
347{
Steve Block3856b092011-10-20 11:56:00 +0100348 ALOGV("CaptureThread %p enter", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700349 while (!exitPending())
350 {
351 usleep(mSleepTimeUs);
352 mReceiver.periodicCapture();
353 }
Steve Block3856b092011-10-20 11:56:00 +0100354 ALOGV("CaptureThread %p exiting", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700355 return false;
356}
357
Eric Laurentda7581b2010-07-02 08:12:41 -0700358}; // namespace android