| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
#include <algorithm> |
| 24 |
|
| 25 |
// Platform specific includes |
| 26 |
|
| 27 |
// 3rd party library includes |
| 28 |
|
| 29 |
// FIFE includes |
| 30 |
// These includes are split up in two parts, separated by one empty line |
| 31 |
// First block: files included from the FIFE root src directory |
| 32 |
// Second block: files included from the same folder |
| 33 |
#include "util/log/logger.h" |
| 34 |
#include "util/base/exception.h" |
| 35 |
#include "util/time/timemanager.h" |
| 36 |
|
| 37 |
#include "soundemitter.h" |
| 38 |
#include "soundmanager.h" |
| 39 |
#include "soundclipmanager.h" |
| 40 |
|
| 41 |
namespace FIFE { |
| 42 |
static Logger _log(LM_AUDIO); |
| 43 |
|
| 44 |
SoundEmitter::SoundEmitter(SoundManager* manager, uint32_t uid) : |
| 45 |
m_manager(manager), |
| 46 |
m_source(0), |
| 47 |
m_directFilter(NULL), |
| 48 |
m_soundClip(), |
| 49 |
m_soundClipId(0), |
| 50 |
m_streamId(0), |
| 51 |
m_emitterId(uid), |
| 52 |
m_group(""), |
| 53 |
m_samplesOffset(0), |
| 54 |
m_active(false), |
| 55 |
m_fadeIn(false), |
| 56 |
m_fadeOut(false), |
| 57 |
m_origGain(0.0f), |
| 58 |
m_fadeInStartTimestamp(0), |
| 59 |
m_fadeInEndTimestamp(0), |
| 60 |
m_fadeOutStartTimestamp(0), |
| 61 |
m_fadeOutEndTimestamp(0), |
| 62 |
m_playCheckDifference(0) { |
| 63 |
|
| 64 |
if (!m_manager->isActive()) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
resetInternData(); |
| 69 |
} |
| 70 |
|
| 71 |
SoundEmitter::~SoundEmitter() { |
| 72 |
if (!m_manager->isActive()) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
reset(); |
| 77 |
} |
| 78 |
|
| 79 |
void SoundEmitter::setSource(ALuint source) { |
| 80 |
if (!source && m_source) { |
| 81 |
alSourceStop(m_source); |
| 82 |
|
| 83 |
// Release all buffers |
| 84 |
alSourcei(m_source, AL_BUFFER, AL_NONE); |
| 85 |
alGetError(); |
| 86 |
|
| 87 |
deactivateEffects(); |
| 88 |
} |
| 89 |
m_source = source; |
| 90 |
if (m_source > 0) { |
| 91 |
m_active = true; |
| 92 |
activateEffects(); |
| 93 |
syncData(); |
| 94 |
} else { |
| 95 |
m_active = false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
ALuint SoundEmitter::getSource() const { |
| 100 |
return m_source; |
| 101 |
} |
| 102 |
|
| 103 |
bool SoundEmitter::isActive() const { |
| 104 |
return m_active; |
| 105 |
} |
| 106 |
|
| 107 |
void SoundEmitter::update() { |
| 108 |
if (!m_soundClip || m_internData.soundState == SD_STOPPED_STATE) { |
| 109 |
return; |
| 110 |
} |
| 111 |
if (m_fadeIn || m_fadeOut) { |
| 112 |
checkFade(); |
| 113 |
} |
| 114 |
// non streaming |
| 115 |
if (!m_soundClip->isStream()) { |
| 116 |
if (getState() == SD_STOPPED_STATE) { |
| 117 |
stop(); |
| 118 |
} |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
ALint procs; |
| 123 |
ALint bufs; |
| 124 |
ALuint buffer; |
| 125 |
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &procs); |
| 126 |
|
| 127 |
while (procs--) { |
| 128 |
// needed for correct cursor position |
| 129 |
float samplesOffset, newOffset; |
| 130 |
alGetSourcef(m_source, AL_SAMPLE_OFFSET, &samplesOffset); |
| 131 |
|
| 132 |
alSourceUnqueueBuffers(m_source, 1, &buffer); |
| 133 |
|
| 134 |
alGetSourcef(m_source, AL_SAMPLE_OFFSET, &newOffset); |
| 135 |
m_samplesOffset += (samplesOffset - newOffset); |
| 136 |
|
| 137 |
if (m_soundClip->getStream(m_streamId, buffer)) { |
| 138 |
// EOF! |
| 139 |
if (m_internData.loop) { |
| 140 |
// play again from the beginning |
| 141 |
m_soundClip->setStreamPos(m_streamId, SD_BYTE_POS, 0); |
| 142 |
m_soundClip->getStream(m_streamId, buffer); |
| 143 |
} else { |
| 144 |
// check if the playback has been finished |
| 145 |
alGetSourcei(m_source, AL_BUFFERS_QUEUED, &bufs); |
| 146 |
if (bufs == 0) { |
| 147 |
stop(); |
| 148 |
} |
| 149 |
continue; |
| 150 |
} |
| 151 |
} |
| 152 |
alSourceQueueBuffers(m_source, 1, &buffer); |
| 153 |
} |
| 154 |
|
| 155 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error while streaming") |
| 156 |
} |
| 157 |
|
| 158 |
uint32_t SoundEmitter::getId() const { |
| 159 |
return m_emitterId; |
| 160 |
} |
| 161 |
|
| 162 |
void SoundEmitter::setRelativePositioning(bool relative) { |
| 163 |
if (isActive()) { |
| 164 |
alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE); |
| 165 |
} |
| 166 |
m_internData.relative = relative; |
| 167 |
} |
| 168 |
|
| 169 |
bool SoundEmitter::isRelativePositioning() const { |
| 170 |
return m_internData.relative; |
| 171 |
} |
| 172 |
|
| 173 |
void SoundEmitter::setDirection(const AudioSpaceCoordinate& direction) { |
| 174 |
if (isActive()) { |
| 175 |
ALfloat vec[3] = { static_cast<ALfloat>(direction.x), static_cast<ALfloat>(direction.y), static_cast<ALfloat>(direction.z) }; |
| 176 |
alSourcefv(m_source, AL_DIRECTION, vec); |
| 177 |
} |
| 178 |
m_internData.direction = direction; |
| 179 |
} |
| 180 |
|
| 181 |
AudioSpaceCoordinate SoundEmitter::getDirection() const { |
| 182 |
return m_internData.direction; |
| 183 |
} |
| 184 |
|
| 185 |
void SoundEmitter::setPitch(float pitch) { |
| 186 |
if (pitch > 0.0) { |
| 187 |
if (isActive()) { |
| 188 |
alSourcef(m_source, AL_PITCH, pitch); |
| 189 |
} |
| 190 |
m_internData.pitch = pitch; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
float SoundEmitter::getPitch() const { |
| 195 |
return m_internData.pitch; |
| 196 |
} |
| 197 |
|
| 198 |
void SoundEmitter::setRolloff(float rolloff) { |
| 199 |
if (isActive()) { |
| 200 |
alSourcef(m_source, AL_ROLLOFF_FACTOR, rolloff); |
| 201 |
} |
| 202 |
m_internData.rolloff = rolloff; |
| 203 |
} |
| 204 |
|
| 205 |
float SoundEmitter::getRolloff() const { |
| 206 |
return m_internData.rolloff; |
| 207 |
} |
| 208 |
|
| 209 |
void SoundEmitter::reset(bool defaultall) { |
| 210 |
// remove effects and filter |
| 211 |
if (m_directFilter) { |
| 212 |
m_manager->deactivateFilter(m_directFilter, this); |
| 213 |
} |
| 214 |
std::vector<SoundEffect*>effects = m_effects; |
| 215 |
for (std::vector<SoundEffect*>::iterator it = effects.begin(); it != effects.end(); ++it) { |
| 216 |
if (*it) { |
| 217 |
m_manager->removeEmitterFromSoundEffect(*it, this); |
| 218 |
} |
| 219 |
} |
| 220 |
// release buffer and source handle |
| 221 |
if (isActive()) { |
| 222 |
alSourceStop(m_source); |
| 223 |
alSourcei(m_source, AL_BUFFER, AL_NONE); |
| 224 |
alGetError(); |
| 225 |
m_manager->releaseSource(this); |
| 226 |
} |
| 227 |
// reset clip |
| 228 |
if (m_soundClip) { |
| 229 |
if (m_soundClip->isStream()) { |
| 230 |
m_soundClip->quitStreaming(m_streamId); |
| 231 |
m_streamId = 0; |
| 232 |
} |
| 233 |
m_soundClipId = 0; |
| 234 |
// release the soundClip |
| 235 |
//SoundClipManager::instance()->free(m_soundClipId); |
| 236 |
m_soundClip.reset(); |
| 237 |
} |
| 238 |
|
| 239 |
// default source properties |
| 240 |
if (defaultall) { |
| 241 |
resetInternData(); |
| 242 |
if (isActive()) { |
| 243 |
syncData(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (m_group != "") { |
| 248 |
m_manager->removeFromGroup(this); |
| 249 |
m_group = ""; |
| 250 |
} |
| 251 |
m_samplesOffset = 0; |
| 252 |
m_fadeIn = false; |
| 253 |
m_fadeOut = false; |
| 254 |
m_origGain = 0; |
| 255 |
} |
| 256 |
|
| 257 |
void SoundEmitter::release() { |
| 258 |
m_manager->releaseEmitter(m_emitterId); |
| 259 |
} |
| 260 |
|
| 261 |
void SoundEmitter::setSoundClip(SoundClipPtr soundClip) { |
| 262 |
// equal clip |
| 263 |
if (m_soundClipId == soundClip->getHandle()) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
detachSoundClip(); |
| 268 |
m_soundClipId = soundClip->getHandle(); |
| 269 |
m_soundClip = soundClip; |
| 270 |
|
| 271 |
attachSoundClip(); |
| 272 |
} |
| 273 |
|
| 274 |
SoundClipPtr SoundEmitter::getSoundClip() { |
| 275 |
return m_soundClip; |
| 276 |
} |
| 277 |
|
| 278 |
void SoundEmitter::setSoundClip(const std::string& name) { |
| 279 |
SoundClipPtr clip = SoundClipManager::instance()->get(name); |
| 280 |
setSoundClip(clip); |
| 281 |
} |
| 282 |
|
| 283 |
void SoundEmitter::attachSoundClip() { |
| 284 |
if (!m_soundClip->isStream()) { |
| 285 |
if (!isActive()) { |
| 286 |
return; |
| 287 |
} |
| 288 |
// non-streaming |
| 289 |
alSourceQueueBuffers(m_source, m_soundClip->countBuffers(), m_soundClip->getBuffers()); |
| 290 |
alSourcei(m_source, AL_LOOPING, m_internData.loop ? AL_TRUE : AL_FALSE); |
| 291 |
|
| 292 |
} else { |
| 293 |
// streaming |
| 294 |
m_streamId = m_soundClip->beginStreaming(); |
| 295 |
m_soundClip->acquireStream(m_streamId); |
| 296 |
if (!isActive()) { |
| 297 |
return; |
| 298 |
} |
| 299 |
// queue initial buffers |
| 300 |
alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundClip->getBuffers(m_streamId)); |
| 301 |
alSourcei(m_source, AL_LOOPING, AL_FALSE); |
| 302 |
} |
| 303 |
|
| 304 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error attaching sound clip") |
| 305 |
} |
| 306 |
|
| 307 |
void SoundEmitter::detachSoundClip() { |
| 308 |
if (!m_soundClip) { |
| 309 |
return; |
| 310 |
} |
| 311 |
SoundStateType state = getState(); |
| 312 |
if (state == SD_PLAYING_STATE || state == SD_PAUSED_STATE) { |
| 313 |
stop(); |
| 314 |
} |
| 315 |
if (isActive()) { |
| 316 |
// detach all buffers |
| 317 |
alSourcei(m_source, AL_BUFFER, AL_NONE); |
| 318 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error detaching sound clip"); |
| 319 |
} |
| 320 |
if (m_soundClip->isStream()) { |
| 321 |
m_soundClip->quitStreaming(m_streamId); |
| 322 |
m_streamId = 0; |
| 323 |
} |
| 324 |
m_soundClipId = 0; |
| 325 |
m_soundClip.reset(); |
| 326 |
} |
| 327 |
|
| 328 |
void SoundEmitter::setLooping(bool loop) { |
| 329 |
if (m_soundClip && isActive()) { |
| 330 |
if (!m_soundClip->isStream()) { |
| 331 |
alSourcei(m_source, AL_LOOPING, loop ? AL_TRUE : AL_FALSE); |
| 332 |
} else { |
| 333 |
alSourcei(m_source, AL_LOOPING, AL_FALSE); |
| 334 |
} |
| 335 |
} |
| 336 |
m_internData.loop = loop; |
| 337 |
} |
| 338 |
|
| 339 |
bool SoundEmitter::isLooping() const { |
| 340 |
return m_internData.loop; |
| 341 |
} |
| 342 |
|
| 343 |
void SoundEmitter::play() { |
| 344 |
if (m_soundClip && isActive()) { |
| 345 |
alSourcePlay(m_source); |
| 346 |
} |
| 347 |
m_internData.playTimestamp = TimeManager::instance()->getTime(); |
| 348 |
m_playCheckDifference = 0; |
| 349 |
// resume |
| 350 |
if (m_internData.soundState == SD_PAUSED_STATE) { |
| 351 |
m_internData.playTimestamp -= static_cast<uint32_t>(getCursor(SD_TIME_POS) * 1000); |
| 352 |
} |
| 353 |
m_internData.soundState = SD_PLAYING_STATE; |
| 354 |
} |
| 355 |
|
| 356 |
void SoundEmitter::play(float inTime, float outTime) { |
| 357 |
float zero = 0; |
| 358 |
m_origGain = m_internData.volume; |
| 359 |
if (!Mathf::Equal(zero, inTime)) { |
| 360 |
m_fadeIn = true; |
| 361 |
setGain(0.0f); |
| 362 |
play(); |
| 363 |
m_fadeInStartTimestamp = m_internData.playTimestamp; |
| 364 |
m_fadeInEndTimestamp = m_fadeInStartTimestamp + static_cast<uint32_t>(inTime * 1000.0f); |
| 365 |
} |
| 366 |
if (getState() != SD_PLAYING_STATE) { |
| 367 |
play(); |
| 368 |
} |
| 369 |
if (!Mathf::Equal(zero, outTime)) { |
| 370 |
m_fadeOut = true; |
| 371 |
setGain(0.0f); |
| 372 |
m_fadeOutEndTimestamp = m_internData.playTimestamp + getDuration(); |
| 373 |
m_fadeOutStartTimestamp = m_fadeOutEndTimestamp - static_cast<uint32_t>(outTime * 1000.0f); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void SoundEmitter::stop() { |
| 378 |
if (m_soundClip && isActive()) { |
| 379 |
alSourceStop(m_source); |
| 380 |
rewind(); |
| 381 |
} |
| 382 |
m_internData.soundState = SD_STOPPED_STATE; |
| 383 |
m_internData.playTimestamp = 0; |
| 384 |
callOnSoundFinished(); |
| 385 |
} |
| 386 |
|
| 387 |
void SoundEmitter::stop(float time) { |
| 388 |
m_fadeOut = true; |
| 389 |
m_origGain = m_internData.volume; |
| 390 |
m_fadeOutStartTimestamp = TimeManager::instance()->getTime(); |
| 391 |
m_fadeOutEndTimestamp = m_fadeOutStartTimestamp + static_cast<uint32_t>(time * 1000.0f); |
| 392 |
} |
| 393 |
|
| 394 |
void SoundEmitter::pause() { |
| 395 |
if (m_soundClip && isActive()) { |
| 396 |
alSourcePause(m_source); |
| 397 |
} |
| 398 |
m_internData.soundState = SD_PAUSED_STATE; |
| 399 |
} |
| 400 |
|
| 401 |
void SoundEmitter::rewind() { |
| 402 |
m_internData.playTimestamp = 0; |
| 403 |
m_samplesOffset = 0; |
| 404 |
if (!isActive() || !m_soundClip) { |
| 405 |
return; |
| 406 |
} |
| 407 |
if (m_soundClip->isStream()) { |
| 408 |
setCursor(SD_BYTE_POS, 0); |
| 409 |
} else { |
| 410 |
alSourceRewind(m_source); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void SoundEmitter::setGain(float gain) { |
| 415 |
if (isActive()) { |
| 416 |
alSourcef(m_source, AL_GAIN, gain); |
| 417 |
} |
| 418 |
m_internData.volume = gain; |
| 419 |
} |
| 420 |
|
| 421 |
float SoundEmitter::getGain() const { |
| 422 |
return m_internData.volume; |
| 423 |
} |
| 424 |
|
| 425 |
void SoundEmitter::setMaxGain(float gain) { |
| 426 |
if (isActive()) { |
| 427 |
alSourcef(m_source, AL_MAX_GAIN, gain); |
| 428 |
} |
| 429 |
m_internData.maxVolume = gain; |
| 430 |
} |
| 431 |
|
| 432 |
float SoundEmitter::getMaxGain() const { |
| 433 |
return m_internData.maxVolume; |
| 434 |
} |
| 435 |
|
| 436 |
void SoundEmitter::setMinGain(float gain) { |
| 437 |
if (isActive()) { |
| 438 |
alSourcef(m_source, AL_MIN_GAIN, gain); |
| 439 |
} |
| 440 |
m_internData.minVolume = gain; |
| 441 |
} |
| 442 |
|
| 443 |
float SoundEmitter::getMinGain() const { |
| 444 |
return m_internData.minVolume; |
| 445 |
} |
| 446 |
|
| 447 |
bool SoundEmitter::isStereo() { |
| 448 |
if (m_soundClip) { |
| 449 |
return m_soundClip->getDecoder()->isStereo(); |
| 450 |
} |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
int16_t SoundEmitter::getBitResolution() { |
| 455 |
if (m_soundClip) { |
| 456 |
return m_soundClip->getDecoder()->getBitResolution(); |
| 457 |
} |
| 458 |
return 0; |
| 459 |
} |
| 460 |
|
| 461 |
uint64_t SoundEmitter::getSampleRate() { |
| 462 |
if (m_soundClip) { |
| 463 |
return m_soundClip->getDecoder()->getSampleRate(); |
| 464 |
} |
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
uint64_t SoundEmitter::getDecodedLength() { |
| 469 |
if (m_soundClip) { |
| 470 |
return m_soundClip->getDecoder()->getDecodedLength(); |
| 471 |
} |
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
uint64_t SoundEmitter::getDuration() { |
| 476 |
if (m_soundClip) { |
| 477 |
//convert to milliseconds |
| 478 |
double samplerate = static_cast<double>(getSampleRate()) / 1000.0; |
| 479 |
double bitres = static_cast<double>(getBitResolution()); |
| 480 |
// convert to bits |
| 481 |
double size = static_cast<double>(getDecodedLength()) * 8.0; |
| 482 |
double stereo = (isStereo() ? 2.0 : 1.0); |
| 483 |
double time = ( size / (samplerate * bitres) ) / stereo; |
| 484 |
|
| 485 |
return static_cast<uint64_t>(time); |
| 486 |
} |
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
uint32_t SoundEmitter::getPlayTimestamp() { |
| 491 |
return m_internData.playTimestamp; |
| 492 |
} |
| 493 |
|
| 494 |
bool SoundEmitter::isFinished() { |
| 495 |
if (isLooping()) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
if (isActive()) { |
| 499 |
return getState() == SD_STOPPED_STATE; |
| 500 |
} |
| 501 |
if (getState() == SD_STOPPED_STATE) { |
| 502 |
return true; |
| 503 |
} |
| 504 |
// roughly check, in the case the clip do not plays (is not active) |
| 505 |
return (m_internData.playTimestamp + m_playCheckDifference + static_cast<uint32_t>(getDuration())) <= TimeManager::instance()->getTime(); |
| 506 |
} |
| 507 |
|
| 508 |
void SoundEmitter::setCursor(SoundPositionType type, float value) { |
| 509 |
if (!m_soundClip || !isActive()) { |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
ALint state = 0; |
| 514 |
|
| 515 |
if (!m_soundClip->isStream()) { |
| 516 |
switch(type) { |
| 517 |
case SD_BYTE_POS: |
| 518 |
alSourcef(m_source, AL_BYTE_OFFSET, value); |
| 519 |
break; |
| 520 |
case SD_SAMPLE_POS: |
| 521 |
alSourcef(m_source, AL_SAMPLE_OFFSET, value); |
| 522 |
break; |
| 523 |
case SD_TIME_POS: |
| 524 |
alSourcef(m_source, AL_SEC_OFFSET, value); |
| 525 |
break; |
| 526 |
} |
| 527 |
|
| 528 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting cursor position") |
| 529 |
} else { |
| 530 |
switch (type) { |
| 531 |
case SD_BYTE_POS: |
| 532 |
m_samplesOffset = value / (getBitResolution() / 8 * (isStereo() ? 2 : 1)); |
| 533 |
break; |
| 534 |
case SD_SAMPLE_POS: |
| 535 |
m_samplesOffset = value; |
| 536 |
break; |
| 537 |
case SD_TIME_POS: |
| 538 |
m_samplesOffset = value * getSampleRate(); |
| 539 |
break; |
| 540 |
} |
| 541 |
alGetSourcei(m_source, AL_SOURCE_STATE, &state); |
| 542 |
if (state == AL_PLAYING || state == AL_PAUSED) { |
| 543 |
alSourceStop(m_source); |
| 544 |
} |
| 545 |
|
| 546 |
m_soundClip->setStreamPos(m_streamId, type, value); |
| 547 |
|
| 548 |
// detach all buffers |
| 549 |
alSourcei(m_source, AL_BUFFER, 0); |
| 550 |
|
| 551 |
// queue the buffers with new data |
| 552 |
m_soundClip->acquireStream(m_streamId); |
| 553 |
alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundClip->getBuffers(m_streamId)); |
| 554 |
|
| 555 |
if (state == AL_PLAYING) { |
| 556 |
alSourcePlay(m_source); |
| 557 |
} |
| 558 |
|
| 559 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting stream cursor position") |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
float SoundEmitter::getCursor(SoundPositionType type) { |
| 564 |
if (!m_soundClip || !isActive()) { |
| 565 |
return 0.0f; |
| 566 |
} |
| 567 |
|
| 568 |
ALfloat pos = 0.0f; |
| 569 |
|
| 570 |
switch(type) { |
| 571 |
case SD_BYTE_POS: |
| 572 |
alGetSourcef(m_source, AL_BYTE_OFFSET, &pos); |
| 573 |
break; |
| 574 |
case SD_SAMPLE_POS: |
| 575 |
alGetSourcef(m_source, AL_SAMPLE_OFFSET, &pos); |
| 576 |
break; |
| 577 |
case SD_TIME_POS: |
| 578 |
alGetSourcef(m_source, AL_SEC_OFFSET, &pos); |
| 579 |
break; |
| 580 |
} |
| 581 |
|
| 582 |
if (m_soundClip->isStream()) { |
| 583 |
switch (type) { |
| 584 |
case SD_BYTE_POS: |
| 585 |
pos += m_samplesOffset * (getBitResolution() / 8 * (isStereo() ? 2 : 1)); |
| 586 |
break; |
| 587 |
case SD_SAMPLE_POS: |
| 588 |
pos += m_samplesOffset; |
| 589 |
break; |
| 590 |
case SD_TIME_POS: |
| 591 |
pos += m_samplesOffset / getSampleRate(); |
| 592 |
break; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error getting cursor") |
| 597 |
|
| 598 |
return pos; |
| 599 |
} |
| 600 |
|
| 601 |
void SoundEmitter::setPosition(const AudioSpaceCoordinate& position) { |
| 602 |
if (isActive()) { |
| 603 |
alSource3f(m_source, AL_POSITION, static_cast<ALfloat>(position.x), static_cast<ALfloat>(position.y), static_cast<ALfloat>(position.z)); |
| 604 |
} |
| 605 |
m_internData.position = position; |
| 606 |
} |
| 607 |
|
| 608 |
AudioSpaceCoordinate SoundEmitter::getPosition() const { |
| 609 |
return m_internData.position; |
| 610 |
} |
| 611 |
|
| 612 |
bool SoundEmitter::isPosition() const { |
| 613 |
double zero = 0; |
| 614 |
return !(Mathd::Equal(zero, m_internData.position.x) && Mathd::Equal(zero, m_internData.position.y) && Mathd::Equal(zero, m_internData.position.z)); |
| 615 |
} |
| 616 |
|
| 617 |
void SoundEmitter::setReferenceDistance(float distance) { |
| 618 |
if (isActive()) { |
| 619 |
alSourcef(m_source, AL_REFERENCE_DISTANCE, distance); |
| 620 |
} |
| 621 |
m_internData.refDistance = distance; |
| 622 |
} |
| 623 |
|
| 624 |
float SoundEmitter::getReferenceDistance() const { |
| 625 |
return m_internData.refDistance; |
| 626 |
} |
| 627 |
|
| 628 |
void SoundEmitter::setMaxDistance(float distance) { |
| 629 |
if (isActive()) { |
| 630 |
alSourcef(m_source, AL_MAX_DISTANCE, distance); |
| 631 |
} |
| 632 |
m_internData.maxDistance = distance; |
| 633 |
} |
| 634 |
|
| 635 |
float SoundEmitter::getMaxDistance() const { |
| 636 |
return m_internData.maxDistance; |
| 637 |
} |
| 638 |
|
| 639 |
void SoundEmitter::setVelocity(const AudioSpaceCoordinate& velocity) { |
| 640 |
if (isActive()) { |
| 641 |
alSource3f(m_source, AL_VELOCITY, static_cast<ALfloat>(velocity.x), static_cast<ALfloat>(velocity.y), static_cast<ALfloat>(velocity.z)); |
| 642 |
} |
| 643 |
m_internData.velocity = velocity; |
| 644 |
} |
| 645 |
|
| 646 |
AudioSpaceCoordinate SoundEmitter::getVelocity() const { |
| 647 |
return m_internData.velocity; |
| 648 |
} |
| 649 |
|
| 650 |
void SoundEmitter::setConeInnerAngle(float inner) { |
| 651 |
if (isActive()) { |
| 652 |
alSourcef(m_source, AL_CONE_INNER_ANGLE, inner); |
| 653 |
} |
| 654 |
m_internData.coneInnerAngle = inner; |
| 655 |
} |
| 656 |
|
| 657 |
float SoundEmitter::getConeInnerAngle() const { |
| 658 |
return m_internData.coneInnerAngle; |
| 659 |
} |
| 660 |
|
| 661 |
void SoundEmitter::setConeOuterAngle(float outer) { |
| 662 |
if (isActive()) { |
| 663 |
alSourcef(m_source, AL_CONE_OUTER_ANGLE, outer); |
| 664 |
} |
| 665 |
m_internData.coneOuterAngle = outer; |
| 666 |
} |
| 667 |
|
| 668 |
float SoundEmitter::getConeOuterAngle() const { |
| 669 |
return m_internData.coneOuterAngle; |
| 670 |
} |
| 671 |
|
| 672 |
void SoundEmitter::setConeOuterGain(float gain) { |
| 673 |
if (isActive()) { |
| 674 |
alSourcef(m_source, AL_CONE_OUTER_GAIN, gain); |
| 675 |
} |
| 676 |
m_internData.coneOuterGain = gain; |
| 677 |
} |
| 678 |
|
| 679 |
float SoundEmitter::getConeOuterGain() const { |
| 680 |
return m_internData.coneOuterGain; |
| 681 |
} |
| 682 |
|
| 683 |
SoundStateType SoundEmitter::getState() { |
| 684 |
if (!isActive()) { |
| 685 |
return m_internData.soundState; |
| 686 |
} |
| 687 |
ALint state; |
| 688 |
alGetSourcei(m_source, AL_SOURCE_STATE, &state); |
| 689 |
switch(state) { |
| 690 |
case AL_INITIAL: |
| 691 |
return SD_INITIAL_STATE; |
| 692 |
break; |
| 693 |
case AL_PLAYING: |
| 694 |
return SD_PLAYING_STATE; |
| 695 |
break; |
| 696 |
case AL_PAUSED: |
| 697 |
return SD_PAUSED_STATE; |
| 698 |
break; |
| 699 |
case AL_STOPPED: |
| 700 |
return SD_STOPPED_STATE; |
| 701 |
break; |
| 702 |
default: |
| 703 |
return SD_UNKNOWN_STATE; |
| 704 |
break; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
void SoundEmitter::setGroup(const std::string& group) { |
| 709 |
if (group != m_group) { |
| 710 |
if (m_group != "") { |
| 711 |
m_manager->removeFromGroup(this); |
| 712 |
} |
| 713 |
m_group = group; |
| 714 |
if (m_group != "") { |
| 715 |
m_manager->addToGroup(this); |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
const std::string& SoundEmitter::getGroup() { |
| 721 |
return m_group; |
| 722 |
} |
| 723 |
|
| 724 |
void SoundEmitter::syncData() { |
| 725 |
setGain(m_internData.volume); |
| 726 |
setMaxGain(m_internData.maxVolume); |
| 727 |
setMinGain(m_internData.minVolume); |
| 728 |
setReferenceDistance(m_internData.refDistance); |
| 729 |
setMaxDistance(m_internData.maxDistance); |
| 730 |
setRolloff(m_internData.rolloff); |
| 731 |
setPitch(m_internData.pitch); |
| 732 |
setConeInnerAngle(m_internData.coneInnerAngle); |
| 733 |
setConeOuterAngle(m_internData.coneOuterAngle); |
| 734 |
setConeOuterGain(m_internData.coneOuterGain); |
| 735 |
setPosition(m_internData.position); |
| 736 |
setDirection(m_internData.direction); |
| 737 |
setVelocity(m_internData.velocity); |
| 738 |
setLooping(m_internData.loop); |
| 739 |
setRelativePositioning(m_internData.relative); |
| 740 |
if (m_internData.soundState == SD_PLAYING_STATE) { |
| 741 |
uint32_t timediff = TimeManager::instance()->getTime() - m_internData.playTimestamp - m_playCheckDifference; |
| 742 |
if (m_internData.loop) { |
| 743 |
timediff = timediff % getDuration(); |
| 744 |
} |
| 745 |
float time = static_cast<float>(timediff) / 1000.0f; |
| 746 |
attachSoundClip(); |
| 747 |
setCursor(SD_TIME_POS, time); |
| 748 |
if (m_soundClip && isActive()) { |
| 749 |
m_internData.playTimestamp = TimeManager::instance()->getTime() - timediff; |
| 750 |
alSourcePlay(m_source); |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
void SoundEmitter::resetInternData() { |
| 756 |
m_internData.volume = 1.0; |
| 757 |
m_internData.maxVolume = 1.0; |
| 758 |
m_internData.minVolume = 0.0; |
| 759 |
m_internData.refDistance = 1.0; |
| 760 |
m_internData.maxDistance = 1000000.0; |
| 761 |
m_internData.rolloff = 1.0; |
| 762 |
m_internData.pitch = 1.0; |
| 763 |
m_internData.coneInnerAngle = 360.0; |
| 764 |
m_internData.coneOuterAngle = 360.0; |
| 765 |
m_internData.coneOuterGain = 0.0; |
| 766 |
m_internData.position = AudioSpaceCoordinate(0.0, 0.0, 0.0); |
| 767 |
m_internData.direction = AudioSpaceCoordinate(0.0, 0.0, 0.0); |
| 768 |
m_internData.velocity = AudioSpaceCoordinate(0.0, 0.0, 0.0); |
| 769 |
m_internData.playTimestamp = 0; |
| 770 |
m_internData.soundState = SD_UNKNOWN_STATE; |
| 771 |
m_internData.loop = false; |
| 772 |
m_internData.relative = false; |
| 773 |
} |
| 774 |
|
| 775 |
void SoundEmitter::checkFade() { |
| 776 |
uint32_t timestamp = TimeManager::instance()->getTime(); |
| 777 |
if (m_fadeIn) { |
| 778 |
float delta = m_origGain / static_cast<float>(m_fadeInEndTimestamp - m_fadeInStartTimestamp); |
| 779 |
if (timestamp >= m_fadeInEndTimestamp) { |
| 780 |
m_fadeIn = false; |
| 781 |
setGain(m_origGain); |
| 782 |
} else { |
| 783 |
float gain = delta * static_cast<float>(timestamp - m_fadeInStartTimestamp); |
| 784 |
gain = std::min(gain, m_origGain); |
| 785 |
setGain(gain); |
| 786 |
} |
| 787 |
} else if (m_fadeOut) { |
| 788 |
float delta = m_origGain / static_cast<float>(m_fadeOutEndTimestamp - m_fadeOutStartTimestamp); |
| 789 |
if (timestamp >= m_fadeOutEndTimestamp) { |
| 790 |
m_fadeOut = false; |
| 791 |
stop(); |
| 792 |
setGain(m_origGain); |
| 793 |
} else { |
| 794 |
float gain = delta * static_cast<float>(m_fadeOutEndTimestamp - timestamp); |
| 795 |
gain = std::max(gain, 0.0f); |
| 796 |
setGain(gain); |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
void SoundEmitter::addEffect(SoundEffect* effect) { |
| 802 |
bool added = false; |
| 803 |
for (std::vector<SoundEffect*>::iterator it = m_effects.begin(); it != m_effects.end(); ++it) { |
| 804 |
if (!(*it)) { |
| 805 |
(*it) = effect; |
| 806 |
added = true; |
| 807 |
break; |
| 808 |
} |
| 809 |
} |
| 810 |
if (!added) { |
| 811 |
m_effects.push_back(effect); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
void SoundEmitter::removeEffect(SoundEffect* effect) { |
| 816 |
for (std::vector<SoundEffect*>::iterator it = m_effects.begin(); it != m_effects.end(); ++it) { |
| 817 |
if (effect == *it) { |
| 818 |
(*it) = NULL; |
| 819 |
break; |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
uint8_t SoundEmitter::getEffectCount() { |
| 825 |
uint8_t counter = 0; |
| 826 |
for (std::vector<SoundEffect*>::iterator it = m_effects.begin(); it != m_effects.end(); ++it) { |
| 827 |
if (*it) { |
| 828 |
++counter; |
| 829 |
} |
| 830 |
} |
| 831 |
return counter; |
| 832 |
} |
| 833 |
|
| 834 |
uint8_t SoundEmitter::getEffectNumber(SoundEffect* effect) { |
| 835 |
uint8_t number = 0; |
| 836 |
for (std::vector<SoundEffect*>::iterator it = m_effects.begin(); it != m_effects.end(); ++it) { |
| 837 |
if (effect == *it) { |
| 838 |
break; |
| 839 |
} |
| 840 |
++number; |
| 841 |
} |
| 842 |
return number; |
| 843 |
} |
| 844 |
|
| 845 |
void SoundEmitter::setDirectFilter(SoundFilter* filter) { |
| 846 |
m_directFilter = filter; |
| 847 |
} |
| 848 |
|
| 849 |
SoundFilter* SoundEmitter::getDirectFilter() { |
| 850 |
return m_directFilter; |
| 851 |
} |
| 852 |
|
| 853 |
void SoundEmitter::activateEffects() { |
| 854 |
for (std::vector<SoundEffect*>::iterator it = m_effects.begin(); it != m_effects.end(); ++it) { |
| 855 |
if (*it) { |
| 856 |
m_manager->activateEffect(*it, this); |
| 857 |
} |
| 858 |
} |
| 859 |
if (m_directFilter) { |
| 860 |
m_manager->activateFilter(m_directFilter, this); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
void SoundEmitter::deactivateEffects() { |
| 865 |
for (std::vector<SoundEffect*>::iterator it = m_effects.begin(); it != m_effects.end(); ++it) { |
| 866 |
if (*it) { |
| 867 |
m_manager->deactivateEffect(*it, this); |
| 868 |
} |
| 869 |
} |
| 870 |
if (m_directFilter) { |
| 871 |
m_manager->deactivateFilter(m_directFilter, this); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
void SoundEmitter::setCheckDifference() { |
| 876 |
if (m_playCheckDifference == 0 && getState() == SD_PLAYING_STATE) { |
| 877 |
m_playCheckDifference = TimeManager::instance()->getTime() - m_internData.playTimestamp; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
void SoundEmitter::addListener(SoundEmitterListener* listener) { |
| 882 |
m_listeners.push_back(listener); |
| 883 |
} |
| 884 |
|
| 885 |
void SoundEmitter::removeListener(SoundEmitterListener* listener) { |
| 886 |
std::vector<SoundEmitterListener*>::iterator i = m_listeners.begin(); |
| 887 |
while (i != m_listeners.end()) { |
| 888 |
if ((*i) == listener) { |
| 889 |
*i = NULL; |
| 890 |
return; |
| 891 |
} |
| 892 |
++i; |
| 893 |
} |
| 894 |
|
| 895 |
FL_WARN(_log, "Cannot remove unknown listener"); |
| 896 |
} |
| 897 |
|
| 898 |
void SoundEmitter::callOnSoundFinished() { |
| 899 |
std::vector<SoundEmitterListener*>::iterator i = m_listeners.begin(); |
| 900 |
for (; i != m_listeners.end(); ++i) { |
| 901 |
if (*i) { |
| 902 |
(*i)->onSoundFinished(m_emitterId, m_soundClipId); |
| 903 |
} |
| 904 |
} |
| 905 |
m_listeners.erase(std::remove(m_listeners.begin(), m_listeners.end(), (SoundEmitterListener*)NULL), m_listeners.end()); |
| 906 |
} |
| 907 |
} |