Rise
The Vieneo Province
Toggle main menu visibility
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.
8
class
EarPopSequence
9
{
10
public
:
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
26
private
:
27
std::uint32_t
randomState
;
28
bool
initialized
=
false
;
29
unsigned
int
character
= 0;
30
int
lastVariant
= -1;
31
bool
pending
=
false
;
32
Voice
second
;
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
42
public
:
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
{
74
second
.
elapsedSeconds
=
elapsed
;
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
()};
86
lastVariant
=
second
.
variant
;
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
};
EarPopSequence::character
unsigned int character
Definition
EarPopSequence.h:29
EarPopSequence::initialized
bool initialized
Definition
EarPopSequence.h:28
EarPopSequence::EarPopSequence
EarPopSequence(std::uint32_t seed=2098u)
Definition
EarPopSequence.h:43
EarPopSequence::randomState
std::uint32_t randomState
Definition
EarPopSequence.h:27
EarPopSequence::pending
bool pending
Definition
EarPopSequence.h:31
EarPopSequence::Random
std::uint32_t Random()
Definition
EarPopSequence.h:35
EarPopSequence::elapsed
float elapsed
Definition
EarPopSequence.h:33
EarPopSequence::second
Voice second
Definition
EarPopSequence.h:32
EarPopSequence::lastVariant
int lastVariant
Definition
EarPopSequence.h:30
EarPopSequence::Update
Playback Update(bool audible, unsigned int characterId, float seconds, bool popped)
Definition
EarPopSequence.h:52
EarPopSequence::Unit
float Unit()
Definition
EarPopSequence.h:40
EarPopSequence::Reset
void Reset()
Definition
EarPopSequence.h:45
EarPopSequence::Playback
Definition
EarPopSequence.h:20
EarPopSequence::Playback::count
int count
Definition
EarPopSequence.h:22
EarPopSequence::Playback::voices
std::array< Voice, 2 > voices
Definition
EarPopSequence.h:21
EarPopSequence::Playback::stop
bool stop
Definition
EarPopSequence.h:23
EarPopSequence::Voice
Definition
EarPopSequence.h:12
EarPopSequence::Voice::variant
int variant
Definition
EarPopSequence.h:13
EarPopSequence::Voice::pan
float pan
Definition
EarPopSequence.h:14
EarPopSequence::Voice::delaySeconds
float delaySeconds
Definition
EarPopSequence.h:16
EarPopSequence::Voice::pitch
float pitch
Definition
EarPopSequence.h:15
EarPopSequence::Voice::elapsedSeconds
float elapsedSeconds
Definition
EarPopSequence.h:17
EarPopSequence.h
Generated on
for Rise by
1.18.0