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
19static float ScreenX(
const RiseMath::Vector3& mount,
float yaw,
float pitch,
float roll,
float zoom,
float* depth =
nullptr)
20{
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{
61 "all three physics engines drive their correct audio channel");
63 "one- and two-engine layouts use the appropriate channels");
68 const std::array<float, SlotCount> equalWeights{1, 1, 1};
69 for (bool e11 : {false, true})
70 {
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");
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;
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)
94 Check(mix.pan * screenX >= 0, "engine pan agrees with where its geometry is drawn");
95 }
96 }
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
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
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");
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
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");
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{
160
162 {
164 camera, yaw, 0.f, 0.f, identity);
165 };
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
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");
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
214{
215 CheckEngineSources();
216 CheckRcsRecording();
217 CheckRcsSources();
219
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");
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
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");
260
261
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)
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)
Sources Create(int count, Kind kind, float sideArmKm, float forwardKm)
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)
constexpr float CockpitGain
bool Near(const Body &b, const Triangle &t, double extra)
Matrix * MatrixRotationX(Matrix *out, float value)
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Matrix * MatrixRotationY(Matrix *out, float value)
Matrix * MatrixRotationYawPitchRoll(Matrix *out, float yaw, float pitch, float roll)
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Matrix * MatrixPerspectiveRH(Matrix *out, float width, float height, float nearZ, float farZ)
Matrix * MatrixIdentity(Matrix *out)
Matrix * MatrixScaling(Matrix *out, float x, float y, float z)
Matrix * MatrixRotationZ(Matrix *out, float value)
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)
constexpr float MammothReferenceDistanceKm
Mix Calculate(const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, float referenceDistanceKm=.005f)