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