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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/**
* @file audioengine_openal.cpp
* @brief implementation of audio engine using OpenAL
* support as a OpenAL 3D implementation
*
* $LicenseInfo:firstyear=2002&license=viewergpl$
*
* Copyright (c) 2002-2008, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlife.com/developers/opensource/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at http://secondlife.com/developers/opensource/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "lldir.h"
#include "audioengine_openal.h"
#include "listener_openal.h"
// Variables and definitions for Wind
#define MAX_NUM_WIND_BUFFERS 40
static int empty_num_wind_buffers = MAX_NUM_WIND_BUFFERS;
static const float wind_buffer_size_sec = 0.05f; // 1/20th sec
static const U32 wind_gen_freq = LLWindGen<S16>::getInputSamplingRate();
static ALuint wind_source;
static S16 *winddata=NULL;
LLAudioEngine_OpenAL::LLAudioEngine_OpenAL()
{
mWindGen = NULL;
}
LLAudioEngine_OpenAL::~LLAudioEngine_OpenAL()
{
}
bool LLAudioEngine_OpenAL::init(const S32 num_channels, void* userdata)
{
mWindGen = NULL;
LLAudioEngine::init(num_channels, userdata);
if(!alutInit(NULL, NULL))
{
llwarns << "LLAudioEngine_OpenAL::init() ALUT initialization failed: " << alutGetErrorString (alutGetError ()) << llendl;
return false;
}
llinfos << "LLAudioEngine_OpenAL::init() OpenAL successfully initialized" << llendl;
llinfos << "OpenAL version: "
<< ll_safe_string(alGetString(AL_VERSION)) << llendl;
llinfos << "OpenAL vendor: "
<< ll_safe_string(alGetString(AL_VENDOR)) << llendl;
llinfos << "OpenAL renderer: "
<< ll_safe_string(alGetString(AL_RENDERER)) << llendl;
ALint major = alutGetMajorVersion ();
ALint minor = alutGetMinorVersion ();
llinfos << "ALUT version: " << major << "." << minor << llendl;
ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext());
alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major);
alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &minor);
llinfos << "ALC version: " << major << "." << minor << llendl;
llinfos << "ALC default device: "
<< ll_safe_string(alcGetString(device,
ALC_DEFAULT_DEVICE_SPECIFIER))
<< llendl;
llinfos << "LLAudioEngine_OpenAL::init() Speed of sound is: " << alGetFloat(AL_SPEED_OF_SOUND) << llendl;
return true;
}
void LLAudioEngine_OpenAL::allocateListener()
{
mListenerp = (LLListener *) new LLListener_OpenAL();
if(!mListenerp)
{
llwarns << "LLAudioEngine_OpenAL::allocateListener() Listener creation failed" << llendl;
}
}
void LLAudioEngine_OpenAL::shutdown()
{
llinfos << "About to LLAudioEngine::shutdown()" << llendl;
LLAudioEngine::shutdown();
llinfos << "About to alutExit()" << llendl;
if(!alutExit())
{
llwarns << "Nuts." << llendl;
llwarns << "LLAudioEngine_OpenAL::shutdown() ALUT shutdown failed: " << alutGetErrorString (alutGetError ()) << llendl;
}
llinfos << "LLAudioEngine_OpenAL::shutdown() OpenAL successfully shut down" << llendl;
delete mListenerp;
mListenerp = NULL;
}
LLAudioBuffer *LLAudioEngine_OpenAL::createBuffer()
{
return new LLAudioBufferOpenAL();
}
LLAudioChannel *LLAudioEngine_OpenAL::createChannel()
{
return new LLAudioChannelOpenAL();
}
void LLAudioEngine_OpenAL::setInternalGain(F32 gain)
{
//llinfos << "LLAudioEngine_OpenAL::setInternalGain() Gain: " << gain << llendl;
alListenerf(AL_GAIN, gain);
}
LLAudioChannelOpenAL::LLAudioChannelOpenAL()
{
alGenSources(1, &mALSource);
}
LLAudioChannelOpenAL::~LLAudioChannelOpenAL()
{
cleanup();
alDeleteSources(1, &mALSource);
}
void LLAudioChannelOpenAL::cleanup()
{
alSourceStop(mALSource);
mCurrentBufferp = NULL;
}
void LLAudioChannelOpenAL::play()
{
if(!isPlaying()){
alSourcePlay(mALSource);
getSource()->setPlayedOnce(true);
}
}
void LLAudioChannelOpenAL::playSynced(LLAudioChannel *channelp)
{
play();
}
bool LLAudioChannelOpenAL::isPlaying()
{
ALint state;
alGetSourcei(mALSource, AL_SOURCE_STATE, &state);
if(state == AL_PLAYING){
return true;
}
return false;
}
bool LLAudioChannelOpenAL::updateBuffer()
{
if (LLAudioChannel::updateBuffer())
{
// Base class update returned true, which means that we need to actually
// set up the source for a different buffer.
LLAudioBufferOpenAL *bufferp = (LLAudioBufferOpenAL *)mCurrentSourcep->getCurrentBuffer();
alSourcei(mALSource, AL_BUFFER, bufferp->getBuffer());
alSourcef(mALSource, AL_GAIN, mCurrentSourcep->getGain());
alSourcei(mALSource, AL_LOOPING, mCurrentSourcep->isLoop() ? AL_TRUE : AL_FALSE);
}
return true;
}
void LLAudioChannelOpenAL::update3DPosition()
{
if(!mCurrentSourcep)
{
return;
}
if (mCurrentSourcep->isAmbient())
{
alSource3f(mALSource, AL_POSITION, 0.0, 0.0, 0.0);
alSource3f(mALSource, AL_VELOCITY, 0.0, 0.0, 0.0);
//alSource3f(mALSource, AL_DIRECTION, 0.0, 0.0, 0.0);
alSourcef (mALSource, AL_ROLLOFF_FACTOR, 0.0);
alSourcei (mALSource, AL_SOURCE_RELATIVE, AL_TRUE);
} else {
LLVector3 float_pos;
float_pos.setVec(mCurrentSourcep->getPositionGlobal());
alSourcefv(mALSource, AL_POSITION, float_pos.mV);
//llinfos << "LLAudioChannelOpenAL::update3DPosition() Velocity: " << mCurrentSourcep->getVelocity() << llendl;
alSourcefv(mALSource, AL_VELOCITY, mCurrentSourcep->getVelocity().mV);
//alSource3f(mALSource, AL_DIRECTION, 0.0, 0.0, 0.0);
alSourcef (mALSource, AL_ROLLOFF_FACTOR, 1.0);
alSourcei (mALSource, AL_SOURCE_RELATIVE, AL_FALSE);
}
//llinfos << "LLAudioChannelOpenAL::update3DPosition() Gain: " << mCurrentSourcep->getGain() << llendl;
alSourcef(mALSource, AL_GAIN, mCurrentSourcep->getGain());
}
LLAudioBufferOpenAL::LLAudioBufferOpenAL()
{
mALBuffer = AL_NONE;
}
LLAudioBufferOpenAL::~LLAudioBufferOpenAL()
{
cleanup();
}
void LLAudioBufferOpenAL::cleanup()
{
if(mALBuffer != AL_NONE)
{
alDeleteBuffers(1, &mALBuffer);
mALBuffer = AL_NONE;
}
}
bool LLAudioBufferOpenAL::loadWAV(const std::string& filename)
{
cleanup();
mALBuffer = alutCreateBufferFromFile(filename.c_str());
if(mALBuffer == AL_NONE){
ALenum error = alutGetError();
if (gDirUtilp->fileExists(filename)) {
llwarns <<
"LLAudioBufferOpenAL::loadWAV() Error loading "
<< filename
<< " " << alutGetErrorString(error) << llendl;
} else {
// It's common for the file to not actually exist.
lldebugs <<
"LLAudioBufferOpenAL::loadWAV() Error loading "
<< filename
<< " " << alutGetErrorString(error) << llendl;
}
return false;
}
return true;
}
U32 LLAudioBufferOpenAL::getLength(){
if(mALBuffer == AL_NONE){
return 0;
}
ALint length;
alGetBufferi(mALBuffer, AL_SIZE, &length);
return length >> 2;
}
// ------------
void LLAudioEngine_OpenAL::initWind(){
ALenum error;
llinfos << "LLAudioEngine_OpenAL::initWind() start" << llendl;
alGetError(); /* clear error */
alGenSources(1,&wind_source);
if((error=alGetError()) != AL_NO_ERROR)
{
llwarns << "LLAudioEngine_OpenAL::initWind() Error creating wind sources: "<<error<<llendl;
}
winddata=(S16*)malloc(sizeof(S16)*llceil(wind_gen_freq*wind_buffer_size_sec*2*2)); //200ms @wind_gen_freqHz Stereo
if(winddata==NULL)
{
llerrs << "LLAudioEngine_OpenAL::initWind() Error creating wind memory buffer" << llendl;
mEnableWind=false;
}
mWindGen = new LLWindGen<S16>;
llinfos << "LLAudioEngine_OpenAL::initWind() done" << llendl;
}
void LLAudioEngine_OpenAL::cleanupWind(){
llinfos << "LLAudioEngine_OpenAL::cleanupWind()" << llendl;
alDeleteSources(1, &wind_source);
if(winddata)
free(winddata);
delete mWindGen;
mWindGen = NULL;
}
void LLAudioEngine_OpenAL::updateWind(LLVector3 wind_vec, F32 camera_altitude)
{
LLVector3 wind_pos;
F64 pitch;
F64 center_freq;
ALenum error;
mMaxWindGain=1.0;
if (mEnableWind)
return;
if(winddata)
return;
if (mWindUpdateTimer.checkExpirationAndReset(LL_WIND_UPDATE_INTERVAL))
{
// wind comes in as Linden coordinate (+X = forward, +Y = left, +Z = up)
// need to convert this to the conventional orientation DS3D and OpenAL use
// where +X = right, +Y = up, +Z = backwards
wind_vec.setVec(-wind_vec.mV[1], wind_vec.mV[2], -wind_vec.mV[0]);
pitch = 1.0 + mapWindVecToPitch(wind_vec);
center_freq = 80.0 * pow(pitch,2.5*(mapWindVecToGain(wind_vec)+1.0));
mWindGen->mTargetFreq = (F32)center_freq;
mWindGen->mTargetGain = (F32)mapWindVecToGain(wind_vec) * mMaxWindGain;
mWindGen->mTargetPanGainR = (F32)mapWindVecToPan(wind_vec);
alSourcei(wind_source, AL_LOOPING, AL_FALSE);
alSource3f(wind_source, AL_POSITION, 0.0, 0.0, 0.0);
alSource3f(wind_source, AL_VELOCITY, 0.0, 0.0, 0.0);
alSourcef(wind_source, AL_ROLLOFF_FACTOR, 0.0);
alSourcei(wind_source, AL_SOURCE_RELATIVE, AL_TRUE);
}
// ok lets make a wind buffer now
int processed, queued, unprocessed;
alGetSourcei(wind_source, AL_BUFFERS_PROCESSED, &processed);
alGetSourcei(wind_source, AL_BUFFERS_QUEUED, &queued);
unprocessed = queued - processed;
// ensure that there are always at least 3x as many filled buffers
// queued as we managed to empty since last time.
empty_num_wind_buffers = llmin(empty_num_wind_buffers + processed * 3 - unprocessed, MAX_NUM_WIND_BUFFERS-unprocessed);
empty_num_wind_buffers = llmax(empty_num_wind_buffers, 0);
//llinfos << "empty_num_wind_buffers: " << empty_num_wind_buffers <<" (" << unprocessed << ":" << processed << ")" << llendl;
while(processed--) // unqueue old buffers
{
ALuint buffer;
int error;
alGetError(); /* clear error */
alSourceUnqueueBuffers(wind_source, 1, &buffer);
error = alGetError();
if(error != AL_NO_ERROR)
{
llwarns << "LLAudioEngine_OpenAL::updateWind() error swapping (unqueuing) buffers" << llendl;
}
else
{
alDeleteBuffers(1, &buffer);
}
}
while (empty_num_wind_buffers > 0) // fill+queue new buffers
{
ALuint buffer;
alGetError(); /* clear error */
alGenBuffers(1,&buffer);
if((error=alGetError()) != AL_NO_ERROR)
{
llwarns << "LLAudioEngine_OpenAL::initWind() Error creating wind buffer: " << error << llendl;
break;
}
alBufferData(buffer,
AL_FORMAT_STEREO16,
mWindGen->windGenerate(winddata,
int(wind_gen_freq*wind_buffer_size_sec), 2),
int(2*wind_gen_freq*wind_buffer_size_sec*sizeof(S16)),
wind_gen_freq);
error = alGetError();
if(error != AL_NO_ERROR)
llwarns << "LLAudioEngine_OpenAL::updateWind() error swapping (bufferdata) buffers" << llendl;
alSourceQueueBuffers(wind_source, 1, &buffer);
error = alGetError();
if(error != AL_NO_ERROR)
llwarns << "LLAudioEngine_OpenAL::updateWind() error swapping (queuing) buffers" << llendl;
--empty_num_wind_buffers;
}
int playing;
alGetSourcei(wind_source, AL_SOURCE_STATE, &playing);
if(playing != AL_PLAYING)
{
alSourcePlay(wind_source);
llinfos << "Wind had stopped - probably ran out of buffers - restarting: " << (unprocessed+empty_num_wind_buffers) << " now queued." << llendl;
}
}
|