| 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_REVERB_ONSET_COMPENSATOR_H_ |
| 18 | #define RESONANCE_AUDIO_DSP_REVERB_ONSET_COMPENSATOR_H_ |
| 19 | |
| 20 | #include <list> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/audio_buffer.h" |
| 24 | #include "dsp/delay_filter.h" |
| 25 | #include "dsp/fft_manager.h" |
| 26 | #include "dsp/partitioned_fft_filter.h" |
| 27 | #include "dsp/reverb_onset_update_processor.h" |
| 28 | |
| 29 | namespace vraudio { |
| 30 | |
| 31 | // Implements a convolutional compensator for the spectral reverb onset curve. |
| 32 | class ReverbOnsetCompensator { |
| 33 | public: |
| 34 | // Constructs a |ReverbOnsetCompensator|. |
| 35 | // |
| 36 | // @param sampling_rate The sampling rate in Hz. |
| 37 | // @param frames_per_buffer The number of frames per buffer in the system. |
| 38 | // @param fft_manager Pointer to a FftManager to perform FFT transformations. |
| 39 | ReverbOnsetCompensator(int sampling_rate, size_t frames_per_buffer, |
| 40 | FftManager* fft_manager); |
| 41 | |
| 42 | // Resets the reverb with a new set of reverberation times. The new tail is |
| 43 | // generated by replacing the current tail buffer by buffer. |
| 44 | // |
| 45 | // @param rt60_values |kNumReverbOctaveBands| values denoting the |
| 46 | // reverberation decay time to -60dB in octave bands starting at |
| 47 | // |kLowestOctaveBand|. |
| 48 | // @param gain Gain to be applied across all frequencies. |
| 49 | void Update(const float* rt60_values, float gain); |
| 50 | |
| 51 | // Processes a mono |AudioBuffer| with a reverberant tail. |
| 52 | // |
| 53 | // @param input A mono |AudioBuffer| of input data. |
| 54 | // @param output Pointer to stereo output buffer. |
| 55 | void Process(const AudioBuffer& input, AudioBuffer* output); |
| 56 | |
| 57 | private: |
| 58 | // Generates the constituent curves which are combined to make up the |
| 59 | // correction curve envelopes. These envelopes ensure the initial part of the |
| 60 | // specral reverb's impulse response, which exhibits 'growing' behaviour, |
| 61 | // follows the desired exponential decay. |
| 62 | void GenerateCorrectionCurves(); |
| 63 | |
| 64 | // Generates a pair of |kNumOctaveBands| band, octave filtered, noise buffers. |
| 65 | void GenerateNoiseVectors(); |
| 66 | |
| 67 | // Manager for all FFT related functionality (not owned). |
| 68 | FftManager* const fft_manager_; |
| 69 | |
| 70 | // The system sampling rate. |
| 71 | const int sampling_rate_; |
| 72 | |
| 73 | // The system number of frames per buffer. |
| 74 | const size_t frames_per_buffer_; |
| 75 | |
| 76 | // Pre-generated band-passed noise to be used as a base for the reverb tail. |
| 77 | std::vector<AudioBuffer> bandpassed_noise_left_; |
| 78 | std::vector<AudioBuffer> bandpassed_noise_right_; |
| 79 | |
| 80 | // The constituent curves used to generate the onset compensation envelopes. |
| 81 | AudioBuffer base_curves_; |
| 82 | AudioBuffer adder_curves_; |
| 83 | |
| 84 | // Filter for processing the left reverberant channel. |
| 85 | PartitionedFftFilter left_filter_; |
| 86 | |
| 87 | // Filter for processing the right reverberant channel. |
| 88 | PartitionedFftFilter right_filter_; |
| 89 | |
| 90 | // Delay filter used to ensure the compensation curve starts at the same point |
| 91 | // as the spectral reverb. |
| 92 | DelayFilter delay_filter_; |
| 93 | |
| 94 | // Number of active update processors. |
| 95 | size_t num_active_processors_; |
| 96 | |
| 97 | // Active reverb update processors to replace the corresponding filter |
| 98 | // partitions of the reverb tail within each process call. |
| 99 | std::list<std::unique_ptr<ReverbOnsetUpdateProcessor>> update_processors_; |
| 100 | |
| 101 | // Temporary buffer used to process filter kernel partitions. |
| 102 | AudioBuffer temp_kernel_buffer_; |
| 103 | |
| 104 | // Temporary buffer to hold FFT frequency domain output. |
| 105 | PartitionedFftFilter::FreqDomainBuffer temp_freq_buffer_; |
| 106 | }; |
| 107 | |
| 108 | } // namespace vraudio |
| 109 | |
| 110 | #endif // RESONANCE_AUDIO_DSP_REVERB_ONSET_COMPENSATOR_H_ |
| 111 | |