载入中...
搜索中...
未找到
Source.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Object.h"
4
5#include <AL/al.h>
6#include <AL/alc.h>
7
8#include <atomic>
9#include <mutex>
10#include <queue>
11#include <string>
12#include <vector>
13
14#include "AudioTypes.h"
15
16namespace eve {
17namespace sound {
18class SoundData;
19class Decoder;
20} // namespace sound
21namespace audio {
22
23class Audio;
24
30class Source : public Object {
31public:
35 Source(Audio *audio, sound::Decoder *decoder, bool streaming, bool takeDecoderOwnership = false);
36 ~Source() override;
37
39 void play();
41 void pause();
43 void stop();
44 bool isPlaying() const;
45
47 void setVolume(float v);
48 float getVolume() const;
50 void setPitch(float p);
51 float getPitch() const;
53 void setLooping(bool l);
54 bool isLooping() const;
55
57 bool seek(double seconds);
59 double tell() const;
61 double getDuration() const;
62
64 void setPosition(float x, float y, float z);
66 void setVelocity(float x, float y, float z);
68 void setDirection(float x, float y, float z);
70 void setRelative(bool relative);
72 void setAttenuationDistances(float ref, float max);
73
74 bool isStreaming() const { return streaming; }
76 void pump();
77
84
85private:
86 friend class Audio;
87
88 void applyGainPitch();
89 ALenum alFormat() const;
90 void ensureStaticBuffer();
91 int decoderSampleRateOr(int fallback) const;
92
93 Audio *audio = nullptr;
94 sound::SoundData *staticData = nullptr;
95 sound::Decoder *decoder = nullptr;
96 bool ownsDecoder = false;
97 bool streaming = false;
98
99 ALuint alSource = 0;
100 ALuint alBuffer = 0;
101 ALuint streamBuffers[kStreamBufferCount]{};
102 int streamBufferCount = 0;
103
104 float volume = 1.f;
105 float pitch = 1.f;
106 bool looping = false;
107
108 // Guards all access to `decoder` (including its lifetime): the Audio worker
109 // thread reads/decodes via fillPendingFromDecoder() while other threads may
110 // seek/stop/destroy the Source (and delete its Decoder) concurrently.
111 mutable std::mutex decoderMutex;
112
113 std::mutex pendingMutex;
114 std::queue<PcmChunk> pending;
115 std::atomic<bool> playing{false};
116 std::atomic<bool> wantsData{false};
117};
118
119} // namespace audio
120} // namespace eve
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
glm::vec4 p[6]
Light2D::Data * data
int v
Object is the base class for all game objects. It provides reference counting and dirty flag for upda...
Definition Object.h:18
OpenAL audio module: device management, master listener state, and Source factory....
Definition Audio.h:27
A playable audio source (static buffer or streaming decoder). Not thread-safe for playback control ex...
Definition Source.h:30
void fillPendingFromDecoder()
Called by the Audio worker thread. Internally synchronized against concurrent decoder teardown (see d...
Definition Source.cpp:250
void play()
Starts (or resumes) playback.
Definition Source.cpp:120
~Source() override
Definition Source.cpp:46
bool isStreaming() const
Definition Source.h:74
void pause()
Pauses playback, keeping the play position.
Definition Source.cpp:135
void setVolume(float v)
Sets source gain (clamped to >= 0).
Definition Source.cpp:181
float getVolume() const
Definition Source.cpp:185
void setVelocity(float x, float y, float z)
Sets the source velocity (used by OpenAL doppler).
Definition Source.cpp:239
bool isPlaying() const
Definition Source.cpp:175
float getPitch() const
Definition Source.cpp:191
void setRelative(bool relative)
Makes the source ignore the listener position (head-relative).
Definition Source.cpp:241
void setPosition(float x, float y, float z)
Sets the source position in world units.
Definition Source.cpp:238
void stop()
Stops playback and rewinds the play position.
Definition Source.cpp:140
double getDuration() const
Total duration in seconds (0 for live/unbounded streams).
Definition Source.cpp:229
double tell() const
Current play time in seconds.
Definition Source.cpp:223
void setAttenuationDistances(float ref, float max)
Sets OpenAL reference and maximum attenuation distances.
Definition Source.cpp:244
bool seek(double seconds)
Seeks to a time in seconds; false when seeking is unsupported.
Definition Source.cpp:200
void setLooping(bool l)
Enables/disables looping.
Definition Source.cpp:193
void setDirection(float x, float y, float z)
Sets the source directional cone orientation.
Definition Source.cpp:240
bool isLooping() const
Definition Source.cpp:198
void pump()
Main thread: queues/unqueues AL buffers for a streaming source.
Definition Source.cpp:286
void setPitch(float p)
Sets playback pitch (clamped to >= 0).
Definition Source.cpp:187
ref is a smart pointer that automatically calls ref() and unref() on the object. Type T must be a sub...
Definition Object.h:54
Streaming audio decoder (medialoader-backed). Owns the raw encoded bytes and decodes incrementally vi...
Definition Decoder.h:20
Raw PCM audio buffer with format metadata (samples/rate/bit depth/channels).
Definition SoundData.h:13
constexpr int kStreamBufferCount
流式 Source 的 OpenAL 缓冲数量。
Definition AudioTypes.h:11
Definition Build.cpp:11