RavEngine
Loading...
Searching...
No Matches
biquad_filter.h
1
/*
2
Copyright 2018 Google Inc. All Rights Reserved.
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS-IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
#ifndef RESONANCE_AUDIO_DSP_BIQUAD_FILTER_H_
18
#define RESONANCE_AUDIO_DSP_BIQUAD_FILTER_H_
19
20
#include <array>
21
22
#include "base/audio_buffer.h"
23
#include "base/logging.h"
24
25
namespace
vraudio {
26
27
// Set of transfer function coefficients.
28
struct
BiquadCoefficients
{
29
// Constructor takes as its arguments 6 floats representing the transfer
30
// function coefficients of a biquad filter.
31
BiquadCoefficients
(
float
a0,
float
a1,
float
a2,
float
b0,
float
b1,
float
b2)
32
: a({{a0, a1, a2}}), b({{b0, b1, b2}}) {}
33
34
// Default constructor that sets a0 and b0 == 1 so that if the coefficients
35
// are used, the filter is stable and has no effect on input. i.e. input
36
// appears to just pass through.
37
BiquadCoefficients
() :
BiquadCoefficients
(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) {}
38
39
// The denominator quadratic coefficients.
40
std::array<float, 3> a;
41
// The numerator quadratic coefficients.
42
std::array<float, 3> b;
43
};
44
45
// Class representing a biquad filter as its transfer function coefficients.
46
// This class also performs filtering.
47
class
BiquadFilter
{
48
public
:
49
// Constructs a BiquadFilter using a BiquadCoefficients struct.
50
//
51
// @param coefficients A BiquadCoefficients to set the internal
52
// |coefficients_|.
53
// @param frames_per_buffer The number of frames in each data buffer to be
54
// processed. This is used to set the number of samples to iterate over
55
// during a change of filter state.
56
BiquadFilter
(
const
BiquadCoefficients
& coefficients,
57
size_t
frames_per_buffer);
58
59
// Filter method for use with AudioBuffer::Channel.
60
//
61
// @param input_channel An AudioBuffer::Channel of input.
62
// @param output_channel A pointer to an AudioBuffer::Channel for output.
63
void
Filter(
const
AudioBuffer::Channel
& input_channel,
64
AudioBuffer::Channel
* output_channel);
65
66
// Sets the fiter's coefficents to those passed, with all
67
// coefficients bar a0 scaled by 1/a0.
68
//
69
// @param coefficients A set of BiquadCoefficients.
70
void
SetCoefficients(
const
BiquadCoefficients
& coefficients);
71
72
// Sets the target coefficients to be interpolated.
73
//
74
// @param coefficents The BiquadCoefficients we wish to interpolate to over
75
// samples_to_iterate_over_ samples of the next input buffer.
76
void
InterpolateToCoefficients(
const
BiquadCoefficients
& coefficients);
77
78
// Clears the internal state of the filter, except for coefficients.
79
void
Clear();
80
81
private
:
82
friend
class
BiquadFilterInterpolateTest
;
83
84
// Filters a single sample of input.
85
//
86
// @param input A single input sample from a Planar or Interleaved buffer.
87
// @param delay_line The delay_line to use (important for interpolation).
88
// @param coefficients The biquad coeffients for use in filtering.
89
// @return An output value to be placed in a Planar or Interleaved buffer.
90
float
FilterSample(
float
input, std::array<float, 2>* delay_line,
91
const
BiquadCoefficients
& coefficients);
92
93
// If InterpolateToState() has been called to assign new filter coefficients,
94
// this function will be called samples_to_iterate_over_ times within the next
95
// call of the Filter() function to slowly transition to the new coefficients
96
// which were passed to InterpolateToState() previously.
97
void
UpdateInterpolate();
98
99
// Filters a single sample of input while transitioning between coefficients.
100
// Performs a linear crossfade over samples_to_iterate_over_ samples.
101
//
102
// @param input A single input sample from a Planar or Interleaved buffer.
103
// @return Output value to be placed in an audio buffer.
104
float
InterpolateFilterSample(
float
input);
105
106
// Stores the memory state of the biquad
107
std::array<float, 2> biquad_delay_line_;
108
109
// Flag that denotes whether or not we are transitioning to another set of
110
// coefficients via interpolation.
111
bool
interpolate_flag_;
112
113
// Counter that denotes how far we are into transitioning to another set of
114
// coefficients via interpolation.
115
size_t
interpolate_counter_;
116
117
// Stores the memory state of the biquad.
118
std::array<float, 2> old_delay_line_;
119
120
// Number of samples over which to apply the filter coefficients.
121
size_t
samples_to_iterate_over_;
122
123
// Value used to crossfade between filter outputs.
124
float
fade_scale_;
125
126
// A set of coefficient which are updated as we interpolate between filter
127
// coefficients.
128
BiquadCoefficients
old_coefficients_;
129
130
// Represents and maintains the state of the biquad filter in terms of its
131
// transfer function coefficients.
132
BiquadCoefficients
coefficients_;
133
};
134
135
}
// namespace vraudio
136
137
#endif
// RESONANCE_AUDIO_DSP_BIQUAD_FILTER_H_
vraudio::BiquadFilterInterpolateTest
Definition
biquad_filter_test.cc:216
vraudio::BiquadFilter
Definition
biquad_filter.h:47
vraudio::ChannelView
Definition
channel_view.h:30
vraudio::BiquadCoefficients
Definition
biquad_filter.h:28
RavEngine
deps
resonance-audio
resonance_audio
dsp
biquad_filter.h
Generated by
1.9.8