2502
|
1 #pragma once
|
|
2 #include "lib/OpenAL/al.h"
|
|
3 #include "lib/OpenAL/alc.h"
|
|
4 #pragma comment(lib, "OpenAL32.lib")
|
|
5
|
2541
|
6 #include "Engine/Engine.h"
|
|
7
|
|
8 void log(char *format, ...)
|
|
9 {
|
|
10 va_list va;
|
|
11 va_start(va, format);
|
|
12 char msg[256];
|
|
13 vsprintf(msg, format, va);
|
|
14 va_end(va);
|
|
15 DWORD w;
|
|
16
|
|
17 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), msg, strlen(msg), &w, 0);
|
|
18 }
|
2502
|
19
|
|
20 class OpenALSoundProvider
|
|
21 {
|
|
22 public:
|
|
23 struct TrackBuffer
|
|
24 {
|
|
25 unsigned int source_id;
|
|
26 unsigned int buffer_id;
|
|
27 };
|
|
28
|
|
29 struct StreamingTrackBuffer
|
|
30 {
|
|
31 unsigned int source_id;
|
|
32 ALenum sample_format;
|
|
33 int sample_rate;
|
|
34 };
|
|
35
|
|
36 inline OpenALSoundProvider()
|
|
37 {
|
|
38 this->device = nullptr;
|
|
39 this->context = nullptr;
|
|
40 }
|
|
41
|
|
42 inline ~OpenALSoundProvider()
|
|
43 {
|
|
44 Release();
|
|
45 }
|
|
46
|
|
47 inline bool Initialize()
|
|
48 {
|
|
49
|
|
50 auto device_names = alcGetString(nullptr, ALC_ALL_DEVICES_SPECIFIER);
|
|
51 if (!device_names)
|
|
52 {
|
|
53 device_names = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
|
|
54 }
|
|
55 if (device_names)
|
|
56 {
|
|
57 for (auto device_name = device_names; device_name[0]; device_name += strlen(device_name))
|
|
58 {
|
|
59 continue;
|
|
60 }
|
|
61 }
|
|
62
|
|
63 device = alcOpenDevice(nullptr);
|
|
64 if (!device || CheckError())
|
|
65 {
|
|
66 Log::Warning(L"Default sound device not present");
|
|
67 return false;
|
|
68 }
|
|
69
|
|
70 context = alcCreateContext(device, nullptr);
|
|
71 if (!context || CheckError())
|
|
72 return Release(), false;
|
|
73
|
|
74 alcMakeContextCurrent(context);
|
|
75
|
|
76 bool eax2 = alIsExtensionPresent("EAX2.0");
|
|
77 bool eax3 = alIsExtensionPresent("EAX3.0");
|
|
78 bool eax4 = alIsExtensionPresent("EAX4.0");
|
|
79 bool eax5 = alIsExtensionPresent("EAX5.0");
|
|
80
|
|
81 auto vendor = alGetString(AL_VENDOR);
|
|
82 auto version = alGetString(AL_VERSION);
|
|
83 auto extensions = alcGetString(device, ALC_EXTENSIONS);
|
|
84
|
|
85 return true;
|
|
86 }
|
|
87
|
|
88 void Release()
|
|
89 {
|
|
90 alcMakeContextCurrent(nullptr);
|
|
91 if (context)
|
|
92 {
|
|
93 alcDestroyContext(context);
|
|
94 }
|
|
95 if (device)
|
|
96 {
|
|
97 alcCloseDevice(device);
|
|
98 }
|
|
99 }
|
|
100
|
|
101 void DeleteStreamingTrack(StreamingTrackBuffer **buffer)
|
|
102 {
|
|
103 if (!buffer && !*buffer)
|
|
104 return;
|
|
105 auto track = *buffer;
|
|
106
|
|
107 int status;
|
|
108 alGetSourcei(track->source_id, AL_SOURCE_STATE, &status);
|
|
109 if (status == AL_PLAYING)
|
|
110 {
|
|
111 alSourceStop(track->source_id);
|
|
112 if (CheckError()) __debugbreak();
|
|
113 }
|
|
114
|
|
115 int num_processed_buffers = 0;
|
|
116 int num_queued_buffers = 0;
|
|
117 alGetSourcei(track->source_id, AL_BUFFERS_PROCESSED, &num_processed_buffers);
|
|
118 alGetSourcei(track->source_id, AL_BUFFERS_QUEUED, &num_queued_buffers);
|
|
119 int num_track_buffers = num_queued_buffers + num_processed_buffers;
|
|
120 for (int i = 0; i < num_processed_buffers; ++i)
|
|
121 {
|
|
122 unsigned int buffer_id;
|
|
123 alSourceUnqueueBuffers(track->source_id, 1, &buffer_id);
|
|
124 if (!CheckError())
|
|
125 alDeleteBuffers(1, &buffer_id);
|
|
126 else __debugbreak();
|
|
127 }
|
|
128
|
|
129 alDeleteSources(1, &track->source_id);
|
|
130 CheckError();
|
|
131
|
|
132 delete *buffer;
|
|
133 *buffer = nullptr;
|
|
134 }
|
|
135
|
|
136 void DeleteBuffer16(TrackBuffer **buffer)
|
|
137 {
|
|
138 alDeleteBuffers(1, &(*buffer)->buffer_id);
|
|
139 CheckError();
|
|
140
|
|
141 delete *buffer;
|
|
142 *buffer = nullptr;
|
|
143 }
|
|
144
|
|
145 float alBufferLength(unsigned int buffer)
|
|
146 {
|
|
147 int size, bits, channels, freq;
|
|
148
|
|
149 alGetBufferi(buffer, AL_SIZE, &size);
|
|
150 alGetBufferi(buffer, AL_BITS, &bits);
|
|
151 alGetBufferi(buffer, AL_CHANNELS, &channels);
|
|
152 alGetBufferi(buffer, AL_FREQUENCY, &freq);
|
|
153 if (CheckError())
|
|
154 return 0.0f;
|
|
155
|
|
156 return (ALfloat)((ALuint)size / channels / (bits / 8)) / (ALfloat)freq;
|
|
157 }
|
|
158
|
|
159 StreamingTrackBuffer *CreateStreamingTrack16(int num_channels, int sample_rate, int bytes_per_sample)
|
|
160 {
|
|
161 Assert(bytes_per_sample == 2, "OpenALSoundProvider: unsupported sample size: %u", bytes_per_sample);
|
|
162
|
|
163 ALenum sound_format;
|
|
164 switch (num_channels)
|
|
165 {
|
|
166 case 1: sound_format = AL_FORMAT_MONO16; break;
|
|
167 case 2: sound_format = AL_FORMAT_STEREO16; break;
|
|
168 default:
|
|
169 if (bool multichannel = alIsExtensionPresent("AL_EXT_MCFORMATS"))
|
|
170 {
|
|
171 switch (num_channels)
|
|
172 {
|
|
173 case 4: sound_format = alGetEnumValue("AL_FORMAT_QUAD16"); break;
|
|
174 case 6: sound_format = alGetEnumValue("AL_FORMAT_51CHN16"); break;
|
|
175 case 7: sound_format = alGetEnumValue("AL_FORMAT_61CHN16"); break;
|
|
176 case 8: sound_format = alGetEnumValue("AL_FORMAT_71CHN16"); break;
|
|
177 }
|
|
178 }
|
|
179 Error("Unsupported number of audio channels: %u", num_channels);
|
|
180 }
|
|
181
|
|
182 unsigned int al_source = -1;
|
|
183 alGetError();
|
|
184 alGenSources(1, &al_source);
|
|
185 if (CheckError())
|
|
186 return nullptr;
|
|
187
|
|
188 float sound_pos[] = {0.0f, 0.0f, 0.0f},
|
|
189 sound_vel[] = {0.0f, 0.0f, 0.0f};
|
|
190
|
|
191 alSourcei(al_source, AL_LOOPING, AL_FALSE);
|
|
192 alSourcef(al_source, AL_PITCH, 1.0f);
|
|
193 alSourcef(al_source, AL_GAIN, 1.0f);
|
|
194 alSourcefv(al_source, AL_POSITION, sound_pos);
|
|
195 alSourcefv(al_source, AL_VELOCITY, sound_vel);
|
|
196
|
|
197 auto ret = new StreamingTrackBuffer;
|
|
198 ret->source_id = al_source;
|
|
199 ret->sample_format = sound_format;
|
|
200 ret->sample_rate = sample_rate;
|
|
201 return ret;
|
|
202 }
|
|
203
|
|
204 void Stream16(StreamingTrackBuffer *buffer, int num_samples, const void *samples, bool wait = false)
|
|
205 {
|
|
206 int bytes_per_sample = 2;
|
|
207
|
|
208 unsigned int al_buffer;
|
|
209 alGenBuffers(1, &al_buffer);
|
|
210 alBufferData(al_buffer, buffer->sample_format, samples, num_samples * bytes_per_sample, buffer->sample_rate);
|
|
211 if (CheckError())
|
|
212 {
|
|
213 alDeleteBuffers(1, &al_buffer);
|
|
214 return;
|
|
215 }
|
|
216
|
|
217 int num_processed_buffers = 0;
|
|
218 alGetSourcei(buffer->source_id, AL_BUFFERS_PROCESSED, &num_processed_buffers);
|
|
219 for (int i = 0; i < num_processed_buffers; ++i)
|
|
220 {
|
|
221 unsigned int processed_buffer_id;
|
|
222 alSourceUnqueueBuffers(buffer->source_id, 1, &processed_buffer_id);
|
|
223 if (!CheckError())
|
|
224 alDeleteBuffers(1, &processed_buffer_id);
|
|
225 }
|
|
226
|
|
227 alSourceQueueBuffers(buffer->source_id, 1, &al_buffer);
|
|
228 if (CheckError())
|
|
229 {
|
|
230 alDeleteBuffers(1, &al_buffer);
|
|
231 return;
|
|
232 }
|
|
233
|
|
234 volatile int status;
|
|
235 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
|
|
236 if (status != AL_PLAYING)
|
|
237 {
|
|
238 float listener_pos[] = {0.0f, 0.0f, 0.0f};
|
|
239 float listener_vel[] = {0.0f, 0.0f, 0.0f};
|
|
240 float listener_orientation[] = {0.0f, 0.0f, -1.0f, // direction
|
|
241 0.0f, 1.0f, 0.0f}; // up vector
|
|
242 alListenerfv(AL_POSITION, listener_pos);
|
|
243 alListenerfv(AL_VELOCITY, listener_vel);
|
|
244 alListenerfv(AL_ORIENTATION, listener_orientation);
|
|
245
|
|
246 alSourcePlay(buffer->source_id);
|
|
247 if (CheckError())
|
|
248 __debugbreak();
|
|
249
|
|
250 if (wait)
|
|
251 {
|
|
252 do
|
|
253 {
|
|
254 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
|
|
255 }
|
|
256 while (status == AL_PLAYING);
|
|
257 }
|
|
258 }
|
|
259 }
|
|
260
|
|
261
|
|
262
|
|
263
|
|
264 TrackBuffer *CreateTrack16(int num_channels, int sample_rate, int bytes_per_sample, int num_samples, const void *samples)
|
|
265 {
|
|
266 Assert(bytes_per_sample == 2, "OpenALSoundProvider: unsupported sample size: %u", bytes_per_sample);
|
|
267
|
|
268 ALenum sound_format;
|
|
269 switch (num_channels)
|
|
270 {
|
|
271 case 1: sound_format = AL_FORMAT_MONO16; break;
|
|
272 case 2: sound_format = AL_FORMAT_STEREO16; break;
|
|
273 default:
|
|
274 if (bool multichannel = alIsExtensionPresent("AL_EXT_MCFORMATS"))
|
|
275 {
|
|
276 switch (num_channels)
|
|
277 {
|
|
278 case 4: sound_format = alGetEnumValue("AL_FORMAT_QUAD16"); break;
|
|
279 case 6: sound_format = alGetEnumValue("AL_FORMAT_51CHN16"); break;
|
|
280 case 7: sound_format = alGetEnumValue("AL_FORMAT_61CHN16"); break;
|
|
281 case 8: sound_format = alGetEnumValue("AL_FORMAT_71CHN16"); break;
|
|
282 }
|
|
283 }
|
|
284 Error("Unsupported number of audio channels: %u", num_channels);
|
|
285 }
|
|
286
|
|
287 unsigned int al_source = -1;
|
|
288 alGenSources(1, &al_source);
|
|
289 if (CheckError())
|
|
290 return nullptr;
|
|
291
|
|
292 float sound_pos[] = {0.0f, 0.0f, 0.0f},
|
|
293 sound_vel[] = {0.0f, 0.0f, 0.0f};
|
|
294
|
|
295 alSourcei(al_source, AL_LOOPING, AL_FALSE);
|
|
296 alSourcef(al_source, AL_PITCH, 1.0f);
|
|
297 alSourcef(al_source, AL_GAIN, 1.0f);
|
|
298 alSourcefv(al_source, AL_POSITION, sound_pos);
|
|
299 alSourcefv(al_source, AL_VELOCITY, sound_vel);
|
|
300
|
|
301 unsigned int al_buffer = -1;
|
|
302 alGenBuffers(1, &al_buffer);
|
|
303 if (CheckError())
|
|
304 {
|
|
305 alDeleteSources(1, &al_source);
|
|
306 return nullptr;
|
|
307 }
|
|
308
|
|
309 alBufferData(al_buffer, sound_format, samples, num_samples * bytes_per_sample, sample_rate);
|
|
310 if (CheckError())
|
|
311 {
|
|
312 alDeleteSources(1, &al_source);
|
|
313 alDeleteBuffers(1, &al_buffer);
|
|
314 return nullptr;
|
|
315 }
|
|
316
|
|
317 alSourcei(al_source, AL_BUFFER, al_buffer);
|
|
318 if (CheckError())
|
|
319 {
|
|
320 alDeleteSources(1, &al_source);
|
|
321 alDeleteBuffers(1, &al_buffer);
|
|
322 return nullptr;
|
|
323 }
|
|
324
|
|
325 auto ret = new TrackBuffer;
|
|
326 ret->source_id = al_source;
|
|
327 ret->buffer_id = al_buffer;
|
|
328 return ret;
|
|
329 }
|
|
330
|
|
331
|
|
332 void PlayTrack16(TrackBuffer *buffer, bool loop = false, bool wait = false)
|
|
333 {
|
|
334 volatile int status;
|
|
335 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
|
|
336 if (status == AL_PLAYING)
|
|
337 Error("Already playing");
|
|
338 else
|
|
339 {
|
|
340 float listener_pos[] = {0.0f, 0.0f, 0.0f};
|
|
341 float listener_vel[] = {0.0f, 0.0f, 0.0f};
|
|
342 float listener_orientation[] = {0.0f, 0.0f, -1.0f, // direction
|
|
343 0.0f, 1.0f, 0.0f}; // up vector
|
|
344 alListenerfv(AL_POSITION, listener_pos);
|
|
345 alListenerfv(AL_VELOCITY, listener_vel);
|
|
346 alListenerfv(AL_ORIENTATION, listener_orientation);
|
|
347
|
|
348 alSourcei(buffer->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
|
|
349 alSourcePlay(buffer->source_id);
|
|
350 if (CheckError())
|
|
351 __debugbreak();
|
|
352
|
|
353 if (wait && !loop)
|
|
354 {
|
|
355 float track_length = alBufferLength(buffer->buffer_id);
|
|
356 do
|
|
357 {
|
|
358 float track_offset = 0;
|
|
359 alGetSourcef(buffer->source_id, AL_SEC_OFFSET, &track_offset);
|
|
360 log("playing: %.4f/%.4f\n", track_offset, track_length);
|
|
361
|
|
362 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
|
|
363 }
|
|
364 while (status == AL_PLAYING);
|
|
365 }
|
|
366 }
|
|
367 }
|
|
368
|
|
369
|
|
370
|
|
371 protected:
|
|
372 ALCdevice *device;
|
|
373 ALCcontext *context;
|
|
374
|
|
375
|
|
376 bool CheckError()
|
|
377 {
|
|
378 ALenum code1 = alGetError();
|
|
379 if (code1 != AL_NO_ERROR)
|
|
380 {
|
|
381 DWORD w;
|
|
382 const char *message = alGetString(code1);
|
|
383 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlenA(message), &w, nullptr);
|
|
384 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "\n", 1, &w, nullptr);
|
|
385 return true;
|
|
386 }
|
|
387
|
|
388 ALenum code2 = alcGetError(device);
|
|
389 if (code2 != ALC_NO_ERROR)
|
|
390 {
|
|
391 DWORD w;
|
|
392 const char *message = alcGetString(device, code2);
|
|
393 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlenA(message), &w, nullptr);
|
|
394 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "\n", 1, &w, nullptr);
|
|
395 return true;
|
|
396 }
|
|
397 return false;
|
|
398 }
|
|
399 }; |