Rise
The Vieneo Province
Loading...
Searching...
No Matches
EngineAudio.h
Go to the documentation of this file.
1#pragma once
2
4#include <array>
5
6namespace EngineAudio
7{
9 enum class Kind { Car, Truck, HeavyDiesel, Jet, Rocket };
10
11 // Match the sound enum order; physics uses left/center/right for three engines.
12 inline int SlotForEngine(int engineCount, int engine)
13 {
14 if (engine < 0 || engine >= engineCount || engineCount > SlotCount) return -1;
15 if (engineCount == 1) return Center;
16 if (engineCount == 3 && engine == 1) return Center;
17 return engine == 0 ? Left : Right;
18 }
19
20 inline float ReferenceDistanceKm(Kind kind)
21 {
22 // Mix tuning, not measured SPL. Retain the recording's close level while
23 // making larger engines carry farther. Rockets match the flyby layer's 100 m reference.
24 switch (kind)
25 {
26 case Kind::Car: return .005f;
27 case Kind::Truck: return .010f;
28 case Kind::HeavyDiesel: return .025f;
29 case Kind::Jet: return .050f;
30 case Kind::Rocket: return .100f;
31 }
32 return .005f;
33 }
34
35 struct Source
36 {
37 RiseMath::Vector3 positionKm{}; // Body coordinates, with the same signs as camera mounts.
38 float referenceDistanceKm = .005f;
39 bool fromModel = false;
40 };
41
42 struct Sources
43 {
44 std::array<Source, SlotCount> source{};
45 int count = 0;
46
47 bool Active(int slot) const
48 {
49 for (int i = 0; i < count; ++i)
50 if (SlotForEngine(count, i) == slot) return true;
51 return false;
52 }
53 };
54
55 inline Sources Create(int count, Kind kind, float sideArmKm, float forwardKm)
56 {
57 Sources result;
58 result.count = std::clamp(count, 0, int(SlotCount));
59 sideArmKm = std::isfinite(sideArmKm) ? std::fabs(sideArmKm) : 0.f;
60 forwardKm = std::isfinite(forwardKm) ? forwardKm : 0.f;
61 for (auto& source : result.source)
62 {
63 source.referenceDistanceKm = ReferenceDistanceKm(kind);
64 source.positionKm.z = forwardKm;
65 }
66 result.source[Left].positionKm.x = -sideArmKm;
67 result.source[Right].positionKm.x = sideArmKm;
68 return result;
69 }
70
71 template<class Component>
72 void Locate(Sources& cache, const Component* components, unsigned count,
73 const RiseMath::Matrix& modelToBody)
74 {
75 if (!components || !cache.count) return;
76 std::array<RiseMath::Vector3, SlotCount> centers{};
77 std::array<unsigned, SlotCount> pieces{};
78 for (unsigned i = 0; i < count; ++i)
79 {
80 const auto& component = components[i];
81 if (component.type < 20 || component.type > 22) continue;
82 RiseMath::Vector3 position;
83 RiseMath::Vec3TransformCoord(&position, &component.nominalxyz, &modelToBody);
84 if (!std::isfinite(position.x) || !std::isfinite(position.y) || !std::isfinite(position.z)) continue;
85 // The exterior renderer reverses model-space positions relative to camera mounts.
86 centers[component.type - 20] -= position;
87 ++pieces[component.type - 20];
88 }
89 unsigned found = 0;
90 for (int i = 0; i < SlotCount; ++i)
91 if (pieces[i]) centers[found++] = centers[i] / float(pieces[i]);
92 std::sort(centers.begin(), centers.begin() + found,
93 [](const auto& a, const auto& b) { return a.x < b.x; });
94 for (unsigned i = 0; i < found; ++i)
95 {
96 // Authored 20/21/22 ordering differs between E-10 and E-11. Resolve by
97 // physical side, with a fallback for partially tagged exterior models.
98 const int slot = cache.count == 1 ? Center
99 : found == unsigned(cache.count) ? SlotForEngine(cache.count, int(i))
100 : centers[i].x < -.0001f ? Left : centers[i].x > .0001f ? Right : Center;
101 if (!cache.Active(slot)) continue;
102 cache.source[slot].positionKm = centers[i];
103 cache.source[slot].fromModel = true;
104 }
105 }
106
107 inline float Strength(float output)
108 {
109 return std::isfinite(output) ? std::max(std::sqrt(std::clamp(output, 0.f, 1.f)), .25f) : 0.f;
110 }
111
112 inline VehicleSoundSpatial::Mix Calculate(const Sources& cache, int slot,
113 const RiseMath::Vector3& cameraFromVehicle, float yaw, float pitch, float roll,
114 const RiseMath::Matrix& bodyToCameraFrame, const std::array<float, SlotCount>& weights)
115 {
116 float total = 0.f, gain = 0.f, pan = 0.f;
117 for (int i = 0; i < SlotCount; ++i)
118 {
119 if (!cache.Active(i) || (slot >= 0 && slot != i)) continue;
120 const float weight = slot >= 0 ? 1.f : weights[i];
121 if (!std::isfinite(weight) || weight <= 0.f) continue;
122 const auto& source = cache.source[i];
123 RiseMath::Vector3 position;
124 RiseMath::Vec3TransformNormal(&position, &source.positionKm, &bodyToCameraFrame);
125 const auto mix = VehicleSoundSpatial::Calculate(cameraFromVehicle - position,
126 yaw, pitch, roll, source.referenceDistanceKm);
127 total += weight;
128 gain += weight * mix.gain;
129 pan += weight * mix.gain * mix.pan;
130 }
131 return {total > 0.f ? gain / total : 0.f, gain > 0.f ? pan / gain : 0.f};
132 }
133}
int SlotForEngine(int engineCount, int engine)
Definition EngineAudio.h:12
VehicleSoundSpatial::Mix Calculate(const Sources &cache, int slot, const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, const RiseMath::Matrix &bodyToCameraFrame, const std::array< float, SlotCount > &weights)
void Locate(Sources &cache, const Component *components, unsigned count, const RiseMath::Matrix &modelToBody)
Definition EngineAudio.h:72
Sources Create(int count, Kind kind, float sideArmKm, float forwardKm)
Definition EngineAudio.h:55
float ReferenceDistanceKm(Kind kind)
Definition EngineAudio.h:20
float Strength(float output)
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:109
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Mix Calculate(const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, float referenceDistanceKm=.005f)
RiseMath::Vector3 positionKm
Definition EngineAudio.h:37
bool Active(int slot) const
Definition EngineAudio.h:47
std::array< Source, SlotCount > source
Definition EngineAudio.h:44