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