annotate OpenALSoundProvider.h @ 2421:fc7456da5a93

Слияние
author Ritor1
date Wed, 23 Jul 2014 01:14:21 +0600
parents 9987f93d7e1f
children a2d95b16e28b
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
58be29479e75 add files
Ritor1
parents:
diff changeset
80
58be29479e75 add files
Ritor1
parents:
diff changeset
81 void DeleteBuffer16(TrackBuffer **buffer)
58be29479e75 add files
Ritor1
parents:
diff changeset
82 {
58be29479e75 add files
Ritor1
parents:
diff changeset
83 alDeleteBuffers(1, &(*buffer)->buffer_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
84 CheckError();
58be29479e75 add files
Ritor1
parents:
diff changeset
85
58be29479e75 add files
Ritor1
parents:
diff changeset
86 delete *buffer;
58be29479e75 add files
Ritor1
parents:
diff changeset
87 *buffer = nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
88 }
58be29479e75 add files
Ritor1
parents:
diff changeset
89
58be29479e75 add files
Ritor1
parents:
diff changeset
90 float alBufferLength(unsigned int buffer)
58be29479e75 add files
Ritor1
parents:
diff changeset
91 {
58be29479e75 add files
Ritor1
parents:
diff changeset
92 int size, bits, channels, freq;
58be29479e75 add files
Ritor1
parents:
diff changeset
93
58be29479e75 add files
Ritor1
parents:
diff changeset
94 alGetBufferi(buffer, AL_SIZE, &size);
58be29479e75 add files
Ritor1
parents:
diff changeset
95 alGetBufferi(buffer, AL_BITS, &bits);
58be29479e75 add files
Ritor1
parents:
diff changeset
96 alGetBufferi(buffer, AL_CHANNELS, &channels);
58be29479e75 add files
Ritor1
parents:
diff changeset
97 alGetBufferi(buffer, AL_FREQUENCY, &freq);
58be29479e75 add files
Ritor1
parents:
diff changeset
98 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
99 return 0.0f;
58be29479e75 add files
Ritor1
parents:
diff changeset
100
58be29479e75 add files
Ritor1
parents:
diff changeset
101 return (ALfloat)((ALuint)size / channels / (bits / 8)) / (ALfloat)freq;
58be29479e75 add files
Ritor1
parents:
diff changeset
102 }
58be29479e75 add files
Ritor1
parents:
diff changeset
103
58be29479e75 add files
Ritor1
parents:
diff changeset
104 StreamingTrackBuffer *CreateStreamingTrack16(int num_channels, int sample_rate, int bytes_per_sample)
58be29479e75 add files
Ritor1
parents:
diff changeset
105 {
58be29479e75 add files
Ritor1
parents:
diff changeset
106 Assert(bytes_per_sample == 2, "OpenALSoundProvider: unsupported sample size: %u", bytes_per_sample);
58be29479e75 add files
Ritor1
parents:
diff changeset
107
58be29479e75 add files
Ritor1
parents:
diff changeset
108 ALenum sound_format;
58be29479e75 add files
Ritor1
parents:
diff changeset
109 switch (num_channels)
58be29479e75 add files
Ritor1
parents:
diff changeset
110 {
58be29479e75 add files
Ritor1
parents:
diff changeset
111 case 1: sound_format = AL_FORMAT_MONO16; break;
58be29479e75 add files
Ritor1
parents:
diff changeset
112 case 2: sound_format = AL_FORMAT_STEREO16; break;
58be29479e75 add files
Ritor1
parents:
diff changeset
113 default:
58be29479e75 add files
Ritor1
parents:
diff changeset
114 if (bool multichannel = alIsExtensionPresent("AL_EXT_MCFORMATS"))
58be29479e75 add files
Ritor1
parents:
diff changeset
115 {
58be29479e75 add files
Ritor1
parents:
diff changeset
116 switch (num_channels)
58be29479e75 add files
Ritor1
parents:
diff changeset
117 {
58be29479e75 add files
Ritor1
parents:
diff changeset
118 case 4: sound_format = alGetEnumValue("AL_FORMAT_QUAD16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
119 case 6: sound_format = alGetEnumValue("AL_FORMAT_51CHN16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
120 case 7: sound_format = alGetEnumValue("AL_FORMAT_61CHN16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
121 case 8: sound_format = alGetEnumValue("AL_FORMAT_71CHN16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
122 }
58be29479e75 add files
Ritor1
parents:
diff changeset
123 }
58be29479e75 add files
Ritor1
parents:
diff changeset
124 Error("Unsupported number of audio channels: %u", num_channels);
58be29479e75 add files
Ritor1
parents:
diff changeset
125 }
58be29479e75 add files
Ritor1
parents:
diff changeset
126
58be29479e75 add files
Ritor1
parents:
diff changeset
127 unsigned int al_source = -1;
58be29479e75 add files
Ritor1
parents:
diff changeset
128 alGenSources(1, &al_source);
58be29479e75 add files
Ritor1
parents:
diff changeset
129 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
130 return nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
131
58be29479e75 add files
Ritor1
parents:
diff changeset
132 float sound_pos[] = {0.0f, 0.0f, 0.0f},
58be29479e75 add files
Ritor1
parents:
diff changeset
133 sound_vel[] = {0.0f, 0.0f, 0.0f};
58be29479e75 add files
Ritor1
parents:
diff changeset
134
58be29479e75 add files
Ritor1
parents:
diff changeset
135 alSourcei(al_source, AL_LOOPING, AL_FALSE);
58be29479e75 add files
Ritor1
parents:
diff changeset
136 alSourcef(al_source, AL_PITCH, 1.0f);
58be29479e75 add files
Ritor1
parents:
diff changeset
137 alSourcef(al_source, AL_GAIN, 1.0f);
58be29479e75 add files
Ritor1
parents:
diff changeset
138 alSourcefv(al_source, AL_POSITION, sound_pos);
58be29479e75 add files
Ritor1
parents:
diff changeset
139 alSourcefv(al_source, AL_VELOCITY, sound_vel);
58be29479e75 add files
Ritor1
parents:
diff changeset
140
58be29479e75 add files
Ritor1
parents:
diff changeset
141 auto ret = new StreamingTrackBuffer;
58be29479e75 add files
Ritor1
parents:
diff changeset
142 ret->source_id = al_source;
58be29479e75 add files
Ritor1
parents:
diff changeset
143 ret->sample_format = sound_format;
58be29479e75 add files
Ritor1
parents:
diff changeset
144 ret->sample_rate = sample_rate;
58be29479e75 add files
Ritor1
parents:
diff changeset
145 return ret;
58be29479e75 add files
Ritor1
parents:
diff changeset
146 }
58be29479e75 add files
Ritor1
parents:
diff changeset
147
58be29479e75 add files
Ritor1
parents:
diff changeset
148 void Stream16(StreamingTrackBuffer *buffer, int num_samples, const void *samples, bool wait = false)
58be29479e75 add files
Ritor1
parents:
diff changeset
149 {
58be29479e75 add files
Ritor1
parents:
diff changeset
150 int bytes_per_sample = 2;
58be29479e75 add files
Ritor1
parents:
diff changeset
151
58be29479e75 add files
Ritor1
parents:
diff changeset
152 unsigned int al_buffer;
58be29479e75 add files
Ritor1
parents:
diff changeset
153 alGenBuffers(1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
154 alBufferData(al_buffer, buffer->sample_format, samples, num_samples * bytes_per_sample, buffer->sample_rate);
58be29479e75 add files
Ritor1
parents:
diff changeset
155 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
156 {
58be29479e75 add files
Ritor1
parents:
diff changeset
157 alDeleteBuffers(1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
158 return;
58be29479e75 add files
Ritor1
parents:
diff changeset
159 }
58be29479e75 add files
Ritor1
parents:
diff changeset
160
58be29479e75 add files
Ritor1
parents:
diff changeset
161 int num_processed_buffers = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
162 alGetSourcei(buffer->source_id, AL_BUFFERS_PROCESSED, &num_processed_buffers);
58be29479e75 add files
Ritor1
parents:
diff changeset
163 for (int i = 0; i < num_processed_buffers; ++i)
58be29479e75 add files
Ritor1
parents:
diff changeset
164 {
58be29479e75 add files
Ritor1
parents:
diff changeset
165 unsigned int processed_buffer_id;
58be29479e75 add files
Ritor1
parents:
diff changeset
166 alSourceUnqueueBuffers(buffer->source_id, 1, &processed_buffer_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
167 if (!CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
168 alDeleteBuffers(1, &processed_buffer_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
169 }
58be29479e75 add files
Ritor1
parents:
diff changeset
170
58be29479e75 add files
Ritor1
parents:
diff changeset
171 alSourceQueueBuffers(buffer->source_id, 1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
172 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
173 {
58be29479e75 add files
Ritor1
parents:
diff changeset
174 alDeleteBuffers(1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
175 return;
58be29479e75 add files
Ritor1
parents:
diff changeset
176 }
58be29479e75 add files
Ritor1
parents:
diff changeset
177
58be29479e75 add files
Ritor1
parents:
diff changeset
178 volatile int status;
58be29479e75 add files
Ritor1
parents:
diff changeset
179 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
58be29479e75 add files
Ritor1
parents:
diff changeset
180 if (status != AL_PLAYING)
58be29479e75 add files
Ritor1
parents:
diff changeset
181 {
58be29479e75 add files
Ritor1
parents:
diff changeset
182 float listener_pos[] = {0.0f, 0.0f, 0.0f};
58be29479e75 add files
Ritor1
parents:
diff changeset
183 float listener_vel[] = {0.0f, 0.0f, 0.0f};
58be29479e75 add files
Ritor1
parents:
diff changeset
184 float listener_orientation[] = {0.0f, 0.0f, -1.0f, // direction
58be29479e75 add files
Ritor1
parents:
diff changeset
185 0.0f, 1.0f, 0.0f}; // up vector
58be29479e75 add files
Ritor1
parents:
diff changeset
186 alListenerfv(AL_POSITION, listener_pos);
58be29479e75 add files
Ritor1
parents:
diff changeset
187 alListenerfv(AL_VELOCITY, listener_vel);
58be29479e75 add files
Ritor1
parents:
diff changeset
188 alListenerfv(AL_ORIENTATION, listener_orientation);
58be29479e75 add files
Ritor1
parents:
diff changeset
189
58be29479e75 add files
Ritor1
parents:
diff changeset
190 alSourcePlay(buffer->source_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
191 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
192 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
193
58be29479e75 add files
Ritor1
parents:
diff changeset
194 if (wait)
58be29479e75 add files
Ritor1
parents:
diff changeset
195 {
58be29479e75 add files
Ritor1
parents:
diff changeset
196 do
58be29479e75 add files
Ritor1
parents:
diff changeset
197 {
58be29479e75 add files
Ritor1
parents:
diff changeset
198 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
58be29479e75 add files
Ritor1
parents:
diff changeset
199 }
58be29479e75 add files
Ritor1
parents:
diff changeset
200 while (status == AL_PLAYING);
58be29479e75 add files
Ritor1
parents:
diff changeset
201 }
58be29479e75 add files
Ritor1
parents:
diff changeset
202 }
58be29479e75 add files
Ritor1
parents:
diff changeset
203 }
58be29479e75 add files
Ritor1
parents:
diff changeset
204
58be29479e75 add files
Ritor1
parents:
diff changeset
205
58be29479e75 add files
Ritor1
parents:
diff changeset
206
58be29479e75 add files
Ritor1
parents:
diff changeset
207
58be29479e75 add files
Ritor1
parents:
diff changeset
208 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
209 {
58be29479e75 add files
Ritor1
parents:
diff changeset
210 Assert(bytes_per_sample == 2, "OpenALSoundProvider: unsupported sample size: %u", bytes_per_sample);
58be29479e75 add files
Ritor1
parents:
diff changeset
211
58be29479e75 add files
Ritor1
parents:
diff changeset
212 ALenum sound_format;
58be29479e75 add files
Ritor1
parents:
diff changeset
213 switch (num_channels)
58be29479e75 add files
Ritor1
parents:
diff changeset
214 {
58be29479e75 add files
Ritor1
parents:
diff changeset
215 case 1: sound_format = AL_FORMAT_MONO16; break;
58be29479e75 add files
Ritor1
parents:
diff changeset
216 case 2: sound_format = AL_FORMAT_STEREO16; break;
58be29479e75 add files
Ritor1
parents:
diff changeset
217 default:
58be29479e75 add files
Ritor1
parents:
diff changeset
218 if (bool multichannel = alIsExtensionPresent("AL_EXT_MCFORMATS"))
58be29479e75 add files
Ritor1
parents:
diff changeset
219 {
58be29479e75 add files
Ritor1
parents:
diff changeset
220 switch (num_channels)
58be29479e75 add files
Ritor1
parents:
diff changeset
221 {
58be29479e75 add files
Ritor1
parents:
diff changeset
222 case 4: sound_format = alGetEnumValue("AL_FORMAT_QUAD16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
223 case 6: sound_format = alGetEnumValue("AL_FORMAT_51CHN16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
224 case 7: sound_format = alGetEnumValue("AL_FORMAT_61CHN16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
225 case 8: sound_format = alGetEnumValue("AL_FORMAT_71CHN16"); break;
58be29479e75 add files
Ritor1
parents:
diff changeset
226 }
58be29479e75 add files
Ritor1
parents:
diff changeset
227 }
58be29479e75 add files
Ritor1
parents:
diff changeset
228 Error("Unsupported number of audio channels: %u", num_channels);
58be29479e75 add files
Ritor1
parents:
diff changeset
229 }
58be29479e75 add files
Ritor1
parents:
diff changeset
230
58be29479e75 add files
Ritor1
parents:
diff changeset
231 unsigned int al_source = -1;
58be29479e75 add files
Ritor1
parents:
diff changeset
232 alGenSources(1, &al_source);
58be29479e75 add files
Ritor1
parents:
diff changeset
233 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
234 return nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
235
58be29479e75 add files
Ritor1
parents:
diff changeset
236 float sound_pos[] = {0.0f, 0.0f, 0.0f},
58be29479e75 add files
Ritor1
parents:
diff changeset
237 sound_vel[] = {0.0f, 0.0f, 0.0f};
58be29479e75 add files
Ritor1
parents:
diff changeset
238
58be29479e75 add files
Ritor1
parents:
diff changeset
239 alSourcei(al_source, AL_LOOPING, AL_FALSE);
58be29479e75 add files
Ritor1
parents:
diff changeset
240 alSourcef(al_source, AL_PITCH, 1.0f);
58be29479e75 add files
Ritor1
parents:
diff changeset
241 alSourcef(al_source, AL_GAIN, 1.0f);
58be29479e75 add files
Ritor1
parents:
diff changeset
242 alSourcefv(al_source, AL_POSITION, sound_pos);
58be29479e75 add files
Ritor1
parents:
diff changeset
243 alSourcefv(al_source, AL_VELOCITY, sound_vel);
58be29479e75 add files
Ritor1
parents:
diff changeset
244
58be29479e75 add files
Ritor1
parents:
diff changeset
245 unsigned int al_buffer = -1;
58be29479e75 add files
Ritor1
parents:
diff changeset
246 alGenBuffers(1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
247 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
248 {
58be29479e75 add files
Ritor1
parents:
diff changeset
249 alDeleteSources(1, &al_source);
58be29479e75 add files
Ritor1
parents:
diff changeset
250 return nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
251 }
58be29479e75 add files
Ritor1
parents:
diff changeset
252
58be29479e75 add files
Ritor1
parents:
diff changeset
253 alBufferData(al_buffer, sound_format, samples, num_samples * bytes_per_sample, sample_rate);
58be29479e75 add files
Ritor1
parents:
diff changeset
254 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
255 {
58be29479e75 add files
Ritor1
parents:
diff changeset
256 alDeleteSources(1, &al_source);
58be29479e75 add files
Ritor1
parents:
diff changeset
257 alDeleteBuffers(1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
258 return nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
259 }
58be29479e75 add files
Ritor1
parents:
diff changeset
260
58be29479e75 add files
Ritor1
parents:
diff changeset
261 alSourcei(al_source, AL_BUFFER, al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
262 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
263 {
58be29479e75 add files
Ritor1
parents:
diff changeset
264 alDeleteSources(1, &al_source);
58be29479e75 add files
Ritor1
parents:
diff changeset
265 alDeleteBuffers(1, &al_buffer);
58be29479e75 add files
Ritor1
parents:
diff changeset
266 return nullptr;
58be29479e75 add files
Ritor1
parents:
diff changeset
267 }
58be29479e75 add files
Ritor1
parents:
diff changeset
268
58be29479e75 add files
Ritor1
parents:
diff changeset
269 auto ret = new TrackBuffer;
58be29479e75 add files
Ritor1
parents:
diff changeset
270 ret->source_id = al_source;
58be29479e75 add files
Ritor1
parents:
diff changeset
271 ret->buffer_id = al_buffer;
58be29479e75 add files
Ritor1
parents:
diff changeset
272 return ret;
58be29479e75 add files
Ritor1
parents:
diff changeset
273 }
58be29479e75 add files
Ritor1
parents:
diff changeset
274
58be29479e75 add files
Ritor1
parents:
diff changeset
275
58be29479e75 add files
Ritor1
parents:
diff changeset
276 void PlayTrack16(TrackBuffer *buffer, bool loop = false, bool wait = false)
58be29479e75 add files
Ritor1
parents:
diff changeset
277 {
58be29479e75 add files
Ritor1
parents:
diff changeset
278 volatile int status;
58be29479e75 add files
Ritor1
parents:
diff changeset
279 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
58be29479e75 add files
Ritor1
parents:
diff changeset
280 if (status == AL_PLAYING)
58be29479e75 add files
Ritor1
parents:
diff changeset
281 Error("Already playing");
58be29479e75 add files
Ritor1
parents:
diff changeset
282 else
58be29479e75 add files
Ritor1
parents:
diff changeset
283 {
58be29479e75 add files
Ritor1
parents:
diff changeset
284 float listener_pos[] = {0.0f, 0.0f, 0.0f};
58be29479e75 add files
Ritor1
parents:
diff changeset
285 float listener_vel[] = {0.0f, 0.0f, 0.0f};
58be29479e75 add files
Ritor1
parents:
diff changeset
286 float listener_orientation[] = {0.0f, 0.0f, -1.0f, // direction
58be29479e75 add files
Ritor1
parents:
diff changeset
287 0.0f, 1.0f, 0.0f}; // up vector
58be29479e75 add files
Ritor1
parents:
diff changeset
288 alListenerfv(AL_POSITION, listener_pos);
58be29479e75 add files
Ritor1
parents:
diff changeset
289 alListenerfv(AL_VELOCITY, listener_vel);
58be29479e75 add files
Ritor1
parents:
diff changeset
290 alListenerfv(AL_ORIENTATION, listener_orientation);
58be29479e75 add files
Ritor1
parents:
diff changeset
291
58be29479e75 add files
Ritor1
parents:
diff changeset
292 alSourcei(buffer->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
58be29479e75 add files
Ritor1
parents:
diff changeset
293 alSourcePlay(buffer->source_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
294 if (CheckError())
58be29479e75 add files
Ritor1
parents:
diff changeset
295 __debugbreak();
58be29479e75 add files
Ritor1
parents:
diff changeset
296
58be29479e75 add files
Ritor1
parents:
diff changeset
297 if (wait && !loop)
58be29479e75 add files
Ritor1
parents:
diff changeset
298 {
58be29479e75 add files
Ritor1
parents:
diff changeset
299 float track_length = alBufferLength(buffer->buffer_id);
58be29479e75 add files
Ritor1
parents:
diff changeset
300 do
58be29479e75 add files
Ritor1
parents:
diff changeset
301 {
58be29479e75 add files
Ritor1
parents:
diff changeset
302 float track_offset = 0;
58be29479e75 add files
Ritor1
parents:
diff changeset
303 alGetSourcef(buffer->source_id, AL_SEC_OFFSET, &track_offset);
58be29479e75 add files
Ritor1
parents:
diff changeset
304 log("playing: %.4f/%.4f\n", track_offset, track_length);
58be29479e75 add files
Ritor1
parents:
diff changeset
305
58be29479e75 add files
Ritor1
parents:
diff changeset
306 alGetSourcei(buffer->source_id, AL_SOURCE_STATE, (int *)&status);
58be29479e75 add files
Ritor1
parents:
diff changeset
307 }
58be29479e75 add files
Ritor1
parents:
diff changeset
308 while (status == AL_PLAYING);
58be29479e75 add files
Ritor1
parents:
diff changeset
309 }
58be29479e75 add files
Ritor1
parents:
diff changeset
310 }
58be29479e75 add files
Ritor1
parents:
diff changeset
311 }
58be29479e75 add files
Ritor1
parents:
diff changeset
312
58be29479e75 add files
Ritor1
parents:
diff changeset
313
58be29479e75 add files
Ritor1
parents:
diff changeset
314
58be29479e75 add files
Ritor1
parents:
diff changeset
315 protected:
58be29479e75 add files
Ritor1
parents:
diff changeset
316 ALCdevice *device;
58be29479e75 add files
Ritor1
parents:
diff changeset
317 ALCcontext *context;
58be29479e75 add files
Ritor1
parents:
diff changeset
318
58be29479e75 add files
Ritor1
parents:
diff changeset
319
58be29479e75 add files
Ritor1
parents:
diff changeset
320 bool CheckError()
58be29479e75 add files
Ritor1
parents:
diff changeset
321 {
58be29479e75 add files
Ritor1
parents:
diff changeset
322 ALenum code1 = alGetError();
58be29479e75 add files
Ritor1
parents:
diff changeset
323 if (code1 != AL_NO_ERROR)
58be29479e75 add files
Ritor1
parents:
diff changeset
324 {
58be29479e75 add files
Ritor1
parents:
diff changeset
325 DWORD w;
58be29479e75 add files
Ritor1
parents:
diff changeset
326 const char *message = alGetString(code1);
58be29479e75 add files
Ritor1
parents:
diff changeset
327 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlenA(message), &w, nullptr);
58be29479e75 add files
Ritor1
parents:
diff changeset
328 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "\n", 1, &w, nullptr);
58be29479e75 add files
Ritor1
parents:
diff changeset
329 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
330 }
58be29479e75 add files
Ritor1
parents:
diff changeset
331
58be29479e75 add files
Ritor1
parents:
diff changeset
332 ALenum code2 = alcGetError(device);
58be29479e75 add files
Ritor1
parents:
diff changeset
333 if (code2 != ALC_NO_ERROR)
58be29479e75 add files
Ritor1
parents:
diff changeset
334 {
58be29479e75 add files
Ritor1
parents:
diff changeset
335 DWORD w;
58be29479e75 add files
Ritor1
parents:
diff changeset
336 const char *message = alcGetString(device, code2);
58be29479e75 add files
Ritor1
parents:
diff changeset
337 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlenA(message), &w, nullptr);
58be29479e75 add files
Ritor1
parents:
diff changeset
338 WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "\n", 1, &w, nullptr);
58be29479e75 add files
Ritor1
parents:
diff changeset
339 return true;
58be29479e75 add files
Ritor1
parents:
diff changeset
340 }
58be29479e75 add files
Ritor1
parents:
diff changeset
341 return false;
58be29479e75 add files
Ritor1
parents:
diff changeset
342 }
58be29479e75 add files
Ritor1
parents:
diff changeset
343 };