载入中...
搜索中...
未找到
AudioZone.cpp
浏览该文件的文档.
1#include "audio/AudioZone.h"
2#include "audio/Source.h"
3
4#include <algorithm>
5#include <cmath>
6
7namespace eve::audio {
8namespace {
9bool finiteItem(const AudioZoneItem& v) {
10 return std::isfinite(v.volume) && std::isfinite(v.fadeInTime) && std::isfinite(v.fadeOutTime) &&
11 std::isfinite(v.duration) && v.volume >= 0 && v.fadeInTime >= 0 && v.fadeOutTime >= 0 && v.duration >= 0;
12}
13float unit(std::uint32_t seed) {
14 seed = seed * 1664525u + 1013904223u;
15 return float(seed >> 8u) * (1.f / 16777216.f);
16}
17
18float trackEnvelopeVolume(const AudioZoneItem* item, const AudioZoneState& state, float now) {
19 if (!item) return 0.f;
20 float volume = item->volume;
21 if (now < state.fadeInEnds && state.fadeInEnds > state.trackStarted)
22 volume = item->volume * (now - state.trackStarted) / (state.fadeInEnds - state.trackStarted);
23 else if (now >= state.fadeOutBegins && now < state.fadeOutEnds && state.fadeOutEnds > state.fadeOutBegins)
24 volume = item->volume * (state.fadeOutEnds - now) / (state.fadeOutEnds - state.fadeOutBegins);
25 else if (now >= state.fadeOutEnds)
26 volume = 0.f;
27 return volume;
28}
29} // namespace
30
32 if (!finiteItem(item))
34 DiagnosticCode::InvalidArgument, "audio-zone track values must be finite and non-negative", "audio.zone"));
35 items_.push_back(item);
36 return Result<int>::success(itemCount() - 1);
37}
38const AudioZoneItem* AudioZoneProfile::itemAt(int index) const noexcept {
39 return index >= 0 && index < itemCount() ? &items_[static_cast<std::size_t>(index)] : nullptr;
40}
41
43 float py, float pz, float master, std::uint32_t seed) {
44 if (!std::isfinite(now) || !std::isfinite(px) || !std::isfinite(py) || !std::isfinite(pz) ||
45 !std::isfinite(master) || !std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z) ||
46 !std::isfinite(p.radius) || !std::isfinite(p.minimumBreakTime) || !std::isfinite(p.maximumBreakTime) ||
47 !std::isfinite(p.deactivationTime) || now < 0 || master < 0 || p.radius < 0 || p.minimumBreakTime < 0 ||
48 p.maximumBreakTime < p.minimumBreakTime || p.deactivationTime < 0)
50 DiagnosticCode::InvalidArgument, "audio-zone inputs must be finite and ranges valid", "audio.zone"));
51 AudioZoneState next = state;
53 const float dx = p.x - px, dy = p.y - py, dz = p.z - pz;
54 const bool inRange = p.global || dx * dx + dy * dy + dz * dz <= p.radius * p.radius;
55 if (inRange) {
56 if (next.phase != AudioZonePhase::Active) {
57 next.phase = AudioZonePhase::Active;
58 next.deactivateAt = 0;
59 next.exitFadeFrom = 0.f;
60 }
61 } else if (next.phase == AudioZonePhase::Active) {
62 const auto* item = p.itemAt(next.selectedTrack);
63 next.exitFadeFrom = next.playing ? std::min(trackEnvelopeVolume(item, next, now), master) : 0.f;
65 next.deactivateAt = now + p.deactivationTime;
66 } else if (next.phase == AudioZonePhase::BecomingInactive && now >= next.deactivateAt) {
67 next.phase = AudioZonePhase::Inactive;
68 next.playing = false;
69 out.stop = true;
70 out.volume = 0.f;
71 next.selectedTrack = -1;
72 next.exitFadeFrom = 0.f;
73 state = next;
75 }
76
77 if (next.phase == AudioZonePhase::BecomingInactive) {
78 const float windowStart = next.deactivateAt - p.deactivationTime;
79 float volume = 0.f;
80 if (p.deactivationTime <= 0.f || now >= next.deactivateAt) {
81 volume = 0.f;
82 } else if (now <= windowStart) {
83 volume = next.exitFadeFrom;
84 } else {
85 volume = next.exitFadeFrom * (next.deactivateAt - now) / p.deactivationTime;
86 }
87 out.volume = std::max(0.f, volume);
88 state = next;
90 }
91
92 if (next.phase == AudioZonePhase::Active && p.itemCount() > 0 && !next.playing && now > next.nextTrackStarts) {
93 int chosen = std::min(p.itemCount() - 1, int(unit(seed) * p.itemCount()));
94 if (chosen == next.selectedTrack && p.itemCount() > 1) chosen = (chosen + 1) % p.itemCount();
95 const auto* item = p.itemAt(chosen);
96 next.selectedTrack = chosen;
97 next.playing = true;
98 next.trackStarted = now;
99 next.fadeInEnds = now + item->fadeInTime;
100 next.fadeOutBegins = now + item->duration - item->fadeOutTime;
101 next.fadeOutEnds = now + item->duration;
102 next.nextTrackStarts = next.fadeOutEnds + p.minimumBreakTime +
103 (p.maximumBreakTime - p.minimumBreakTime) * unit(seed ^ 0x9e3779b9u);
104 out.play = true;
105 out.trackIndex = chosen;
106 }
107 if (next.playing) {
108 const auto* item = p.itemAt(next.selectedTrack);
109 float volume = trackEnvelopeVolume(item, next, now);
110 if (item && now >= next.fadeOutEnds) {
111 next.playing = false;
112 out.stop = true;
113 volume = 0.f;
114 }
115 out.volume = std::min(volume, master);
116 }
117 state = next;
119}
120
122 if (!source || !std::isfinite(output.volume) || output.volume < 0.f)
125 "audio-zone output requires a source and finite non-negative volume", "audio.zone"));
126 source->setVolume(output.volume);
127 if (output.stop) source->stop();
128 if (output.play) source->play();
129 return Result<void>::success();
130}
131} // namespace eve::audio
std::string output
double volume
glm::vec4 p[6]
float v
std::vector< Colorf > px
glm::dvec3 dx
glm::dvec3 dz
ScriptStructure out
std::string_view source
float dy
Definition Shadow.cpp:151
TacticalUnit * unit
uint32_t seed
Definition TreeMesh.cpp:276
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Caller-owned Pcg audio-zone configuration and track list.
Definition AudioZone.h:19
int itemCount() const noexcept
Definition AudioZone.h:26
const AudioZoneItem * itemAt(int index) const noexcept
Borrow track data.
Definition AudioZone.cpp:38
Result< int > addItem(const AudioZoneItem &item)
Add one validated track profile.
Definition AudioZone.cpp:31
A playable audio source (static buffer or streaming decoder). Not thread-safe for playback control ex...
Definition Source.h:30
Result< AudioZoneOutput > evaluateAudioZone(const AudioZoneProfile &p, AudioZoneState &state, float now, float px, float py, float pz, float master, std::uint32_t seed)
Evaluate PcgAudioManager/PcgAudioZone for one explicit time and player observation.
Definition AudioZone.cpp:42
Result< void > applyAudioZoneOutput(Source *source, const AudioZoneOutput &output)
Apply one evaluated command to the caller-selected track Source without retaining it.
One Pcg audio-zone track timing and gain profile.
Definition AudioZone.h:13
Commands to apply to a caller-owned audio Source.
Definition AudioZone.h:49
Persistent playback state owned by the caller.
Definition AudioZone.h:39