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
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_BIQUAD_FILTER_NODE_H_
27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_BIQUAD_FILTER_NODE_H_
28 
29 #include "base/memory/weak_ptr.h"
30 #include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
31 #include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
32 #include "third_party/blink/renderer/modules/webaudio/audio_basic_processor_handler.h"
33 #include "third_party/blink/renderer/modules/webaudio/audio_node.h"
34 #include "third_party/blink/renderer/modules/webaudio/biquad_processor.h"
35 
36 namespace blink {
37 
38 class BaseAudioContext;
39 class AudioParam;
40 class BiquadFilterOptions;
41 
42 class BiquadFilterHandler : public AudioBasicProcessorHandler,
43                             public base::SupportsWeakPtr<BiquadFilterHandler> {
44  public:
45   static scoped_refptr<BiquadFilterHandler> Create(AudioNode&,
46                                                    float sample_rate,
47                                                    AudioParamHandler& frequency,
48                                                    AudioParamHandler& q,
49                                                    AudioParamHandler& gain,
50                                                    AudioParamHandler& detune);
51 
52   void Process(uint32_t frames_to_process) override;
53 
54  private:
55   BiquadFilterHandler(AudioNode&,
56                       float sample_rate,
57                       AudioParamHandler& frequency,
58                       AudioParamHandler& q,
59                       AudioParamHandler& gain,
60                       AudioParamHandler& detune);
61 
62   void NotifyBadState() const;
63 
64   // Only notify the user of the once.  No need to spam the console with
65   // messages, because once we're in a bad state, it usually stays that way
66   // forever.  Only accessed from audio thread.
67   bool did_warn_bad_filter_state_ = false;
68 
69   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
70 };
71 
72 class BiquadFilterNode final : public AudioNode {
73   DEFINE_WRAPPERTYPEINFO();
74 
75  public:
76   // These must be defined as in the .idl file and must match those in the
77   // BiquadProcessor class.
78   enum {
79     LOWPASS = 0,
80     HIGHPASS = 1,
81     BANDPASS = 2,
82     LOWSHELF = 3,
83     HIGHSHELF = 4,
84     PEAKING = 5,
85     NOTCH = 6,
86     ALLPASS = 7
87   };
88 
89   static BiquadFilterNode* Create(BaseAudioContext&, ExceptionState&);
90   static BiquadFilterNode* Create(BaseAudioContext*,
91                                   const BiquadFilterOptions*,
92                                   ExceptionState&);
93 
94   BiquadFilterNode(BaseAudioContext&);
95 
96   void Trace(Visitor*) const override;
97 
98   String type() const;
99   void setType(const String&);
100 
frequency()101   AudioParam* frequency() { return frequency_; }
q()102   AudioParam* q() { return q_; }
gain()103   AudioParam* gain() { return gain_; }
detune()104   AudioParam* detune() { return detune_; }
105 
106   // Get the magnitude and phase response of the filter at the given
107   // set of frequencies (in Hz). The phase response is in radians.
108   void getFrequencyResponse(NotShared<const DOMFloat32Array> frequency_hz,
109                             NotShared<DOMFloat32Array> mag_response,
110                             NotShared<DOMFloat32Array> phase_response,
111                             ExceptionState&);
112 
113   // InspectorHelperMixin
114   void ReportDidCreate() final;
115   void ReportWillBeDestroyed() final;
116 
117  private:
118   BiquadProcessor* GetBiquadProcessor() const;
119   bool SetType(BiquadProcessor::FilterType);  // Returns true on success.
120 
121   Member<AudioParam> frequency_;
122   Member<AudioParam> q_;
123   Member<AudioParam> gain_;
124   Member<AudioParam> detune_;
125 };
126 
127 }  // namespace blink
128 
129 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_BIQUAD_FILTER_NODE_H_
130