Rise
The Vieneo Province
Loading...
Searching...
No Matches
EarPopSequence.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <cstdint>
6
7// Head-local audio presentation; independent of pressure thresholds and their RNG.
9{
10public:
11 struct Voice
12 {
13 int variant = 0;
14 float pan = 0.0f;
15 float pitch = 1.0f;
16 float delaySeconds = 0.0f;
17 float elapsedSeconds = 0.0f;
18 };
19 struct Playback
20 {
21 std::array<Voice, 2> voices{};
22 int count = 0;
23 bool stop = false;
24 };
25
26private:
27 std::uint32_t randomState;
28 bool initialized = false;
29 unsigned int character = 0;
30 int lastVariant = -1;
31 bool pending = false;
33 float elapsed = 0.0f;
34
35 std::uint32_t Random()
36 {
37 randomState = randomState * 1664525u + 1013904223u;
38 return randomState >> 8;
39 }
40 float Unit() { return static_cast<float>(Random()) / 16777215.0f; }
41
42public:
43 explicit EarPopSequence(std::uint32_t seed = 2098u) : randomState(seed) {}
44
45 void Reset()
46 {
47 initialized = pending = false;
48 lastVariant = -1;
49 elapsed = 0.0f;
50 }
51
52 Playback Update(bool audible, unsigned int characterId, float seconds, bool popped)
53 {
54 Playback result;
55 if (!audible || !std::isfinite(seconds) || seconds < 0.0f || seconds > 1.0f)
56 {
57 Reset();
58 result.stop = true;
59 return result;
60 }
61 if (initialized && character != characterId)
62 {
63 Reset();
64 result.stop = true;
65 }
66 initialized = true;
67 character = characterId;
68 if (pending)
69 {
70 elapsed += seconds;
71 // Dispatch on the first game update at/after the selected delay.
72 if (elapsed + 0.000001f >= second.delaySeconds)
73 {
75 result.voices[result.count++] = second;
76 pending = false;
77 }
78 }
79 if (popped)
80 {
81 const int firstVariant = lastVariant < 0 ? Random() % 3 : (lastVariant + 1 + Random() % 2) % 3;
82 const float firstPan = Random() % 2 == 0 ? -1.0f : 1.0f;
83 result.voices[result.count++] = {firstVariant, firstPan, 0.94f + 0.12f * Unit()};
84 second = {static_cast<int>((firstVariant + 1 + Random() % 2) % 3), -firstPan,
85 0.94f + 0.12f * Unit(), 0.01f + 0.24f * Unit()};
87 elapsed = 0.0f;
88 pending = true;
89 // Do not consume this triggering frame's elapsed time from the new delay.
90 }
91 return result;
92 }
93};
unsigned int character
EarPopSequence(std::uint32_t seed=2098u)
std::uint32_t randomState
std::uint32_t Random()
Playback Update(bool audible, unsigned int characterId, float seconds, bool popped)
std::array< Voice, 2 > voices