annotate OpenALSoundProvider.h @ 2468:48cc5e0d8fc9

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