Home | History | Annotate | Download | only in audio
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 
     33 #if ENABLE(WEB_AUDIO)
     34 
     35 #include "core/platform/audio/AudioDSPKernelProcessor.h"
     36 
     37 #include "core/platform/audio/AudioDSPKernel.h"
     38 #include "wtf/MainThread.h"
     39 
     40 namespace WebCore {
     41 
     42 // setNumberOfChannels() may later be called if the object is not yet in an "initialized" state.
     43 AudioDSPKernelProcessor::AudioDSPKernelProcessor(float sampleRate, unsigned numberOfChannels)
     44     : AudioProcessor(sampleRate, numberOfChannels)
     45     , m_hasJustReset(true)
     46 {
     47 }
     48 
     49 void AudioDSPKernelProcessor::initialize()
     50 {
     51     if (isInitialized())
     52         return;
     53 
     54     MutexLocker locker(m_processLock);
     55     ASSERT(!m_kernels.size());
     56 
     57     // Create processing kernels, one per channel.
     58     for (unsigned i = 0; i < numberOfChannels(); ++i)
     59         m_kernels.append(createKernel());
     60 
     61     m_initialized = true;
     62     m_hasJustReset = true;
     63 }
     64 
     65 void AudioDSPKernelProcessor::uninitialize()
     66 {
     67     if (!isInitialized())
     68         return;
     69 
     70     MutexLocker locker(m_processLock);
     71     m_kernels.clear();
     72 
     73     m_initialized = false;
     74 }
     75 
     76 void AudioDSPKernelProcessor::process(const AudioBus* source, AudioBus* destination, size_t framesToProcess)
     77 {
     78     ASSERT(source && destination);
     79     if (!source || !destination)
     80         return;
     81 
     82     if (!isInitialized()) {
     83         destination->zero();
     84         return;
     85     }
     86 
     87     MutexTryLocker tryLocker(m_processLock);
     88     if (tryLocker.locked()) {
     89         bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
     90         ASSERT(channelCountMatches);
     91         if (!channelCountMatches)
     92             return;
     93 
     94         for (unsigned i = 0; i < m_kernels.size(); ++i)
     95             m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
     96     } else {
     97         // Unfortunately, the kernel is being processed by another thread.
     98         // See also ConvolverNode::process().
     99         destination->zero();
    100     }
    101 }
    102 
    103 // Resets filter state
    104 void AudioDSPKernelProcessor::reset()
    105 {
    106     ASSERT(isMainThread());
    107     if (!isInitialized())
    108         return;
    109 
    110     // Forces snap to parameter values - first time.
    111     // Any processing depending on this value must set it to false at the appropriate time.
    112     m_hasJustReset = true;
    113 
    114     MutexLocker locker(m_processLock);
    115     for (unsigned i = 0; i < m_kernels.size(); ++i)
    116         m_kernels[i]->reset();
    117 }
    118 
    119 void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels)
    120 {
    121     if (numberOfChannels == m_numberOfChannels)
    122         return;
    123 
    124     ASSERT(!isInitialized());
    125     if (!isInitialized())
    126         m_numberOfChannels = numberOfChannels;
    127 }
    128 
    129 double AudioDSPKernelProcessor::tailTime() const
    130 {
    131     ASSERT(!isMainThread());
    132     MutexTryLocker tryLocker(m_processLock);
    133     if (tryLocker.locked()) {
    134         // It is expected that all the kernels have the same tailTime.
    135         return !m_kernels.isEmpty() ? m_kernels.first()->tailTime() : 0;
    136     }
    137     // Since we don't want to block the Audio Device thread, we return a large value
    138     // instead of trying to acquire the lock.
    139     return std::numeric_limits<double>::infinity();
    140 }
    141 
    142 double AudioDSPKernelProcessor::latencyTime() const
    143 {
    144     ASSERT(!isMainThread());
    145     MutexTryLocker tryLocker(m_processLock);
    146     if (tryLocker.locked()) {
    147         // It is expected that all the kernels have the same latencyTime.
    148         return !m_kernels.isEmpty() ? m_kernels.first()->latencyTime() : 0;
    149     }
    150     // Since we don't want to block the Audio Device thread, we return a large value
    151     // instead of trying to acquire the lock.
    152     return std::numeric_limits<double>::infinity();
    153 }
    154 
    155 } // namespace WebCore
    156 
    157 #endif // ENABLE(WEB_AUDIO)
    158