-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm8_presentation.cpp
More file actions
164 lines (149 loc) · 4.8 KB
/
Copy pathm8_presentation.cpp
File metadata and controls
164 lines (149 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// m8_presentation.cpp - see header.
#include "m8_presentation.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
namespace m8presentation
{
namespace
{
constexpr std::uint64_t kFnvOffset = 1469598103934665603ULL;
constexpr std::uint64_t kFnvPrime = 1099511628211ULL;
std::uint64_t signature_of(Cue cue, const std::vector<std::int16_t>& samples)
{
std::uint64_t hash = kFnvOffset;
hash ^= static_cast<std::uint8_t>(cue);
hash *= kFnvPrime;
for (const std::int16_t sample : samples)
{
const std::uint16_t bits = static_cast<std::uint16_t>(sample);
hash ^= static_cast<std::uint8_t>(bits & 0xffU);
hash *= kFnvPrime;
hash ^= static_cast<std::uint8_t>((bits >> 8U) & 0xffU);
hash *= kFnvPrime;
}
return hash;
}
}
CueSpec cue_spec(Cue cue)
{
switch (cue)
{
case Cue::AttackMiss: return { 130, 520, 180, 1, 7000 };
case Cue::Hit: return { 100, 190, 100, 1, 12000 };
case Cue::PlayerDamage: return { 220, 125, 62, 2, 13000 };
case Cue::EnemyDeath: return { 300, 330, 72, 2, 12000 };
case Cue::RewardOffered: return { 260, 440, 660, 2, 9000 };
case Cue::RewardChosen: return { 360, 660, 1040, 3, 10000 };
case Cue::BuildProc: return { 280, 880, 1320, 3, 10500 };
case Cue::FloorStart: return { 420, 260, 620, 2, 8500 };
case Cue::WardenBreak: return { 520, 110, 920, 3, 13000 };
case Cue::Victory: return { 720, 523, 1046, 4, 11000 };
case Cue::Defeat: return { 650, 220, 55, 3, 11500 };
case Cue::Count:
default: return {};
}
}
const char* cue_name(Cue cue)
{
switch (cue)
{
case Cue::AttackMiss: return "attack-miss";
case Cue::Hit: return "hit";
case Cue::PlayerDamage: return "player-damage";
case Cue::EnemyDeath: return "enemy-death";
case Cue::RewardOffered: return "reward-offered";
case Cue::RewardChosen: return "reward-chosen";
case Cue::BuildProc: return "build-proc";
case Cue::FloorStart: return "floor-start";
case Cue::WardenBreak: return "warden-break";
case Cue::Victory: return "victory";
case Cue::Defeat: return "defeat";
case Cue::Count:
default: return "invalid";
}
}
Overlay choose_overlay(
bool result_visible,
bool paused,
bool contract_pending,
bool reward_pending,
bool onboarding_visible)
{
if (result_visible)
{
return Overlay::Result;
}
if (paused)
{
return Overlay::Pause;
}
if (contract_pending)
{
return Overlay::Contract;
}
if (reward_pending)
{
return Overlay::Reward;
}
return onboarding_visible ? Overlay::Onboarding : Overlay::None;
}
bool show_onboarding(
int floor_index,
int chosen_pick_count,
double elapsed_game_seconds,
double duration_seconds)
{
return floor_index == 1
&& chosen_pick_count == 0
&& elapsed_game_seconds >= 0.0
&& duration_seconds > 0.0
&& elapsed_game_seconds < duration_seconds;
}
PcmBuffer generate_pcm(Cue cue, int sample_rate)
{
PcmBuffer out;
out.sample_rate = std::max(8000, sample_rate);
const CueSpec spec = cue_spec(cue);
const int sample_count = std::max(2, (out.sample_rate * spec.duration_ms) / 1000);
out.samples.resize(static_cast<std::size_t>(sample_count), 0);
std::uint64_t phase = 0;
const int attack_samples = std::max(1, out.sample_rate / 200); // 5 ms
const int release_samples = std::max(1, out.sample_rate / 30); // ~33 ms
for (int i = 0; i < sample_count; ++i)
{
const std::int64_t frequency = spec.start_hz
+ (static_cast<std::int64_t>(spec.end_hz - spec.start_hz) * i)
/ std::max(1, sample_count - 1);
phase += (static_cast<std::uint64_t>(std::max<std::int64_t>(1, frequency)) << 32U)
/ static_cast<std::uint64_t>(out.sample_rate);
const std::uint32_t unit = static_cast<std::uint32_t>((phase >> 16U) & 0xffffU);
const std::int32_t triangle = unit < 32768U
? static_cast<std::int32_t>(unit * 2U) - 32768
: static_cast<std::int32_t>((65535U - unit) * 2U) - 32768;
const int attack = std::min(32767, (i * 32767) / attack_samples);
const int release = std::min(32767, ((sample_count - 1 - i) * 32767) / release_samples);
int envelope = std::min(attack, release);
// Separate multi-pulse cues with a short deterministic valley. This produces
// distinct readable rhythms without floating point or external wave assets.
if (spec.pulse_count > 1)
{
const int pulse_position = (i * spec.pulse_count * 100) / sample_count;
if ((pulse_position % 100) >= 82)
{
envelope /= 6;
}
}
const std::int64_t scaled = static_cast<std::int64_t>(triangle)
* spec.amplitude * envelope / (32768LL * 32767LL);
out.samples[static_cast<std::size_t>(i)] = static_cast<std::int16_t>(
std::clamp<std::int64_t>(scaled, -32768, 32767));
}
// Exact silent endpoints prevent clicks and give the tests a format-independent invariant.
out.samples.front() = 0;
out.samples.back() = 0;
out.signature = signature_of(cue, out.samples);
return out;
}
} // namespace m8presentation