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 "platform/audio/ReverbConvolver.h"
     34 
     35 #include "platform/Task.h"
     36 #include "platform/audio/AudioBus.h"
     37 #include "platform/audio/VectorMath.h"
     38 #include "public/platform/Platform.h"
     39 #include "public/platform/WebThread.h"
     40 
     41 namespace blink {
     42 
     43 using namespace VectorMath;
     44 
     45 const int InputBufferSize = 8 * 16384;
     46 
     47 // We only process the leading portion of the impulse response in the real-time thread.  We don't exceed this length.
     48 // It turns out then, that the background thread has about 278msec of scheduling slop.
     49 // Empirically, this has been found to be a good compromise between giving enough time for scheduling slop,
     50 // while still minimizing the amount of processing done in the primary (high-priority) thread.
     51 // This was found to be a good value on Mac OS X, and may work well on other platforms as well, assuming
     52 // the very rough scheduling latencies are similar on these time-scales.  Of course, this code may need to be
     53 // tuned for individual platforms if this assumption is found to be incorrect.
     54 const size_t RealtimeFrameLimit = 8192  + 4096; // ~278msec @ 44.1KHz
     55 
     56 const size_t MinFFTSize = 128;
     57 const size_t MaxRealtimeFFTSize = 2048;
     58 
     59 ReverbConvolver::ReverbConvolver(AudioChannel* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads)
     60     : m_impulseResponseLength(impulseResponse->length())
     61     , m_accumulationBuffer(impulseResponse->length() + renderSliceSize)
     62     , m_inputBuffer(InputBufferSize)
     63     , m_minFFTSize(MinFFTSize) // First stage will have this size - successive stages will double in size each time
     64     , m_maxFFTSize(maxFFTSize) // until we hit m_maxFFTSize
     65 {
     66     // If we are using background threads then don't exceed this FFT size for the
     67     // stages which run in the real-time thread.  This avoids having only one or two
     68     // large stages (size 16384 or so) at the end which take a lot of time every several
     69     // processing slices.  This way we amortize the cost over more processing slices.
     70     m_maxRealtimeFFTSize = MaxRealtimeFFTSize;
     71 
     72     // For the moment, a good way to know if we have real-time constraint is to check if we're using background threads.
     73     // Otherwise, assume we're being run from a command-line tool.
     74     bool hasRealtimeConstraint = useBackgroundThreads;
     75 
     76     const float* response = impulseResponse->data();
     77     size_t totalResponseLength = impulseResponse->length();
     78 
     79     // The total latency is zero because the direct-convolution is used in the leading portion.
     80     size_t reverbTotalLatency = 0;
     81 
     82     size_t stageOffset = 0;
     83     int i = 0;
     84     size_t fftSize = m_minFFTSize;
     85     while (stageOffset < totalResponseLength) {
     86         size_t stageSize = fftSize / 2;
     87 
     88         // For the last stage, it's possible that stageOffset is such that we're straddling the end
     89         // of the impulse response buffer (if we use stageSize), so reduce the last stage's length...
     90         if (stageSize + stageOffset > totalResponseLength)
     91             stageSize = totalResponseLength - stageOffset;
     92 
     93         // This "staggers" the time when each FFT happens so they don't all happen at the same time
     94         int renderPhase = convolverRenderPhase + i * renderSliceSize;
     95 
     96         bool useDirectConvolver = !stageOffset;
     97 
     98         OwnPtr<ReverbConvolverStage> stage = adoptPtr(new ReverbConvolverStage(response, totalResponseLength, reverbTotalLatency, stageOffset, stageSize, fftSize, renderPhase, renderSliceSize, &m_accumulationBuffer, useDirectConvolver));
     99 
    100         bool isBackgroundStage = false;
    101 
    102         if (useBackgroundThreads && stageOffset > RealtimeFrameLimit) {
    103             m_backgroundStages.append(stage.release());
    104             isBackgroundStage = true;
    105         } else
    106             m_stages.append(stage.release());
    107 
    108         stageOffset += stageSize;
    109         ++i;
    110 
    111         if (!useDirectConvolver) {
    112             // Figure out next FFT size
    113             fftSize *= 2;
    114         }
    115 
    116         if (hasRealtimeConstraint && !isBackgroundStage && fftSize > m_maxRealtimeFFTSize)
    117             fftSize = m_maxRealtimeFFTSize;
    118         if (fftSize > m_maxFFTSize)
    119             fftSize = m_maxFFTSize;
    120     }
    121 
    122     // Start up background thread
    123     // FIXME: would be better to up the thread priority here.  It doesn't need to be real-time, but higher than the default...
    124     if (useBackgroundThreads && m_backgroundStages.size() > 0)
    125         m_backgroundThread = adoptPtr(Platform::current()->createThread("Reverb convolution background thread"));
    126 }
    127 
    128 ReverbConvolver::~ReverbConvolver()
    129 {
    130     // Wait for background thread to stop
    131     m_backgroundThread.clear();
    132 }
    133 
    134 void ReverbConvolver::processInBackground()
    135 {
    136     // Process all of the stages until their read indices reach the input buffer's write index
    137     int writeIndex = m_inputBuffer.writeIndex();
    138 
    139     // Even though it doesn't seem like every stage needs to maintain its own version of readIndex
    140     // we do this in case we want to run in more than one background thread.
    141     int readIndex;
    142 
    143     while ((readIndex = m_backgroundStages[0]->inputReadIndex()) != writeIndex) { // FIXME: do better to detect buffer overrun...
    144         // The ReverbConvolverStages need to process in amounts which evenly divide half the FFT size
    145         const int SliceSize = MinFFTSize / 2;
    146 
    147         // Accumulate contributions from each stage
    148         for (size_t i = 0; i < m_backgroundStages.size(); ++i)
    149             m_backgroundStages[i]->processInBackground(this, SliceSize);
    150     }
    151 }
    152 
    153 void ReverbConvolver::process(const AudioChannel* sourceChannel, AudioChannel* destinationChannel, size_t framesToProcess)
    154 {
    155     bool isSafe = sourceChannel && destinationChannel && sourceChannel->length() >= framesToProcess && destinationChannel->length() >= framesToProcess;
    156     ASSERT(isSafe);
    157     if (!isSafe)
    158         return;
    159 
    160     const float* source = sourceChannel->data();
    161     float* destination = destinationChannel->mutableData();
    162     bool isDataSafe = source && destination;
    163     ASSERT(isDataSafe);
    164     if (!isDataSafe)
    165         return;
    166 
    167     // Feed input buffer (read by all threads)
    168     m_inputBuffer.write(source, framesToProcess);
    169 
    170     // Accumulate contributions from each stage
    171     for (size_t i = 0; i < m_stages.size(); ++i)
    172         m_stages[i]->process(source, framesToProcess);
    173 
    174     // Finally read from accumulation buffer
    175     m_accumulationBuffer.readAndClear(destination, framesToProcess);
    176 
    177     // Now that we've buffered more input, post another task to the background thread.
    178     if (m_backgroundThread)
    179         m_backgroundThread->postTask(new Task(WTF::bind(&ReverbConvolver::processInBackground, this)));
    180 }
    181 
    182 void ReverbConvolver::reset()
    183 {
    184     for (size_t i = 0; i < m_stages.size(); ++i)
    185         m_stages[i]->reset();
    186 
    187     for (size_t i = 0; i < m_backgroundStages.size(); ++i)
    188         m_backgroundStages[i]->reset();
    189 
    190     m_accumulationBuffer.reset();
    191     m_inputBuffer.reset();
    192 }
    193 
    194 size_t ReverbConvolver::latencyFrames() const
    195 {
    196     return 0;
    197 }
    198 
    199 } // namespace blink
    200 
    201 #endif // ENABLE(WEB_AUDIO)
    202