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
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 
     31 #if ENABLE(WEB_AUDIO)
     32 
     33 #include "ReverbConvolver.h"
     34 
     35 #include "VectorMath.h"
     36 #include "AudioBus.h"
     37 
     38 namespace WebCore {
     39 
     40 using namespace VectorMath;
     41 
     42 const int InputBufferSize = 8 * 16384;
     43 
     44 // We only process the leading portion of the impulse response in the real-time thread.  We don't exceed this length.
     45 // It turns out then, that the background thread has about 278msec of scheduling slop.
     46 // Empirically, this has been found to be a good compromise between giving enough time for scheduling slop,
     47 // while still minimizing the amount of processing done in the primary (high-priority) thread.
     48 // This was found to be a good value on Mac OS X, and may work well on other platforms as well, assuming
     49 // the very rough scheduling latencies are similar on these time-scales.  Of course, this code may need to be
     50 // tuned for individual platforms if this assumption is found to be incorrect.
     51 const size_t RealtimeFrameLimit = 8192  + 4096; // ~278msec @ 44.1KHz
     52 
     53 const size_t MinFFTSize = 256;
     54 const size_t MaxRealtimeFFTSize = 2048;
     55 
     56 static void* backgroundThreadEntry(void* threadData)
     57 {
     58     ReverbConvolver* reverbConvolver = static_cast<ReverbConvolver*>(threadData);
     59     reverbConvolver->backgroundThreadEntry();
     60     return 0;
     61 }
     62 
     63 ReverbConvolver::ReverbConvolver(AudioChannel* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads)
     64     : m_impulseResponseLength(impulseResponse->length())
     65     , m_accumulationBuffer(impulseResponse->length() + renderSliceSize)
     66     , m_inputBuffer(InputBufferSize)
     67     , m_renderSliceSize(renderSliceSize)
     68     , m_minFFTSize(MinFFTSize) // First stage will have this size - successive stages will double in size each time
     69     , m_maxFFTSize(maxFFTSize) // until we hit m_maxFFTSize
     70     , m_useBackgroundThreads(useBackgroundThreads)
     71     , m_backgroundThread(0)
     72     , m_wantsToExit(false)
     73     , m_moreInputBuffered(false)
     74 {
     75     // If we are using background threads then don't exceed this FFT size for the
     76     // stages which run in the real-time thread.  This avoids having only one or two
     77     // large stages (size 16384 or so) at the end which take a lot of time every several
     78     // processing slices.  This way we amortize the cost over more processing slices.
     79     m_maxRealtimeFFTSize = MaxRealtimeFFTSize;
     80 
     81     // For the moment, a good way to know if we have real-time constraint is to check if we're using background threads.
     82     // Otherwise, assume we're being run from a command-line tool.
     83     bool hasRealtimeConstraint = useBackgroundThreads;
     84 
     85     float* response = impulseResponse->data();
     86     size_t totalResponseLength = impulseResponse->length();
     87 
     88     // Because we're not using direct-convolution in the leading portion, the reverb has an overall latency of half the first-stage FFT size
     89     size_t reverbTotalLatency = m_minFFTSize / 2;
     90 
     91     size_t stageOffset = 0;
     92     int i = 0;
     93     size_t fftSize = m_minFFTSize;
     94     while (stageOffset < totalResponseLength) {
     95         size_t stageSize = fftSize / 2;
     96 
     97         // For the last stage, it's possible that stageOffset is such that we're straddling the end
     98         // of the impulse response buffer (if we use stageSize), so reduce the last stage's length...
     99         if (stageSize + stageOffset > totalResponseLength)
    100             stageSize = totalResponseLength - stageOffset;
    101 
    102         // This "staggers" the time when each FFT happens so they don't all happen at the same time
    103         int renderPhase = convolverRenderPhase + i * renderSliceSize;
    104 
    105         OwnPtr<ReverbConvolverStage> stage(new ReverbConvolverStage(response, totalResponseLength, reverbTotalLatency, stageOffset, stageSize, fftSize, renderPhase, renderSliceSize, &m_accumulationBuffer));
    106 
    107         bool isBackgroundStage = false;
    108 
    109         if (this->useBackgroundThreads() && stageOffset > RealtimeFrameLimit) {
    110             m_backgroundStages.append(stage.release());
    111             isBackgroundStage = true;
    112         } else
    113             m_stages.append(stage.release());
    114 
    115         stageOffset += stageSize;
    116         ++i;
    117 
    118         // Figure out next FFT size
    119         fftSize *= 2;
    120         if (hasRealtimeConstraint && !isBackgroundStage && fftSize > m_maxRealtimeFFTSize)
    121             fftSize = m_maxRealtimeFFTSize;
    122         if (fftSize > m_maxFFTSize)
    123             fftSize = m_maxFFTSize;
    124     }
    125 
    126     // Start up background thread
    127     // FIXME: would be better to up the thread priority here.  It doesn't need to be real-time, but higher than the default...
    128     if (this->useBackgroundThreads() && m_backgroundStages.size() > 0)
    129         m_backgroundThread = createThread(WebCore::backgroundThreadEntry, this, "convolution background thread");
    130 }
    131 
    132 ReverbConvolver::~ReverbConvolver()
    133 {
    134     // Wait for background thread to stop
    135     if (useBackgroundThreads() && m_backgroundThread) {
    136         m_wantsToExit = true;
    137 
    138         // Wake up thread so it can return
    139         {
    140             MutexLocker locker(m_backgroundThreadLock);
    141             m_moreInputBuffered = true;
    142             m_backgroundThreadCondition.signal();
    143         }
    144 
    145         waitForThreadCompletion(m_backgroundThread, 0);
    146     }
    147 }
    148 
    149 void ReverbConvolver::backgroundThreadEntry()
    150 {
    151     while (!m_wantsToExit) {
    152         // Wait for realtime thread to give us more input
    153         m_moreInputBuffered = false;
    154         {
    155             MutexLocker locker(m_backgroundThreadLock);
    156             while (!m_moreInputBuffered && !m_wantsToExit)
    157                 m_backgroundThreadCondition.wait(m_backgroundThreadLock);
    158         }
    159 
    160         // Process all of the stages until their read indices reach the input buffer's write index
    161         int writeIndex = m_inputBuffer.writeIndex();
    162 
    163         // Even though it doesn't seem like every stage needs to maintain its own version of readIndex
    164         // we do this in case we want to run in more than one background thread.
    165         int readIndex;
    166 
    167         while ((readIndex = m_backgroundStages[0]->inputReadIndex()) != writeIndex) { // FIXME: do better to detect buffer overrun...
    168             // The ReverbConvolverStages need to process in amounts which evenly divide half the FFT size
    169             const int SliceSize = MinFFTSize / 2;
    170 
    171             // Accumulate contributions from each stage
    172             for (size_t i = 0; i < m_backgroundStages.size(); ++i)
    173                 m_backgroundStages[i]->processInBackground(this, SliceSize);
    174         }
    175     }
    176 }
    177 
    178 void ReverbConvolver::process(AudioChannel* sourceChannel, AudioChannel* destinationChannel, size_t framesToProcess)
    179 {
    180     bool isSafe = sourceChannel && destinationChannel && sourceChannel->length() >= framesToProcess && destinationChannel->length() >= framesToProcess;
    181     ASSERT(isSafe);
    182     if (!isSafe)
    183         return;
    184 
    185     float* source = sourceChannel->data();
    186     float* destination = destinationChannel->data();
    187     bool isDataSafe = source && destination;
    188     ASSERT(isDataSafe);
    189     if (!isDataSafe)
    190         return;
    191 
    192     // Feed input buffer (read by all threads)
    193     m_inputBuffer.write(source, framesToProcess);
    194 
    195     // Accumulate contributions from each stage
    196     for (size_t i = 0; i < m_stages.size(); ++i)
    197         m_stages[i]->process(source, framesToProcess);
    198 
    199     // Finally read from accumulation buffer
    200     m_accumulationBuffer.readAndClear(destination, framesToProcess);
    201 
    202     // Now that we've buffered more input, wake up our background thread.
    203 
    204     // Not using a MutexLocker looks strange, but we use a tryLock() instead because this is run on the real-time
    205     // thread where it is a disaster for the lock to be contended (causes audio glitching).  It's OK if we fail to
    206     // signal from time to time, since we'll get to it the next time we're called.  We're called repeatedly
    207     // and frequently (around every 3ms).  The background thread is processing well into the future and has a considerable amount of
    208     // leeway here...
    209     if (m_backgroundThreadLock.tryLock()) {
    210         m_moreInputBuffered = true;
    211         m_backgroundThreadCondition.signal();
    212         m_backgroundThreadLock.unlock();
    213     }
    214 }
    215 
    216 void ReverbConvolver::reset()
    217 {
    218     for (size_t i = 0; i < m_stages.size(); ++i)
    219         m_stages[i]->reset();
    220 
    221     for (size_t i = 0; i < m_backgroundStages.size(); ++i)
    222         m_backgroundStages[i]->reset();
    223 
    224     m_accumulationBuffer.reset();
    225     m_inputBuffer.reset();
    226 }
    227 
    228 } // namespace WebCore
    229 
    230 #endif // ENABLE(WEB_AUDIO)
    231