RavEngine
Loading...
Searching...
No Matches
impulse_response_computer.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_IMPULSE_RESPONSE_COMPUTER_H_
18#define RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_IMPULSE_RESPONSE_COMPUTER_H_
19
20#include <memory>
21#include <vector>
22
23#include "geometrical_acoustics/acoustic_listener.h"
24#include "geometrical_acoustics/collection_kernel.h"
25#include "geometrical_acoustics/path.h"
26#include "geometrical_acoustics/scene_manager.h"
27
28namespace vraudio {
29
30// A class that computes impulse responses from traced sound propagation paths
31// and modifies the listener's data (namely listener.energy_impulse_response)
32// in place.
33// Each listener is associated to a sphere with finite volume, and rays that
34// intersect with the sphere contributes energy to the listener.
35//
36// This class is to be used in conjunction with a PathTracer, which produces
37// batches of sound propagation paths that this class consumes.
38// Example use (which follows a create-collect-finalize pattern):
39//
40// // Create.
41// ImpulseResponseComputer impulse_response_computer(0.1f, 44100.0f,
42// std::move(listeners));
43// // Collect.
44// for (const auto& source : sources) {
45// // Divide |total_num_paths| into |num_batch| batches, each of size
46// // |num_rays_per_batch|.
47// for (size_t i = 0; i < num_batch; ++i) {
48// paths_batch =
49// path_tracer.TracePaths(source, num_rays_per_batch, 10, 1e-6);
50// impulse_response_computer.CollectContributions(paths_batch,
51// total_num_paths);
52// }
53// }
54//
55// // Finalize and use the impulse responses in listeners e.g. write them to
56// // a file or pass them to an audio render.
57// for (const auto& listener:
58// impulse_response_computer. GetFinalizedListeners()) {
59// const auto& responses = listener.energy_impulse_responses;
60// // Do something with |responses|.
61// }
63 public:
64 // Constructor.
65 //
66 // @param listener_sphere_radius Radius of listener spheres (m).
67 // @param sampling_rate Sampling rate (Hz).
68 // @param listeners Vector of AcousticListener's whose impulse responses are
69 // to be computed.
70 // @param scene_manager SceneManager.
72 float listener_sphere_radius, float sampling_rate,
73 std::unique_ptr<std::vector<AcousticListener>> listeners,
74 SceneManager* scene_manager);
76
77 // Collects contributions from a batch of paths to all listeners if
78 // the collection is not finalized yet.
79 //
80 // @param paths_batch All sound propagation paths in a batch.
81 void CollectContributions(const std::vector<Path>& paths_batch);
82
83 // Finalizes the listeners and returns them. After calling this, further
84 // calls to CollectContributions() have no effect.
85 //
86 // @return Vector of finalized listeners.
87 const std::vector<AcousticListener>& GetFinalizedListeners();
88
89 private:
90 // Vector of listeners.
91 const std::unique_ptr<std::vector<AcousticListener>> listeners_;
92
93 // Collection kernel used to collect contributions from rays to listeners.
94 CollectionKernel collection_kernel_;
95
96 // Scene manager. Used to keep records of listener spheres for ray-sphere
97 // intersection tests. Also used in the diffuse rain algorithm to to test
98 // whether there is an un-obstructed path from a reflection point to a
99 // listener.
100 SceneManager* scene_manager_;
101
102 // Is the collection finalized.
103 bool finalized_;
104
105 // Total number of paths (contributing or not) that energies are collected
106 // from before the collection is finalized. This will be used to average
107 // energy contributions.
108 size_t num_total_paths_;
109};
110
111} // namespace vraudio
112#endif // RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_IMPULSE_RESPONSE_COMPUTER_H_
Definition collection_kernel.h:26
Definition impulse_response_computer.h:62
Definition scene_manager.h:36