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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "titanic/sound/sound_manager.h"
#include "titanic/events.h"
#include "titanic/titanic.h"
namespace Titanic {
const uint LATENCY = 100;
const uint CHANNELS_COUNT = 16;
CSoundManager::CSoundManager() : _musicPercent(75.0), _speechPercent(75.0),
_masterPercent(75.0), _parrotPercent(75.0), _handleCtr(1) {
}
uint CSoundManager::getModeVolume(VolumeMode mode) {
switch (mode) {
case VOL_NORMAL:
return (uint)_masterPercent;
case VOL_QUIET:
return (uint)(_masterPercent * 30 / 100);
case VOL_VERY_QUIET:
return (uint)(_masterPercent * 15 / 100);
default:
return 0;
}
}
/*------------------------------------------------------------------------*/
void QSoundManagerSounds::add(CWaveFile *waveFile, int iChannel, CEndTalkerFn endFn, TTtalker *talker) {
push_back(new QSoundManagerSound(waveFile, iChannel, endFn, talker));
}
void QSoundManagerSounds::flushChannel(int iChannel) {
for (iterator i = begin(); i != end(); ++i) {
QSoundManagerSound *item = *i;
if (item->_iChannel == iChannel) {
if (item->_endFn)
item->_endFn(item->_talker);
remove(item);
delete item;
break;
}
}
}
void QSoundManagerSounds::flushChannel(CWaveFile *waveFile, int iChannel) {
for (iterator i = begin(); i != end(); ++i) {
QSoundManagerSound *item = *i;
if (item->_waveFile->isLoaded() && item->_iChannel == iChannel) {
if (item->_endFn)
item->_endFn(item->_talker);
remove(item);
delete item;
break;
}
}
}
bool QSoundManagerSounds::contains(const CWaveFile *waveFile) const {
for (const_iterator i = begin(); i != end(); ++i) {
const QSoundManagerSound *item = *i;
if (item->_waveFile == waveFile)
return true;
}
return false;
}
/*------------------------------------------------------------------------*/
void QSoundManager::Slot::clear() {
_waveFile = nullptr;
_isTimed = false;
_ticks = 0;
_channel = -1;
_handle = 0;
_positioningMode = POSMODE_NONE;
}
/*------------------------------------------------------------------------*/
QSoundManager::QSoundManager(Audio::Mixer *mixer) : CSoundManager(), QMixer(mixer),
_field18(0), _field1C(0) {
_slots.resize(48);
Common::fill(&_channelsVolume[0], &_channelsVolume[16], 0);
Common::fill(&_channelsMode[0], &_channelsMode[16], 0);
qsWaveMixInitEx(QMIXCONFIG(AUDIO_SAMPLING_RATE, CHANNELS_COUNT, LATENCY));
qsWaveMixActivate(true);
qsWaveMixOpenChannel(0, QMIX_OPENALL);
}
QSoundManager::~QSoundManager() {
// Close down the mixer
qsWaveMixCloseSession();
}
CWaveFile *QSoundManager::loadSound(const CString &name) {
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified sound
if (!waveFile->loadSound(name)) {
delete waveFile;
return nullptr;
}
return waveFile;
}
CWaveFile *QSoundManager::loadSpeech(CDialogueFile *dialogueFile, int speechId) {
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified sound
if (!waveFile->loadSpeech(dialogueFile, speechId)) {
delete waveFile;
return nullptr;
}
return waveFile;
}
CWaveFile *QSoundManager::loadMusic(const CString &name) {
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified sound
if (!waveFile->loadMusic(name)) {
delete waveFile;
return nullptr;
}
return waveFile;
}
CWaveFile *QSoundManager::loadMusic(CAudioBuffer *buffer, DisposeAfterUse::Flag disposeAfterUse) {
CWaveFile *waveFile = new CWaveFile(_mixer);
// Try to load the specified audio buffer
if (!waveFile->loadMusic(buffer, disposeAfterUse)) {
delete waveFile;
return nullptr;
}
return waveFile;
}
int QSoundManager::playSound(CWaveFile &waveFile, CProximity &prox) {
int channel = -1;
uint flags = QMIX_CLEARQUEUE;
if (prox._priorSoundHandle >= 1) {
// This sound should only be started after a prior one finishes,
// so scan the slots for the specified sound
for (uint idx = 0; idx < _slots.size(); ++idx) {
if (_slots[idx]._handle == prox._priorSoundHandle) {
channel = _slots[idx]._channel;
flags = QMIX_QUEUEWAVE;
break;
}
}
}
if (channel >= 0 || (channel = resetChannel(prox._channelMode)) != -1) {
return playWave(&waveFile, channel, flags, prox);
}
return 0;
}
void QSoundManager::stopSound(int handle) {
resetChannel(10);
for (uint idx = 0; idx < _slots.size(); ++idx) {
Slot &slot = _slots[idx];
if (slot._handle == handle) {
qsWaveMixFlushChannel(slot._channel);
_sounds.flushChannel(slot._channel);
resetChannel(10);
}
}
}
void QSoundManager::stopChannel(int channel) {
int endChannel;
switch (channel) {
case 0:
case 3:
endChannel = channel + 3;
break;
case 6:
endChannel = 10;
break;
case 10:
endChannel = 48;
break;
default:
return;
}
for (; channel < endChannel; ++channel) {
qsWaveMixFlushChannel(channel);
_sounds.flushChannel(channel);
}
}
void QSoundManager::setCanFree(int handle) {
for (uint idx = 0; idx < _slots.size(); ++idx) {
if (_slots[idx]._handle == handle)
_slots[idx]._isTimed = true;
}
}
void QSoundManager::stopAllChannels() {
qsWaveMixFlushChannel(0, QMIX_OPENALL);
for (int idx = 0; idx < 16; ++idx)
_sounds.flushChannel(idx);
resetChannel(10);
}
int QSoundManager::resetChannel(int iChannel) {
int newChannel = -1;
int channelStart = 10;
int channelEnd = 16;
if (iChannel != 10) {
qsWaveMixFlushChannel(iChannel);
_sounds.flushChannel(iChannel);
channelStart = iChannel;
channelEnd = iChannel + 1;
} else {
uint ticks = g_vm->_events->getTicksCount();
for (uint idx = 0; idx < _slots.size(); ++idx) {
Slot &slot = _slots[idx];
if (slot._isTimed && slot._ticks && ticks > slot._ticks) {
qsWaveMixFlushChannel(slot._channel);
_sounds.flushChannel(slot._channel);
}
}
}
for (iChannel = channelStart; iChannel < channelEnd; ++iChannel) {
if (qsWaveMixIsChannelDone(iChannel)) {
// Scan through the slots, and reset any slot using the channel
for (uint idx = 0; idx < _slots.size(); ++idx) {
Slot &slot = _slots[idx];
if (slot._channel == iChannel)
slot.clear();
}
// Use the empty channel
newChannel = iChannel;
}
}
return newChannel;
}
void QSoundManager::setVolume(int handle, uint volume, uint seconds) {
for (uint idx = 0; idx < _slots.size(); ++idx) {
Slot &slot = _slots[idx];
if (slot._handle == handle) {
assert(slot._channel >= 0);
_channelsVolume[slot._channel] = volume;
updateVolume(slot._channel, seconds * 1000);
if (!volume) {
uint ticks = g_vm->_events->getTicksCount() + seconds * 1000;
if (!slot._ticks || ticks >= slot._ticks)
slot._ticks = ticks;
} else {
slot._ticks = 0;
}
break;
}
}
}
void QSoundManager::setVectorPosition(int handle, double x, double y, double z, uint panRate) {
for (uint idx = 0; idx < _slots.size(); ++idx) {
Slot &slot = _slots[idx];
if (slot._handle == handle) {
qsWaveMixSetPanRate(slot._channel, QMIX_USEONCE, panRate);
qsWaveMixSetSourcePosition(slot._channel, QMIX_USEONCE, QSVECTOR(x, y, z));
break;
}
}
}
void QSoundManager::setPolarPosition(int handle, double range, double azimuth, double elevation, uint panRate) {
for (uint idx = 0; idx < _slots.size(); ++idx) {
Slot &slot = _slots[idx];
if (slot._handle == handle) {
qsWaveMixSetPanRate(slot._channel, QMIX_USEONCE, panRate);
qsWaveMixSetPolarPosition(slot._channel, QMIX_USEONCE,
QSPOLAR(azimuth, range, elevation));
break;
}
}
}
bool QSoundManager::isActive(int handle) {
resetChannel(10);
for (uint idx = 0; idx < _slots.size(); ++idx) {
if (_slots[idx]._handle == handle)
return true;
}
return false;
}
bool QSoundManager::isActive(const CWaveFile *waveFile) {
return _sounds.contains(waveFile);
}
void QSoundManager::waveMixPump() {
qsWaveMixPump();
}
uint QSoundManager::getLatency() const {
return LATENCY;
}
void QSoundManager::setMusicPercent(double percent) {
_musicPercent = percent;
updateVolumes();
}
void QSoundManager::setSpeechPercent(double percent) {
_speechPercent = percent;
updateVolumes();
}
void QSoundManager::setMasterPercent(double percent) {
_masterPercent = percent;
updateVolumes();
}
void QSoundManager::setParrotPercent(double percent) {
_parrotPercent = percent;
}
void QSoundManager::setListenerPosition(double posX, double posY, double posZ,
double directionX, double directionY, double directionZ, bool stopSounds) {
if (stopSounds) {
// Stop any running sounds
for (uint idx = 0; idx < _slots.size(); ++idx) {
if (_slots[idx]._positioningMode != 0)
stopSound(_slots[idx]._handle);
}
}
qsWaveMixSetListenerPosition(QSVECTOR(posX, posY, posZ));
qsWaveMixSetListenerOrientation(QSVECTOR(directionX, directionY, directionZ),
QSVECTOR(0.0, 0.0, -1.0));
}
int QSoundManager::playWave(CWaveFile *waveFile, int iChannel, uint flags, CProximity &prox) {
if (!waveFile || !waveFile->isLoaded())
return 0;
prox._channelVolume = CLIP(prox._channelVolume, 0, 100);
prox._balance = CLIP(prox._balance, -100, 100);
int slotIndex = findFreeSlot();
if (slotIndex == -1)
return -1;
// Set the volume
setChannelVolume(iChannel, prox._channelVolume, prox._channelMode);
switch (prox._positioningMode) {
case POSMODE_POLAR:
qsWaveMixSetPolarPosition(iChannel, 8, QSPOLAR(prox._azimuth, prox._range, prox._elevation));
qsWaveMixEnableChannel(iChannel, QMIX_CHANNEL_ELEVATION, true);
qsWaveMixSetDistanceMapping(iChannel, 8, QMIX_DISTANCES(5.0, 3.0, 1.0));
break;
case POSMODE_VECTOR:
qsWaveMixSetSourcePosition(iChannel, 8, QSVECTOR(prox._posX, prox._posY, prox._posZ));
qsWaveMixEnableChannel(iChannel, QMIX_CHANNEL_ELEVATION, true);
qsWaveMixSetDistanceMapping(iChannel, 8, QMIX_DISTANCES(5.0, 3.0, 1.0));
break;
default:
qsWaveMixEnableChannel(iChannel, QMIX_CHANNEL_ELEVATION, true);
qsWaveMixSetPolarPosition(iChannel, 8, QSPOLAR(0.0, 1.0, 0.0));
break;
}
if (prox._frequencyMultiplier || prox._frequencyAdjust != 1.875) {
uint freq = (uint)(waveFile->getFrequency() * prox._frequencyMultiplier);
qsWaveMixSetFrequency(iChannel, 8, freq);
}
_sounds.add(waveFile, iChannel, prox._endTalkerFn, prox._talker);
QMIXPLAYPARAMS playParams;
playParams.callback = soundFinished;
playParams.dwUser = this;
if (!qsWaveMixPlayEx(iChannel, flags, waveFile, prox._repeated ? -1 : 0, playParams)) {
Slot &slot = _slots[slotIndex];
slot._handle = _handleCtr++;
slot._channel = iChannel;
slot._waveFile = waveFile;
slot._positioningMode = prox._positioningMode;
return slot._handle;
} else {
_sounds.flushChannel(waveFile, iChannel);
if (prox._disposeAfterUse == DisposeAfterUse::YES)
delete waveFile;
return 0;
}
}
void QSoundManager::soundFreed(Audio::SoundHandle &handle) {
qsWaveMixFreeWave(handle);
}
void QSoundManager::updateVolume(int channel, uint panRate) {
double volume = _channelsVolume[channel] * 327;
switch (_channelsMode[channel]) {
case 0:
case 1:
case 2:
volume = (_speechPercent * volume) / 100;
break;
case 3:
case 4:
case 5:
volume = (75 * volume) / 100;
break;
case 6:
case 7:
case 8:
case 9:
volume = (_masterPercent * volume) / 100;
break;
default:
break;
}
volume = (_musicPercent * volume) / 100;
qsWaveMixSetPanRate(channel, 0, panRate);
qsWaveMixSetVolume(channel, 0, (uint)volume);
}
void QSoundManager::updateVolumes() {
for (uint idx = 0; idx < CHANNELS_COUNT; ++idx)
updateVolume(idx, 250);
}
void QSoundManager::soundFinished(int iChannel, CWaveFile *waveFile, void *soundManager) {
static_cast<QSoundManager *>(soundManager)->_sounds.flushChannel(waveFile, iChannel);
}
int QSoundManager::findFreeSlot() {
for (uint idx = 0; idx < _slots.size(); ++idx) {
if (!_slots[idx]._waveFile)
return idx;
}
return -1;
}
void QSoundManager::setChannelVolume(int iChannel, uint volume, uint mode) {
_channelsVolume[iChannel] = volume;
_channelsMode[iChannel] = mode;
updateVolume(iChannel, 250);
}
} // End of namespace Titanic z
|