-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudio_System.cpp
More file actions
310 lines (253 loc) · 7.64 KB
/
Audio_System.cpp
File metadata and controls
310 lines (253 loc) · 7.64 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "Audio_System.h"
#include "Channel.h"
#include "Sound.h"
#include <iostream>
#include "FileProcessing_MP3.h"
#include "FileProcessing_OGG.h"
#include "FileProcessing_WAV.h"
Audio_System::Audio_System()
{
assert(m_instance == nullptr && "There should be only one instance of Audio_System alive at the same time");
// constructor
std::ignore = CoInitializeEx(NULL, COINIT_MULTITHREADED);
XAudio2Create(&m_pEngine);
m_pEngine->CreateMasteringVoice(&m_pMaster, 2, 44100);
DWORD dwChannelMask;
m_pMaster->GetChannelMask(&dwChannelMask);
X3DAudioInitialize(dwChannelMask, X3DAUDIO_SPEED_OF_SOUND, m_X3DInstance);
m_X3DListener.OrientFront.z = 1.0f;
m_X3DListener.OrientTop.y = 1.0f;
m_X3DListener.pCone = nullptr;
// //
registerDefaultProcessors();
m_instance = this;
}
Audio_System::~Audio_System()
{
m_pEngine->StopEngine();
m_pMaster->DestroyVoice();
for (auto [name, voice] : m_subVoices)
{
voice->DestroyVoice();
}
m_activeChannelPtrs.clear();
m_buffers.clear();
m_pEngine->Release();
CoUninitialize();
std::lock_guard<std::mutex> lock(m_ChannelList_mutex);
m_instance = nullptr;
}
bool Audio_System::addSubMixer(const std::string& _subMixerName)
{
if (!m_subVoices.contains(_subMixerName) && _subMixerName != "master")
{
m_pEngine->CreateSubmixVoice(&m_subVoices[_subMixerName], 2, 44100);
return true;
}
return false;
}
bool Audio_System::removeSubMixer(const std::string& _subMixerName)
{
if (m_subVoices.contains(_subMixerName) && _subMixerName != "master")
{
m_subVoices[_subMixerName]->DestroyVoice();
m_subVoices.erase(_subMixerName);
while (!m_activeChannelPtrs[_subMixerName].empty())
{
for (auto& c : m_activeChannelPtrs[_subMixerName])
{
removeChannel(*c);
}
}
m_activeChannelPtrs.erase(_subMixerName);
return true;
}
return false;
}
bool Audio_System::doesSubMixerExist(const std::string& _subMixerName) const
{
if (_subMixerName == "master")
{
return true;
}
return m_subVoices.contains(_subMixerName);
}
void Audio_System::setMasterVolume(float volume)
{
m_pMaster->SetVolume(std::clamp(volume / 100.f, 0.f, 1.f));
}
float Audio_System::getMasterVolume() const
{
float volume = 0.f;
m_pMaster->GetVolume(&volume);
return volume * 100.f;
}
void Audio_System::setSubMixerVolume(const std::string& _subMixerName, float _volume)
{
_volume = std::clamp(_volume, 0.f, 100.f);
if (m_subVoices.contains(_subMixerName))
{
m_subVoices[_subMixerName]->SetVolume(_volume / 100.f);
}
if (_subMixerName == "master")
{
setMasterVolume(_volume);
}
}
float Audio_System::getSubMixerVolume(const std::string& _subMixerName) const
{
float volume = 0.f;
if (m_subVoices.contains(_subMixerName))
{
m_subVoices.at(_subMixerName)->GetVolume(&volume);
}
if (_subMixerName == "master")
{
return getMasterVolume();
}
return volume * 100.f;
}
void Audio_System::setListenerPosition(float _x, float _y, float _z)
{
m_X3DListener.Position = X3DAUDIO_VECTOR{ _x, _y, _z };
recalculate3D();
}
void Audio_System::setListenerPosition(X3DAUDIO_VECTOR _pos)
{
m_X3DListener.Position = _pos;
recalculate3D();
}
void Audio_System::moveListenerPosition(float _x, float _y, float _z)
{
m_X3DListener.Position.x += _x;
m_X3DListener.Position.y += _y;
m_X3DListener.Position.z += _z;
recalculate3D();
}
void Audio_System::moveListenerPosition(X3DAUDIO_VECTOR _pos)
{
m_X3DListener.Position.x += _pos.x;
m_X3DListener.Position.y += _pos.y;
m_X3DListener.Position.z += _pos.z;
recalculate3D();
}
void Audio_System::setListenerOrientation(X3DAUDIO_VECTOR _forward, X3DAUDIO_VECTOR _topDirection)
{
m_X3DListener.OrientFront = _forward;
m_X3DListener.OrientTop = _topDirection;
recalculate3D();
}
void Audio_System::setListenerOrientation(X3DAUDIO_VECTOR _forward, float _topDir_x, float _topDir_y, float _topDir_z)
{
setListenerOrientation(_forward, { _topDir_x, _topDir_y, _topDir_z });
}
void Audio_System::setListenerOrientation(float _fwd_x, float _fwd_y, float _fwd_z, X3DAUDIO_VECTOR _topDirection)
{
setListenerOrientation({ _fwd_x, _fwd_y, _fwd_z }, _topDirection);
}
void Audio_System::setListenerOrientation(float _fwd_x, float _fwd_y, float _fwd_z, float _topDir_x, float _topDir_y, float _topDir_z)
{
setListenerOrientation({ _fwd_x, _fwd_y, _fwd_z }, { _topDir_x, _topDir_y, _topDir_z });
}
void Audio_System::setListenerForward(X3DAUDIO_VECTOR forward)
{
m_X3DListener.OrientFront = forward;
recalculate3D();
}
void Audio_System::setListenerForward(float _x, float _y, float _z)
{
setListenerForward({ _x, _y, _z });
}
void Audio_System::setListenerTop(X3DAUDIO_VECTOR _top)
{
m_X3DListener.OrientTop = _top;
recalculate3D();
}
void Audio_System::setListenerTop(float _x, float _y, float _z)
{
setListenerTop({ _x, _y, _z });
}
void Audio_System::setDefaultMaxDistance(float maxDistance)
{
m_DefaultMaxDistance = maxDistance;
}
void Audio_System::removeChannel(const Channel& channel)
{
std::lock_guard<std::mutex> lock(m_ChannelList_mutex);
const std::string Target_Mixer = channel.getMixerName();
auto it = std::find_if(std::begin(m_activeChannelPtrs[Target_Mixer]), std::end(m_activeChannelPtrs[Target_Mixer]),
[&channel](const std::unique_ptr<Channel>& pChan) {
return &channel == pChan.get();
});
if (it != std::end(m_activeChannelPtrs[Target_Mixer]))
{
m_activeChannelPtrs[Target_Mixer].erase(it);
}
}
bool Audio_System::containFile(const std::filesystem::path& path) const
{
return m_buffers.find(path) != m_buffers.end();
}
void Audio_System::registerDefaultProcessors()
{
m_factory.registerExtension<FileProcessing_WAV>(".wav");
m_factory.registerExtension<FileProcessing_OGG>(".ogg");
m_factory.registerExtension<FileProcessing_MP3>(".mp3");
}
void Audio_System::play(Sound& sound)
{
std::lock_guard<std::mutex> lock(m_ChannelList_mutex);
std::string Target_Mixer(sound.getMixer());
if (Target_Mixer != "master" && !m_subVoices.contains(Target_Mixer))
{
sound.setMixer("master");
Target_Mixer = sound.getMixer();
}
if (Target_Mixer == "master" || m_subVoices.contains(Target_Mixer))
{
if (m_activeChannelPtrs[Target_Mixer].size() < static_cast<size_t>(m_MaxChannelsPerMixer))
{
m_activeChannelPtrs[Target_Mixer].emplace_back(std::make_unique<Channel>(*this, Target_Mixer, sound));
try
{
m_activeChannelPtrs[Target_Mixer].back()->play();
}
catch (...)
{
m_activeChannelPtrs[Target_Mixer].pop_back();
throw;
}
}
}
}
XAUDIO2_VOICE_DETAILS Audio_System::getVoiceDetails()
{
XAUDIO2_VOICE_DETAILS deviceDetails;
m_pMaster->GetVoiceDetails(&deviceDetails);
return deviceDetails;
}
void Audio_System::updateBuffersLife()
{
std::vector<std::filesystem::path> erraseList;
for (const auto& [path, buffer] : m_buffers)
{
if (buffer.use_count() <= 1)
{
erraseList.push_back(path);
}
}
for (const auto& path : erraseList)
{
m_buffers.erase(path);
}
}
void Audio_System::recalculate3D()
{
for (auto& [mixer, Vec_channel] : m_activeChannelPtrs)
{
for (auto& channel : Vec_channel)
{
channel->calculate3D();
}
}
}