1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "dsp/reverb_onset_compensator.h"
18
19#include <algorithm>
20#include <cmath>
21#include <iterator>
22
23#include "base/constants_and_types.h"
24#include "dsp/spectral_reverb_constants_and_tables.h"
25#include "dsp/utils.h"
26
27namespace vraudio {
28
29namespace {
30
31// Number of reverb updaters. Twelve were chosen as this represents one update
32// per buffer length at 24kHz, a number we are very unlikely to exceed.
33const size_t kNumReverbUpdaters = 12;
34
35} // namespace
36
37ReverbOnsetCompensator::ReverbOnsetCompensator(int sampling_rate,
38 size_t frames_per_buffer,
39 FftManager* fft_manager)
40 : fft_manager_(fft_manager),
41 sampling_rate_(sampling_rate),
42 frames_per_buffer_(frames_per_buffer),
43 base_curves_(kNumStereoChannels, kCorrectionCurveLength),
44 adder_curves_(kNumStereoChannels, kCorrectionCurveLength),
45 left_filter_(CeilToMultipleOfFramesPerBuffer(size: kCorrectionCurveLength,
46 frames_per_buffer: frames_per_buffer_),
47 frames_per_buffer_, fft_manager_),
48 right_filter_(CeilToMultipleOfFramesPerBuffer(size: kCorrectionCurveLength,
49 frames_per_buffer: frames_per_buffer_),
50 frames_per_buffer_, fft_manager_),
51 delay_filter_(CeilToMultipleOfFramesPerBuffer(size: kCorrectionCurveLength,
52 frames_per_buffer: frames_per_buffer_),
53 frames_per_buffer_),
54 num_active_processors_(0),
55 temp_kernel_buffer_(kNumStereoChannels, frames_per_buffer_),
56 temp_freq_buffer_(kNumMonoChannels, fft_manager_->GetFftSize()) {
57 CHECK(fft_manager_);
58 DCHECK_GT(sampling_rate_, 0);
59 DCHECK_GT(frames_per_buffer_, 0U);
60
61 temp_kernel_buffer_.Clear();
62 temp_freq_buffer_.Clear();
63
64 GenerateNoiseVectors();
65 GenerateCorrectionCurves();
66
67 // Insert reverb updaters.
68 for (size_t i = 0; i < kNumReverbUpdaters; ++i) {
69 update_processors_.emplace_front(args: new ReverbOnsetUpdateProcessor(
70 frames_per_buffer_, sampling_rate_, &base_curves_, &adder_curves_));
71 }
72}
73
74void ReverbOnsetCompensator::Process(const AudioBuffer& input,
75 AudioBuffer* output) {
76 DCHECK(output);
77 DCHECK_EQ(kNumMonoChannels, input.num_channels());
78 DCHECK_EQ(frames_per_buffer_, input.num_frames());
79 DCHECK_EQ(kNumStereoChannels, output->num_channels());
80 DCHECK_EQ(frames_per_buffer_, output->num_frames());
81
82 delay_filter_.InsertData(input: input[0]);
83 delay_filter_.GetDelayedData(delay_samples: kCompensationOnsetLength, buffer: &(*output)[0]);
84
85 // Process reverb updates.
86 AudioBuffer::Channel* kernel_channel_left = &temp_kernel_buffer_[0];
87 AudioBuffer::Channel* kernel_channel_right = &temp_kernel_buffer_[1];
88
89 size_t processor_index = 0;
90 while (processor_index < num_active_processors_) {
91 auto current_processor = update_processors_.begin();
92 std::advance(i&: current_processor, n: processor_index);
93 const size_t partition_index =
94 (*current_processor)->GetCurrentPartitionIndex();
95 if ((*current_processor)
96 ->Process(bandpassed_noise_left: bandpassed_noise_left_, bandpassed_noise_right: bandpassed_noise_right_,
97 kernel_channel_left, kernel_channel_right)) {
98 left_filter_.ReplacePartition(partition_index, kernel_chunk: *kernel_channel_left);
99 right_filter_.ReplacePartition(partition_index, kernel_chunk: *kernel_channel_right);
100 ++processor_index;
101 } else {
102 // Update of the |current_processor| is finished, move it to the end of
103 // the list and reduce the number of active processors.
104 update_processors_.splice(position: update_processors_.end(), x&: update_processors_,
105 i: current_processor);
106 --num_active_processors_;
107 }
108 }
109
110 // Filter the input (Using the output buffer due to the delay operation).
111 fft_manager_->FreqFromTimeDomain(time_channel: (*output)[0], freq_channel: &temp_freq_buffer_[0]);
112
113 left_filter_.Filter(input: temp_freq_buffer_[0]);
114 right_filter_.Filter(input: temp_freq_buffer_[0]);
115
116 left_filter_.GetFilteredSignal(output: &(*output)[0]);
117 right_filter_.GetFilteredSignal(output: &(*output)[1]);
118}
119
120void ReverbOnsetCompensator::Update(const float* rt60_values, float gain) {
121 DCHECK(rt60_values);
122 // Reset a reverb update processor from the end of the list and place it at
123 // the front. If the list is full, rotate the list and reuse the oldest active
124 // processor.
125 std::list<std::unique_ptr<ReverbOnsetUpdateProcessor>>::iterator
126 new_processor;
127 if (num_active_processors_ < kNumReverbUpdaters) {
128 new_processor = update_processors_.end();
129 std::advance(i&: new_processor, n: -1);
130 } else {
131 new_processor = update_processors_.begin();
132 }
133
134 (*new_processor)->SetReverbTimes(rt60_values);
135 (*new_processor)->SetGain(gain);
136
137 if (new_processor != update_processors_.begin()) {
138 auto list_item = update_processors_.begin();
139 std::advance(i&: list_item, n: num_active_processors_);
140 if (list_item != new_processor) {
141 update_processors_.splice(position: list_item, x&: update_processors_, first: new_processor,
142 last: std::next(x: new_processor));
143 }
144 ++num_active_processors_;
145 } else {
146 std::rotate(first: update_processors_.begin(),
147 middle: std::next(x: update_processors_.begin()),
148 last: update_processors_.end());
149 }
150}
151
152void ReverbOnsetCompensator::GenerateCorrectionCurves() {
153 // Copy into the adder curves such that the memory is aligned.
154 std::copy(first: kLowCorrectionCurve, last: kLowCorrectionCurve + kCorrectionCurveLength,
155 result: adder_curves_[0].begin());
156 std::copy(first: kHighCorrectionCurve, last: kHighCorrectionCurve + kCorrectionCurveLength,
157 result: adder_curves_[1].begin());
158
159 // Evaluate the polynomials to generate the base curves. Here the 'low' and
160 // 'high' names refer to the reverberation times.
161 AudioBuffer::Channel* low_channel = &base_curves_[0];
162 AudioBuffer::Channel* high_channel = &base_curves_[1];
163 for (size_t i = 0; i < kCorrectionCurveLength; ++i) {
164 // Scaled independent variable (Allowed better conditioning).
165 const float conditioning_scalar =
166 (static_cast<float>(i) - kCurveOffset) * kCurveScale;
167 (*low_channel)[i] = kLowReverberationCorrectionCurve[0];
168 (*high_channel)[i] = kHighReverberationCorrectionCurve[0];
169 float power = conditioning_scalar;
170 for (size_t k = 1; k < kCurvePolynomialLength; ++k) {
171 (*low_channel)[i] += power * kLowReverberationCorrectionCurve[k];
172 (*high_channel)[i] += power * kHighReverberationCorrectionCurve[k];
173 power *= conditioning_scalar;
174 }
175 (*low_channel)[i] = std::max(a: (*low_channel)[i], b: 0.0f);
176 (*high_channel)[i] = std::max(a: (*high_channel)[i], b: 0.0f);
177 }
178}
179
180void ReverbOnsetCompensator::GenerateNoiseVectors() {
181 const size_t num_octave_bands = GetNumReverbOctaveBands(sampling_rate: sampling_rate_);
182 const size_t noise_length = CeilToMultipleOfFramesPerBuffer(
183 size: kCorrectionCurveLength, frames_per_buffer: frames_per_buffer_);
184 for (size_t band = 0; band < num_octave_bands; ++band) {
185 // Generate preset tail.
186 bandpassed_noise_left_.emplace_back(args: kNumMonoChannels, args: noise_length);
187 GenerateBandLimitedGaussianNoise(center_frequency: kOctaveBandCentres[band], sampling_rate: sampling_rate_,
188 /*seed=*/1U,
189 noise_buffer: &bandpassed_noise_left_[band]);
190 bandpassed_noise_right_.emplace_back(args: kNumMonoChannels, args: noise_length);
191 GenerateBandLimitedGaussianNoise(center_frequency: kOctaveBandCentres[band], sampling_rate: sampling_rate_,
192 /*seed=*/2U,
193 noise_buffer: &bandpassed_noise_right_[band]);
194
195 auto min_max = std::minmax_element(first: bandpassed_noise_left_[band][0].begin(),
196 last: bandpassed_noise_left_[band][0].end());
197 const float left_scale =
198 std::max(a: std::fabs(x: *min_max.first), b: std::fabs(x: *min_max.second));
199 min_max = std::minmax_element(first: bandpassed_noise_right_[band][0].begin(),
200 last: bandpassed_noise_right_[band][0].end());
201 const float right_scale =
202 std::max(a: std::fabs(x: *min_max.first), b: std::fabs(x: *min_max.second));
203
204 const float scale = std::max(a: left_scale, b: right_scale);
205
206 ScalarMultiply(length: noise_length, gain: scale, input: bandpassed_noise_left_[band][0].begin(),
207 output: bandpassed_noise_left_[band][0].begin());
208 ScalarMultiply(length: noise_length, gain: scale,
209 input: bandpassed_noise_right_[band][0].begin(),
210 output: bandpassed_noise_right_[band][0].begin());
211 }
212}
213
214} // namespace vraudio
215

source code of qtmultimedia/src/3rdparty/resonance-audio/resonance_audio/dsp/reverb_onset_compensator.cc