载入中...
搜索中...
未找到
Source.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4#include <AL/al.h>
5#include <AL/alc.h>
6
7#include <atomic>
8#include <mutex>
9#include <queue>
10#include <string>
11#include <vector>
12
13#include "AudioTypes.h"
14#include "common/Result.h"
15
16namespace eve {
17namespace sound {
18class SoundData;
19class Decoder;
20} // namespace sound
21namespace audio {
22
23class Audio;
24
31public:
35 Source(Audio *audio, sound::Decoder *decoder, bool streaming, bool takeDecoderOwnership = false);
37 ~Source();
38
40 void play();
42 void pause();
44 void stop();
46 bool isPlaying() const;
47
49 void setVolume(float v);
51 float getVolume() const;
53 void setPitch(float p);
55 float getPitch() const;
57 void setLooping(bool l);
59 bool isLooping() const;
60
62 bool seek(double seconds);
64 double tell() const;
66 double getDuration() const;
67
69 void setPosition(float x, float y, float z);
71 void setVelocity(float x, float y, float z);
73 void setDirection(float x, float y, float z);
75 void setRelative(bool relative);
77 [[nodiscard]] bool isRelative() const noexcept { return relative_; }
79 void setAttenuationDistances(float ref, float max);
80
87 [[nodiscard]] Result<void> fadeTo(float targetVolume, float durationSeconds, float now);
93 [[nodiscard]] Result<void> fadeOutAndStop(float durationSeconds, float now);
95 [[nodiscard]] Result<void> cancelFade();
101 [[nodiscard]] Result<void> advanceFade(float now);
103 [[nodiscard]] float getFadeWeight() const noexcept { return fadeWeight_; }
105 [[nodiscard]] bool isFading() const noexcept { return fading_; }
106
111 [[nodiscard]] Result<void> setSpatialBlend(float blend);
113 [[nodiscard]] float getSpatialBlend() const noexcept { return spatialBlend_; }
114
120 [[nodiscard]] Result<void> setBalance(float balance);
122 [[nodiscard]] float getBalance() const noexcept { return balance_; }
123
125 [[nodiscard]] Result<void> setRolloffFactor(float factor);
127 [[nodiscard]] float getRolloffFactor() const noexcept { return rolloffFactor_; }
128
130 [[nodiscard]] int getChannelCount() const;
131
133 bool isStreaming() const { return streaming; }
135 void pump();
136
142 void fillPendingFromDecoder();
143
144private:
145 friend class Audio;
146
147 void applyGainPitch();
148 void applyRolloff();
149 void applyBalanceToAl();
150 ALenum alFormat() const;
151 void ensureStaticBuffer();
152 int decoderSampleRateOr(int fallback) const;
153 float currentAudibleVolume() const noexcept;
154 [[nodiscard]] Result<void> beginFade(float targetVolume, float durationSeconds, float now, bool stopWhenSilent);
155
156 Audio* audio = nullptr;
157 sound::SoundData* staticData = nullptr;
158 sound::Decoder* decoder = nullptr;
159 bool ownsDecoder = false;
160 bool streaming = false;
161
162 ALuint alSource = 0;
163 ALuint alBuffer = 0;
164 ALuint streamBuffers[kStreamBufferCount]{};
165 int streamBufferCount = 0;
166
167 float volume = 1.f;
168 float audibleVolume_ = 1.f;
169 float pitch = 1.f;
170 bool looping = false;
171 bool relative_ = false;
172 float spatialBlend_ = 1.f;
173 float balance_ = 0.f;
174 float rolloffFactor_ = 1.f;
175 float positionX_ = 0.f;
176 float positionY_ = 0.f;
177 float positionZ_ = 0.f;
178 bool balanceUsesStereoAngles_ = false;
179 bool balanceUsesMonoPosition_ = false;
180
181 bool fading_ = false;
182 bool stopWhenSilent_ = false;
183 float fadeWeight_ = 1.f;
184 float fadeFrom_ = 1.f;
185 float fadeToVolume_ = 1.f;
186 float fadeStart_ = 0.f;
187 float fadeEnd_ = 0.f;
188
189 // Guards all access to `decoder` (including its lifetime): the Audio worker
190 // thread reads/decodes via fillPendingFromDecoder() while other threads may
191 // seek/stop/destroy the Source (and delete its Decoder) concurrently.
192 mutable std::mutex decoderMutex;
193
194 std::mutex pendingMutex;
195 std::queue<PcmChunk> pending;
196 std::atomic<bool> playing{false};
197 std::atomic<bool> wantsData{false};
198};
199
200} // namespace audio
201} // namespace eve
double z
double y
double x
Vec3 relative
Definition AnimSmr.cpp:164
Audio * audio
double volume
std::vector< QuestEvent > pending
glm::vec4 p[6]
#define EVENGINE_API_BACKENDS
Definition Export.h:108
float v
float blend
Move-only, checked operation results for the common layer.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
OpenAL audio module: device management, master listener state, and Source factory....
Definition Audio.h:34
A playable audio source (static buffer or streaming decoder). Not thread-safe for playback control ex...
Definition Source.h:30
float getSpatialBlend() const noexcept
Returns the spatial blend in [0,1].
Definition Source.h:113
bool isFading() const noexcept
True while a fade envelope is active.
Definition Source.h:105
bool isStreaming() const
True when streaming.
Definition Source.h:133
float getBalance() const noexcept
Returns the balance in [-1,1].
Definition Source.h:122
bool isRelative() const noexcept
True when the source is head-relative.
Definition Source.h:77
float getFadeWeight() const noexcept
Fade progress in [0,1]; 1 when idle.
Definition Source.h:103
float getRolloffFactor() const noexcept
Returns the rolloff factor.
Definition Source.h:127
Streaming audio decoder (medialoader-backed). Native builds own encoded bytes and decode incrementall...
Definition Decoder.h:23
Raw PCM audio buffer with format metadata (samples/rate/bit depth/channels).
Definition SoundData.h:15
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16