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 39namespace WebCore { 40 41// setNumberOfChannels() may later be called if the object is not yet in an "initialized" state. 42AudioDSPKernelProcessor::AudioDSPKernelProcessor(float sampleRate, unsigned numberOfChannels) 43 : AudioProcessor(sampleRate, numberOfChannels) 44 , m_hasJustReset(true) 45{ 46} 47 48void AudioDSPKernelProcessor::initialize() 49{ 50 if (isInitialized()) 51 return; 52 53 ASSERT(!m_kernels.size()); 54 55 // Create processing kernels, one per channel. 56 for (unsigned i = 0; i < numberOfChannels(); ++i) 57 m_kernels.append(createKernel()); 58 59 m_initialized = true; 60 m_hasJustReset = true; 61} 62 63void AudioDSPKernelProcessor::uninitialize() 64{ 65 if (!isInitialized()) 66 return; 67 68 m_kernels.clear(); 69 70 m_initialized = false; 71} 72 73void AudioDSPKernelProcessor::process(const 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)->mutableData(), framesToProcess); 91} 92 93// Resets filter state 94void 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 107void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels) 108{ 109 if (numberOfChannels == m_numberOfChannels) 110 return; 111 112 ASSERT(!isInitialized()); 113 if (!isInitialized()) 114 m_numberOfChannels = numberOfChannels; 115} 116 117double AudioDSPKernelProcessor::tailTime() const 118{ 119 // It is expected that all the kernels have the same tailTime. 120 return !m_kernels.isEmpty() ? m_kernels.first()->tailTime() : 0; 121} 122 123double AudioDSPKernelProcessor::latencyTime() const 124{ 125 // It is expected that all the kernels have the same latencyTime. 126 return !m_kernels.isEmpty() ? m_kernels.first()->latencyTime() : 0; 127} 128 129} // namespace WebCore 130 131#endif // ENABLE(WEB_AUDIO) 132