| 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 | #include "dsp/shoe_box_room.h" |
| 18 | |
| 19 | #include "base/constants_and_types.h" |
| 20 | |
| 21 | namespace vraudio { |
| 22 | |
| 23 | void ComputeReflections(const WorldPosition& relative_listener_position, |
| 24 | const WorldPosition& room_dimensions, |
| 25 | const float* reflection_coefficients, |
| 26 | std::vector<Reflection>* reflections) { |
| 27 | DCHECK(reflection_coefficients); |
| 28 | DCHECK(reflections); |
| 29 | DCHECK_EQ(reflections->size(), kNumRoomSurfaces); |
| 30 | const WorldPosition kOrigin(0.0f, 0.0f, 0.0f); |
| 31 | if (!IsPositionInAabb(position: relative_listener_position, aabb_center: kOrigin, aabb_dimensions: room_dimensions)) { |
| 32 | // Listener is outside the room, skip computation. |
| 33 | std::fill(first: reflections->begin(), last: reflections->end(), value: Reflection()); |
| 34 | return; |
| 35 | } |
| 36 | // Calculate the distance of the listener to each wall. |
| 37 | // Since all the sources are 'attached' to the listener in the computation |
| 38 | // of reflections, the distance travelled is arbitrary. So, we add 1.0f to |
| 39 | // the computed distance in order to avoid delay time approaching 0 and the |
| 40 | // magnitude approaching +inf. |
| 41 | const WorldPosition offsets = 0.5f * room_dimensions; |
| 42 | const float distances_travelled[kNumRoomSurfaces] = { |
| 43 | offsets[0] + relative_listener_position[0] + 1.0f, |
| 44 | offsets[0] - relative_listener_position[0] + 1.0f, |
| 45 | offsets[1] + relative_listener_position[1] + 1.0f, |
| 46 | offsets[1] - relative_listener_position[1] + 1.0f, |
| 47 | offsets[2] + relative_listener_position[2] + 1.0f, |
| 48 | offsets[2] - relative_listener_position[2] + 1.0f}; |
| 49 | |
| 50 | for (size_t i = 0; i < kNumRoomSurfaces; ++i) { |
| 51 | // Convert distances to time delays in seconds. |
| 52 | (*reflections)[i].delay_time_seconds = |
| 53 | distances_travelled[i] / kSpeedOfSound; |
| 54 | // Division by distance is performed here as we don't want this applied more |
| 55 | // than once. |
| 56 | (*reflections)[i].magnitude = |
| 57 | reflection_coefficients[i] / distances_travelled[i]; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | } // namespace vraudio |
| 62 | |