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
31namespace blink {
32
33BiquadFilterNode::BiquadFilterNode(AudioContext* context, float sampleRate)
34    : AudioBasicProcessorNode(context, sampleRate)
35{
36    // Initially setup as lowpass filter.
37    m_processor = new BiquadProcessor(context, sampleRate, 1, false);
38    setNodeType(NodeTypeBiquadFilter);
39}
40
41String BiquadFilterNode::type() const
42{
43    switch (const_cast<BiquadFilterNode*>(this)->biquadProcessor()->type()) {
44    case BiquadProcessor::LowPass:
45        return "lowpass";
46    case BiquadProcessor::HighPass:
47        return "highpass";
48    case BiquadProcessor::BandPass:
49        return "bandpass";
50    case BiquadProcessor::LowShelf:
51        return "lowshelf";
52    case BiquadProcessor::HighShelf:
53        return "highshelf";
54    case BiquadProcessor::Peaking:
55        return "peaking";
56    case BiquadProcessor::Notch:
57        return "notch";
58    case BiquadProcessor::Allpass:
59        return "allpass";
60    default:
61        ASSERT_NOT_REACHED();
62        return "lowpass";
63    }
64}
65
66void BiquadFilterNode::setType(const String& type)
67{
68    if (type == "lowpass")
69        setType(BiquadProcessor::LowPass);
70    else if (type == "highpass")
71        setType(BiquadProcessor::HighPass);
72    else if (type == "bandpass")
73        setType(BiquadProcessor::BandPass);
74    else if (type == "lowshelf")
75        setType(BiquadProcessor::LowShelf);
76    else if (type == "highshelf")
77        setType(BiquadProcessor::HighShelf);
78    else if (type == "peaking")
79        setType(BiquadProcessor::Peaking);
80    else if (type == "notch")
81        setType(BiquadProcessor::Notch);
82    else if (type == "allpass")
83        setType(BiquadProcessor::Allpass);
84}
85
86bool BiquadFilterNode::setType(unsigned type)
87{
88    if (type > BiquadProcessor::Allpass)
89        return false;
90
91    biquadProcessor()->setType(static_cast<BiquadProcessor::FilterType>(type));
92    return true;
93}
94
95void BiquadFilterNode::getFrequencyResponse(const Float32Array* frequencyHz,
96                                            Float32Array* magResponse,
97                                            Float32Array* phaseResponse)
98{
99    if (!frequencyHz || !magResponse || !phaseResponse)
100        return;
101
102    int n = std::min(frequencyHz->length(),
103                     std::min(magResponse->length(), phaseResponse->length()));
104
105    if (n) {
106        biquadProcessor()->getFrequencyResponse(n,
107                                                frequencyHz->data(),
108                                                magResponse->data(),
109                                                phaseResponse->data());
110    }
111}
112
113} // namespace blink
114
115#endif // ENABLE(WEB_AUDIO)
116