annotate Media/Audio/OpenALSoundProvider.h @ 2551:4008b2f34be0

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