1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "third_party/blink/renderer/platform/audio/reverb_convolver_stage.h"
30 
31 #include <algorithm>
32 #include <memory>
33 #include <utility>
34 
35 #include "third_party/blink/renderer/platform/audio/reverb_accumulation_buffer.h"
36 #include "third_party/blink/renderer/platform/audio/reverb_convolver.h"
37 #include "third_party/blink/renderer/platform/audio/reverb_input_buffer.h"
38 #include "third_party/blink/renderer/platform/audio/vector_math.h"
39 
40 namespace blink {
41 
ReverbConvolverStage(const float * impulse_response,size_t,size_t reverb_total_latency,size_t stage_offset,size_t stage_length,size_t fft_size,size_t render_phase,size_t render_slice_size,ReverbAccumulationBuffer * accumulation_buffer,float scale,bool direct_mode)42 ReverbConvolverStage::ReverbConvolverStage(
43     const float* impulse_response,
44     size_t,
45     size_t reverb_total_latency,
46     size_t stage_offset,
47     size_t stage_length,
48     size_t fft_size,
49     size_t render_phase,
50     size_t render_slice_size,
51     ReverbAccumulationBuffer* accumulation_buffer,
52     float scale,
53     bool direct_mode)
54     : accumulation_buffer_(accumulation_buffer),
55       accumulation_read_index_(0),
56       input_read_index_(0),
57       direct_mode_(direct_mode) {
58   DCHECK(impulse_response);
59   DCHECK(accumulation_buffer);
60 
61   if (!direct_mode_) {
62     fft_kernel_ = std::make_unique<FFTFrame>(fft_size);
63     fft_kernel_->DoPaddedFFT(impulse_response + stage_offset, stage_length);
64     // Account for the normalization (if any) of the convolver.  By linearity,
65     // we can scale the FFT by the factor instead of the input.  We do it this
66     // way so we don't need to create a temporary for the scaled result before
67     // computing the FFT.
68     if (scale != 1) {
69       fft_kernel_->ScaleFFT(scale);
70     }
71     fft_convolver_ = std::make_unique<FFTConvolver>(fft_size);
72   } else {
73     DCHECK(!stage_offset);
74     DCHECK_LE(stage_length, fft_size / 2);
75 
76     auto direct_kernel = std::make_unique<AudioFloatArray>(fft_size / 2);
77     direct_kernel->CopyToRange(impulse_response, 0, stage_length);
78     // Account for the normalization (if any) of the convolver node.
79     if (scale != 1) {
80       vector_math::Vsmul(direct_kernel->Data(), 1, &scale,
81                          direct_kernel->Data(), 1, stage_length);
82     }
83     direct_convolver_ = std::make_unique<DirectConvolver>(
84         render_slice_size, std::move(direct_kernel));
85   }
86   temporary_buffer_.Allocate(render_slice_size);
87 
88   // The convolution stage at offset stageOffset needs to have a corresponding
89   // delay to cancel out the offset.
90   size_t total_delay = stage_offset + reverb_total_latency;
91 
92   // But, the FFT convolution itself incurs fftSize / 2 latency, so subtract
93   // this out...
94   size_t half_size = fft_size / 2;
95   if (!direct_mode_) {
96     DCHECK_GE(total_delay, half_size);
97     if (total_delay >= half_size)
98       total_delay -= half_size;
99   }
100 
101   // We divide up the total delay, into pre and post delay sections so that we
102   // can schedule at exactly the moment when the FFT will happen.  This is
103   // coordinated with the other stages, so they don't all do their FFTs at the
104   // same time...
105   int max_pre_delay_length = std::min(half_size, total_delay);
106   pre_delay_length_ = total_delay > 0 ? render_phase % max_pre_delay_length : 0;
107   if (pre_delay_length_ > total_delay)
108     pre_delay_length_ = 0;
109 
110   post_delay_length_ = total_delay - pre_delay_length_;
111   pre_read_write_index_ = 0;
112   frames_processed_ = 0;  // total frames processed so far
113 
114   size_t delay_buffer_size =
115       pre_delay_length_ < fft_size ? fft_size : pre_delay_length_;
116   delay_buffer_size = delay_buffer_size < render_slice_size ? render_slice_size
117                                                             : delay_buffer_size;
118   pre_delay_buffer_.Allocate(delay_buffer_size);
119 }
120 
ProcessInBackground(ReverbConvolver * convolver,uint32_t frames_to_process)121 void ReverbConvolverStage::ProcessInBackground(ReverbConvolver* convolver,
122                                                uint32_t frames_to_process) {
123   ReverbInputBuffer* input_buffer = convolver->InputBuffer();
124   float* source =
125       input_buffer->DirectReadFrom(&input_read_index_, frames_to_process);
126   Process(source, frames_to_process);
127 }
128 
Process(const float * source,uint32_t frames_to_process)129 void ReverbConvolverStage::Process(const float* source,
130                                    uint32_t frames_to_process) {
131   DCHECK(source);
132   if (!source)
133     return;
134 
135   // Deal with pre-delay stream : note special handling of zero delay.
136 
137   const float* pre_delayed_source;
138   float* pre_delayed_destination;
139   float* temporary_buffer;
140   bool is_temporary_buffer_safe = false;
141   if (pre_delay_length_ > 0) {
142     // Handles both the read case (call to process() ) and the write case
143     // (memcpy() )
144     bool is_pre_delay_safe =
145         pre_read_write_index_ + frames_to_process <= pre_delay_buffer_.size();
146     DCHECK(is_pre_delay_safe);
147     if (!is_pre_delay_safe)
148       return;
149 
150     is_temporary_buffer_safe = frames_to_process <= temporary_buffer_.size();
151 
152     pre_delayed_destination = pre_delay_buffer_.Data() + pre_read_write_index_;
153     pre_delayed_source = pre_delayed_destination;
154     temporary_buffer = temporary_buffer_.Data();
155   } else {
156     // Zero delay
157     pre_delayed_destination = nullptr;
158     pre_delayed_source = source;
159     temporary_buffer = pre_delay_buffer_.Data();
160 
161     is_temporary_buffer_safe = frames_to_process <= pre_delay_buffer_.size();
162   }
163 
164   DCHECK(is_temporary_buffer_safe);
165   if (!is_temporary_buffer_safe)
166     return;
167 
168   if (frames_processed_ < pre_delay_length_) {
169     // For the first m_preDelayLength frames don't process the convolver,
170     // instead simply buffer in the pre-delay.  But while buffering the
171     // pre-delay, we still need to update our index.
172     accumulation_buffer_->UpdateReadIndex(&accumulation_read_index_,
173                                           frames_to_process);
174   } else {
175     // Now, run the convolution (into the delay buffer).
176     // An expensive FFT will happen every fftSize / 2 frames.
177     // We process in-place here...
178     if (!direct_mode_)
179       fft_convolver_->Process(fft_kernel_.get(), pre_delayed_source,
180                               temporary_buffer, frames_to_process);
181     else
182       direct_convolver_->Process(pre_delayed_source, temporary_buffer,
183                                  frames_to_process);
184 
185     // Now accumulate into reverb's accumulation buffer.
186     accumulation_buffer_->Accumulate(temporary_buffer, frames_to_process,
187                                      &accumulation_read_index_,
188                                      post_delay_length_);
189   }
190 
191   // Finally copy input to pre-delay.
192   if (pre_delay_length_ > 0) {
193     memcpy(pre_delayed_destination, source, sizeof(float) * frames_to_process);
194     pre_read_write_index_ += frames_to_process;
195 
196     DCHECK_LE(pre_read_write_index_, pre_delay_length_);
197     if (pre_read_write_index_ >= pre_delay_length_)
198       pre_read_write_index_ = 0;
199   }
200 
201   frames_processed_ += frames_to_process;
202 }
203 
Reset()204 void ReverbConvolverStage::Reset() {
205   if (!direct_mode_)
206     fft_convolver_->Reset();
207   else
208     direct_convolver_->Reset();
209   pre_delay_buffer_.Zero();
210   accumulation_read_index_ = 0;
211   input_read_index_ = 0;
212   frames_processed_ = 0;
213 }
214 
215 }  // namespace blink
216