1 | /* |
2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
3 | * Copyright (C) 2015 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
15 | * its contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #ifndef ReverbConvolver_h |
31 | #define ReverbConvolver_h |
32 | |
33 | #include "ReverbAccumulationBuffer.h" |
34 | #include "ReverbConvolverStage.h" |
35 | #include "ReverbInputBuffer.h" |
36 | #include <memory> |
37 | #include <wtf/Condition.h> |
38 | #include <wtf/Lock.h> |
39 | #include <wtf/Threading.h> |
40 | #include <wtf/Vector.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | class AudioChannel; |
45 | |
46 | class ReverbConvolver { |
47 | WTF_MAKE_FAST_ALLOCATED; |
48 | public: |
49 | // maxFFTSize can be adjusted (from say 2048 to 32768) depending on how much precision is necessary. |
50 | // For certain tweaky de-convolving applications the phase errors add up quickly and lead to non-sensical results with |
51 | // larger FFT sizes and single-precision floats. In these cases 2048 is a good size. |
52 | // If not doing multi-threaded convolution, then should not go > 8192. |
53 | ReverbConvolver(AudioChannel* impulseResponse, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads); |
54 | ~ReverbConvolver(); |
55 | |
56 | void process(const AudioChannel* sourceChannel, AudioChannel* destinationChannel, size_t framesToProcess); |
57 | void reset(); |
58 | |
59 | size_t impulseResponseLength() const { return m_impulseResponseLength; } |
60 | |
61 | ReverbInputBuffer* inputBuffer() { return &m_inputBuffer; } |
62 | |
63 | bool useBackgroundThreads() const { return m_useBackgroundThreads; } |
64 | |
65 | size_t latencyFrames() const; |
66 | private: |
67 | void backgroundThreadEntry(); |
68 | |
69 | Vector<std::unique_ptr<ReverbConvolverStage>> m_stages; |
70 | Vector<std::unique_ptr<ReverbConvolverStage>> m_backgroundStages; |
71 | size_t m_impulseResponseLength; |
72 | |
73 | ReverbAccumulationBuffer m_accumulationBuffer; |
74 | |
75 | // One or more background threads read from this input buffer which is fed from the realtime thread. |
76 | ReverbInputBuffer m_inputBuffer; |
77 | |
78 | // First stage will be of size m_minFFTSize. Each next stage will be twice as big until we hit m_maxFFTSize. |
79 | size_t m_minFFTSize; |
80 | size_t m_maxFFTSize; |
81 | |
82 | // But don't exceed this size in the real-time thread (if we're doing background processing). |
83 | size_t m_maxRealtimeFFTSize; |
84 | |
85 | // Background thread and synchronization |
86 | bool m_useBackgroundThreads; |
87 | RefPtr<Thread> m_backgroundThread; |
88 | bool m_wantsToExit { false }; |
89 | bool m_moreInputBuffered { false }; |
90 | mutable Lock m_backgroundThreadMutex; |
91 | mutable Condition m_backgroundThreadConditionVariable; |
92 | }; |
93 | |
94 | } // namespace WebCore |
95 | |
96 | #endif // ReverbConvolver_h |
97 | |