annotate OpenALSoundProvider.h @ 2432:99e7bd7ec0a2

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