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