| 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_NEAR_FIELD_PROCESSOR_H_ |
| 18 | #define RESONANCE_AUDIO_DSP_NEAR_FIELD_PROCESSOR_H_ |
| 19 | |
| 20 | #include "base/audio_buffer.h" |
| 21 | #include "dsp/biquad_filter.h" |
| 22 | #include "dsp/delay_filter.h" |
| 23 | |
| 24 | namespace vraudio { |
| 25 | |
| 26 | // Class which applies an approximate near field effect to a source mono input. |
| 27 | // The effect consists of a +6dB bass boost (shelf-filter) as well as phase |
| 28 | // correction (HRTF group delay compensation) when the signal is to be combined |
| 29 | // with the binaural output. |
| 30 | // |
| 31 | // For more information: [internal ref] |
| 32 | class NearFieldProcessor { |
| 33 | public: |
| 34 | // Constructor of the |NearFieldProcessor|, uses the following parameters: |
| 35 | // |
| 36 | // @param sample_rate Sampling rate in [Hz]. |
| 37 | // @param frames_per_buffer Number of frames per buffer in the input/output |
| 38 | // signal. |
| 39 | NearFieldProcessor(int sample_rate, size_t frames_per_buffer); |
| 40 | |
| 41 | // Returns currently used delay compensation in samples. |
| 42 | size_t GetDelayCompensation() const { return delay_compensation_; } |
| 43 | |
| 44 | // Applies approximate near field effect to the source mono input signal. |
| 45 | // |
| 46 | // @param input Mono input channel. |
| 47 | // @param output Pointer to mono output channel. |
| 48 | // @param enable_hrtf Whether to enable delay compensation for HRTF filtering. |
| 49 | void Process(const AudioBuffer::Channel& input, AudioBuffer::Channel* output, |
| 50 | bool enable_hrtf); |
| 51 | |
| 52 | private: |
| 53 | // Number of frames per buffer. |
| 54 | const size_t frames_per_buffer_; |
| 55 | |
| 56 | // Delay compensation computed as average group delay of the HRTF filter |
| 57 | // minus average group delay of the shelf-filter. Should be disabled when |
| 58 | // using with stereo-panned sound sources. |
| 59 | const size_t delay_compensation_; |
| 60 | |
| 61 | // Biquad filters that apply frequency splitting of the input mono signal. |
| 62 | BiquadFilter lo_pass_filter_; |
| 63 | BiquadFilter hi_pass_filter_; |
| 64 | |
| 65 | // Buffer for the low-passed signal. We do not modify the high-passed signal |
| 66 | // so we can write it directly to the output channel. |
| 67 | AudioBuffer low_passed_buffer_; |
| 68 | |
| 69 | // Delay filter used to delay the incoming input mono buffer. |
| 70 | DelayFilter delay_filter_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace vraudio |
| 74 | |
| 75 | #endif // RESONANCE_AUDIO_DSP_NEAR_FIELD_PROCESSOR_H_ |
| 76 | |