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 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_CONVOLVER_H_
30 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_CONVOLVER_H_
31 
32 #include <memory>
33 
34 #include "third_party/blink/renderer/platform/audio/audio_array.h"
35 #include "third_party/blink/renderer/platform/audio/direct_convolver.h"
36 #include "third_party/blink/renderer/platform/audio/fft_convolver.h"
37 #include "third_party/blink/renderer/platform/audio/reverb_accumulation_buffer.h"
38 #include "third_party/blink/renderer/platform/audio/reverb_convolver_stage.h"
39 #include "third_party/blink/renderer/platform/audio/reverb_input_buffer.h"
40 #include "third_party/blink/renderer/platform/scheduler/public/thread.h"
41 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
42 #include "third_party/blink/renderer/platform/wtf/vector.h"
43 
44 namespace blink {
45 
46 class AudioChannel;
47 
48 class PLATFORM_EXPORT ReverbConvolver {
49   USING_FAST_MALLOC(ReverbConvolver);
50 
51  public:
52   // maxFFTSize can be adjusted (from say 2048 to 32768) depending on how much
53   // precision is necessary.  For certain tweaky de-convolving applications the
54   // phase errors add up quickly and lead to non-sensical results with larger
55   // FFT sizes and single-precision floats.  In these cases 2048 is a good
56   // size.  If not doing multi-threaded convolution, then should not go > 8192.
57   ReverbConvolver(AudioChannel* impulse_response,
58                   size_t render_slice_size,
59                   size_t max_fft_size,
60                   size_t convolver_render_phase,
61                   bool use_background_threads,
62                   float scale);
63   ~ReverbConvolver();
64 
65   void Process(const AudioChannel* source_channel,
66                AudioChannel* destination_channel,
67                uint32_t frames_to_process);
68   void Reset();
69 
InputBuffer()70   ReverbInputBuffer* InputBuffer() { return &input_buffer_; }
71 
72   size_t LatencyFrames() const;
73 
74  private:
75   void ProcessInBackground();
76 
77   Vector<std::unique_ptr<ReverbConvolverStage>> stages_;
78   Vector<std::unique_ptr<ReverbConvolverStage>> background_stages_;
79   size_t impulse_response_length_;
80 
81   ReverbAccumulationBuffer accumulation_buffer_;
82 
83   // One or more background threads read from this input buffer which is fed
84   // from the realtime thread.
85   ReverbInputBuffer input_buffer_;
86 
87   // First stage will be of size m_minFFTSize.  Each next stage will be twice as
88   // big until we hit m_maxFFTSize.
89   size_t min_fft_size_;
90   size_t max_fft_size_;
91 
92   // But don't exceed this size in the real-time thread (if we're doing
93   // background processing).
94   size_t max_realtime_fft_size_;
95 
96   // Background thread and synchronization
97   std::unique_ptr<Thread> background_thread_;
98 
99   DISALLOW_COPY_AND_ASSIGN(ReverbConvolver);
100 };
101 
102 }  // namespace blink
103 
104 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_REVERB_CONVOLVER_H_
105