| 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_MULTI_CHANNEL_IIR_H_ |
| 18 | #define RESONANCE_AUDIO_DSP_MULTI_CHANNEL_IIR_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/audio_buffer.h" |
| 24 | |
| 25 | namespace vraudio { |
| 26 | |
| 27 | // Class that performs IIR filtering on interleaved data. This class can be used |
| 28 | // to implement any order of IIR filter on multichannel data where the number of |
| 29 | // channels is a multiple of the SIMD vector length. |
| 30 | class MultiChannelIir { |
| 31 | public: |
| 32 | // Returns a |MultiChannelIir| given valid parameters. |
| 33 | // |
| 34 | // @param num_channels Number of channels in each input buffer. The number of |
| 35 | // channels must be divisible by |SIMD_LENGTH|. |
| 36 | // @param frames_per_buffer Number of frames in each input buffer. |
| 37 | // @param numerators Numerator coefficients, one set per channel. |
| 38 | // @param denominators Denominator coefficients, should be equal in length to |
| 39 | // the |numerator| vector, one set per channel. |
| 40 | // @return A |MultiChannelIir| instance. |
| 41 | static std::unique_ptr<MultiChannelIir> Create( |
| 42 | size_t num_channels, size_t frames_per_buffer, |
| 43 | const std::vector<std::vector<float>>& numerators, |
| 44 | const std::vector<std::vector<float>>& denominators); |
| 45 | |
| 46 | // Processes an interleaved buffer of input data with the given IIR filter. |
| 47 | // |
| 48 | // @param interleaved_buffer A single channel of data containing input in |
| 49 | // interleaved format, this will contain output data in interleaved format |
| 50 | // on return. |
| 51 | void Process(AudioBuffer::Channel* interleaved_buffer); |
| 52 | |
| 53 | private: |
| 54 | // Constructs a |MultiChannelIir|. |
| 55 | // |
| 56 | // @param num_channels Number of channels in each input buffer. The number of |
| 57 | // channels must be divisible by |SIMD_LENGTH|. |
| 58 | // @param frames_per_buffer Number of frames in each input buffer. |
| 59 | // @param num_coefficients Number of coefficients in the numerator, which |
| 60 | // equals the number in the denominator. |
| 61 | MultiChannelIir(size_t num_channels, size_t frames_per_buffer, |
| 62 | size_t num_coefficients); |
| 63 | |
| 64 | // Number of channels in each input buffer. |
| 65 | const size_t num_channels_; |
| 66 | |
| 67 | // Number of frames in each input buffer. |
| 68 | const size_t frames_per_buffer_; |
| 69 | |
| 70 | // Number of coefficients in the numerator and denominator polynomials. |
| 71 | const size_t num_coefficients_; |
| 72 | |
| 73 | // Current front of the delay line which is circularly indexed. |
| 74 | size_t delay_line_front_; |
| 75 | |
| 76 | // Stores numerator coefficients in repeated fashion. |
| 77 | AudioBuffer numerator_; |
| 78 | |
| 79 | // Stores denominator coefficients in repeated fashion. |
| 80 | AudioBuffer denominator_; |
| 81 | |
| 82 | // Holds previous data computed from the numerator section. |
| 83 | AudioBuffer delay_line_; |
| 84 | }; |
| 85 | |
| 86 | } // namespace vraudio |
| 87 | |
| 88 | #endif // RESONANCE_AUDIO_DSP_MULTI_CHANNEL_IIR_H_ |
| 89 | |