blob: bcd6ae4901e73896ad0be648d3b58bc5dc6bb37f [file] [log] [blame]
Eric Laurentdf9b81c2010-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 Zavin24fc2fb2011-04-19 22:30:36 -070027#include <cutils/bitops.h>
28
Eric Laurentdf9b81c2010-07-02 08:12:41 -070029#include <media/Visualizer.h>
Glenn Kastenaa967522012-01-16 13:11:50 -080030#include <audio_utils/fixedfft.h>
Eric Laurentdf9b81c2010-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),
44 mCaptureCallBack(NULL),
45 mCaptureCbkUser(NULL)
46{
47 initCaptureSize();
Eric Laurentdf9b81c2010-07-02 08:12:41 -070048}
49
50Visualizer::~Visualizer()
51{
Eric Laurentdf9b81c2010-07-02 08:12:41 -070052}
53
54status_t Visualizer::setEnabled(bool enabled)
55{
Glenn Kasten55fa4fb2012-01-17 10:06:38 -080056 Mutex::Autolock _l(mCaptureLock);
Eric Laurentdf9b81c2010-07-02 08:12:41 -070057
58 sp<CaptureThread> t = mCaptureThread;
59 if (t != 0) {
60 if (enabled) {
61 if (t->exitPending()) {
62 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block3762c312012-01-06 19:20:56 +000063 ALOGE("Visualizer::enable() called from thread");
Eric Laurentdf9b81c2010-07-02 08:12:41 -070064 return INVALID_OPERATION;
65 }
66 }
67 }
68 t->mLock.lock();
Glenn Kasten18db49a2012-03-12 16:29:55 -070069 }
Eric Laurentdf9b81c2010-07-02 08:12:41 -070070
71 status_t status = AudioEffect::setEnabled(enabled);
72
73 if (status == NO_ERROR) {
74 if (t != 0) {
75 if (enabled) {
Glenn Kasten24c255a2012-01-19 08:14:08 -080076 t->run("Visualizer");
Eric Laurentdf9b81c2010-07-02 08:12:41 -070077 } else {
78 t->requestExit();
79 }
80 }
81 }
82
83 if (t != 0) {
84 t->mLock.unlock();
85 }
86
87 return status;
88}
89
90status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags, uint32_t rate)
91{
92 if (rate > CAPTURE_RATE_MAX) {
93 return BAD_VALUE;
94 }
Glenn Kasten55fa4fb2012-01-17 10:06:38 -080095 Mutex::Autolock _l(mCaptureLock);
Eric Laurentdf9b81c2010-07-02 08:12:41 -070096
97 if (mEnabled) {
98 return INVALID_OPERATION;
99 }
100
101 sp<CaptureThread> t = mCaptureThread;
102 if (t != 0) {
103 t->mLock.lock();
104 }
105 mCaptureThread.clear();
106 mCaptureCallBack = cbk;
107 mCaptureCbkUser = user;
108 mCaptureFlags = flags;
109 mCaptureRate = rate;
110
111 if (t != 0) {
112 t->mLock.unlock();
113 }
114
115 if (cbk != NULL) {
116 mCaptureThread = new CaptureThread(*this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700117 }
Steve Block71f2cf12011-10-20 11:56:00 +0100118 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700119 rate, mCaptureThread.get(), mCaptureFlags);
120 return NO_ERROR;
121}
122
123status_t Visualizer::setCaptureSize(uint32_t size)
124{
125 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
126 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700127 popcount(size) != 1) {
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700128 return BAD_VALUE;
129 }
130
Glenn Kasten55fa4fb2012-01-17 10:06:38 -0800131 Mutex::Autolock _l(mCaptureLock);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700132 if (mEnabled) {
133 return INVALID_OPERATION;
134 }
135
136 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
137 effect_param_t *p = (effect_param_t *)buf32;
138
139 p->psize = sizeof(uint32_t);
140 p->vsize = sizeof(uint32_t);
Eric Laurent5cc05262011-06-24 07:01:31 -0700141 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700142 *((int32_t *)p->data + 1)= size;
143 status_t status = setParameter(p);
144
Steve Block71f2cf12011-10-20 11:56:00 +0100145 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700146
147 if (status == NO_ERROR) {
148 status = p->status;
149 }
150 if (status == NO_ERROR) {
151 mCaptureSize = size;
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700152 }
153
154 return status;
155}
156
157status_t Visualizer::getWaveForm(uint8_t *waveform)
158{
159 if (waveform == NULL) {
160 return BAD_VALUE;
161 }
162 if (mCaptureSize == 0) {
163 return NO_INIT;
164 }
165
166 status_t status = NO_ERROR;
167 if (mEnabled) {
Eric Laurenta4c72ac2010-07-28 05:40:18 -0700168 uint32_t replySize = mCaptureSize;
Eric Laurent5cc05262011-06-24 07:01:31 -0700169 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block71f2cf12011-10-20 11:56:00 +0100170 ALOGV("getWaveForm() command returned %d", status);
John Grossman3540a012012-01-11 12:23:42 -0800171 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700172 status = NOT_ENOUGH_DATA;
173 }
174 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100175 ALOGV("getWaveForm() disabled");
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700176 memset(waveform, 0x80, mCaptureSize);
177 }
178 return status;
179}
180
181status_t Visualizer::getFft(uint8_t *fft)
182{
183 if (fft == NULL) {
184 return BAD_VALUE;
185 }
186 if (mCaptureSize == 0) {
187 return NO_INIT;
188 }
189
190 status_t status = NO_ERROR;
191 if (mEnabled) {
192 uint8_t buf[mCaptureSize];
Eric Laurent4d3fb502010-09-24 11:52:04 -0700193 status = getWaveForm(buf);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700194 if (status == NO_ERROR) {
195 status = doFft(fft, buf);
196 }
197 } else {
198 memset(fft, 0, mCaptureSize);
199 }
200 return status;
201}
202
203status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
204{
Chia-chi Yeh58d3bd02010-08-19 15:34:10 +0800205 int32_t workspace[mCaptureSize >> 1];
206 int32_t nonzero = 0;
207
208 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh67f41772010-11-01 10:56:45 +0800209 workspace[i >> 1] =
210 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yeh58d3bd02010-08-19 15:34:10 +0800211 nonzero |= workspace[i >> 1];
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700212 }
213
Chia-chi Yeh58d3bd02010-08-19 15:34:10 +0800214 if (nonzero) {
215 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700216 }
Chia-chi Yeh58d3bd02010-08-19 15:34:10 +0800217
218 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissendbc0fe92011-01-18 16:44:28 -0800219 short tmp = workspace[i >> 1] >> 21;
220 while (tmp > 127 || tmp < -128) tmp >>= 1;
221 fft[i] = tmp;
222 tmp = workspace[i >> 1];
223 tmp >>= 5;
224 while (tmp > 127 || tmp < -128) tmp >>= 1;
225 fft[i + 1] = tmp;
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700226 }
Chia-chi Yeh58d3bd02010-08-19 15:34:10 +0800227
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700228 return NO_ERROR;
229}
230
231void Visualizer::periodicCapture()
232{
Glenn Kasten55fa4fb2012-01-17 10:06:38 -0800233 Mutex::Autolock _l(mCaptureLock);
Steve Block71f2cf12011-10-20 11:56:00 +0100234 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700235 this, mCaptureCallBack, mCaptureFlags);
236 if (mCaptureCallBack != NULL &&
237 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
238 mCaptureSize != 0) {
239 uint8_t waveform[mCaptureSize];
240 status_t status = getWaveForm(waveform);
241 if (status != NO_ERROR) {
242 return;
243 }
244 uint8_t fft[mCaptureSize];
245 if (mCaptureFlags & CAPTURE_FFT) {
246 status = doFft(fft, waveform);
247 }
248 if (status != NO_ERROR) {
249 return;
250 }
251 uint8_t *wavePtr = NULL;
252 uint8_t *fftPtr = NULL;
253 uint32_t waveSize = 0;
254 uint32_t fftSize = 0;
255 if (mCaptureFlags & CAPTURE_WAVEFORM) {
256 wavePtr = waveform;
257 waveSize = mCaptureSize;
258 }
259 if (mCaptureFlags & CAPTURE_FFT) {
260 fftPtr = fft;
261 fftSize = mCaptureSize;
262 }
263 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
264 }
265}
266
267uint32_t Visualizer::initCaptureSize()
268{
269 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
270 effect_param_t *p = (effect_param_t *)buf32;
271
272 p->psize = sizeof(uint32_t);
273 p->vsize = sizeof(uint32_t);
Eric Laurent5cc05262011-06-24 07:01:31 -0700274 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700275 status_t status = getParameter(p);
276
277 if (status == NO_ERROR) {
278 status = p->status;
279 }
280
281 uint32_t size = 0;
282 if (status == NO_ERROR) {
283 size = *((int32_t *)p->data + 1);
284 }
285 mCaptureSize = size;
286
Steve Block71f2cf12011-10-20 11:56:00 +0100287 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700288
289 return size;
290}
291
292//-------------------------------------------------------------------------
293
294Visualizer::CaptureThread::CaptureThread(Visualizer& receiver, uint32_t captureRate, bool bCanCallJava)
295 : Thread(bCanCallJava), mReceiver(receiver)
296{
297 mSleepTimeUs = 1000000000 / captureRate;
Steve Block71f2cf12011-10-20 11:56:00 +0100298 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700299}
300
301bool Visualizer::CaptureThread::threadLoop()
302{
Steve Block71f2cf12011-10-20 11:56:00 +0100303 ALOGV("CaptureThread %p enter", this);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700304 while (!exitPending())
305 {
306 usleep(mSleepTimeUs);
307 mReceiver.periodicCapture();
308 }
Steve Block71f2cf12011-10-20 11:56:00 +0100309 ALOGV("CaptureThread %p exiting", this);
Eric Laurentdf9b81c2010-07-02 08:12:41 -0700310 return false;
311}
312
313status_t Visualizer::CaptureThread::readyToRun()
314{
315 return NO_ERROR;
316}
317
318void Visualizer::CaptureThread::onFirstRef()
319{
320}
321
322}; // namespace android