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
|
#include "SoundSource.h"
#include <climits>
#include <alc.h>
#include <SDL_timer.h>
#include "SoundBuffer.h"
#include "SoundItem.h"
#include "OggStream.h"
#include "ALShared.h"
#include "float3.h"
#include "LogOutput.h"
float SoundSource::globalPitch = 1.0;
float SoundSource::heightAdjustedRolloffModifier = 1.0;
float SoundSource::referenceDistance = 200.0f;
struct SoundSource::StreamControl
{
StreamControl(ALuint id) : stream(id), current(NULL), next(NULL), stopRequest(false) {};
COggStream stream;
struct TrackItem
{
std::string file;
float volume;
bool enqueue;
};
TrackItem* current;
TrackItem* next;
bool stopRequest;
};
SoundSource::SoundSource() : curPlaying(NULL), curStream(NULL)
{
alGenSources(1, &id);
if (!CheckError("SoundSource::SoundSource"))
{
id = 0;
}
else
{
alSourcef(id, AL_REFERENCE_DISTANCE, referenceDistance);
CheckError("SoundSource::SoundSource");
}
}
SoundSource::~SoundSource()
{
alDeleteSources(1, &id);
CheckError("SoundSource::~SoundSource");
}
void SoundSource::Update()
{
if (curStream)
{
boost::mutex::scoped_lock lock(streamMutex);
if (curStream->stopRequest)
{
Stop();
delete curStream->current;
delete curStream->next;
delete curStream;
curStream = NULL;
CheckError("SoundSource::Update()");
}
else
{
bool finished = curStream->stream.Valid() ? curStream->stream.GetPlayTime() >= curStream->stream.GetTotalTime() : true;
if (curStream->next != NULL && (finished || !curStream->next->enqueue))
{
Stop();
// COggStreams only appends buffers, giving errors when a buffer of another format is still asigned
alSourcei(id, AL_BUFFER, AL_NONE);
curStream->stream.Stop();
delete curStream->current;
curStream->current = curStream->next;
curStream->next = NULL;
alSource3f(id, AL_POSITION, 0.0, 0.0, -1.0); // in case of mono streams
alSourcef(id, AL_GAIN, curStream->current->volume);
alSource3f(id, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
alSource3f(id, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSourcef(id, AL_ROLLOFF_FACTOR, 0.0f);
alSourcei(id, AL_SOURCE_RELATIVE, true);
curStream->stream.Play(curStream->current->file, curStream->current->volume);
}
curStream->stream.Update();
CheckError("SoundSource::Update");
}
}
if (curPlaying && (!IsPlaying() || ((curPlaying->loopTime > 0) && (loopStop < SDL_GetTicks()))))
Stop();
}
int SoundSource::GetCurrentPriority() const
{
if (curStream)
{
return INT_MAX;
}
else if (!curPlaying) {
logOutput.Print("Warning: SoundSource::GetCurrentPriority() curPlaying is NULL (id %d)", id);
return INT_MIN;
}
return curPlaying->priority;
}
bool SoundSource::IsPlaying() const
{
CheckError("SoundSource::IsPlaying");
ALint state;
alGetSourcei(id, AL_SOURCE_STATE, &state);
CheckError("SoundSource::IsPlaying");
if (state == AL_PLAYING || curStream)
return true;
else
{
return false;
}
}
void SoundSource::Stop()
{
alSourceStop(id);
if (curPlaying)
{
curPlaying->StopPlay();
curPlaying = NULL;
}
CheckError("SoundSource::Stop");
}
void SoundSource::Play(SoundItem* item, const float3& pos, float3 velocity, float volume, bool relative)
{
assert(!curStream);
if (!item->PlayNow())
return;
Stop();
curPlaying = item;
alSourcei(id, AL_BUFFER, item->buffer->GetId());
alSourcef(id, AL_GAIN, item->GetGain() * volume);
alSourcef(id, AL_PITCH, item->GetPitch() * globalPitch);
velocity *= item->dopplerScale;
alSource3f(id, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
if (item->loopTime > 0)
alSourcei(id, AL_LOOPING, AL_TRUE);
else
alSourcei(id, AL_LOOPING, AL_FALSE);
loopStop = SDL_GetTicks() + item->loopTime;
if (relative || !item->in3D)
{
alSourcei(id, AL_SOURCE_RELATIVE, AL_TRUE);
alSource3f(id, AL_POSITION, 0.0, 0.0, -1.0);
}
else
{
alSourcei(id, AL_SOURCE_RELATIVE, AL_FALSE);
alSource3f(id, AL_POSITION, pos.x, pos.y, pos.z);
alSourcef(id, AL_ROLLOFF_FACTOR, item->rolloff * heightAdjustedRolloffModifier);
#ifdef __APPLE__
alSourcef(id, AL_MAX_DISTANCE, 1000000.0f);
// Max distance is too small by default on my Mac...
ALfloat gain = item->GetGain() * volume;
if (gain > 1.0f) {
// OpenAL on Mac cannot handle AL_GAIN > 1 well, so we will adjust settings to get the same output with AL_GAIN = 1.
ALint model = alGetInteger(AL_DISTANCE_MODEL);
ALfloat rolloff = item->rolloff * heightAdjustedRolloffModifier, ref = referenceDistance;
if ((model == AL_INVERSE_DISTANCE_CLAMPED) || (model == AL_INVERSE_DISTANCE)) {
alSourcef(id, AL_REFERENCE_DISTANCE, ((gain - 1.0f) * ref / rolloff) + ref);
alSourcef(id, AL_ROLLOFF_FACTOR, (gain + rolloff - 1.0f) / gain);
alSourcef(id, AL_GAIN, 1.0f);
}
} else {
alSourcef(id, AL_REFERENCE_DISTANCE, referenceDistance);
}
#endif
}
alSourcePlay(id);
if (item->buffer->GetId() == 0)
logOutput.Print("SoundSource::Play: Empty buffer for item %s (file %s)", item->name.c_str(), item->buffer->GetFilename().c_str());
CheckError("SoundSource::Play");
}
void SoundSource::PlayStream(const std::string& file, float volume, bool enqueue)
{
boost::mutex::scoped_lock lock(streamMutex);
if (!curStream)
{
StreamControl* temp = new StreamControl(id);
curStream = temp;
}
delete curStream->next;
curStream->next = new StreamControl::TrackItem;
curStream->next->file = file;
curStream->next->volume = volume;
curStream->next->enqueue = enqueue;
}
void SoundSource::StreamStop()
{
if (curStream)
{
boost::mutex::scoped_lock lock(streamMutex);
if (curStream->current)
{
curStream->stopRequest = true;
}
}
else
assert(false);
}
void SoundSource::StreamPause()
{
if (curStream)
{
boost::mutex::scoped_lock lock(streamMutex);
if (curStream->current)
{
if (curStream->stream.TogglePause())
alSourcePause(id);
else
alSourcePlay(id);
}
}
else
assert(false);
}
float SoundSource::GetStreamTime()
{
if (curStream)
{
boost::mutex::scoped_lock lock(streamMutex);
if (curStream->current)
return curStream->stream.GetTotalTime();
else
return 0;
}
assert(false);
return 0;
}
float SoundSource::GetStreamPlayTime()
{
if (curStream)
{
boost::mutex::scoped_lock lock(streamMutex);
if (curStream->current)
return curStream->stream.GetPlayTime();
else
return 0;
}
assert(false);
return 0;
}
void SoundSource::SetPitch(float newPitch)
{
globalPitch = newPitch;
}
void SoundSource::SetVolume(float newVol)
{
if (curStream && curStream->current)
{
alSourcef(id, AL_GAIN, newVol * curStream->current->volume);
}
}
|