Home | History | Annotate | Download | only in webaudio
      1 /*
      2  * Copyright (C) 2011, 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  * 1.  Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2.  Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23  */
     24 
     25 #include "config.h"
     26 
     27 #if ENABLE(WEB_AUDIO)
     28 
     29 #include "modules/webaudio/BiquadFilterNode.h"
     30 
     31 namespace WebCore {
     32 
     33 BiquadFilterNode::BiquadFilterNode(AudioContext* context, float sampleRate)
     34     : AudioBasicProcessorNode(context, sampleRate)
     35 {
     36     ScriptWrappable::init(this);
     37     // Initially setup as lowpass filter.
     38     m_processor = adoptPtr(new BiquadProcessor(context, sampleRate, 1, false));
     39     setNodeType(NodeTypeBiquadFilter);
     40 }
     41 
     42 String BiquadFilterNode::type() const
     43 {
     44     switch (const_cast<BiquadFilterNode*>(this)->biquadProcessor()->type()) {
     45     case BiquadProcessor::LowPass:
     46         return "lowpass";
     47     case BiquadProcessor::HighPass:
     48         return "highpass";
     49     case BiquadProcessor::BandPass:
     50         return "bandpass";
     51     case BiquadProcessor::LowShelf:
     52         return "lowshelf";
     53     case BiquadProcessor::HighShelf:
     54         return "highshelf";
     55     case BiquadProcessor::Peaking:
     56         return "peaking";
     57     case BiquadProcessor::Notch:
     58         return "notch";
     59     case BiquadProcessor::Allpass:
     60         return "allpass";
     61     default:
     62         ASSERT_NOT_REACHED();
     63         return "lowpass";
     64     }
     65 }
     66 
     67 void BiquadFilterNode::setType(const String& type)
     68 {
     69     if (type == "lowpass")
     70         setType(BiquadProcessor::LowPass);
     71     else if (type == "highpass")
     72         setType(BiquadProcessor::HighPass);
     73     else if (type == "bandpass")
     74         setType(BiquadProcessor::BandPass);
     75     else if (type == "lowshelf")
     76         setType(BiquadProcessor::LowShelf);
     77     else if (type == "highshelf")
     78         setType(BiquadProcessor::HighShelf);
     79     else if (type == "peaking")
     80         setType(BiquadProcessor::Peaking);
     81     else if (type == "notch")
     82         setType(BiquadProcessor::Notch);
     83     else if (type == "allpass")
     84         setType(BiquadProcessor::Allpass);
     85     else
     86         ASSERT_NOT_REACHED();
     87 }
     88 
     89 bool BiquadFilterNode::setType(unsigned type)
     90 {
     91     if (type > BiquadProcessor::Allpass)
     92         return false;
     93 
     94     biquadProcessor()->setType(static_cast<BiquadProcessor::FilterType>(type));
     95     return true;
     96 }
     97 
     98 void BiquadFilterNode::getFrequencyResponse(const Float32Array* frequencyHz,
     99                                             Float32Array* magResponse,
    100                                             Float32Array* phaseResponse)
    101 {
    102     if (!frequencyHz || !magResponse || !phaseResponse)
    103         return;
    104 
    105     int n = std::min(frequencyHz->length(),
    106                      std::min(magResponse->length(), phaseResponse->length()));
    107 
    108     if (n) {
    109         biquadProcessor()->getFrequencyResponse(n,
    110                                                 frequencyHz->data(),
    111                                                 magResponse->data(),
    112                                                 phaseResponse->data());
    113     }
    114 }
    115 
    116 } // namespace WebCore
    117 
    118 #endif // ENABLE(WEB_AUDIO)
    119