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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
// MIDI and digital music class
#include "saga/saga.h"
#include "saga/resource.h"
#include "saga/music.h"
#include "audio/adlib_ms.h"
#include "audio/audiostream.h"
#include "audio/mididrv.h"
#include "audio/midiparser.h"
#include "audio/midiparser_qt.h"
#include "audio/miles.h"
#include "audio/decoders/flac.h"
#include "audio/decoders/mp3.h"
#include "audio/decoders/raw.h"
#include "audio/decoders/vorbis.h"
#include "audio/mods/mod_xm_s3m.h"
#include "audio/softsynth/fmtowns_pc98/towns_pc98_driver.h"
#include "common/config-manager.h"
#include "common/file.h"
#include "common/substream.h"
#include "common/translation.h"
#include "gui/message.h"
namespace Saga {
const uint8 Music::MT32_GOODBYE_MSG[] = { 0x47, 0x6F, 0x6F, 0x64, 0x62, 0x79, 0x65, 0x21, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
Music::Music(SagaEngine *vm, Audio::Mixer *mixer) : _vm(vm), _mixer(mixer), _parser(nullptr), _driver(nullptr), _driverPC98(nullptr), _musicContext(nullptr) {
_currentVolume = 0;
_currentMusicBuffer = nullptr;
if (_vm->getPlatform() == Common::kPlatformAmiga) {
_musicType = _driverType = MT_AMIGA;
} else if (_vm->getPlatform() == Common::kPlatformPC98) {
_musicType = _driverType = MT_PC98;
_driverPC98 = new TownsPC98_AudioDriver(mixer, PC98AudioPluginDriver::kType86);
_driverPC98->init();
} else {
_musicType = (_vm->getGameId() == GID_ITE && _vm->getPlatform() == Common::kPlatformDOS ? MT_MT32 : MT_GM);
MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | (_musicType == MT_MT32 ? MDT_PREFER_MT32 : MDT_PREFER_GM));
_driverType = MidiDriver::getMusicType(dev);
if (_driverType == MT_GM && ConfMan.getBool("native_mt32"))
_driverType = MT_MT32;
switch (_driverType) {
case MT_ADLIB:
if (_vm->getPlatform() == Common::kPlatformDOS) {
const char *opl2InstDefFilename;
const char *opl3InstDefFilename;
if (_vm->getGameId() == GID_ITE) {
opl2InstDefFilename = "INSTR.AD";
opl3InstDefFilename = "INSTR.OPL";
} else {
// IHNM
opl2InstDefFilename = "SAMPLE.AD";
opl3InstDefFilename = "SAMPLE.OPL";
}
if (Common::File::exists(opl2InstDefFilename) && Common::File::exists(opl3InstDefFilename)) {
_driver = (MidiDriver_Multisource *)Audio::MidiDriver_Miles_AdLib_create(opl2InstDefFilename, opl3InstDefFilename);
_driver->property(MidiDriver::PROP_MILES_VERSION, _vm->getGameId() == GID_ITE ?
Audio::MILES_VERSION_2 : Audio::MILES_VERSION_3);
} else {
// WORKAROUND The GOG version of IHNM is missing the AdLib
// instrument definition files. In this case we fall back
// to the regular AdLib driver, which has a built-in set of
// instrument definitions.
// We cannot distinguish between this GOG version and the
// case where the user has a physical version of the game,
// but has forgotten to copy the instrument definition
// files. So we show a warning that these files are missing.
GUI::MessageDialog dialog(
Common::U32String::format(
_("Could not find AdLib instrument definition files\n"
"%s and %s. Without these files,\n"
"the music will not sound the same as the original game."),
opl2InstDefFilename, opl3InstDefFilename),
_("OK"));
dialog.runModal();
OPL::Config::OplType oplType =
MidiDriver_ADLIB_Multisource::detectOplType(OPL::Config::kOpl3) ? OPL::Config::kOpl3 : OPL::Config::kOpl2;
_driver = new MidiDriver_ADLIB_Multisource(oplType);
}
} else {
OPL::Config::OplType oplType =
MidiDriver_ADLIB_Multisource::detectOplType(OPL::Config::kOpl3) ? OPL::Config::kOpl3 : OPL::Config::kOpl2;
_driver = new MidiDriver_ADLIB_Multisource(oplType);
}
break;
case MT_MT32:
case MT_GM:
if (_vm->getPlatform() == Common::kPlatformDOS) {
_driver = Audio::MidiDriver_Miles_MIDI_create(_musicType, "");
_driver->property(MidiDriver::PROP_MILES_VERSION, _vm->getGameId() == GID_ITE ?
Audio::MILES_VERSION_2 : Audio::MILES_VERSION_3);
} else {
_driver = new MidiDriver_MT32GM(_musicType);
}
break;
default:
_driver = new MidiDriver_NULL_Multisource();
break;
}
if (_driver) {
_driver->property(MidiDriver::PROP_USER_VOLUME_SCALING, true);
if (_driver->open() != 0)
error("Failed to open MIDI driver.");
_driver->setTimerCallback(this, &timerCallback);
_driver->setSourceNeutralVolume(255);
}
}
_digitalMusicContext = _vm->_resource->getContext(GAME_DIGITALMUSICFILE);
if (_driverType != MT_ADLIB)
_musicContext = _vm->_resource->getContext(GAME_MUSICFILE_GM);
if (!_musicContext)
_musicContext = _vm->_resource->getContext(GAME_MUSICFILE_FM);
if (!_musicContext) {
if (_vm->getGameId() == GID_ITE) {
_musicContext = _vm->_resource->getContext(GAME_RESOURCEFILE);
} else if (_vm->getGameId() == GID_IHNM) {
// TODO If program flow gets here, this getContext call previously
// returned null...
_musicContext = _vm->_resource->getContext(GAME_MUSICFILE_FM);
}
}
_trackNumber = 0;
_userVolume = 0;
_userMute = false;
_targetVolume = 0;
_currentVolumePercent = 100;
_digitalMusic = false;
}
Music::~Music() {
_vm->getTimerManager()->removeTimerProc(&musicVolumeGaugeCallback);
_mixer->stopHandle(_musicHandle);
if (_parser) {
_parser->stopPlaying();
delete _parser;
}
if (_driver) {
_driver->setTimerCallback(nullptr, nullptr);
_driver->close();
delete _driver;
}
if (_driverPC98) {
_driverPC98->reset();
delete _driverPC98;
}
}
void Music::close() {
if (_parser)
_parser->stopPlaying();
if (_vm->getGameId() == GID_ITE && _vm->getPlatform() == Common::kPlatformDOS && _driver) {
MidiDriver_MT32GM *mt32Driver = dynamic_cast<MidiDriver_MT32GM *>(_driver);
if (mt32Driver)
mt32Driver->sysExMT32(MT32_GOODBYE_MSG, MidiDriver_MT32GM::MT32_DISPLAY_NUM_CHARS,
MidiDriver_MT32GM::MT32_DISPLAY_MEMORY_ADDRESS, false, false);
}
}
void Music::musicVolumeGaugeCallback(void *refCon) {
((Music *)refCon)->musicVolumeGauge();
}
void Music::musicVolumeGauge() {
// CHECKME: This is potentially called from a different thread because it is
// called from a timer callback. However, it does not seem to take any
// precautions to avoid race conditions.
int volume;
_currentVolumePercent += 10;
if (_currentVolume - _targetVolume > 0) { // Volume decrease
volume = _targetVolume + (_currentVolume - _targetVolume) * (100 - _currentVolumePercent) / 100;
} else {
volume = _currentVolume + (_targetVolume - _currentVolume) * _currentVolumePercent / 100;
}
if (volume < 0)
volume = 1;
int scaledVolume;
if (_userMute) {
scaledVolume = 0;
} else {
scaledVolume = (volume * _userVolume) >> 8;
}
_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, scaledVolume);
if (_driverPC98)
_driverPC98->setMusicVolume(scaledVolume);
if (_currentVolumePercent == 100) {
_vm->getTimerManager()->removeTimerProc(&musicVolumeGaugeCallback);
_currentVolume = _targetVolume;
}
}
void Music::setVolume(int volume, int time) {
_targetVolume = volume;
if (volume == -1) // Set Full volume
volume = 255;
if (time == 1) {
if (_driver) {
if (_driver->isFading(0))
_driver->abortFade(0, MidiDriver_Multisource::FADE_ABORT_TYPE_CURRENT_VOLUME);
_driver->setSourceVolume(0, volume);
}
_currentVolumePercent = 100;
_vm->getTimerManager()->removeTimerProc(&musicVolumeGaugeCallback);
int scaledVolume;
if (_userMute) {
scaledVolume = 0;
} else {
scaledVolume = (volume * _userVolume) >> 8;
}
_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, scaledVolume);
if (_driverPC98)
_driverPC98->setMusicVolume(scaledVolume);
_currentVolume = volume;
return;
}
if (_driver)
_driver->startFade(0, time * 3, volume);
_currentVolumePercent = 0;
_vm->getTimerManager()->installTimerProc(&musicVolumeGaugeCallback, time * 300L, this, "sagaMusicVolume");
}
void Music::resetVolume() {
// Abort a fade / gauge if active and set volume to max.
setVolume(255);
}
bool Music::isFading() {
bool isFading = false;
if (_driver)
isFading = _driver->isFading(0);
isFading = isFading || (_currentVolumePercent < 100);
return isFading;
}
bool Music::isPlaying() {
return _mixer->isSoundHandleActive(_musicHandle) || (_parser ? _parser->isPlaying() : false) || (_driverPC98 ? _driverPC98->musicPlaying() : false);
}
void Music::play(uint32 resourceId, MusicFlags flags) {
debug(2, "Music::play %d, %d", resourceId, flags);
if (isPlaying() && _trackNumber == resourceId)
return;
if (_vm->getFeatures() & GF_ITE_DOS_DEMO) {
warning("TODO: Music::play %d, %d for ITE DOS demo", resourceId, flags);
return;
}
_trackNumber = resourceId;
_mixer->stopHandle(_musicHandle);
if (_parser) {
_parser->unloadMusic();
delete _parser;
_parser = nullptr;
}
if (_driverPC98)
_driverPC98->reset();
resetVolume();
bool digital = playDigital(resourceId, flags);
if (!digital) {
// Load MIDI/XMI resource data
if (_vm->getGameId() == GID_ITE && _vm->getPlatform() == Common::Platform::kPlatformAmiga) {
playProtracker(resourceId, flags);
} else if (_vm->getGameId() == GID_IHNM && _vm->isMacResources()) {
// Load the external music file for Mac IHNM
playQuickTime(resourceId, flags);
} else {
playMidi(resourceId, flags);
}
}
}
bool Music::playDigital(uint32 resourceId, MusicFlags flags) {
Audio::SeekableAudioStream *audioStream = nullptr;
uint32 loopStart = 0;
int realTrackNumber = 0;
if (_vm->getGameId() == GID_ITE) {
if (resourceId != 13 && resourceId != 19)
flags = MUSIC_LOOP;
realTrackNumber = resourceId - 8;
} else if (_vm->getGameId() == GID_IHNM) {
realTrackNumber = resourceId + 1;
}
// Try to open standalone digital track
char trackName[2][16];
Common::sprintf_s(trackName[0], "track%d", realTrackNumber);
Common::sprintf_s(trackName[1], "track%02d", realTrackNumber);
Audio::SeekableAudioStream *stream = nullptr;
for (int i = 0; i < 2; ++i) {
stream = Audio::SeekableAudioStream::openStreamFile(trackName[i]);
if (stream) {
_mixer->playStream(Audio::Mixer::kMusicSoundType, &_musicHandle,
Audio::makeLoopingAudioStream(stream, (flags == MUSIC_LOOP) ? 0 : 1));
_digitalMusic = true;
return true;
}
}
if (_vm->getGameId() == GID_ITE) {
if (resourceId >= 9 && resourceId <= 34) {
if (_digitalMusicContext != nullptr) {
loopStart = 0;
// Fix ITE sunstatm/sunspot score
if (resourceId == MUSIC_SUNSPOT)
loopStart = 18727;
// Digital music
ResourceData *resData = _digitalMusicContext->getResourceData(resourceId - 9);
Common::SeekableReadStream *musicFile = _digitalMusicContext->getFile(resData);
int offs = (_digitalMusicContext->isCompressed()) ? 9 : 0;
Common::SeekableSubReadStream *musicStream = new Common::SeekableSubReadStream(musicFile,
(uint32)resData->offset + offs, (uint32)resData->offset + resData->size - offs);
if (!_digitalMusicContext->isCompressed()) {
byte musicFlags = Audio::FLAG_STEREO |
Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN;
if (_vm->isBigEndian() || (_vm->getFeatures() & GF_SOME_MAC_RESOURCES))
musicFlags &= ~Audio::FLAG_LITTLE_ENDIAN;
// The newer ITE Mac demo version contains a music file, but it has mono music.
// This is the only music file that is about 7MB, whereas all the other ones
// are much larger. Thus, we use this simple heuristic to determine if we got
// mono music in the ITE demos or not.
if (!strcmp(_digitalMusicContext->fileName(), "musicd.rsc") &&
_digitalMusicContext->fileSize() < 8000000)
musicFlags &= ~Audio::FLAG_STEREO;
audioStream = Audio::makeRawStream(musicStream, 11025, musicFlags, DisposeAfterUse::YES);
} else {
// Read compressed header to determine compression type
musicFile->seek((uint32)resData->offset, SEEK_SET);
byte identifier = musicFile->readByte();
if (identifier == 0) { // MP3
#ifdef USE_MAD
audioStream = Audio::makeMP3Stream(musicStream, DisposeAfterUse::YES);
#endif
} else if (identifier == 1) { // OGG
#ifdef USE_VORBIS
audioStream = Audio::makeVorbisStream(musicStream, DisposeAfterUse::YES);
#endif
} else if (identifier == 2) { // FLAC
#ifdef USE_FLAC
audioStream = Audio::makeFLACStream(musicStream, DisposeAfterUse::YES);
#endif
}
}
if (!audioStream)
delete musicStream;
}
}
}
if (audioStream) {
debug(2, "Playing digitized music");
if (loopStart) {
_mixer->playStream(Audio::Mixer::kMusicSoundType, &_musicHandle,
new Audio::SubLoopingAudioStream(audioStream,
(flags == MUSIC_LOOP ? 0 : 1),
Audio::Timestamp(0, loopStart, audioStream->getRate()),
audioStream->getLength()));
} else {
_mixer->playStream(Audio::Mixer::kMusicSoundType, &_musicHandle,
Audio::makeLoopingAudioStream(audioStream, (flags == MUSIC_LOOP ? 0 : 1)));
}
_digitalMusic = true;
return true;
}
return false;
}
void Music::playQuickTime(uint32 resourceId, MusicFlags flags) {
// IHNM Mac uses QuickTime MIDI
_parser = MidiParser::createParser_QT();
_parser->setMidiDriver(_driver);
_parser->setTimerRate(_driver->getBaseTempo());
_parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
_parser->property(MidiParser::mpSendSustainOffOnNotesOff, 1);
// Handle music looping
_parser->property(MidiParser::mpAutoLoop, flags & MUSIC_LOOP);
Common::Path musicName(Common::String::format("Music/Music%02x", resourceId));
if (!((MidiParser_QT *)_parser)->loadFromContainerFile(musicName))
error("Music::playQuickTime(): Failed to load file '%s'", musicName.toString().c_str());
_parser->setTrack(0);
}
void Music::playProtracker(uint32 resourceId, MusicFlags flags) {
ByteArray ba;
_vm->_resource->loadResource(_musicContext, resourceId, ba);
Common::MemoryReadStream ms(ba.getBuffer(), ba.size());
/* No reference to the 'stream' object is kept, so you can safely delete it after
invoking this factory. */
Audio::RewindableAudioStream *amigaModStream = Audio::makeModXmS3mStream(&ms, DisposeAfterUse::NO);
_mixer->playStream(Audio::Mixer::kMusicSoundType, &_musicHandle,
Audio::makeLoopingAudioStream(amigaModStream, (flags == MUSIC_LOOP ? 0 : 1)));
}
void Music::playMidi(uint32 resourceId, MusicFlags flags) {
if (_currentMusicBuffer == &_musicBuffer[1]) {
_currentMusicBuffer = &_musicBuffer[0];
} else {
_currentMusicBuffer = &_musicBuffer[1];
}
_vm->_resource->loadResource(_musicContext, resourceId, *_currentMusicBuffer);
if (_driverPC98) {
_driverPC98->loadMusicData(_currentMusicBuffer->data() + 4);
} else {
if (_currentMusicBuffer->size() < 4) {
error("Music::playMidi() wrong music resource size");
}
// Check if the game is using XMIDI or SMF music
if (!memcmp(_currentMusicBuffer->getBuffer(), "FORM", 4)) {
_parser = MidiParser::createParser_XMIDI(nullptr, nullptr, 0);
} else {
_parser = MidiParser::createParser_SMF(0);
}
_parser->setMidiDriver(_driver);
_parser->setTimerRate(_driver->getBaseTempo());
if (_vm->getGameId() == GID_IHNM) {
// IHNM XMIDI uses sustain and does not reset pitch bend at the
// start of a new track.
_parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
_parser->property(MidiParser::mpSendSustainOffOnNotesOff, 1);
}
// Handle music looping
_parser->property(MidiParser::mpAutoLoop, flags & MUSIC_LOOP);
if (!_parser->loadMusic(_currentMusicBuffer->getBuffer(), _currentMusicBuffer->size()))
error("Music::play() wrong music resource");
}
}
void Music::pause() {
if (_parser) {
_parser->pausePlaying();
} else if (_driverPC98) {
_driverPC98->pause();
}
}
void Music::resume() {
if (_parser) {
_parser->resumePlaying();
} else if (_driverPC98) {
_driverPC98->cont();
}
}
void Music::stop() {
if (_parser)
_parser->stopPlaying();
else if (_driverPC98)
_driverPC98->reset();
}
void Music::syncSoundSettings() {
if (_driver)
_driver->syncSoundSettings();
_userVolume = ConfMan.getInt("music_volume");
_userMute = ConfMan.hasKey("mute") && ConfMan.getBool("mute");
setVolume(_currentVolume);
}
void Music::onTimer() {
if (_parser)
_parser->onTimer();
}
void Music::timerCallback(void *data) {
((Music *)data)->onTimer();
}
} // End of namespace Saga
|