Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | #include <media/Visualizer.h> |
Glenn Kasten | 3f6448e | 2012-01-16 13:11:50 -0800 | [diff] [blame] | 28 | #include <audio_utils/fixedfft.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 29 | #include <utils/Thread.h> |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 35 | Visualizer::Visualizer (const String16& opPackageName, |
| 36 | int32_t priority, |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 37 | effect_callback_t cbf, |
| 38 | void* user, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 39 | audio_session_t sessionId) |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 40 | : AudioEffect(SL_IID_VISUALIZATION, opPackageName, NULL, priority, cbf, user, sessionId), |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 41 | mCaptureRate(CAPTURE_RATE_DEF), |
| 42 | mCaptureSize(CAPTURE_SIZE_DEF), |
| 43 | mSampleRate(44100000), |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 44 | mScalingMode(VISUALIZER_SCALING_MODE_NORMALIZED), |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 45 | mMeasurementMode(MEASUREMENT_MODE_NONE), |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 46 | mCaptureCallBack(NULL), |
| 47 | mCaptureCbkUser(NULL) |
| 48 | { |
| 49 | initCaptureSize(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Visualizer::~Visualizer() |
| 53 | { |
Haynes Mathew George | 63f6ffb | 2014-09-25 10:33:12 -0700 | [diff] [blame] | 54 | ALOGV("Visualizer::~Visualizer()"); |
Ricardo Garcia | 9b030df | 2015-06-18 21:01:53 -0700 | [diff] [blame] | 55 | setEnabled(false); |
ganxiaolin | 2e5b570 | 2016-01-29 19:57:57 +0800 | [diff] [blame] | 56 | setCaptureCallBack(NULL, NULL, 0, 0); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 57 | } |
| 58 | |
zengjing | 704c577 | 2018-09-29 13:25:35 +0800 | [diff] [blame] | 59 | void Visualizer::release() |
| 60 | { |
| 61 | ALOGV("Visualizer::release()"); |
| 62 | setEnabled(false); |
| 63 | Mutex::Autolock _l(mCaptureLock); |
| 64 | |
| 65 | mCaptureThread.clear(); |
| 66 | mCaptureCallBack = NULL; |
| 67 | mCaptureCbkUser = NULL; |
| 68 | mCaptureFlags = 0; |
| 69 | mCaptureRate = 0; |
| 70 | } |
| 71 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 72 | status_t Visualizer::setEnabled(bool enabled) |
| 73 | { |
Glenn Kasten | a9b21c5 | 2012-01-17 10:06:38 -0800 | [diff] [blame] | 74 | Mutex::Autolock _l(mCaptureLock); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 75 | |
| 76 | sp<CaptureThread> t = mCaptureThread; |
| 77 | if (t != 0) { |
| 78 | if (enabled) { |
| 79 | if (t->exitPending()) { |
Aniket Kumar Lata | e8e1c69 | 2019-07-03 10:44:47 -0700 | [diff] [blame] | 80 | mCaptureLock.unlock(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 81 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
Aniket Kumar Lata | e8e1c69 | 2019-07-03 10:44:47 -0700 | [diff] [blame] | 82 | mCaptureLock.lock(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 83 | ALOGE("Visualizer::enable() called from thread"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 84 | return INVALID_OPERATION; |
| 85 | } |
Aniket Kumar Lata | e8e1c69 | 2019-07-03 10:44:47 -0700 | [diff] [blame] | 86 | mCaptureLock.lock(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | t->mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 90 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 91 | |
| 92 | status_t status = AudioEffect::setEnabled(enabled); |
| 93 | |
ganxiaolin | 2e5b570 | 2016-01-29 19:57:57 +0800 | [diff] [blame] | 94 | if (t != 0) { |
| 95 | if (enabled && status == NO_ERROR) { |
| 96 | t->run("Visualizer"); |
| 97 | } else { |
| 98 | t->requestExit(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | if (t != 0) { |
| 103 | t->mLock.unlock(); |
| 104 | } |
| 105 | |
| 106 | return status; |
| 107 | } |
| 108 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 109 | status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags, |
ganxiaolin | 2e5b570 | 2016-01-29 19:57:57 +0800 | [diff] [blame] | 110 | uint32_t rate) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 111 | { |
| 112 | if (rate > CAPTURE_RATE_MAX) { |
| 113 | return BAD_VALUE; |
| 114 | } |
Glenn Kasten | a9b21c5 | 2012-01-17 10:06:38 -0800 | [diff] [blame] | 115 | Mutex::Autolock _l(mCaptureLock); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 116 | |
ganxiaolin | 2e5b570 | 2016-01-29 19:57:57 +0800 | [diff] [blame] | 117 | if (mEnabled) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 118 | return INVALID_OPERATION; |
| 119 | } |
| 120 | |
Haynes Mathew George | 63f6ffb | 2014-09-25 10:33:12 -0700 | [diff] [blame] | 121 | if (mCaptureThread != 0) { |
| 122 | mCaptureLock.unlock(); |
| 123 | mCaptureThread->requestExitAndWait(); |
| 124 | mCaptureLock.lock(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 125 | } |
Haynes Mathew George | 63f6ffb | 2014-09-25 10:33:12 -0700 | [diff] [blame] | 126 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 127 | mCaptureThread.clear(); |
| 128 | mCaptureCallBack = cbk; |
| 129 | mCaptureCbkUser = user; |
| 130 | mCaptureFlags = flags; |
| 131 | mCaptureRate = rate; |
| 132 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 133 | if (cbk != NULL) { |
zengjing | 704c577 | 2018-09-29 13:25:35 +0800 | [diff] [blame] | 134 | mCaptureThread = new CaptureThread(this, rate, ((flags & CAPTURE_CALL_JAVA) != 0)); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 135 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 136 | ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 137 | rate, mCaptureThread.get(), mCaptureFlags); |
| 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
| 141 | status_t Visualizer::setCaptureSize(uint32_t size) |
| 142 | { |
| 143 | if (size > VISUALIZER_CAPTURE_SIZE_MAX || |
| 144 | size < VISUALIZER_CAPTURE_SIZE_MIN || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 145 | popcount(size) != 1) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 146 | return BAD_VALUE; |
| 147 | } |
| 148 | |
Glenn Kasten | a9b21c5 | 2012-01-17 10:06:38 -0800 | [diff] [blame] | 149 | Mutex::Autolock _l(mCaptureLock); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 150 | if (mEnabled) { |
| 151 | return INVALID_OPERATION; |
| 152 | } |
| 153 | |
| 154 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 155 | effect_param_t *p = (effect_param_t *)buf32; |
| 156 | |
| 157 | p->psize = sizeof(uint32_t); |
| 158 | p->vsize = sizeof(uint32_t); |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 159 | *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 160 | *((int32_t *)p->data + 1)= size; |
| 161 | status_t status = setParameter(p); |
| 162 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 163 | ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 164 | |
| 165 | if (status == NO_ERROR) { |
| 166 | status = p->status; |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 167 | if (status == NO_ERROR) { |
| 168 | mCaptureSize = size; |
| 169 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 170 | } |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 171 | |
| 172 | return status; |
| 173 | } |
| 174 | |
| 175 | status_t Visualizer::setScalingMode(uint32_t mode) { |
| 176 | if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED) |
| 177 | && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) { |
| 178 | return BAD_VALUE; |
| 179 | } |
| 180 | |
| 181 | Mutex::Autolock _l(mCaptureLock); |
| 182 | |
| 183 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 184 | effect_param_t *p = (effect_param_t *)buf32; |
| 185 | |
| 186 | p->psize = sizeof(uint32_t); |
| 187 | p->vsize = sizeof(uint32_t); |
| 188 | *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE; |
| 189 | *((int32_t *)p->data + 1)= mode; |
| 190 | status_t status = setParameter(p); |
| 191 | |
| 192 | ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status); |
| 193 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 194 | if (status == NO_ERROR) { |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 195 | status = p->status; |
| 196 | if (status == NO_ERROR) { |
| 197 | mScalingMode = mode; |
| 198 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return status; |
| 202 | } |
| 203 | |
Jean-Michel Trivi | 09647d2 | 2013-09-20 11:58:40 -0700 | [diff] [blame] | 204 | status_t Visualizer::setMeasurementMode(uint32_t mode) { |
| 205 | if ((mode != MEASUREMENT_MODE_NONE) |
| 206 | //Note: needs to be handled as a mask when more measurement modes are added |
| 207 | && ((mode & MEASUREMENT_MODE_PEAK_RMS) != mode)) { |
| 208 | return BAD_VALUE; |
| 209 | } |
| 210 | |
| 211 | Mutex::Autolock _l(mCaptureLock); |
| 212 | |
| 213 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 214 | effect_param_t *p = (effect_param_t *)buf32; |
| 215 | |
| 216 | p->psize = sizeof(uint32_t); |
| 217 | p->vsize = sizeof(uint32_t); |
| 218 | *(int32_t *)p->data = VISUALIZER_PARAM_MEASUREMENT_MODE; |
| 219 | *((int32_t *)p->data + 1)= mode; |
| 220 | status_t status = setParameter(p); |
| 221 | |
| 222 | ALOGV("setMeasurementMode mode %d status %d p->status %d", mode, status, p->status); |
| 223 | |
| 224 | if (status == NO_ERROR) { |
| 225 | status = p->status; |
| 226 | if (status == NO_ERROR) { |
| 227 | mMeasurementMode = mode; |
| 228 | } |
| 229 | } |
| 230 | return status; |
| 231 | } |
| 232 | |
| 233 | status_t Visualizer::getIntMeasurements(uint32_t type, uint32_t number, int32_t *measurements) { |
| 234 | if (mMeasurementMode == MEASUREMENT_MODE_NONE) { |
| 235 | ALOGE("Cannot retrieve int measurements, no measurement mode set"); |
| 236 | return INVALID_OPERATION; |
| 237 | } |
| 238 | if (!(mMeasurementMode & type)) { |
| 239 | // measurement type has not been set on this Visualizer |
| 240 | ALOGE("Cannot retrieve int measurements, requested measurement mode 0x%x not set(0x%x)", |
| 241 | type, mMeasurementMode); |
| 242 | return INVALID_OPERATION; |
| 243 | } |
| 244 | // only peak+RMS measurement supported |
| 245 | if ((type != MEASUREMENT_MODE_PEAK_RMS) |
| 246 | // for peak+RMS measurement, the results are 2 int32_t values |
| 247 | || (number != 2)) { |
| 248 | ALOGE("Cannot retrieve int measurements, MEASUREMENT_MODE_PEAK_RMS returns 2 ints, not %d", |
| 249 | number); |
| 250 | return BAD_VALUE; |
| 251 | } |
| 252 | |
| 253 | status_t status = NO_ERROR; |
| 254 | if (mEnabled) { |
| 255 | uint32_t replySize = number * sizeof(int32_t); |
| 256 | status = command(VISUALIZER_CMD_MEASURE, |
| 257 | sizeof(uint32_t) /*cmdSize*/, |
| 258 | &type /*cmdData*/, |
| 259 | &replySize, measurements); |
| 260 | ALOGV("getMeasurements() command returned %d", status); |
| 261 | if ((status == NO_ERROR) && (replySize == 0)) { |
| 262 | status = NOT_ENOUGH_DATA; |
| 263 | } |
| 264 | } else { |
| 265 | ALOGV("getMeasurements() disabled"); |
| 266 | return INVALID_OPERATION; |
| 267 | } |
| 268 | return status; |
| 269 | } |
| 270 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 271 | status_t Visualizer::getWaveForm(uint8_t *waveform) |
| 272 | { |
| 273 | if (waveform == NULL) { |
| 274 | return BAD_VALUE; |
| 275 | } |
| 276 | if (mCaptureSize == 0) { |
| 277 | return NO_INIT; |
| 278 | } |
| 279 | |
| 280 | status_t status = NO_ERROR; |
| 281 | if (mEnabled) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 282 | uint32_t replySize = mCaptureSize; |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 283 | status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 284 | ALOGV("getWaveForm() command returned %d", status); |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 285 | if ((status == NO_ERROR) && (replySize == 0)) { |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 286 | status = NOT_ENOUGH_DATA; |
| 287 | } |
| 288 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 289 | ALOGV("getWaveForm() disabled"); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 290 | memset(waveform, 0x80, mCaptureSize); |
| 291 | } |
| 292 | return status; |
| 293 | } |
| 294 | |
| 295 | status_t Visualizer::getFft(uint8_t *fft) |
| 296 | { |
| 297 | if (fft == NULL) { |
| 298 | return BAD_VALUE; |
| 299 | } |
| 300 | if (mCaptureSize == 0) { |
| 301 | return NO_INIT; |
| 302 | } |
| 303 | |
| 304 | status_t status = NO_ERROR; |
| 305 | if (mEnabled) { |
| 306 | uint8_t buf[mCaptureSize]; |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 307 | status = getWaveForm(buf); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 308 | if (status == NO_ERROR) { |
| 309 | status = doFft(fft, buf); |
| 310 | } |
| 311 | } else { |
| 312 | memset(fft, 0, mCaptureSize); |
| 313 | } |
| 314 | return status; |
| 315 | } |
| 316 | |
| 317 | status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform) |
| 318 | { |
Chia-chi Yeh | dbd2b7e | 2010-08-19 15:34:10 +0800 | [diff] [blame] | 319 | int32_t workspace[mCaptureSize >> 1]; |
| 320 | int32_t nonzero = 0; |
| 321 | |
| 322 | for (uint32_t i = 0; i < mCaptureSize; i += 2) { |
Chia-chi Yeh | 6b6a736 | 2010-11-01 10:56:45 +0800 | [diff] [blame] | 323 | workspace[i >> 1] = |
| 324 | ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8); |
Chia-chi Yeh | dbd2b7e | 2010-08-19 15:34:10 +0800 | [diff] [blame] | 325 | nonzero |= workspace[i >> 1]; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Chia-chi Yeh | dbd2b7e | 2010-08-19 15:34:10 +0800 | [diff] [blame] | 328 | if (nonzero) { |
| 329 | fixed_fft_real(mCaptureSize >> 1, workspace); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 330 | } |
Chia-chi Yeh | dbd2b7e | 2010-08-19 15:34:10 +0800 | [diff] [blame] | 331 | |
| 332 | for (uint32_t i = 0; i < mCaptureSize; i += 2) { |
Marco Nelissen | 209821c | 2011-01-18 16:44:28 -0800 | [diff] [blame] | 333 | short tmp = workspace[i >> 1] >> 21; |
| 334 | while (tmp > 127 || tmp < -128) tmp >>= 1; |
| 335 | fft[i] = tmp; |
| 336 | tmp = workspace[i >> 1]; |
| 337 | tmp >>= 5; |
| 338 | while (tmp > 127 || tmp < -128) tmp >>= 1; |
| 339 | fft[i + 1] = tmp; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 340 | } |
Chia-chi Yeh | dbd2b7e | 2010-08-19 15:34:10 +0800 | [diff] [blame] | 341 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 342 | return NO_ERROR; |
| 343 | } |
| 344 | |
| 345 | void Visualizer::periodicCapture() |
| 346 | { |
Glenn Kasten | a9b21c5 | 2012-01-17 10:06:38 -0800 | [diff] [blame] | 347 | Mutex::Autolock _l(mCaptureLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 348 | ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x", |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 349 | this, mCaptureCallBack, mCaptureFlags); |
| 350 | if (mCaptureCallBack != NULL && |
| 351 | (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) && |
| 352 | mCaptureSize != 0) { |
| 353 | uint8_t waveform[mCaptureSize]; |
| 354 | status_t status = getWaveForm(waveform); |
| 355 | if (status != NO_ERROR) { |
| 356 | return; |
| 357 | } |
| 358 | uint8_t fft[mCaptureSize]; |
| 359 | if (mCaptureFlags & CAPTURE_FFT) { |
| 360 | status = doFft(fft, waveform); |
| 361 | } |
| 362 | if (status != NO_ERROR) { |
| 363 | return; |
| 364 | } |
| 365 | uint8_t *wavePtr = NULL; |
| 366 | uint8_t *fftPtr = NULL; |
| 367 | uint32_t waveSize = 0; |
| 368 | uint32_t fftSize = 0; |
| 369 | if (mCaptureFlags & CAPTURE_WAVEFORM) { |
| 370 | wavePtr = waveform; |
| 371 | waveSize = mCaptureSize; |
| 372 | } |
| 373 | if (mCaptureFlags & CAPTURE_FFT) { |
| 374 | fftPtr = fft; |
| 375 | fftSize = mCaptureSize; |
| 376 | } |
| 377 | mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | uint32_t Visualizer::initCaptureSize() |
| 382 | { |
| 383 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 384 | effect_param_t *p = (effect_param_t *)buf32; |
| 385 | |
| 386 | p->psize = sizeof(uint32_t); |
| 387 | p->vsize = sizeof(uint32_t); |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 388 | *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 389 | status_t status = getParameter(p); |
| 390 | |
| 391 | if (status == NO_ERROR) { |
| 392 | status = p->status; |
| 393 | } |
| 394 | |
| 395 | uint32_t size = 0; |
| 396 | if (status == NO_ERROR) { |
| 397 | size = *((int32_t *)p->data + 1); |
| 398 | } |
| 399 | mCaptureSize = size; |
| 400 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 401 | ALOGV("initCaptureSize size %d status %d", mCaptureSize, status); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 402 | |
| 403 | return size; |
| 404 | } |
| 405 | |
Jean-Michel Trivi | 3476de6 | 2012-04-15 17:15:07 -0700 | [diff] [blame] | 406 | void Visualizer::controlStatusChanged(bool controlGranted) { |
| 407 | if (controlGranted) { |
| 408 | // this Visualizer instance regained control of the effect, reset the scaling mode |
| 409 | // and capture size as has been cached through it. |
| 410 | ALOGV("controlStatusChanged(true) causes effect parameter reset:"); |
| 411 | ALOGV(" scaling mode reset to %d", mScalingMode); |
| 412 | setScalingMode(mScalingMode); |
| 413 | ALOGV(" capture size reset to %d", mCaptureSize); |
| 414 | setCaptureSize(mCaptureSize); |
| 415 | } |
| 416 | AudioEffect::controlStatusChanged(controlGranted); |
| 417 | } |
| 418 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 419 | //------------------------------------------------------------------------- |
| 420 | |
zengjing | 704c577 | 2018-09-29 13:25:35 +0800 | [diff] [blame] | 421 | Visualizer::CaptureThread::CaptureThread(Visualizer* receiver, uint32_t captureRate, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 422 | bool bCanCallJava) |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 423 | : Thread(bCanCallJava), mReceiver(receiver) |
| 424 | { |
| 425 | mSleepTimeUs = 1000000000 / captureRate; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 426 | ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | bool Visualizer::CaptureThread::threadLoop() |
| 430 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 431 | ALOGV("CaptureThread %p enter", this); |
zengjing | 704c577 | 2018-09-29 13:25:35 +0800 | [diff] [blame] | 432 | sp<Visualizer> receiver = mReceiver.promote(); |
| 433 | if (receiver == NULL) { |
| 434 | return false; |
| 435 | } |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 436 | while (!exitPending()) |
| 437 | { |
| 438 | usleep(mSleepTimeUs); |
zengjing | 704c577 | 2018-09-29 13:25:35 +0800 | [diff] [blame] | 439 | receiver->periodicCapture(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 440 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 441 | ALOGV("CaptureThread %p exiting", this); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 445 | } // namespace android |