-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoundSource.cpp
More file actions
242 lines (202 loc) · 5.73 KB
/
Copy pathSoundSource.cpp
File metadata and controls
242 lines (202 loc) · 5.73 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "Sound/SoundSource.h"
#include "Sound/SoundBuffer.h"
#include "Sound/SoundSystem.h"
#include "fmt/format.h"
#include <AL/al.h>
#include <AL/alc.h>
#include <stdexcept>
namespace
{
void ThrowIfAlError(std::string_view operation)
{
const ALenum errorCode = alGetError();
if (errorCode != AL_NO_ERROR)
throw std::runtime_error{ fmt::format("OpenAL error while {}: {}", operation, static_cast<int>(errorCode)) };
}
void EnsureSoundSystemReady()
{
if (!sh::sound::SoundSystem::GetInstance()->IsInit())
throw std::runtime_error{ "SoundSystem is not initialized." };
}
auto ToSoundState(ALint state) -> sh::sound::SoundState
{
switch (state)
{
case AL_INITIAL:
return sh::sound::SoundState::Initial;
case AL_PLAYING:
return sh::sound::SoundState::Playing;
case AL_PAUSED:
return sh::sound::SoundState::Paused;
case AL_STOPPED:
return sh::sound::SoundState::Stopped;
}
return sh::sound::SoundState::Stopped;
}
}
namespace sh::sound
{
SH_SOUND_API SoundSource::SoundSource()
{
EnsureSoundSystemReady();
alGetError();
alGenSources(1, &handle);
ThrowIfAlError("creating sound source");
}
SH_SOUND_API SoundSource::SoundSource(SoundSource&& other) noexcept :
handle(other.handle)
{
other.handle = 0;
}
SH_SOUND_API auto SoundSource::operator=(SoundSource&& other) noexcept -> SoundSource&
{
if (this == &other)
return *this;
Release();
handle = other.handle;
other.handle = 0;
return *this;
}
SH_SOUND_API SoundSource::~SoundSource()
{
Release();
}
SH_SOUND_API void SoundSource::SetBuffer(const SoundBuffer& buffer)
{
alSourcei(handle, AL_BUFFER, static_cast<ALint>(buffer.GetHandle()));
ThrowIfAlError("binding sound buffer to source");
}
SH_SOUND_API void SoundSource::ClearBuffer()
{
Stop();
alSourcei(handle, AL_BUFFER, 0);
ThrowIfAlError("clearing sound buffer from source");
}
SH_SOUND_API void SoundSource::Play()
{
alSourcePlay(handle);
ThrowIfAlError("playing sound source");
}
SH_SOUND_API void SoundSource::Pause()
{
alSourcePause(handle);
ThrowIfAlError("pausing sound source");
}
SH_SOUND_API void SoundSource::Stop()
{
alSourceStop(handle);
ThrowIfAlError("stopping sound source");
}
SH_SOUND_API void SoundSource::Rewind()
{
alSourceRewind(handle);
ThrowIfAlError("rewinding sound source");
}
SH_SOUND_API void SoundSource::SetLoop(bool looping)
{
alSourcei(handle, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
ThrowIfAlError("setting sound source loop state");
}
SH_SOUND_API auto SoundSource::IsLooping() const -> bool
{
ALint value = AL_FALSE;
alGetSourcei(handle, AL_LOOPING, &value);
ThrowIfAlError("querying sound source loop state");
return value == AL_TRUE;
}
SH_SOUND_API void SoundSource::SetGain(float gain)
{
if (gain < 0.f)
throw std::invalid_argument{ "Gain must be greater than or equal to zero." };
alSourcef(handle, AL_GAIN, gain);
ThrowIfAlError("setting sound source gain");
}
SH_SOUND_API auto SoundSource::GetGain() const -> float
{
float gain = 0.f;
alGetSourcef(handle, AL_GAIN, &gain);
ThrowIfAlError("querying sound source gain");
return gain;
}
SH_SOUND_API void SoundSource::SetPitch(float pitch)
{
if (pitch <= 0.f)
throw std::invalid_argument{ "Pitch must be greater than zero." };
alSourcef(handle, AL_PITCH, pitch);
ThrowIfAlError("setting sound source pitch");
}
SH_SOUND_API auto SoundSource::GetPitch() const -> float
{
float pitch = 1.f;
alGetSourcef(handle, AL_PITCH, &pitch);
ThrowIfAlError("querying sound source pitch");
return pitch;
}
SH_SOUND_API void SoundSource::SetPosition(float x, float y, float z)
{
alSource3f(handle, AL_POSITION, x, y, z);
ThrowIfAlError("setting sound source position");
}
SH_SOUND_API void SoundSource::SetPosition(const std::array<float, 3>& position)
{
SetPosition(position[0], position[1], position[2]);
}
SH_SOUND_API void SoundSource::SetVelocity(float x, float y, float z)
{
alSource3f(handle, AL_VELOCITY, x, y, z);
ThrowIfAlError("setting sound source velocity");
}
SH_SOUND_API void SoundSource::SetVelocity(const std::array<float, 3>& velocity)
{
SetVelocity(velocity[0], velocity[1], velocity[2]);
}
SH_SOUND_API void SoundSource::SetRelative(bool relative)
{
alSourcei(handle, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
ThrowIfAlError("setting sound source relative mode");
}
SH_SOUND_API void SoundSource::SetReferenceDistance(float dis)
{
alSourcef(handle, AL_REFERENCE_DISTANCE, dis);
ThrowIfAlError("setting sound source reference distance");
}
SH_SOUND_API void SoundSource::SetRolloffFactor(float factor)
{
alSourcef(handle, AL_ROLLOFF_FACTOR, factor);
ThrowIfAlError("setting sound source rolloff factor");
}
SH_SOUND_API void SoundSource::SetMaxDistance(float dis)
{
alSourcef(handle, AL_MAX_DISTANCE, dis);
ThrowIfAlError("setting sound source max distance");
}
SH_SOUND_API void SoundSource::SetPlaybackOffset(float seconds)
{
if (seconds < 0.f)
throw std::invalid_argument{ "Playback offset must be greater than or equal to zero." };
alSourcef(handle, AL_SEC_OFFSET, seconds);
ThrowIfAlError("setting sound source playback offset");
}
SH_SOUND_API auto SoundSource::GetPlaybackOffset() const -> float
{
float seconds = 0.f;
alGetSourcef(handle, AL_SEC_OFFSET, &seconds);
ThrowIfAlError("querying sound source playback offset");
return seconds;
}
SH_SOUND_API auto SoundSource::GetState() const -> SoundState
{
ALint state = AL_STOPPED;
alGetSourcei(handle, AL_SOURCE_STATE, &state);
ThrowIfAlError("querying sound source state");
return ToSoundState(state);
}
void SoundSource::Release() noexcept
{
if (handle == 0)
return;
if (alcGetCurrentContext() != nullptr)
alDeleteSources(1, &handle);
handle = 0;
}
}//namespace