annotate Media/Audio/OpenALSoundProvider.h @ 2511:fb1c61a82a55

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