annotate OpenALSoundProvider.h @ 2492:85a12b497698

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