1#include "../WiperAudio.h"
2#include "../RainAirAudio.h"
3#include "../Viewscreen/WindshieldWipers.h"
4#include <cstdlib>
5#include <iostream>
6#include <limits>
7#include <string>
8#include <vector>
9
10static void Check(bool ok, const char* message)
11{
12 if (!ok) { std::cerr << "FAIL: " << message << " (BASS " << BASS_ErrorGetCode() << ")\n"; std::exit(1); }
13}
14static double Seconds(HSTREAM stream)
15{
16 return BASS_ChannelBytes2Seconds(stream, BASS_ChannelGetPosition(stream, BASS_POS_BYTE));
17}
18#include "WiperFrameFixture.h"
19int main(int argc, char** argv)
20{
21 const bool wipersOnly = argc == 2 && std::string(argv[1]) == "--wipers-only";
22 Check(BASS_Init(0, 44100, 0, nullptr, nullptr), "silent BASS device");
23 HSTREAM rainRaw[2]{}, rainGlass[2]{};
24 const char* rainPaths[] = {"Sounds/Cockpits/BleedAir-DuctGlass-Loop.wav", "Sounds/Cockpits/BleedAir-ExteriorRush-Loop.wav"};
25 for (int i = 0; i < 2; ++i)
26 {
27 const HSTREAM decoder = BASS_StreamCreateFile(FALSE, rainPaths[i], 0, 0, BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT);
28 Check(decoder != 0, "load approved rain-air stem");
29 BASS_CHANNELINFO stemInfo{};
30 Check(BASS_ChannelGetInfo(decoder, &stemInfo) && stemInfo.freq == 48000 && stemInfo.chans == 2,
31 "both stems are stereo 48 kHz");
32 const QWORD length = BASS_ChannelGetLength(decoder, BASS_POS_BYTE);
33 Check(BASS_ChannelBytes2Seconds(decoder, length) == 4., "both stems are exactly four seconds");
34 float samples[4096]; double energy = 0;
35 for (
DWORD bytes; (bytes = BASS_ChannelGetData(decoder, samples,
sizeof samples)) !=
static_cast<DWORD>(-1) && bytes > 0;)
36 for (
DWORD j = 0; j < bytes /
sizeof(float); ++j)
37 {
38 Check(std::isfinite(samples[j]) && std::fabs(samples[j]) < 1.f, "stem has no clipping or invalid samples");
39 energy += samples[j] * samples[j];
40 }
41 Check(energy > 0, "stem contains audible data");
42
43 Check(BASS_ChannelSetPosition(decoder, 0, BASS_POS_BYTE) != FALSE, "rewind stem");
44 std::vector<float> first(static_cast<size_t>(length) / sizeof(float)), repeated(first.size());
45 Check(BASS_ChannelGetData(decoder, first.data(),
static_cast<DWORD>(length)) == length,
"decode full stem");
46 Check(BASS_ChannelFlags(decoder, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP) !=
static_cast<DWORD>(-1),
"enable stem looping");
47 Check(BASS_ChannelSetPosition(decoder, 0, BASS_POS_BYTE) != FALSE, "rewind loop");
48 for (int repeat = 0; repeat < 2; ++repeat)
49 {
50 Check(BASS_ChannelGetData(decoder, repeated.data(),
static_cast<DWORD>(length)) == length,
"decode complete repeated loop");
51 Check(first == repeated, "loop wraps without added silence or sample changes");
52 }
53 BASS_StreamFree(decoder);
54 rainRaw[i] = BASS_StreamCreateFile(FALSE, rainPaths[i], 0, 0, BASS_SAMPLE_LOOP);
55 rainGlass[i] = BASS_StreamCreateFile(FALSE, rainPaths[i], 0, 0, BASS_SAMPLE_LOOP);
56 Check(rainRaw[i] && rainGlass[i], "load both camera variants on the silent playback device");
57 }
58 WiperFrameFixture::Run(rainRaw, rainGlass, 4.f);
59 for (int i = 0; i < 2; ++i)
60 Check(BASS_StreamFree(rainRaw[i]) && BASS_StreamFree(rainGlass[i]), "release rain-air streams");
61 const HSTREAM raw = BASS_StreamCreateFile(FALSE, "Sounds/Cockpits/wipers.wav", 0, 0, BASS_STREAM_DECODE);
62 const HSTREAM glass = BASS_StreamCreateFile(FALSE, "Sounds/Cockpits/wipers.wav", 0, 0, BASS_STREAM_DECODE);
63 Check(raw && glass, "load supplied clip for both camera streams");
64 BASS_CHANNELINFO
info{};
65 Check(BASS_ChannelGetInfo(raw, &info), "read recording format");
66 const float duration = static_cast<float>(BASS_ChannelBytes2Seconds(raw, BASS_ChannelGetLength(raw, BASS_POS_BYTE)));
67 Check(
info.freq == 44100 &&
info.chans == 2 && duration > 1.80f && duration < 1.82f,
"original stereo recording is intact");
69 Check(normalRate > 2.16f && normalRate < 2.18f, "recording follows the production 1.2 Hz blade cycle");
71 {
73 Check(std::fabs(duration / rate - 1.f / speed) < .00001f, "one complete clip per animation cycle at each speed");
74 Check(BASS_ChannelSetAttribute(raw, BASS_ATTRIB_FREQ,
info.freq * rate),
"apply speed-dependent pitch");
75 float frequency = 0;
76 Check(BASS_ChannelGetAttribute(raw, BASS_ATTRIB_FREQ, &frequency) && std::fabs(frequency -
info.freq * rate) < 1.f,
77 "BASS retains requested playback frequency");
78 for (HSTREAM stream : {raw, glass})
79 {
81 Check(std::fabs(Seconds(stream) - .25 * duration) < .0001, "resumption seeks within one sample");
82 const double before = Seconds(stream);
83 Check(
WiperAudio::Synchronize(stream, .25f + speed * .02f, duration, speed,
false),
"small normal clock difference");
84 Check(Seconds(stream) == before, "normal playback does not repeatedly seek");
86 Check(std::fabs(Seconds(stream) - .7 * duration) < .0001, "stall correction follows current blade position");
88 const double end = Seconds(stream);
90 "loop wrap does not cause an unnecessary seek");
91 }
92 }
93 for (int fps : {30, 144})
94 {
96 for (int i = 0; i < fps * 10; ++i)
97 {
98 motion.
Update(1.f / fps,
true);
99 Check(motion.
moving,
"running animation remains audible across wraps");
100 }
102 Check(!motion.
moving,
"zero elapsed time is silent");
103 motion.
Update(1.f / fps,
true,
false);
104 Check(!motion.
moving,
"power loss is silent");
105 for (int i = 0; i < fps * 2; ++i)
106 {
107 motion.
Update(1.f / fps,
false);
108 Check(motion.
moving == (motion.
phase != 0),
"OFF sounds through return stroke then stops at park");
109 }
110 Check(motion.
phase == 0 && !motion.
moving,
"parked wipers stay silent");
111 }
113 enabled.
Update(.25f,
true);
114 disabled.Update(.25f, false);
115 Check(enabled.
phase == 0 && enabled.
moving,
"exact cycle boundary is still moving while ON");
116 Check(disabled.phase == 0 && !disabled.moving, "same boundary parks silently after OFF");
118 Check(!
WiperAudio::Synchronize(raw, std::numeric_limits<float>::quiet_NaN(), duration, .6f,
true),
"invalid phase rejected");
119 if (!wipersOnly)
120 for (const char* model : {"car", "mammoth", "truck", "ecto"})
121 for (const char* section : {"start", "loop", "end"})
122 {
123 const std::string
path = std::string(
"Sounds/Horns/") + model +
"-" + section +
".wav";
124 const HSTREAM horn = BASS_StreamCreateFile(FALSE,
path.c_str(), 0, 0, BASS_STREAM_DECODE);
125 Check(horn != 0, "load horn attack, sustain, and release");
126 BASS_CHANNELINFO hornInfo{};
127 Check(BASS_ChannelGetInfo(horn, &hornInfo) && hornInfo.freq == 44100 && hornInfo.chans == 2,
128 "horn sections preserve stereo PCM format");
129 Check(BASS_ChannelGetLength(horn, BASS_POS_BYTE) > 44100 * 4 / 10, "horn section contains playable audio");
130 Check(BASS_StreamFree(horn), "release horn section");
131 }
132 Check(BASS_StreamFree(raw) && BASS_StreamFree(glass) && BASS_Free(), "clean audio shutdown");
133 std::cout << "PASS: original WAV, speed/pitch, sample-accurate start/resume, drift correction, seamless wrap, 30/144 FPS, parking and inactive cases.\n";
134}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
float PlaybackRate(float duration, float cyclesPerSecond)
bool Synchronize(HSTREAM stream, float phase, float duration, float cyclesPerSecond, bool force)
static constexpr float rate
void Update(float elapsed, bool enabled, bool powered=true)