Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/VehicleSoundSpatialTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../VehicleSoundSpatial.h"
2#include "../EngineAudio.h"
3#include "../RcsAudio.h"
4#include "../VehicleHorn.h"
5#include <cstdlib>
6#include <fstream>
7#include <iostream>
8#include <limits>
9#include <vector>
10
11static void Check(bool pass, const char* message)
12{
13 if (!pass) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
14}
15
16static bool Near(float a, float b) { return std::fabs(a - b) < .00001f; }
17
18// Independently project the vehicle origin using the renderer's matrix sequence.
19static float ScreenX(const RiseMath::Vector3& mount, float yaw, float pitch, float roll, float zoom, float* depth = nullptr)
20{
21 RiseMath::Matrix view, look, scale, projection, flip;
22 RiseMath::MatrixTranslation(&view, mount.x, mount.y, mount.z);
23 RiseMath::MatrixRotationYawPitchRoll(&look, yaw, pitch, roll);
24 RiseMath::MatrixInverse(&look, nullptr, &look);
25 RiseMath::MatrixScaling(&scale, zoom, zoom, 1.f);
26 RiseMath::MatrixPerspectiveRH(&projection, .001f, .0006f, .001f, 100.f);
28 const auto transform = view * look * scale * flip * projection;
29 if (depth) *depth = transform._44;
30 return transform._41 / transform._44;
31}
32
33struct EngineComponent
34{
35 unsigned char type = 0;
36 RiseMath::Vector3 nominalxyz{};
37};
38
39static std::vector<EngineComponent> ReadEngines(const char* path)
40{
41 std::ifstream file(path, std::ios::binary);
42 Check(bool(file), "open shipped exterior engine metadata");
43 unsigned short count = 0;
44 file.read(reinterpret_cast<char*>(&count), 2);
45 std::vector<EngineComponent> components(count);
46 for (unsigned i = 0; i < count; ++i)
47 {
48 file.seekg(99 + i * 100 + 4);
49 file.read(reinterpret_cast<char*>(&components[i].nominalxyz), 12);
50 file.seekg(99 + i * 100 + 58);
51 file.read(reinterpret_cast<char*>(&components[i].type), 1);
52 Check(bool(file), "read complete exterior engine metadata");
53 }
54 return components;
55}
56
57static void CheckEngineSources()
58{
59 using namespace EngineAudio;
60 Check(SlotForEngine(3, 0) == Left && SlotForEngine(3, 1) == Center && SlotForEngine(3, 2) == Right,
61 "all three physics engines drive their correct audio channel");
62 Check(SlotForEngine(2, 0) == Left && SlotForEngine(2, 1) == Right && SlotForEngine(1, 0) == Center,
63 "one- and two-engine layouts use the appropriate channels");
64 Check(SlotForEngine(3, 3) == -1 && SlotForEngine(0, 0) == -1, "invalid engine indexes have no channel");
65 RiseMath::Matrix identity, pitch, yaw;
66 RiseMath::MatrixIdentity(&identity);
68 const std::array<float, SlotCount> equalWeights{1, 1, 1};
69 for (bool e11 : {false, true})
70 {
71 auto cache = Create(3, Kind::Rocket, .018804f, 0);
72 const auto components = ReadEngines(e11 ? "Meshes/E-11.dix" : "Meshes/e-10.dix");
74 Locate(cache, components.data(), unsigned(components.size()), pitch * yaw);
75 Check(cache.source[Left].fromModel && cache.source[Right].fromModel && cache.source[Center].fromModel,
76 "all E-10/E-11 engine positions come from authored exterior geometry");
77 Check(Near(cache.source[Left].positionKm.x, -.01880388f) &&
78 Near(cache.source[Right].positionKm.x, .01880388f) &&
79 Near(cache.source[Center].positionKm.x, 0.f), "different model labels resolve to physical left/center/right");
80 Check(Near(cache.source[Left].positionKm.z, -.00661618f) &&
81 Near(cache.source[Center].positionKm.z, -.02646472f), "cache uses engine centers rather than hull origin or exhaust tip");
82 for (const RiseMath::Vector3 camera : {RiseMath::Vector3(-.0186f, -.0015f, -.007f),
83 RiseMath::Vector3(-.0186f, .0064f, -.0016f)})
84 {
85 const auto nearEngine = Calculate(cache, Left, camera, 1.329f, .024f, 0, identity, equalWeights);
86 Check(nearEngine.gain == 1.f, "E-10 wing/above-engine camera retains close rocket level");
87 const auto relative = camera - cache.source[Left].positionKm;
88 for (float look : {1.329f, 1.329f + RiseMath::Pi})
89 {
90 float depth;
91 const float screenX = ScreenX(relative, look, .024f, 0, 1, &depth);
92 const auto mix = Calculate(cache, Left, camera, look, .024f, 0, identity, equalWeights);
93 if (depth > 0) // Behind-camera projection reverses X and is not drawn.
94 Check(mix.pan * screenX >= 0, "engine pan agrees with where its geometry is drawn");
95 }
96 }
97 const RiseMath::Vector3 camera{0, 0, -.2f};
98 const auto left = Calculate(cache, Left, camera, 0, 0, 0, identity, equalWeights);
99 const auto right = Calculate(cache, Right, camera, 0, 0, 0, identity, equalWeights);
100 Check(left.pan < 0 && right.pan > 0, "left and right engines remain distinct stereo sources");
101 const auto bypass = Calculate(cache, -1, camera, 0, 0, 0, identity, {1, 0, 0});
102 Check(Near(bypass.gain, left.gain) && Near(bypass.pan, left.pan),
103 "shared bypass sound follows audible engines rather than the last engine in the loop");
104
105 cache = Create(1, Kind::Car, 0, .0010155f);
106 const auto car = ReadEngines("Meshes/C-2 Dasher.dix");
107 Locate(cache, car.data(), unsigned(car.size()), pitch * yaw);
108 Check(!cache.Active(Left) && !cache.Active(Right) && !cache.source[Center].fromModel &&
109 cache.source[Center].referenceDistanceKm == .005f && Near(cache.source[Center].positionKm.z, .0010155f),
110 "switching from rocket to unmarked car resets source positions, topology and level");
111 const auto chase = Calculate(cache, Center, {.0006f, .0067f, -.0161f}, 0, .175f, 0, identity, equalWeights);
112 Check(chase.gain > .26f && chase.gain < .29f, "car chase retains the quieter distance balance");
113 }
114
115 const auto car = Create(1, Kind::Car, 0, 0);
116 const auto jet = Create(1, Kind::Jet, 0, 0);
117 const auto rocket = Create(1, Kind::Rocket, 0, 0);
118 const auto at100m = [&](const Sources& cache) { return Calculate(cache, Center, {0, 0, -.1f}, 0, 0, 0, identity, equalWeights).gain; };
119 Check(Near(at100m(car), .05f) && Near(at100m(jet), .5f) && Near(at100m(rocket), 1.f),
120 "car, jet and rocket have intentionally different exterior reference levels");
121 Check(Near(Calculate(rocket, Center, {0, 0, -1.f}, 0, 0, 0, identity, equalWeights).gain, .1f),
122 "powerful engines still fall off at long range");
123 auto rotated = Create(2, Kind::Car, .020f, 0);
125 const auto parentCamera = Calculate(rotated, Left, {0, 0, .030f}, 0, 0, 0, yaw, equalWeights);
126 Check(Near(parentCamera.gain, .5f) && Near(parentCamera.pan, 0.f),
127 "parent camera accounts for child engine orientation before measuring distance");
128 Check(Calculate(Create(0, Kind::Rocket, 0, 0), -1, {}, 0, 0, 0, identity, equalWeights).gain == 0,
129 "engine-free cockpit retains no previous engine sources");
130}
131
132static void CheckRcsRecording()
133{
134 std::ifstream file("Sounds/rumble1.wav", std::ios::binary);
135 Check(bool(file), "open normalized thruster recording");
136 // This asset is 44.1 kHz stereo PCM16 with a standard 44-byte WAV header.
137 file.seekg(0, std::ios::end);
138 Check(file.tellg() == std::streamoff(44 + 589305 * 4), "normalization preserves the complete thruster loop length");
139 file.seekg(44);
140 std::vector<short> samples(589305 * 2);
141 file.read(reinterpret_cast<char*>(samples.data()), samples.size() * sizeof(short));
142 Check(bool(file), "read complete normalized thruster recording");
143 double energy = 0.;
144 int peak = 0;
145 for (short sample : samples)
146 {
147 peak = std::max(peak, std::abs(int(sample)));
148 energy += double(sample) * sample;
149 }
150 Check(peak > 32300 && peak < 32400, "thruster recording uses available headroom without clipping");
151 const double cockpitRms = std::sqrt(energy / samples.size()) / 32768. * RcsAudio::CockpitGain;
152 const double originalRms = std::pow(10., -19.059537710655906 / 20.);
153 Check(std::fabs(cockpitRms - originalRms) < .000001, "normalized source with cockpit compensation retains original RMS level");
154}
155
156static void CheckRcsSources()
157{
158 RiseMath::Matrix identity;
159 RiseMath::MatrixIdentity(&identity);
160 // E-10 limits and hull heights. F9 exhaust is positive Y in propulsion.
161 const auto mix = [&](RiseMath::Vector3 output, RiseMath::Vector3 camera, float yaw = 0.f)
162 {
163 return RcsAudio::CalculateExterior(output, .0116f, .00625f, .00625f, .003134f, .002089f,
164 camera, yaw, 0.f, 0.f, identity);
165 };
166 const RiseMath::Vector3 fullLift{0.f, .0116f, 0.f};
167 const auto below = mix(fullLift, {0.f, -.020f, 0.f});
168 Check(Near(below.gain, 1.f) && Near(below.pan, 0.f), "F9 reaches full close level beneath the vehicle");
169 Check(Near(mix(fullLift, {0.f, -.103134f, 0.f}).gain, 1.f), "dustoff retains rocket level out to 100m from the belly");
170 Check(Near(mix(fullLift, {0.f, -.203134f, 0.f}).gain, .5f), "dustoff falls off beyond its reference distance");
171 Check(Near(mix(fullLift, {0.f, .020f, 0.f}).gain, .4f), "hull shields the lift exhaust from above");
172 Check(Near(mix({0.f, .0058f, 0.f}, {0.f, -.020f, 0.f}).gain, .5f), "dustoff follows actual spool output");
173 Check(Near(mix({0.f, .0174f, 0.f}, {0.f, -.020f, 0.f}).gain, 1.f), "ground effect cannot overdrive dustoff");
174 const auto quiet = mix({}, {0.f, -.020f, 0.f});
175 Check(quiet.gain == 0.f && quiet.pan == 0.f, "zero thrust has no exterior sound");
176 Check(Near(mix({0.f, -.00625f, 0.f}, {0.f, .004f, 0.f}).gain, .25f), "F8 upper thrusters stay quieter at close range");
177 Check(Near(mix({.00625f, 0.f, 0.f}, {.004f, 0.f, 0.f}).gain, .25f), "lateral thrusters stay quieter at close range");
178 Check(Near(mix({0.f, 0.f, -.002f}, {0.f, 0.f, -.004f}).gain, .25f), "fine longitudinal thrusters stay quieter at close range");
179 Check(mix({.00625f, 0.f, -.002f}, {0.f, -.020f, 0.f}).gain < .15f,
180 "combined maneuvering jets retain short range instead of inheriting dustoff range");
181 const auto combined = mix({.00625f, .0116f, -.002f}, {0.f, -.020f, 0.f});
182 Check(Near(combined.gain, 1.f), "maneuvering during dustoff neither dilutes nor overdrives the lift sound");
183
184 const RiseMath::Vector3 camera{.015f, -.020f, -.040f};
185 const RiseMath::Vector3 relative = camera - RiseMath::Vector3{0.f, -.003134f, 0.f};
186 for (float yaw : {-.5f, 0.f, .5f, RiseMath::Pi})
187 {
188 const auto directional = mix(fullLift, camera, yaw);
189 Check(Near(directional.gain, 1.f), "looking around preserves dustoff loudness");
190 float depth;
191 const float screenX = ScreenX(relative, yaw, 0.f, 0.f, 1.f, &depth);
192 if (depth > 0.f)
193 Check(directional.pan * screenX > 0.f, "dustoff stereo follows the underside source on screen");
194 }
195 Check(mix(fullLift, camera).pan * mix(fullLift, camera, RiseMath::Pi).pan < 0.f,
196 "looking behind reverses dustoff stereo direction");
197 RiseMath::Matrix rotation;
199 RiseMath::Vector3 rotatedCamera;
200 RiseMath::Vec3TransformNormal(&rotatedCamera, &camera, &rotation);
201 const auto parent = RcsAudio::CalculateExterior(fullLift, .0116f, .00625f, .00625f, .003134f, .002089f,
202 rotatedCamera, 0.f, 0.f, RiseMath::HalfPi, rotation);
203 const auto child = mix(fullLift, camera);
204 Check(Near(parent.gain, child.gain) && Near(parent.pan, child.pan),
205 "parent camera rotates both the belly source and exhaust direction");
206 const float nan = std::numeric_limits<float>::quiet_NaN();
207 Check(mix({nan, nan, nan}, camera).gain == 0.f, "invalid thrust cannot make an exterior sound");
208 Check(mix(fullLift, {nan, 0.f, 0.f}).gain == 0.f, "invalid camera cannot make a loud thruster");
209 Check(RcsAudio::CalculateExterior(fullLift, 0.f, 0.f, 0.f, 0.f, 0.f, camera, 0.f, 0.f, 0.f, identity).gain == 0.f,
210 "vehicle without lift thrusters cannot retain dustoff sound");
211}
212
213int main()
214{
215 CheckEngineSources();
216 CheckRcsRecording();
217 CheckRcsSources();
219 // Shipped T-27 trailing camera from Cockpit::RefreshExternalCameras.
220 const RiseMath::Vector3 mammothCamera{.0009f, .0136f, -.0397f};
221 const auto genericHorn = Calculate(mammothCamera, .068f, .157f, 0);
222 const auto mammothHorn = Calculate(mammothCamera, .068f, .157f, 0,
224 Check(genericHorn.gain < .12f, "generic hull falloff reproduces the quiet Mammoth horn");
225 Check(Near(mammothHorn.gain, 1.f) && mammothHorn.gain > .75f,
226 "Mammoth trailing camera retains full horn level above the cockpit's 75 percent mix");
227 Check(Near(mammothHorn.pan, genericHorn.pan), "Mammoth horn level correction preserves stereo direction");
228 Check(Near(Calculate({0, 0, -.100f}, 0, 0, 0, VehicleHorn::MammothReferenceDistanceKm).gain, .5f) &&
229 Near(Calculate({0, 0, -.200f}, 0, 0, 0, VehicleHorn::MammothReferenceDistanceKm).gain, .25f),
230 "Mammoth horn still falls off at distances beyond its camera");
231 const auto atOrigin = Calculate({}, 0, 0, 0);
232 Check(atOrigin.gain == 1 && atOrigin.pan == 0, "coincident camera is finite and centered");
233 Check(Calculate({0, 0, -.001f}, 0, 0, 0).gain == 1, "close mount retains source level");
234 Check(Near(Calculate({0, 0, -.010f}, 0, 0, 0).gain, .5f), "ten metres halves source amplitude");
235 Check(Near(Calculate({0, 0, -.020f}, 0, 0, 0).gain, .25f), "doubling distance halves amplitude again");
236 Check(Calculate({0, 0, -1.f}, 0, 0, 0).gain < .01f, "distant hull becomes quiet");
237
238 const RiseMath::Vector3 c2{.0006f, .0067f, -.0161f};
239 const RiseMath::Vector3 c2a{0, .005f, -.0124f};
240 const auto chase = Calculate(c2, 0, .175f, 0);
241 const auto chaseA = Calculate(c2a, .003f, .175f, 0);
242 Check(chase.gain > .28f && chase.gain < .30f, "C-2 chase level is about 29 percent of close level");
243 Check(chaseA.gain > .37f && chaseA.gain < .38f, "C-2A chase level is about 37 percent of close level");
244 Check(std::fabs(chase.pan) < .04f && std::fabs(chaseA.pan) < .01f, "chase cameras keep vehicle sound near center");
245
246 for (float yaw : {-.8f, -.2f, 0.f, .2f, .8f})
247 for (float pitch : {-.3f, 0.f, .3f})
248 for (float roll : {-.5f, 0.f, .5f})
249 {
250 const auto mix = Calculate(c2, yaw, pitch, roll);
251 Check(Near(mix.gain, chase.gain), "looking around never changes source distance");
252 for (float zoom : {.5f, 1.f, 4.f})
253 Check(mix.pan * ScreenX(c2, yaw, pitch, roll, zoom) >= 0.f,
254 "stereo side matches the actual flipped projection across pan, tilt, roll and zoom");
255 }
256 Check(Calculate({-.01f, 0, 0}, 0, 0, 0).pan == 1, "vehicle at screen right pans right");
257 Check(Calculate({.01f, 0, 0}, 0, 0, 0).pan == -1, "vehicle at screen left pans left");
258 Check(Calculate({0, .01f, 0}, 0, 0, 0).pan == 0, "overhead source stays centered");
259 Check(std::fabs(Calculate(c2, RiseMath::Pi, 0, 0).pan) < 1, "rear source retains valid stereo pan");
260
261 // Parent-mounted cameras translate from the child's dock offset, just as rendering does.
262 const RiseMath::Vector3 parentMount{0, 0, -.020f}, childOffset{.010f, 0, 0};
263 const auto child = Calculate(parentMount - childOffset, 0, 0, 0);
264 Check(child.pan > 0 && child.gain < .25f, "parent mount measures distance and bearing to the child hull");
265 const float nan = std::numeric_limits<float>::quiet_NaN();
266 Check(Calculate({nan, 0, 0}, 0, 0, 0).gain == 0, "invalid position cannot produce a loud channel");
267 Check(Calculate(c2, nan, 0, 0).gain == 0, "invalid orientation cannot produce a loud channel");
268 std::cout << "Vehicle sound spatial tests passed\n";
269}
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
VehicleSoundSpatial::Mix CalculateExterior(const RiseMath::Vector3 &output, float downLimit, float upLimit, float lateralLimit, float bellyDepthKm, float roofHeightKm, const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, const RiseMath::Matrix &bodyToCameraFrame)
Definition RcsAudio.h:21
constexpr float CockpitGain
Definition RcsAudio.h:10
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141
constexpr float Pi
Definition ClientMath.h:12
Matrix * MatrixRotationX(Matrix *out, float value)
Definition ClientMath.h:126
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Matrix * MatrixRotationY(Matrix *out, float value)
Definition ClientMath.h:127
Matrix * MatrixRotationYawPitchRoll(Matrix *out, float yaw, float pitch, float roll)
Definition ClientMath.h:137
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition ClientMath.h:118
Matrix * MatrixPerspectiveRH(Matrix *out, float width, float height, float nearZ, float farZ)
Definition ClientMath.h:143
Matrix * MatrixIdentity(Matrix *out)
Definition ClientMath.h:115
Matrix * MatrixScaling(Matrix *out, float x, float y, float z)
Definition ClientMath.h:124
Matrix * MatrixRotationZ(Matrix *out, float value)
Definition ClientMath.h:128
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)
Definition ClientMath.h:125
constexpr float HalfPi
Definition ClientMath.h:12
constexpr float MammothReferenceDistanceKm
Definition VehicleHorn.h:7
Mix Calculate(const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, float referenceDistanceKm=.005f)
type
Definition erin.py:143