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_GRAPH_AMBISONIC_BINAURAL_DECODER_NODE_H_ |
18 | #define RESONANCE_AUDIO_GRAPH_AMBISONIC_BINAURAL_DECODER_NODE_H_ |
19 | |
20 | #include <memory> |
21 | #include <string> |
22 | |
23 | #include "ambisonics/ambisonic_binaural_decoder.h" |
24 | #include "base/audio_buffer.h" |
25 | #include "dsp/fft_manager.h" |
26 | #include "dsp/resampler.h" |
27 | #include "graph/system_settings.h" |
28 | #include "node/processing_node.h" |
29 | #include "utils/buffer_crossfader.h" |
30 | |
31 | namespace vraudio { |
32 | |
33 | // Node that takes an ambisonic soundfield as input and renders a binaural |
34 | // stereo buffer as output. |
35 | class AmbisonicBinauralDecoderNode : public ProcessingNode { |
36 | public: |
37 | // Initializes AmbisonicBinauralDecoderNode class. |
38 | // |
39 | // @param system_settings Global system configuration. |
40 | // @param ambisonic_order Ambisonic order. |
41 | // @param sh_hrir_filename Filename to load the HRIR data from. |
42 | // @param fft_manager Pointer to a manager to perform FFT transformations. |
43 | // @resampler Pointer to a resampler used to convert HRIRs to the system rate. |
44 | AmbisonicBinauralDecoderNode(const SystemSettings& system_settings, |
45 | int ambisonic_order, |
46 | const std::string& sh_hrir_filename, |
47 | FftManager* fft_manager, Resampler* resampler); |
48 | |
49 | ~AmbisonicBinauralDecoderNode() override; |
50 | |
51 | protected: |
52 | // Implements ProcessingNode. |
53 | const AudioBuffer* AudioProcess(const NodeInput& input) override; |
54 | |
55 | private: |
56 | const SystemSettings& system_settings_; |
57 | |
58 | // Number of Ambisonic channels. |
59 | const size_t num_ambisonic_channels_; |
60 | |
61 | // Denotes if the stereo speaker mode is enabled. |
62 | bool is_stereo_speaker_mode_; |
63 | |
64 | // Ambisonic decoder used to render binaural output. |
65 | std::unique_ptr<AmbisonicBinauralDecoder> ambisonic_binaural_decoder_; |
66 | |
67 | size_t num_frames_processed_on_empty_input_; |
68 | |
69 | // Stereo output buffer. |
70 | AudioBuffer stereo_output_buffer_; |
71 | |
72 | // Silence mono buffer to render reverb tails. |
73 | AudioBuffer silence_input_buffer_; |
74 | |
75 | // Buffer crossfader to apply linear crossfade when the stereo speaker mode is |
76 | // changed. |
77 | BufferCrossfader crossfader_; |
78 | |
79 | // Stereo output buffer to store the crossfaded decode output when necessary. |
80 | AudioBuffer crossfaded_output_buffer_; |
81 | |
82 | // Temporary crossfade buffer to store the intermediate stereo output. |
83 | AudioBuffer temp_crossfade_buffer_; |
84 | }; |
85 | |
86 | } // namespace vraudio |
87 | |
88 | #endif // RESONANCE_AUDIO_GRAPH_AMBISONIC_BINAURAL_DECODER_NODE_H_ |
89 | |