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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "SoundSource.h"
#include <climits>
#include <alc.h>
#include "ALShared.h"
#include "EFX.h"
#include "System/Sound/IAudioChannel.h"
#include "OggStream.h"
#include "System/Sound/SoundLog.h"
#include "SoundBuffer.h"
#include "SoundItem.h"
#include "Sound.h" //remove when unified ElmoInMeters
#include "Sim/Misc/GlobalConstants.h"
#include "System/float3.h"
#include "System/StringUtil.h"
#include "System/SpringMath.h"
static constexpr float ROLLOFF_FACTOR = 5.0f;
static constexpr float REFERENCE_DIST = 200.0f;
// used to adjust the pitch to the GameSpeed (optional)
float CSoundSource::globalPitch = 1.0f;
// reduce the rolloff when the camera is height above the ground (so we still hear something in tab mode or far zoom)
float CSoundSource::heightRolloffModifier = 1.0f;
CSoundSource::CSoundSource()
: curChannel(nullptr)
, curVolume(1.0f)
, loopStop(1e9)
, in3D(false)
, efxEnabled(false)
, efxUpdates(0)
, curHeightRolloffModifier(1.0f)
{
alGenSources(1, &id);
if (!CheckError("CSoundSource::CSoundSource")) {
id = 0;
} else {
alSourcef(id, AL_REFERENCE_DISTANCE, REFERENCE_DIST * ELMOS_TO_METERS);
CheckError("CSoundSource::CSoundSource");
}
curPlayingItem = {0, 0, 0, 0.0f, 0.0f};
}
void CSoundSource::Update()
{
if (asyncPlayItem.id != 0) {
// Sound::Update() holds mutex, soundItems can not be accessed concurrently
Play(asyncPlayItem.channel, sound->GetSoundItem(asyncPlayItem.id), asyncPlayItem.position, asyncPlayItem.velocity, asyncPlayItem.volume, asyncPlayItem.relative);
asyncPlayItem = AsyncSoundItemData();
}
if (curPlayingItem.id != 0) {
if (in3D && (efxEnabled != efx.Enabled())) {
alSourcef(id, AL_AIR_ABSORPTION_FACTOR, (efx.Enabled()) ? efx.GetAirAbsorptionFactor() : 0);
alSource3i(id, AL_AUXILIARY_SEND_FILTER, (efx.Enabled()) ? efx.sfxSlot : AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
alSourcei(id, AL_DIRECT_FILTER, (efx.Enabled()) ? efx.sfxFilter : AL_FILTER_NULL);
efxEnabled = efx.Enabled();
efxUpdates = efx.updates;
}
if (heightRolloffModifier != curHeightRolloffModifier) {
curHeightRolloffModifier = heightRolloffModifier;
alSourcef(id, AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR * curPlayingItem.rolloff * heightRolloffModifier);
}
if (!IsPlaying(true) || ((curPlayingItem.loopTime > 0) && (spring_gettime() > loopStop)))
Stop();
}
if (curStream.Valid()) {
if (curStream.IsFinished()) {
Stop();
} else {
curStream.Update();
CheckError("CSoundSource::Update");
}
}
if (efxEnabled && (efxUpdates != efx.updates)) {
// airAbsorption & LowPass aren't auto updated by OpenAL on change, so we need to do it per source
alSourcef(id, AL_AIR_ABSORPTION_FACTOR, efx.GetAirAbsorptionFactor());
alSourcei(id, AL_DIRECT_FILTER, efx.sfxFilter);
efxUpdates = efx.updates;
}
}
void CSoundSource::Delete()
{
if (efxEnabled) {
alSource3i(id, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
alSourcei(id, AL_DIRECT_FILTER, AL_FILTER_NULL);
}
Stop();
alDeleteSources(1, &id);
CheckError("CSoundSource::Delete");
}
int CSoundSource::GetCurrentPriority() const
{
if (asyncPlayItem.id != 0)
return asyncPlayItem.priority;
if (curStream.Valid())
return INT_MAX;
if (curPlayingItem.id == 0)
return INT_MIN;
return (curPlayingItem.priority);
}
bool CSoundSource::IsPlaying(const bool checkOpenAl) const
{
if (curStream.Valid())
return true;
if (asyncPlayItem.id != 0)
return true;
if (curPlayingItem.id == 0)
return false;
// calling OpenAL has a high chance of generating a L2 cache miss, avoid if possible
if (!checkOpenAl)
return true;
CheckError("CSoundSource::IsPlaying");
ALint state;
alGetSourcei(id, AL_SOURCE_STATE, &state);
CheckError("CSoundSource::IsPlaying");
return (state == AL_PLAYING);
}
void CSoundSource::Stop()
{
alSourceStop(id);
{
SoundItem* item = nullptr;
// callers marked * are mutex-guarded
// ::Delete via ~CSoundSource via CSound::Kill
// ::Play via ::Update (*)
// ::PlayStream via AudioChannel::StreamPlay (*)
// ::StreamStop via AudioChannel::StreamStop (*)
// AudioChannel::FindSourceAndPlay (*)
if (sound != nullptr)
item = sound->GetSoundItem(curPlayingItem.id);
if (item != nullptr)
item->StopPlay();
curPlayingItem = {};
}
if (curStream.Valid())
curStream.Stop();
if (curChannel != nullptr) {
IAudioChannel* oldChannel = curChannel;
curChannel = nullptr;
oldChannel->SoundSourceFinished(this);
}
CheckError("CSoundSource::Stop");
}
void CSoundSource::Play(IAudioChannel* channel, SoundItem* item, float3 pos, float3 velocity, float volume, bool relative)
{
assert(!curStream.Valid());
assert(channel);
if (!item->PlayNow())
return;
const SoundBuffer& itemBuffer = SoundBuffer::GetById(item->GetSoundBufferID());
Stop();
curVolume = volume;
curPlayingItem = {item->soundItemID, item->loopTime, item->priority, item->GetGain(), item->rolloff};
curChannel = channel;
alSourcei(id, AL_BUFFER, itemBuffer.GetId());
alSourcef(id, AL_GAIN, volume * item->GetGain() * channel->volume);
alSourcef(id, AL_PITCH, item->GetPitch() * globalPitch);
velocity *= item->dopplerScale * ELMOS_TO_METERS;
alSource3f(id, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
alSourcei(id, AL_LOOPING, (item->loopTime > 0) ? AL_TRUE : AL_FALSE);
loopStop = spring_gettime() + spring_msecs(item->loopTime);
if (relative || !item->in3D) {
in3D = false;
if (efxEnabled) {
alSource3i(id, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
alSourcei(id, AL_DIRECT_FILTER, AL_FILTER_NULL);
efxEnabled = false;
}
alSourcei(id, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcef(id, AL_ROLLOFF_FACTOR, 0.f);
alSource3f(id, AL_POSITION, 0.0f, 0.0f, -1.0f * ELMOS_TO_METERS);
#ifdef __APPLE__
alSourcef(id, AL_REFERENCE_DISTANCE, REFERENCE_DIST * ELMOS_TO_METERS);
#endif
} else {
if (itemBuffer.GetChannels() > 1)
LOG_L(L_WARNING, "Can not play non-mono \"%s\" in 3d.", itemBuffer.GetFilename().c_str());
in3D = true;
if (efx.Enabled()) {
efxEnabled = true;
alSourcef(id, AL_AIR_ABSORPTION_FACTOR, efx.GetAirAbsorptionFactor());
alSource3i(id, AL_AUXILIARY_SEND_FILTER, efx.sfxSlot, 0, AL_FILTER_NULL);
alSourcei(id, AL_DIRECT_FILTER, efx.sfxFilter);
efxUpdates = efx.updates;
}
alSourcei(id, AL_SOURCE_RELATIVE, AL_FALSE);
pos *= ELMOS_TO_METERS;
alSource3f(id, AL_POSITION, pos.x, pos.y, pos.z);
curHeightRolloffModifier = heightRolloffModifier;
alSourcef(id, AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR * item->rolloff * heightRolloffModifier);
#ifdef __APPLE__
alSourcef(id, AL_MAX_DISTANCE, 1000000.0f);
// Max distance is too small by default on my Mac...
ALfloat gain = channel->volume * 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.
const ALint model = alGetInteger(AL_DISTANCE_MODEL);
const ALfloat rolloff = ROLLOFF_FACTOR * item->rolloff * heightRolloffModifier;
const ALfloat refDist = REFERENCE_DIST * ELMOS_TO_METERS;
if ((model == AL_INVERSE_DISTANCE_CLAMPED) || (model == AL_INVERSE_DISTANCE)) {
alSourcef(id, AL_REFERENCE_DISTANCE, ((gain - 1.0f) * refDist / rolloff) + refDist);
alSourcef(id, AL_ROLLOFF_FACTOR, (gain + rolloff - 1.0f) / gain);
alSourcef(id, AL_GAIN, 1.0f);
}
} else {
alSourcef(id, AL_REFERENCE_DISTANCE, REFERENCE_DIST * ELMOS_TO_METERS);
}
#endif
}
alSourcePlay(id);
if (itemBuffer.GetId() == 0)
LOG_L(L_WARNING, "CSoundSource::Play: Empty buffer for item %s (file %s)", item->name.c_str(), itemBuffer.GetFilename().c_str());
CheckError("CSoundSource::Play");
}
void CSoundSource::PlayAsync(IAudioChannel* channel, size_t id, float3 pos, float3 velocity, float volume, float priority, bool relative)
{
asyncPlayItem.channel = channel;
asyncPlayItem.id = id;
asyncPlayItem.position = pos;
asyncPlayItem.velocity = velocity;
asyncPlayItem.volume = volume;
asyncPlayItem.priority = priority;
asyncPlayItem.relative = relative;
}
void CSoundSource::PlayStream(IAudioChannel* channel, const std::string& file, float volume)
{
// stop any current playback
Stop();
if (!curStream.Valid())
curStream = COggStream(id);
// OpenAL params
curChannel = channel;
curVolume = volume;
in3D = false;
if (efxEnabled) {
alSource3i(id, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
alSourcei(id, AL_DIRECT_FILTER, AL_FILTER_NULL);
efxEnabled = false;
}
alSource3f(id, AL_POSITION, 0.0f, 0.0f, 0.0f);
alSourcef(id, AL_GAIN, volume);
alSourcef(id, AL_PITCH, globalPitch);
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, AL_TRUE);
// COggStreams only appends buffers, giving errors when a buffer of another format is still assigned
alSourcei(id, AL_BUFFER, AL_NONE);
curStream.Play(file, volume);
curStream.Update();
CheckError("CSoundSource::Update");
}
void CSoundSource::StreamStop()
{
if (!curStream.Valid())
return;
Stop();
}
void CSoundSource::StreamPause()
{
if (!curStream.Valid())
return;
if (curStream.TogglePause())
alSourcePause(id);
else
alSourcePlay(id);
}
float CSoundSource::GetStreamTime()
{
return (curStream.Valid())? curStream.GetTotalTime() : 0.0f;
}
float CSoundSource::GetStreamPlayTime()
{
return (curStream.Valid())? curStream.GetPlayTime() : 0.0f;
}
void CSoundSource::UpdateVolume()
{
if (curChannel == nullptr)
return;
if (curStream.Valid()) {
alSourcef(id, AL_GAIN, curVolume * curChannel->volume);
return;
}
if (curPlayingItem.id != 0) {
alSourcef(id, AL_GAIN, curVolume * curPlayingItem.rndGain * curChannel->volume);
return;
}
}
|