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 "AudioDSPKernelProcessor.h"
     36 
     37 #include "AudioDSPKernel.h"
     38 
     39 namespace WebCore {
     40 
     41 // setNumberOfChannels() may later be called if the object is not yet in an "initialized" state.
     42 AudioDSPKernelProcessor::AudioDSPKernelProcessor(double sampleRate, unsigned numberOfChannels)
     43     : AudioProcessor(sampleRate)
     44     , m_numberOfChannels(numberOfChannels)
     45     , m_hasJustReset(true)
     46 {
     47 }
     48 
     49 void AudioDSPKernelProcessor::initialize()
     50 {
     51     if (isInitialized())
     52         return;
     53 
     54     ASSERT(!m_kernels.size());
     55 
     56     // Create processing kernels, one per channel.
     57     for (unsigned i = 0; i < numberOfChannels(); ++i)
     58         m_kernels.append(createKernel());
     59 
     60     m_initialized = true;
     61 }
     62 
     63 void AudioDSPKernelProcessor::uninitialize()
     64 {
     65     if (!isInitialized())
     66         return;
     67 
     68     m_kernels.clear();
     69 
     70     m_initialized = false;
     71 }
     72 
     73 void AudioDSPKernelProcessor::process(AudioBus* source, AudioBus* destination, size_t framesToProcess)
     74 {
     75     ASSERT(source && destination);
     76     if (!source || !destination)
     77         return;
     78 
     79     if (!isInitialized()) {
     80         destination->zero();
     81         return;
     82     }
     83 
     84     bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
     85     ASSERT(channelCountMatches);
     86     if (!channelCountMatches)
     87         return;
     88 
     89     for (unsigned i = 0; i < m_kernels.size(); ++i)
     90         m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->data(), framesToProcess);
     91 }
     92 
     93 // Resets filter state
     94 void AudioDSPKernelProcessor::reset()
     95 {
     96     if (!isInitialized())
     97         return;
     98 
     99     // Forces snap to parameter values - first time.
    100     // Any processing depending on this value must set it to false at the appropriate time.
    101     m_hasJustReset = true;
    102 
    103     for (unsigned i = 0; i < m_kernels.size(); ++i)
    104         m_kernels[i]->reset();
    105 }
    106 
    107 void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels)
    108 {
    109     ASSERT(!isInitialized());
    110     if (!isInitialized())
    111         m_numberOfChannels = numberOfChannels;
    112 }
    113 
    114 } // namespace WebCore
    115 
    116 #endif // ENABLE(WEB_AUDIO)
    117