1#include "../EarMuffle.h"
2#include "../EarPopSequence.h"
3#include "../OwnedAudioStream.h"
4#include <limits>
5#include <cstdlib>
6#include <iostream>
7#include <vector>
8
9static void Check(bool value, const char* description)
10{
11 if (!value)
12 {
13 std::cerr << "FAIL: " << description << "; BASS error " << BASS_ErrorGetCode() << '\n';
14 std::exit(1);
15 }
16}
17
18static std::vector<unsigned char> DecodeEntireStream(HSTREAM stream)
19{
20 std::vector<unsigned char> decoded;
21 unsigned char block[4096];
22 for (;;)
23 {
24 const DWORD count = BASS_ChannelGetData(stream, block,
sizeof block);
25 if (count ==
static_cast<DWORD>(-1))
26 {
27 Check(BASS_ErrorGetCode() == BASS_ERROR_ENDED, "decode reaches the normal end");
28 break;
29 }
30 Check(count != 0, "decoder makes progress");
31 decoded.insert(decoded.end(), block, block + count);
32 }
33 return decoded;
34}
35
36static void CheckOwnedCommunicationsAudio()
37{
38
39
40 const unsigned samples = 16000 * 30;
41 std::vector<unsigned char> packet(44 + samples * 2);
42 auto word = [&](size_t offset, unsigned value, unsigned count)
43 {
44 for (unsigned i = 0; i < count; ++i) packet[offset + i] = static_cast<unsigned char>(value >> (8 * i));
45 };
46 std::memcpy(packet.data(), "RIFF", 4);
47 word(4, static_cast<unsigned>(packet.size() - 8), 4);
48 std::memcpy(packet.data() + 8, "WAVEfmt ", 8);
49 word(16, 16, 4); word(20, 1, 2); word(22, 1, 2);
50 word(24, 16000, 4); word(28, 32000, 4); word(32, 2, 2); word(34, 16, 2);
51 std::memcpy(packet.data() + 36, "data", 4);
52 word(40, samples * 2, 4);
53 for (unsigned i = 0; i < samples; ++i) word(44 + i * 2, (i * 37) % 60000, 2);
54 const HSTREAM reference = BASS_StreamCreateFile(TRUE, packet.data(), 0, packet.size(), BASS_STREAM_DECODE);
55 Check(reference != 0, "create reference communications decoder");
56 const auto expected = DecodeEntireStream(reference);
57 Check(expected.size() == samples * 2 && BASS_StreamFree(reference), "decode and free reference audio");
58 auto borrowedPacket = packet;
59 const HSTREAM borrowed = BASS_StreamCreateFile(TRUE, borrowedPacket.data(), 0, borrowedPacket.size(), BASS_STREAM_DECODE);
60 Check(borrowed != 0, "create old-style borrowed-memory decoder");
61 std::fill(borrowedPacket.begin() + 44, borrowedPacket.end(), static_cast<unsigned char>(0xa5));
62 Check(DecodeEntireStream(borrowed) != expected && BASS_StreamFree(borrowed),
63 "control confirms BASS continues reading the original packet after creation");
64 for (int replacement = 0; replacement < 20; ++replacement)
65 {
66 auto transientPacket = packet;
67 int error = 0;
68 const HSTREAM stream =
OwnedAudioStream::Create(transientPacket.data(), transientPacket.size(), BASS_STREAM_DECODE, error);
69 Check(stream && error == BASS_OK, "create stream owning packet audio");
70 std::fill(transientPacket.begin(), transientPacket.end(), static_cast<unsigned char>(0xa5));
71 transientPacket.clear();
72 transientPacket.shrink_to_fit();
73 Check(DecodeEntireStream(stream) == expected, "all audio survives destroyed network packet");
74 Check(BASS_StreamFree(stream), "stream replacement frees its owned audio");
75 }
76 int error = 0;
78 "empty audio is rejected");
79 const unsigned char invalid[] = {1, 2, 3, 4};
81 "invalid encoded audio is rejected");
82
83
85 "create owned stream for device shutdown");
86 std::cout << "PASS: communications audio survives packet destruction, 20 replacements, invalid input, and device shutdown setup.\n";
87}
88
89static double Tone(
EarMuffleOutput& output,
float hz,
int rate,
int channels)
90{
91 std::vector<float> data(rate * channels);
92 for (int i = 0; i < rate; ++i)
93 for (int c = 0; c < channels; ++c)
94 data[i * channels + c] = 0.2f * std::sin(6.28318530718f * hz * i / rate);
95 const DWORD length =
static_cast<DWORD>(data.size() *
sizeof(
float));
96 Check(BASS_ChannelGetData(
output.stream, data.data(), length) == length,
"process floating-point PCM");
97 double squares = 0;
98 for (size_t i = data.size() / 2; i < data.size(); ++i)
99 {
100 Check(std::isfinite(data[i]) && std::fabs(data[i]) < 1, "finite, unclipped output");
101 squares += data[i] * data[i];
102 }
103 return std::sqrt(squares / (data.size() / 2));
104}
105
106static void CheckInterfaceSeparation()
107{
109 EarMuffleOutput world{BASS_StreamCreate(44100, 2, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, STREAMPROC_DUMMY,
nullptr)};
110 EarMuffleOutput gui{BASS_StreamCreate(44100, 2, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, STREAMPROC_DUMMY,
nullptr)};
111 Check(world.
stream &&
gui.stream,
"create game and interface sources on the same device");
112 const double dry = Tone(gui, 12000, 44100, 2);
114 const double muffled = Tone(world, 12000, 44100, 2);
115 Check(muffled < dry * 0.4, "game sound retains pressure muffling");
116 Check(std::fabs(Tone(gui, 12000, 44100, 2) - dry) < 0.00001, "interface sound stays dry on shared output");
117 for (int i = 0; i < 10; ++i)
118 Check(hearing.
Apply(world.
stream),
"registering playback repeatedly succeeds");
119 Check(std::fabs(Tone(world, 12000, 44100, 2) - muffled) < 0.00001, "replaying does not stack filters");
120
121 EarMuffleOutput radio{BASS_StreamCreate(44100, 2, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, STREAMPROC_DUMMY,
nullptr)};
122 Check(radio.
stream && hearing.
Apply(radio.
stream),
"new radio/comms source inherits current pressure before playback");
123 Check(std::fabs(Tone(radio, 12000, 44100, 2) - muffled) < 0.00001, "new game source receives the same filter");
124 Check(hearing.
SetAmount(1.0f),
"update all registered game sources");
125 const double releasing = Tone(world, 12000, 44100, 2);
126 Check(releasing > muffled && std::fabs(Tone(radio, 12000, 44100, 2) - releasing) < 0.00001,
127 "pressure release updates both game sources together");
128 Check(std::fabs(Tone(gui, 12000, 44100, 2) - dry) < 0.00001, "pressure changes leave GUI audio unchanged");
129
131 Check(std::fabs(Tone(world, 12000, 44100, 2) - dry) < 0.00001, "removing a source clears its hearing filters");
132 Check(BASS_StreamFree(world.
stream),
"free registered source before reloading");
133 world.
stream = BASS_StreamCreate(44100, 2, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, STREAMPROC_DUMMY,
nullptr);
134 Check(world.
stream && hearing.
Apply(world.
stream),
"reload source while pressure is active");
135 Check(std::fabs(Tone(world, 12000, 44100, 2) - releasing) < 0.00001, "reloaded source has fresh effect handles");
136 Check(hearing.
SetAmount(0),
"external camera clears hearing effects");
137 Check(std::fabs(Tone(world, 12000, 44100, 2) - dry) < 0.00001 &&
138 std::fabs(Tone(radio, 12000, 44100, 2) - dry) < 0.00001, "all game sources return to dry response");
139 Check(hearing.
SetAmount(2) && hearing.
Apply(world.
stream),
"internal camera restores pressure filtering");
141 Check(std::fabs(Tone(world, 12000, 44100, 2) - dry) < 0.00001, "shutdown clears hearing effects");
142 Check(BASS_StreamFree(world.
stream) && BASS_StreamFree(
gui.stream) && BASS_StreamFree(radio.
stream),
"free routing test sources");
143 std::cout << "PASS: shared-output GUI isolation, new/reloaded game sources, repeat playback and pressure/camera lifecycle.\n";
144}
145
146static void CheckEarPopPairs()
147{
148 int leftFirst = 0, rightFirst = 0;
149 float shortest = 1, longest = 0;
150 for (unsigned seed = 0; seed < 1000; ++seed)
151 for (int hz : {20, 30, 60, 120})
152 {
154 int previous = -1;
155 for (int pair = 0; pair < 5; ++pair)
156 {
157
158 auto first = sequence.Update(true, 1, 0.4f, true);
159 Check(first.count == 1 && !first.stop, "one immediate leading ear");
160 const auto lead = first.voices[0];
161 Check(lead.pan == -1 || lead.pan == 1, "leading ear is hard-panned");
162 Check(lead.variant >= 0 && lead.variant < 3 && lead.variant != previous,
163 "valid leading recording without immediate repetition");
164 Check(lead.delaySeconds == 0 && lead.elapsedSeconds == 0, "leading ear has no delay");
165 lead.pan < 0 ? ++leftFirst : ++rightFirst;
166 Check(sequence.Update(true, 1, 0, false).count == 0, "zero-time frame cannot consume delay");
167 int following = 0;
168 for (int frame = 1; frame <= hz / 2; ++frame)
169 {
170 auto result = sequence.Update(true, 1, 1.0f / hz, false);
171 Check(!result.stop && result.count <= 1, "at most one delayed ear per frame");
172 if (!result.count) continue;
173 ++following;
174 const auto trail = result.voices[0];
175 Check(trail.pan == -lead.pan, "trailing ear goes fully to the opposite side");
176 Check(trail.variant >= 0 && trail.variant < 3 && trail.variant != lead.variant,
177 "left and right always use different recordings");
178 Check(lead.pitch >= 0.94f && lead.pitch <= 1.060001f &&
179 trail.pitch >= 0.94f && trail.pitch <= 1.060001f, "independent pitches remain bounded");
180 Check(trail.delaySeconds >= 0.01f && trail.delaySeconds <= 0.25f,
181 "random separation is positive and no more than 250 ms");
182 Check(trail.elapsedSeconds + 0.000001f >= trail.delaySeconds &&
183 trail.elapsedSeconds <= trail.delaySeconds + 1.0f / hz + 0.000001f,
184 "delayed ear dispatches on the first due frame");
185 Check(trail.elapsedSeconds < 0.35f, "both ears begin before the filter release finishes");
186 shortest = (std::min)(shortest, trail.delaySeconds);
187 longest = (std::max)(longest, trail.delaySeconds);
188 previous = trail.variant;
189 }
190 Check(following == 1, "exactly one trailing ear, with no duplicate playback");
191 }
192 }
193 Check(leftFirst > 8000 && rightFirst > 8000, "either ear can lead across randomized pairs");
194 Check(shortest < 0.02f && longest > 0.24f, "delay varies across the requested range");
195 for (int reason = 0; reason < 7; ++reason)
196 {
198 sequence.Update(true, 1, 0.016f, true);
200 switch (reason)
201 {
202 case 0: reset = sequence.Update(false, 1, 0.016f, true); break;
203 case 1: reset = sequence.Update(true, 2, 0.016f, false); break;
204 case 2: reset = sequence.Update(true, 1, 1.1f, false); break;
205 case 3: reset = sequence.Update(true, 1, -0.1f, false); break;
206 case 4: reset = sequence.Update(true, 1, std::numeric_limits<float>::quiet_NaN(), false); break;
207 case 5: reset = sequence.Update(true, 1, std::numeric_limits<float>::infinity(), false); break;
208 case 6: sequence.Reset(); break;
209 }
210 Check(reset.
count == 0 && (reason == 6 || reset.
stop),
"lifecycle change stops sounding ears");
211 Check(sequence.Update(true, reason == 1 ? 2 : 1, 0.5f, false).count == 0,
212 "cancelled trailing ear cannot leak back inside or into a new session");
213 Check(sequence.Update(true, 1, 0.016f, true).count == 1, "fresh pair works after cancellation");
214 }
215 std::cout << "PASS: 20000 stereo pairs at 20/30/60/120 Hz; distinct variants, opposite hard pans, "
216 << "random order/delay, no repeats and lifecycle cancellation. L/R leads: "
217 << leftFirst << '/' << rightFirst << "; delay range " << shortest << '-' << longest << " s.\n";
218}
219
220int main(
int argc,
char**)
221{
222 CheckEarPopPairs();
223 Check(HIWORD(BASS_FX_GetVersion()) == BASSVERSION, "BASS FX version");
224 Check(BASS_Init(0, 44100, 0, nullptr, nullptr), "initialize silent decode device");
225 CheckOwnedCommunicationsAudio();
226 CheckInterfaceSeparation();
227 for (const auto path : {"Sounds/People/earpop1.wav", "Sounds/People/earpop2.wav", "Sounds/People/earpop3.wav"})
228 {
229 HSTREAM clip = BASS_StreamCreateFile(false, path, 0, 0, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT);
230 Check(clip != 0, "load shipped ear pop");
231 BASS_CHANNELINFO
info{};
232 Check(BASS_ChannelGetInfo(clip, &info) &&
info.freq == 44100 && (
info.chans == 1 ||
info.chans == 2),
233 "mono/stereo 44.1 kHz asset");
234 QWORD length = BASS_ChannelGetLength(clip, BASS_POS_BYTE);
235 double seconds = BASS_ChannelBytes2Seconds(clip, length);
236 Check(seconds >= 0.05 && seconds <= 0.5, "short pop recording duration");
237 std::vector<float> data(static_cast<size_t>(length / sizeof(float)));
238 Check(BASS_ChannelGetData(clip, data.data(),
static_cast<DWORD>(length)) == length,
"decode complete clip");
239 for (
DWORD channel = 0; channel <
info.chans; ++channel)
240 Check(std::fabs(data[channel]) < 0.01f && std::fabs(data[data.size() - 1 - channel]) < 0.01f,
241 "quiet recording endpoints on each channel");
242 float peak = 0;
243 for (float sample : data)
244 {
245 Check(std::isfinite(sample), "finite decoded recording samples");
246 peak = (std::max)(peak, std::fabs(sample));
247 }
248 Check(peak > 0.01f && peak < 1.0f, "audible unclipped PCM");
249 Check(BASS_StreamFree(clip), "free decoded clip");
250 }
251 for (int rate : {8000, 22050, 44100, 48000})
252 for (int channels : {1, 2, 6})
253 {
254 EarMuffleOutput output{BASS_StreamCreate(rate, channels, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, STREAMPROC_DUMMY,
nullptr)};
255 Check(
output.stream != 0,
"create DSP test channel");
256 const double dry = Tone(output, 500, rate, channels);
257 Check(
output.Apply(2.0f),
"apply ear EQ at supported output format");
258 const double low = Tone(output, 500, rate, channels);
259 const double high = Tone(output, rate * 0.4f, rate, channels);
260 Check(low > dry * 0.9 && low < dry * 1.1, "preserve low frequency body");
261 std::cout << rate << " Hz / " << channels << " channels: HF/LF " << high / low << "\n";
262 Check(high < low * (rate == 8000 ? 0.8 : 0.4), "attenuate high frequencies within available bandwidth");
263 Check(
output.Apply(0),
"clear ear EQ");
264 Check(
output.lowpass == 0 &&
output.shelf == 0,
"remove only owned filters");
265 Check(std::fabs(Tone(output, 500, rate, channels) - dry) < 0.00001, "restore original response");
266 Check(BASS_StreamFree(
output.stream),
"free DSP test channel");
267 }
268 Check(BASS_Free(), "release silent device");
269 if (argc > 1)
270 {
271
272 Check(BASS_Init(-1, 44100, 0, nullptr, nullptr), "initialize default output");
273 for (const auto path : {"Sounds/People/earpop1.wav", "Sounds/People/earpop2.wav", "Sounds/People/earpop3.wav", "Sounds/GUI/test.wav"})
274 {
275 const HSTREAM clip = BASS_StreamCreateFile(false, path, 0, 0, 0);
276 Check(clip != 0, "load native playback stream");
278 Check(
output.Apply(1.0f) &&
output.Apply(2.0f),
"update source EQ on native playback format");
279 Check(
output.Apply(0.0f),
"remove source EQ");
280 for (float pan : {-1.0f, 1.0f})
281 {
282 float actual = 0;
283 Check(BASS_ChannelSetAttribute(clip, BASS_ATTRIB_PAN, pan) &&
284 BASS_ChannelGetAttribute(clip, BASS_ATTRIB_PAN, &actual) && actual == pan,
285 "native stereo recording accepts and retains full left/right pan");
286 }
287 Check(BASS_StreamFree(clip), "free unplayed panning test stream");
288 }
289 std::cout << "PASS: native source EQ and full left/right pan on ear pops and settings test (no audio played).\n";
290 Check(BASS_Free(), "release default output");
291 }
292 std::cout << "PASS: 3 WAV assets decoded; low-pass/shelf response and clear verified at 4 sample rates in mono, stereo and 5.1.\n";
293}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
bool Apply(HSTREAM stream)
bool SetAmount(float value)
void Remove(HSTREAM stream)
HSTREAM Create(const unsigned char *data, std::size_t length, DWORD flags, int &error)