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