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_REFLECTIONS_PROCESSOR_H_ |
18 | #define RESONANCE_AUDIO_DSP_REFLECTIONS_PROCESSOR_H_ |
19 | |
20 | #include <utility> |
21 | #include <vector> |
22 | |
23 | #include "api/resonance_audio_api.h" |
24 | #include "base/audio_buffer.h" |
25 | #include "base/misc_math.h" |
26 | #include "dsp/delay_filter.h" |
27 | #include "dsp/gain_processor.h" |
28 | #include "dsp/mono_pole_filter.h" |
29 | #include "dsp/reflection.h" |
30 | #include "utils/buffer_crossfader.h" |
31 | |
32 | namespace vraudio { |
33 | |
34 | // Class that accepts single mono buffer as input and outputs a first order |
35 | // ambisonic buffer of the mix of all the rooms early reflections computed for |
36 | // the buffer. |
37 | // |
38 | // The input consists of a mono mix of all the sound objects in the system. The |
39 | // reflections are assumed to be aligned with the aabb axes and thus the first |
40 | // order ambisonic axes. |
41 | class ReflectionsProcessor { |
42 | public: |
43 | // Constructs a |ReflectionsProcessor|. |
44 | // |
45 | // @param sample_rate System sampling rate. |
46 | // @param frames_per_buffer System frames per buffer. |
47 | ReflectionsProcessor(int sample_rate, size_t frames_per_buffer); |
48 | |
49 | // Updates reflections according to the new |ReflectionProperties|. |
50 | // |
51 | // @param reflection_properties New reflection properties. |
52 | // @param listener_position New listener position. |
53 | void Update(const ReflectionProperties& reflection_properties, |
54 | const WorldPosition& listener_position); |
55 | |
56 | // Processes a mono |AudioBuffer| into an ambisonic |AudioBuffer|. |
57 | // |
58 | // @param input Mono input buffer. |
59 | // @param output Ambisonic output buffer. |
60 | void Process(const AudioBuffer& input, AudioBuffer* output); |
61 | |
62 | // Returns the number of frames required to keep processing on empty input |
63 | // signal. This value can be used to avoid any potential artifacts on the |
64 | // final output signal when the input signal stream is empty. |
65 | size_t num_frames_to_process_on_empty_input() const { |
66 | return num_frames_to_process_on_empty_input_; |
67 | } |
68 | |
69 | private: |
70 | // Updates |gains_| and |delays_| vectors of the |ReflectionsProcessor|. |
71 | void UpdateGainsAndDelays(); |
72 | |
73 | // Applies |target_reflections_| and fast-encodes them into first order |
74 | // ambisonics. |
75 | // |
76 | // @param output Ambisonic output buffer. |
77 | void ApplyReflections(AudioBuffer* output); |
78 | |
79 | // System sampling rate. |
80 | const int sample_rate_; |
81 | |
82 | // System number of frames per buffer. |
83 | const size_t frames_per_buffer_; |
84 | |
85 | // Maximum allowed delay time for a reflection (in samples). |
86 | const size_t max_delay_samples_; |
87 | |
88 | // Low pass filter to be applied to input signal. |
89 | MonoPoleFilter low_pass_filter_; |
90 | |
91 | // Audio buffer to store mono low pass filtered buffers during processing. |
92 | AudioBuffer temp_mono_buffer_; |
93 | |
94 | // Audio buffers to store FOA reflections buffers during crossfading. |
95 | AudioBuffer current_reflection_buffer_; |
96 | AudioBuffer target_reflection_buffer_; |
97 | |
98 | // Target reflections filled with new data when |Update| is called. |
99 | std::vector<Reflection> target_reflections_; |
100 | |
101 | // Indicates whether relfections have been updated and a crossfade is needed. |
102 | bool crossfade_; |
103 | |
104 | // Buffer crossfader to apply linear crossfade during reflections update. |
105 | BufferCrossfader crossfader_; |
106 | |
107 | // Number of frames needed to keep processing on empty input signal. |
108 | size_t num_frames_to_process_on_empty_input_; |
109 | |
110 | // Number of samples of delay to be applied for each reflection. |
111 | std::vector<size_t> delays_; |
112 | |
113 | // Delay filter to delay the incoming buffer. |
114 | DelayFilter delay_filter_; |
115 | |
116 | // Delay buffer used to store delayed reflections before scaling and encoding. |
117 | AudioBuffer delay_buffer_; |
118 | |
119 | // Gains to be applied for each reflection. |
120 | std::vector<float> gains_; |
121 | |
122 | // |GainProcessor|s to apply |gains_|. |
123 | std::vector<GainProcessor> gain_processors_; |
124 | }; |
125 | |
126 | } // namespace vraudio |
127 | |
128 | #endif // RESONANCE_AUDIO_DSP_REFLECTIONS_PROCESSOR_H_ |
129 | |