blob: 8196e107de85aeb0cca3ecfec4507a2c93da2404 [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
91status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags, uint32_t rate)
92{
93 if (rate > CAPTURE_RATE_MAX) {
94 return BAD_VALUE;
95 }
Glenn Kastena9b21c52012-01-17 10:06:38 -080096 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070097
98 if (mEnabled) {
99 return INVALID_OPERATION;
100 }
101
102 sp<CaptureThread> t = mCaptureThread;
103 if (t != 0) {
104 t->mLock.lock();
105 }
106 mCaptureThread.clear();
107 mCaptureCallBack = cbk;
108 mCaptureCbkUser = user;
109 mCaptureFlags = flags;
110 mCaptureRate = rate;
111
112 if (t != 0) {
113 t->mLock.unlock();
114 }
115
116 if (cbk != NULL) {
117 mCaptureThread = new CaptureThread(*this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentda7581b2010-07-02 08:12:41 -0700118 }
Steve Block3856b092011-10-20 11:56:00 +0100119 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700120 rate, mCaptureThread.get(), mCaptureFlags);
121 return NO_ERROR;
122}
123
124status_t Visualizer::setCaptureSize(uint32_t size)
125{
126 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
127 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700128 popcount(size) != 1) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700129 return BAD_VALUE;
130 }
131
Glenn Kastena9b21c52012-01-17 10:06:38 -0800132 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700133 if (mEnabled) {
134 return INVALID_OPERATION;
135 }
136
137 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
138 effect_param_t *p = (effect_param_t *)buf32;
139
140 p->psize = sizeof(uint32_t);
141 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700142 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700143 *((int32_t *)p->data + 1)= size;
144 status_t status = setParameter(p);
145
Steve Block3856b092011-10-20 11:56:00 +0100146 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700147
148 if (status == NO_ERROR) {
149 status = p->status;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700150 if (status == NO_ERROR) {
151 mCaptureSize = size;
152 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700153 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700154
155 return status;
156}
157
158status_t Visualizer::setScalingMode(uint32_t mode) {
159 if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED)
160 && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) {
161 return BAD_VALUE;
162 }
163
164 Mutex::Autolock _l(mCaptureLock);
165
166 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
167 effect_param_t *p = (effect_param_t *)buf32;
168
169 p->psize = sizeof(uint32_t);
170 p->vsize = sizeof(uint32_t);
171 *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE;
172 *((int32_t *)p->data + 1)= mode;
173 status_t status = setParameter(p);
174
175 ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status);
176
Eric Laurentda7581b2010-07-02 08:12:41 -0700177 if (status == NO_ERROR) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700178 status = p->status;
179 if (status == NO_ERROR) {
180 mScalingMode = mode;
181 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700182 }
183
184 return status;
185}
186
187status_t Visualizer::getWaveForm(uint8_t *waveform)
188{
189 if (waveform == NULL) {
190 return BAD_VALUE;
191 }
192 if (mCaptureSize == 0) {
193 return NO_INIT;
194 }
195
196 status_t status = NO_ERROR;
197 if (mEnabled) {
Eric Laurent25f43952010-07-28 05:40:18 -0700198 uint32_t replySize = mCaptureSize;
Eric Laurent6d8b6942011-06-24 07:01:31 -0700199 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block3856b092011-10-20 11:56:00 +0100200 ALOGV("getWaveForm() command returned %d", status);
John Grossmanaf7d8182012-01-11 12:23:42 -0800201 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700202 status = NOT_ENOUGH_DATA;
203 }
204 } else {
Steve Block3856b092011-10-20 11:56:00 +0100205 ALOGV("getWaveForm() disabled");
Eric Laurentda7581b2010-07-02 08:12:41 -0700206 memset(waveform, 0x80, mCaptureSize);
207 }
208 return status;
209}
210
211status_t Visualizer::getFft(uint8_t *fft)
212{
213 if (fft == NULL) {
214 return BAD_VALUE;
215 }
216 if (mCaptureSize == 0) {
217 return NO_INIT;
218 }
219
220 status_t status = NO_ERROR;
221 if (mEnabled) {
222 uint8_t buf[mCaptureSize];
Eric Laurent0fa449c2010-09-24 11:52:04 -0700223 status = getWaveForm(buf);
Eric Laurentda7581b2010-07-02 08:12:41 -0700224 if (status == NO_ERROR) {
225 status = doFft(fft, buf);
226 }
227 } else {
228 memset(fft, 0, mCaptureSize);
229 }
230 return status;
231}
232
233status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
234{
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800235 int32_t workspace[mCaptureSize >> 1];
236 int32_t nonzero = 0;
237
238 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh6b6a7362010-11-01 10:56:45 +0800239 workspace[i >> 1] =
240 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800241 nonzero |= workspace[i >> 1];
Eric Laurentda7581b2010-07-02 08:12:41 -0700242 }
243
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800244 if (nonzero) {
245 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentda7581b2010-07-02 08:12:41 -0700246 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800247
248 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissen209821c2011-01-18 16:44:28 -0800249 short tmp = workspace[i >> 1] >> 21;
250 while (tmp > 127 || tmp < -128) tmp >>= 1;
251 fft[i] = tmp;
252 tmp = workspace[i >> 1];
253 tmp >>= 5;
254 while (tmp > 127 || tmp < -128) tmp >>= 1;
255 fft[i + 1] = tmp;
Eric Laurentda7581b2010-07-02 08:12:41 -0700256 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800257
Eric Laurentda7581b2010-07-02 08:12:41 -0700258 return NO_ERROR;
259}
260
261void Visualizer::periodicCapture()
262{
Glenn Kastena9b21c52012-01-17 10:06:38 -0800263 Mutex::Autolock _l(mCaptureLock);
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700265 this, mCaptureCallBack, mCaptureFlags);
266 if (mCaptureCallBack != NULL &&
267 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
268 mCaptureSize != 0) {
269 uint8_t waveform[mCaptureSize];
270 status_t status = getWaveForm(waveform);
271 if (status != NO_ERROR) {
272 return;
273 }
274 uint8_t fft[mCaptureSize];
275 if (mCaptureFlags & CAPTURE_FFT) {
276 status = doFft(fft, waveform);
277 }
278 if (status != NO_ERROR) {
279 return;
280 }
281 uint8_t *wavePtr = NULL;
282 uint8_t *fftPtr = NULL;
283 uint32_t waveSize = 0;
284 uint32_t fftSize = 0;
285 if (mCaptureFlags & CAPTURE_WAVEFORM) {
286 wavePtr = waveform;
287 waveSize = mCaptureSize;
288 }
289 if (mCaptureFlags & CAPTURE_FFT) {
290 fftPtr = fft;
291 fftSize = mCaptureSize;
292 }
293 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
294 }
295}
296
297uint32_t Visualizer::initCaptureSize()
298{
299 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
300 effect_param_t *p = (effect_param_t *)buf32;
301
302 p->psize = sizeof(uint32_t);
303 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700304 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700305 status_t status = getParameter(p);
306
307 if (status == NO_ERROR) {
308 status = p->status;
309 }
310
311 uint32_t size = 0;
312 if (status == NO_ERROR) {
313 size = *((int32_t *)p->data + 1);
314 }
315 mCaptureSize = size;
316
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700318
319 return size;
320}
321
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700322void Visualizer::controlStatusChanged(bool controlGranted) {
323 if (controlGranted) {
324 // this Visualizer instance regained control of the effect, reset the scaling mode
325 // and capture size as has been cached through it.
326 ALOGV("controlStatusChanged(true) causes effect parameter reset:");
327 ALOGV(" scaling mode reset to %d", mScalingMode);
328 setScalingMode(mScalingMode);
329 ALOGV(" capture size reset to %d", mCaptureSize);
330 setCaptureSize(mCaptureSize);
331 }
332 AudioEffect::controlStatusChanged(controlGranted);
333}
334
Eric Laurentda7581b2010-07-02 08:12:41 -0700335//-------------------------------------------------------------------------
336
337Visualizer::CaptureThread::CaptureThread(Visualizer& receiver, uint32_t captureRate, bool bCanCallJava)
338 : Thread(bCanCallJava), mReceiver(receiver)
339{
340 mSleepTimeUs = 1000000000 / captureRate;
Steve Block3856b092011-10-20 11:56:00 +0100341 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700342}
343
344bool Visualizer::CaptureThread::threadLoop()
345{
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("CaptureThread %p enter", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700347 while (!exitPending())
348 {
349 usleep(mSleepTimeUs);
350 mReceiver.periodicCapture();
351 }
Steve Block3856b092011-10-20 11:56:00 +0100352 ALOGV("CaptureThread %p exiting", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700353 return false;
354}
355
Eric Laurentda7581b2010-07-02 08:12:41 -0700356}; // namespace android