-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
OpenALStream.cpp
350 lines (289 loc) · 9.26 KB
/
OpenALStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <climits>
#include <cstring>
#include <thread>
#include "AudioCommon/OpenALStream.h"
#include "AudioCommon/aldlist.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/Thread.h"
#include "Core/ConfigManager.h"
#if defined HAVE_OPENAL && HAVE_OPENAL
#ifdef _WIN32
#pragma comment(lib, "openal32.lib")
#endif
//
// AyuanX: Spec says OpenAL1.1 is thread safe already
//
bool OpenALStream::Start()
{
m_run_thread.Set();
bool b_return = false;
ALDeviceList device_list;
if (device_list.GetNumDevices())
{
char* def_dev_name = device_list.GetDeviceName(device_list.GetDefaultDevice());
INFO_LOG(AUDIO, "Found OpenAL device %s", def_dev_name);
ALCdevice* device = alcOpenDevice(def_dev_name);
if (device)
{
ALCcontext* context = alcCreateContext(device, nullptr);
if (context)
{
// Used to determine an appropriate period size (2x period = total buffer size)
// ALCint refresh;
// alcGetIntegerv(device, ALC_REFRESH, 1, &refresh);
// period_size_in_millisec = 1000 / refresh;
alcMakeContextCurrent(context);
thread = std::thread(&OpenALStream::SoundLoop, this);
b_return = true;
}
else
{
alcCloseDevice(device);
PanicAlertT("OpenAL: can't create context for device %s", def_dev_name);
}
}
else
{
PanicAlertT("OpenAL: can't open device %s", def_dev_name);
}
}
else
{
PanicAlertT("OpenAL: can't find sound devices");
}
return b_return;
}
void OpenALStream::Stop()
{
m_run_thread.Clear();
// kick the thread if it's waiting
sound_sync_event.Set();
thread.join();
alSourceStop(source);
alSourcei(source, AL_BUFFER, 0);
// Clean up buffers and sources
alDeleteSources(1, &source);
source = 0;
alDeleteBuffers(OAL_BUFFERS, buffers.data());
ALCcontext* context = alcGetCurrentContext();
ALCdevice* device = alcGetContextsDevice(context);
alcMakeContextCurrent(nullptr);
alcDestroyContext(context);
alcCloseDevice(device);
}
void OpenALStream::SetVolume(int volume)
{
m_volume = (float)volume / 100.0f;
if (source)
alSourcef(source, AL_GAIN, m_volume);
}
void OpenALStream::Update()
{
sound_sync_event.Set();
}
void OpenALStream::Clear(bool mute)
{
m_muted = mute;
if (m_muted)
{
alSourceStop(source);
}
else
{
alSourcePlay(source);
}
}
static ALenum CheckALError(const char* desc)
{
ALenum err = alGetError();
if (err != AL_NO_ERROR)
{
std::string type;
switch (err)
{
case AL_INVALID_NAME:
type = "AL_INVALID_NAME";
break;
case AL_INVALID_ENUM:
type = "AL_INVALID_ENUM";
break;
case AL_INVALID_VALUE:
type = "AL_INVALID_VALUE";
break;
case AL_INVALID_OPERATION:
type = "AL_INVALID_OPERATION";
break;
case AL_OUT_OF_MEMORY:
type = "AL_OUT_OF_MEMORY";
break;
default:
type = "UNKNOWN_ERROR";
break;
}
ERROR_LOG(AUDIO, "Error %s: %08x %s", desc, err, type.c_str());
}
return err;
}
static bool IsCreativeXFi()
{
return strstr(alGetString(AL_RENDERER), "X-Fi") != nullptr;
}
void OpenALStream::SoundLoop()
{
Common::SetCurrentThreadName("Audio thread - openal");
bool float32_capable = alIsExtensionPresent("AL_EXT_float32") != 0;
bool surround_capable = alIsExtensionPresent("AL_EXT_MCFORMATS") || IsCreativeXFi();
bool use_surround = SConfig::GetInstance().bDPL2Decoder && surround_capable;
// As there is no extension to check for 32-bit fixed point support
// and we know that only a X-Fi with hardware OpenAL supports it,
// we just check if one is being used.
bool fixed32_capable = IsCreativeXFi();
u32 frequency = m_mixer->GetSampleRate();
u32 frames_per_buffer;
// Can't have zero samples per buffer
if (SConfig::GetInstance().iLatency > 0)
{
frames_per_buffer = frequency / 1000 * SConfig::GetInstance().iLatency / OAL_BUFFERS;
}
else
{
frames_per_buffer = frequency / 1000 * 1 / OAL_BUFFERS;
}
if (frames_per_buffer > OAL_MAX_FRAMES)
{
frames_per_buffer = OAL_MAX_FRAMES;
}
// DPL2 needs a minimum number of samples to work (FWRDURATION)
if (use_surround && frames_per_buffer < 240)
{
frames_per_buffer = 240;
}
INFO_LOG(AUDIO, "Using %d buffers, each with %d audio frames for a total of %d.", OAL_BUFFERS,
frames_per_buffer, frames_per_buffer * OAL_BUFFERS);
// Should we make these larger just in case the mixer ever sends more samples
// than what we request?
realtime_buffer.resize(frames_per_buffer * STEREO_CHANNELS);
source = 0;
// Clear error state before querying or else we get false positives.
ALenum err = alGetError();
// Generate some AL Buffers for streaming
alGenBuffers(OAL_BUFFERS, (ALuint*)buffers.data());
err = CheckALError("generating buffers");
// Generate a Source to playback the Buffers
alGenSources(1, &source);
err = CheckALError("generating sources");
// Set the default sound volume as saved in the config file.
alSourcef(source, AL_GAIN, m_volume);
// TODO: Error handling
// ALenum err = alGetError();
unsigned int next_buffer = 0;
unsigned int num_buffers_queued = 0;
ALint state = 0;
while (m_run_thread.IsSet())
{
// Block until we have a free buffer
int num_buffers_processed;
alGetSourcei(source, AL_BUFFERS_PROCESSED, &num_buffers_processed);
if (num_buffers_queued == OAL_BUFFERS && !num_buffers_processed)
{
// sound_sync_event.Wait();
continue;
}
// Remove the Buffer from the Queue.
if (num_buffers_processed)
{
std::array<ALuint, OAL_BUFFERS> unqueued_buffer_ids;
alSourceUnqueueBuffers(source, num_buffers_processed, unqueued_buffer_ids.data());
err = CheckALError("unqueuing buffers");
num_buffers_queued -= num_buffers_processed;
}
unsigned int min_frames = frames_per_buffer;
if (use_surround)
{
std::array<float, OAL_MAX_FRAMES * SURROUND_CHANNELS> dpl2;
u32 rendered_frames = m_mixer->MixSurround(dpl2.data(), min_frames);
if (rendered_frames < min_frames)
continue;
// zero-out the subwoofer channel - DPL2Decode generates a pretty
// good 5.0 but not a good 5.1 output. Sadly there is not a 5.0
// AL_FORMAT_50CHN32 to make this super-explicit.
// DPL2Decode output: LEFTFRONT, RIGHTFRONT, CENTREFRONT, (sub), LEFTREAR, RIGHTREAR
for (u32 i = 0; i < rendered_frames; ++i)
{
dpl2[i * SURROUND_CHANNELS + 3 /*sub/lfe*/] = 0.0f;
}
if (float32_capable)
{
alBufferData(buffers[next_buffer], AL_FORMAT_51CHN32, dpl2.data(),
rendered_frames * FRAME_SURROUND_FLOAT, frequency);
}
else if (fixed32_capable)
{
std::array<int, OAL_MAX_FRAMES * SURROUND_CHANNELS> surround_int32;
for (u32 i = 0; i < rendered_frames * SURROUND_CHANNELS; ++i)
{
// For some reason the ffdshow's DPL2 decoder outputs samples bigger than 1.
// Most are close to 2.5 and some go up to 8. Hard clamping here, we need to
// fix the decoder or implement a limiter.
dpl2[i] = dpl2[i] * (INT64_C(1) << 31);
if (dpl2[i] > INT_MAX)
surround_int32[i] = INT_MAX;
else if (dpl2[i] < INT_MIN)
surround_int32[i] = INT_MIN;
else
surround_int32[i] = static_cast<int>(dpl2[i]);
}
alBufferData(buffers[next_buffer], AL_FORMAT_51CHN32, surround_int32.data(),
rendered_frames * FRAME_SURROUND_INT32, frequency);
}
else
{
std::array<short, OAL_MAX_FRAMES * SURROUND_CHANNELS> surround_short;
for (u32 i = 0; i < rendered_frames * SURROUND_CHANNELS; ++i)
{
dpl2[i] = dpl2[i] * (1 << 15);
if (dpl2[i] > SHRT_MAX)
surround_short[i] = SHRT_MAX;
else if (dpl2[i] < SHRT_MIN)
surround_short[i] = SHRT_MIN;
else
surround_short[i] = static_cast<int>(dpl2[i]);
}
alBufferData(buffers[next_buffer], AL_FORMAT_51CHN16, surround_short.data(),
rendered_frames * FRAME_SURROUND_SHORT, frequency);
}
err = CheckALError("buffering data");
if (err == AL_INVALID_ENUM)
{
// 5.1 is not supported by the host, fallback to stereo
WARN_LOG(AUDIO,
"Unable to set 5.1 surround mode. Updating OpenAL Soft might fix this issue.");
use_surround = false;
}
}
else
{
u32 rendered_frames = m_mixer->Mix(realtime_buffer.data(), min_frames);
if (!rendered_frames)
continue;
alBufferData(buffers[next_buffer], AL_FORMAT_STEREO16, realtime_buffer.data(),
rendered_frames * FRAME_STEREO_SHORT, frequency);
}
alSourceQueueBuffers(source, 1, &buffers[next_buffer]);
err = CheckALError("queuing buffers");
num_buffers_queued++;
next_buffer = (next_buffer + 1) % OAL_BUFFERS;
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
// Buffer underrun occurred, resume playback
alSourcePlay(source);
err = CheckALError("occurred resuming playback");
}
}
}
#endif // HAVE_OPENAL