diff options
author | Sven Hesse | 2007-01-23 19:40:28 +0000 |
---|---|---|
committer | Sven Hesse | 2007-01-23 19:40:28 +0000 |
commit | a8c6b02a9c77364ed83159082f94bdd9dcd54547 (patch) | |
tree | df44d74623cb0e38fad9adcce0eb993f0e4627f8 | |
parent | ac6bb86c96a82a9a7e3b9fdc409228752c7af559 (diff) | |
download | scummvm-rg350-a8c6b02a9c77364ed83159082f94bdd9dcd54547.tar.gz scummvm-rg350-a8c6b02a9c77364ed83159082f94bdd9dcd54547.tar.bz2 scummvm-rg350-a8c6b02a9c77364ed83159082f94bdd9dcd54547.zip |
Added support for Infogrames/RobHubbard2 (amiga) music
svn-id: r25160
-rw-r--r-- | engines/gob/game.cpp | 4 | ||||
-rw-r--r-- | engines/gob/game_v1.cpp | 2 | ||||
-rw-r--r-- | engines/gob/game_v2.cpp | 2 | ||||
-rw-r--r-- | engines/gob/gob.cpp | 26 | ||||
-rw-r--r-- | engines/gob/gob.h | 11 | ||||
-rw-r--r-- | engines/gob/inter_v1.cpp | 8 | ||||
-rw-r--r-- | engines/gob/inter_v2.cpp | 38 | ||||
-rw-r--r-- | engines/gob/music.cpp | 627 | ||||
-rw-r--r-- | engines/gob/music.h | 200 |
9 files changed, 848 insertions, 70 deletions
diff --git a/engines/gob/game.cpp b/engines/gob/game.cpp index 5424a931b4..2ddec2fa56 100644 --- a/engines/gob/game.cpp +++ b/engines/gob/game.cpp @@ -348,8 +348,8 @@ void Game::freeSoundSlot(int16 slot) { return; if (_soundADL[slot]) { - if (_vm->_music->getIndex() == slot) - _vm->_music->stopPlay(); + if (_vm->_adlib->getIndex() == slot) + _vm->_adlib->stopPlay(); if (_soundFromExt[slot] == 1) { delete[] ((char *) _soundSamples[slot]); _soundFromExt[slot] = 0; diff --git a/engines/gob/game_v1.cpp b/engines/gob/game_v1.cpp index 051baec1b7..80d6709da9 100644 --- a/engines/gob/game_v1.cpp +++ b/engines/gob/game_v1.cpp @@ -82,7 +82,7 @@ void Game_v1::playTot(int16 skipPlay) { } if (_vm->_features & GF_MAC) - _vm->_music->stopPlay(); + _vm->_adlib->stopPlay(); else _vm->_cdrom->stopPlaying(); _vm->_draw->animateCursor(4); diff --git a/engines/gob/game_v2.cpp b/engines/gob/game_v2.cpp index 13672b473e..2ecddfdb70 100644 --- a/engines/gob/game_v2.cpp +++ b/engines/gob/game_v2.cpp @@ -74,7 +74,7 @@ void Game_v2::playTot(int16 skipPlay) { if (_vm->_global->_inter_variables != 0) _vm->_draw->animateCursor(4); if (_vm->_features & GF_MAC) - _vm->_music->stopPlay(); + _vm->_adlib->stopPlay(); else _vm->_cdrom->stopPlaying(); diff --git a/engines/gob/gob.cpp b/engines/gob/gob.cpp index 2dbcfb0db7..c4d6242d7f 100644 --- a/engines/gob/gob.cpp +++ b/engines/gob/gob.cpp @@ -84,8 +84,8 @@ static const GameSettings gob_games[] = { // CD 1.02 version. Multilingual {"gob1", "Gobliiins (CD)", GF_GOB1 | GF_CD, Common::UNK_LANG, "8bd873137b6831c896ee8ad217a6a398", "intro"}, - {"gob1", "Gobliiins (Amiga)", GF_GOB1, Common::UNK_LANG, "c65e9cc8ba23a38456242e1f2b1caad4", "intro"}, - {"gob1", "Gobliiins (Amiga)", GF_GOB1, Common::UNK_LANG, "972f22c6ff8144a6636423f0354ca549", "intro"}, + {"gob1", "Gobliiins (Amiga)", GF_GOB1 | GF_AMIGA, Common::UNK_LANG, "c65e9cc8ba23a38456242e1f2b1caad4", "intro"}, + {"gob1", "Gobliiins (Amiga)", GF_GOB1 | GF_AMIGA, Common::UNK_LANG, "972f22c6ff8144a6636423f0354ca549", "intro"}, {"gob1", "Gobliiins (Interactive Demo)", GF_GOB1, Common::UNK_LANG, "e72bd1e3828c7dec4c8a3e58c48bdfdb", "intro"}, @@ -97,6 +97,8 @@ static const GameSettings gob_games[] = { {"gob2", "Gobliins 2 (DOS Deu)", GF_GOB2, Common::DE_DEU, "a13892cdf4badda85a6f6fb47603a128", "intro"}, {"gob2", "Gobliins 2 (DOS Ru)", GF_GOB2, Common::RU_RUS, "cd3e1df8b273636ee32e34b7064f50e8", "intro"}, + {"gob2", "Gobliins 2 (Amiga Deu)", GF_GOB2 | GF_AMIGA, Common::DE_DEU, "d28b9e9b41f31acfa58dcd12406c7b2c", "intro"}, + // Supplied by blackwhiteeagle in bug report #1605235 {"gob2", "Gobliins 2 (DOS Deu)", GF_GOB2, Common::DE_DEU, "3e4e7db0d201587dd2df4003b2993ef6", "intro"}, @@ -107,7 +109,7 @@ static const GameSettings gob_games[] = { {"gob2", "Gobliins 2 (Demo)", GF_GOB2, Common::UNK_LANG, "8b1c98ff2ab2e14f47a1b891e9b92217", "usa"}, {"gob2", "Gobliins 2 (Interactive Demo)", GF_GOB2, Common::UNK_LANG, "cf1c95b2939bd8ff58a25c756cb6125e", "intro"}, - {"gob2", "Gobliins 2 (Amiga Interactive Demo)", GF_GOB2, Common::UNK_LANG, "4b278c2678ea01383fd5ca114d947eea", "intro"}, + {"gob2", "Gobliins 2 (Amiga Interactive Demo)", GF_GOB2 | GF_AMIGA, Common::UNK_LANG, "4b278c2678ea01383fd5ca114d947eea", "intro"}, {"gob2", "Ween: The Prohpecy", GF_GOB2, Common::UNK_LANG, "2bb8878a8042244dd2b96ff682381baa", "intro"}, {"gob2", "Ween: The Prophecy (Fr)", GF_GOB2, Common::UNK_LANG, "4b10525a3782aa7ecd9d833b5c1d308b", "intro"}, @@ -195,7 +197,7 @@ GobEngine::GobEngine(OSystem * syst, uint32 features, Common::Language lang, Common::addSpecialDebugLevel(kDebugFuncOp, "FuncOpcodes", "Script FuncOpcodes debug level"); Common::addSpecialDebugLevel(kDebugDrawOp, "DrawOpcodes", "Script DrawOpcodes debug level"); Common::addSpecialDebugLevel(kDebugGobOp, "GoblinOpcodes", "Script GoblinOpcodes debug level"); - Common::addSpecialDebugLevel(kDebugMusic, "Music", "CD and adlib music debug level"); + Common::addSpecialDebugLevel(kDebugMusic, "Music", "CD, Adlib and Infogrames music debug level"); Common::addSpecialDebugLevel(kDebugParser, "Parser", "Parser debug level"); Common::addSpecialDebugLevel(kDebugGameFlow, "Gameflow", "Gameflow debug level"); Common::addSpecialDebugLevel(kDebugFileIO, "FileIO", "File Input/Output debug level"); @@ -222,7 +224,12 @@ GobEngine::~GobEngine() { delete _scenery; delete _gtimer; delete _util; - delete _music; + if (_adlib) + delete _adlib; + if (_infogrames) { + _infogrames->unload(true); + delete _infogrames; + } delete _video; delete[] _startTot; delete[] _startTot0; @@ -680,6 +687,8 @@ uint32 GobEngine::readDataEndian(Common::InSaveFile &in, char *varBuf, byte *siz } int GobEngine::init() { + _infogrames = 0; + _adlib = 0; _snd = new Snd(this); _global = new Global(this); _anim = new Anim(); @@ -715,11 +724,12 @@ int GobEngine::init() { } else error("GobEngine::init(): Unknown version of game engine"); - if ((_features & Gob::GF_MAC) || (_features & Gob::GF_GOB1) || (_features & Gob::GF_GOB2)) { + if (!(_features & Gob::GF_AMIGA) && + ((_features & Gob::GF_MAC) || (_features & Gob::GF_GOB1) || (_features & Gob::GF_GOB2))) { if (MidiDriver::parseMusicDriver(ConfMan.get("music_driver")) == MD_NULL) - _music = new Music_Dummy(this); + _adlib = new Adlib_Dummy(this); else - _music = new Music(this); + _adlib = new Adlib(this); } _vm = this; diff --git a/engines/gob/gob.h b/engines/gob/gob.h index 7291853bd4..3c79908971 100644 --- a/engines/gob/gob.h +++ b/engines/gob/gob.h @@ -50,7 +50,8 @@ class Parse; class Scenery; class GTimer; class Util; -class Music; +class Adlib; +class Infogrames; /* #define VAR_OFFSET(offs) (*(uint32 *)(_vm->_global->_inter_variables + (offs))) @@ -92,14 +93,15 @@ enum { GF_WOODRUFF = 1 << 3, GF_CD = 1 << 4, GF_MAC = 1 << 5, - GF_EGA = 1 << 6 + GF_EGA = 1 << 6, + GF_AMIGA = 1 << 7 }; enum { kDebugFuncOp = 1 << 0, kDebugDrawOp = 1 << 1, kDebugGobOp = 1 << 2, - kDebugMusic = 1 << 3, // CD and adlib music + kDebugMusic = 1 << 3, // CD, Adlib and Infogrames music kDebugParser = 1 << 4, kDebugGameFlow = 1 << 5, kDebugFileIO = 1 << 6, @@ -165,7 +167,8 @@ public: GTimer *_gtimer; Util *_util; Inter *_inter; - Music *_music; + Adlib *_adlib; + Infogrames *_infogrames; GobEngine *_vm; void writeVarDebug(uint32 offs, uint32 v); diff --git a/engines/gob/inter_v1.cpp b/engines/gob/inter_v1.cpp index 0be5e54f52..5a9862157f 100644 --- a/engines/gob/inter_v1.cpp +++ b/engines/gob/inter_v1.cpp @@ -1210,7 +1210,7 @@ bool Inter_v1::o1_playComposition(char &cmdCount, int16 &counter, int16 &retFlag } bool Inter_v1::o1_stopSound(char &cmdCount, int16 &counter, int16 &retFlag) { - _vm->_music->stopPlay(); + _vm->_adlib->stopPlay(); _vm->_snd->stopSound(_vm->_parse->parseValExpr()); _soundEndTimeKey = 0; return false; @@ -1641,7 +1641,7 @@ void Inter_v1::o1_loadCurLayer(void) { void Inter_v1::o1_playCDTrack(void) { evalExpr(0); if (_vm->_features & GF_MAC) - _vm->_music->playTrack(_vm->_global->_inter_resStr); + _vm->_adlib->playTrack(_vm->_global->_inter_resStr); else // Used in gob1 CD _vm->_cdrom->startTrack(_vm->_global->_inter_resStr); @@ -1664,7 +1664,7 @@ void Inter_v1::o1_getCDTrackPos(void) { void Inter_v1::o1_stopCD(void) { if (_vm->_features & GF_MAC) - _vm->_music->stopPlay(); + _vm->_adlib->stopPlay(); else // Used in gob1 CD _vm->_cdrom->stopPlaying(); @@ -2588,7 +2588,7 @@ void Inter_v1::o1_drawObjects(int16 &extraData, int32 *retVarPtr, Goblin::Gob_Ob _vm->_goblin->drawObjects(); if (_vm->_features & GF_MAC) - _vm->_music->playBgMusic(); + _vm->_adlib->playBgMusic(); else if (_vm->_cdrom->getTrackPos() == -1) _vm->_cdrom->playBgMusic(); } diff --git a/engines/gob/inter_v2.cpp b/engines/gob/inter_v2.cpp index 3da95c0bcd..f0389dc201 100644 --- a/engines/gob/inter_v2.cpp +++ b/engines/gob/inter_v2.cpp @@ -1600,9 +1600,9 @@ bool Inter_v2::o2_playSound(char &cmdCount, int16 &counter, int16 &retFlag) { } // loc_E2F3 if (_vm->_game->_soundTypes[index] & 8) { - _vm->_music->loadFromMemory((byte *) _vm->_game->_soundSamples[index], index); - _vm->_music->setRepeating(repCount - 1); - _vm->_music->startPlay(); + _vm->_adlib->load((byte *) _vm->_game->_soundSamples[index], index); + _vm->_adlib->setRepeating(repCount - 1); + _vm->_adlib->startPlay(); } else { _vm->_snd->stopSound(0); _vm->_snd->playSample(_vm->_game->_soundSamples[index], repCount, frequency); @@ -1613,11 +1613,33 @@ bool Inter_v2::o2_playSound(char &cmdCount, int16 &counter, int16 &retFlag) { bool Inter_v2::o2_goblinFunc(char &cmdCount, int16 &counter, int16 &retFlag) { int16 cmd; + char fileName[20]; cmd = load16(); _vm->_global->_inter_execPtr += 2; - if (cmd == 100) { + switch (cmd) { + case 0: + case 1: + case 2: + case 3: + case 11: + case 13: + load16(); + break; + + case 10: + strcpy(fileName, GET_VAR_STR(load16())); + strcat(fileName, ".DUM"); + if (!_vm->_infogrames) { + _vm->_infogrames = new Infogrames(_vm, true); + _vm->_infogrames->loadInstruments("i1.ins"); + } + _vm->_infogrames->load(fileName); + _vm->_infogrames->startPlay(); + break; + + case 100: _vm->_goblin->_word_2F9C0 = VAR(load16()); _vm->_goblin->_word_2F9BE = VAR(load16()); _vm->_goblin->_dword_2F9B6 = load16(); @@ -1625,10 +1647,16 @@ bool Inter_v2::o2_goblinFunc(char &cmdCount, int16 &counter, int16 &retFlag) { _vm->_goblin->_word_2F9BC = VAR(load16()); _vm->_goblin->_word_2F9BA = VAR(load16()); _vm->_goblin->sub_19BD3(); - } else if (cmd != 101) { + break; + + case 101: + break; + + default: _vm->_global->_inter_execPtr -= 2; cmd = load16(); _vm->_global->_inter_execPtr += cmd << 1; + break; } return false; diff --git a/engines/gob/music.cpp b/engines/gob/music.cpp index 9bf6c658a9..0411f82761 100644 --- a/engines/gob/music.cpp +++ b/engines/gob/music.cpp @@ -32,7 +32,582 @@ namespace Gob { -const char *Music::_tracks[][2] = { +Paula::Paula(GobEngine *vm, bool stereo, int intFreq) : _vm(vm) { + _playing = false; + + _stereo = stereo; + _rate = _vm->_mixer->getOutputRate(); + _vm->_mixer->setupPremix(this, Audio::Mixer::kMusicSoundType); + _intFreq = intFreq; + + clearVoices(); + _voice[0].panning = 0; + _voice[1].panning = 1; + _voice[2].panning = 1; + _voice[3].panning = 0; + + if (_intFreq <= 0) + _intFreq = _rate; + + _curInt = _intFreq; + _end = true; +} + +Paula::~Paula() { + _vm->_mixer->setupPremix(0); +} + +void Paula::clearVoice(int voice) { + _voice[voice].data = 0; + _voice[voice].dataRepeat = 0; + _voice[voice].length = 0; + _voice[voice].lengthRepeat = 0; + _voice[voice].period = 0; + _voice[voice].volume = 0; + _voice[voice].offset = 0; +} + +int Paula::readBuffer(int16 *buffer, const int numSamples) { + int voice; + int samples; + int nSamples; + int sLen; + double frequency; + double rate; + double offset; + int16 *p; + int8 *data; + + memset(buffer, 0, numSamples * 2); + if (!_playing) + return numSamples; + + samples = _stereo ? numSamples / 2 : numSamples; + while (samples > 0) { + if (_curInt == _intFreq) { + interrupt(); + _curInt = 0; + } + nSamples = MIN(samples, _intFreq - _curInt); + for (voice = 0; voice < 4; voice++) { + if (!_voice[voice].data || (_voice[voice].period <= 0)) + continue; + + frequency = (7093789.2 / 2.0) / _voice[voice].period; + rate = frequency / _rate; + offset = _voice[voice].offset; + sLen = _voice[voice].length; + data = _voice[voice].data; + p = buffer; + + _voice[voice].volume &= 0x3F; + if ((_voice[voice].lengthRepeat > 2) && ((int)(offset + nSamples * rate) >= sLen)) { + int neededSamples = nSamples; + + int end = (int)((sLen - offset) / rate); + + for (int i = 0; i < end; i++) + mix(p, data[(int)(offset + rate * i)], voice); + + _voice[voice].length = sLen = _voice[voice].lengthRepeat; + _voice[voice].data = data = _voice[voice].dataRepeat; + _voice[voice].offset = offset = 0; + neededSamples -= end; + + while (neededSamples > 0) { + if (neededSamples >= (int) ((sLen - offset) / rate)) { + end = (int)((sLen - offset) / rate); + + for (int i = 0; i < end; i++) + mix(p, data[(int)(offset + rate * i)], voice); + + _voice[voice].data = data = _voice[voice].dataRepeat; + _voice[voice].length = sLen = _voice[voice].lengthRepeat; + _voice[voice].offset = offset = 0; + + neededSamples -= end; + } else { + for (int i = 0; i < neededSamples; i++) + mix(p, data[(int)(offset + rate * i)], voice); + _voice[voice].offset += rate * neededSamples; + if (ceil(_voice[voice].offset) >= sLen) { + _voice[voice].data = data = _voice[voice].dataRepeat; + _voice[voice].length = sLen = _voice[voice].lengthRepeat; + _voice[voice].offset = offset = 0; + } + neededSamples = 0; + } + } + } else { + if (offset < sLen) { + if ((int)(offset + nSamples * rate) >= sLen) { + // The end of the sample is the limiting factor + + int end = (int)((sLen - offset) / rate); + for (int i = 0; i < end; i++) + mix(p, data[(int)(offset + rate * i)], voice); + _voice[voice].offset = sLen; + } else { + // The requested number of samples is the limiting factor, not the sample + + for (int i = 0; i < nSamples; i++) + mix(p, data[(int)(offset + rate * i)], voice); + _voice[voice].offset += rate * nSamples; + } + } + } + } + buffer += _stereo ? nSamples * 2 : nSamples; + _curInt += nSamples; + samples -= nSamples; + } + return numSamples; +} + +Infogrames::Instruments::Instruments() { + int i; + + for (i = 0; i < 32; i++) { + _samples[i].data = 0; + _samples[i].dataRepeat = 0; + _samples[i].length = 0; + _samples[i].lengthRepeat = 0; + } + _count = 0; + _sampleData = 0; +} + +Infogrames::Instruments::~Instruments() { + if (_sampleData) + delete[] _sampleData; +} + +bool Infogrames::Instruments::load(Common::SeekableReadStream &ins) { + int i; + uint32 fsize; + uint32 offset[32]; + uint32 offsetRepeat[32]; + uint32 dataOffset; + + fsize = ins.readUint32BE(); + dataOffset = fsize; + for (i = 0; (i < 32) && !ins.eos(); i++) { + offset[i] = ins.readUint32BE(); + offsetRepeat[i] = ins.readUint32BE(); + if ((offset[i] > fsize) || (offsetRepeat[i] > fsize) || + (offset[i] < (ins.pos() + 4)) || (offsetRepeat[i] < (ins.pos() + 4))) { + // Definitely no real entry anymore + ins.seek(-8, SEEK_CUR); + break; + } + + dataOffset = MIN(dataOffset, MIN(offset[i], offsetRepeat[i])); + ins.skip(4); // Unknown + _samples[i].length = ins.readUint16BE() * 2; + _samples[i].lengthRepeat = ins.readUint16BE() * 2; + } + + if (dataOffset >= fsize) + return false; + + _count = i; + _sampleData = new int8[fsize - dataOffset]; + ins.seek(dataOffset + 4); + ins.read(_sampleData, fsize - dataOffset); + + for (i--; i >= 0; i--) { + _samples[i].data = _sampleData + (offset[i] - dataOffset); + _samples[i].dataRepeat = _sampleData + (offsetRepeat[i] - dataOffset); + } + + return true; +} + +const uint16 Infogrames::periods[] = + {0x6ACC, 0x64CC, 0x5F25, 0x59CE, 0x54C3, 0x5003, 0x4B86, 0x4747, 0x4346, + 0x3F8B, 0x3BF3, 0x3892, 0x3568, 0x3269, 0x2F93, 0x2CEA, 0x2A66, 0x2801, + 0x2566, 0x23A5, 0x21AF, 0x1FC4, 0x1DFE, 0x1C4E, 0x1ABC, 0x1936, 0x17CC, + 0x1676, 0x1533, 0x1401, 0x12E4, 0x11D5, 0x10D4, 0xFE3, 0xEFE, 0xE26, + 0xD5B, 0xC9B, 0xBE5, 0xB3B, 0xA9B, 0xA02, 0x972, 0x8E9, 0x869, 0x7F1, + 0x77F, 0x713, 0x6AD, 0x64D, 0x5F2, 0x59D, 0x54D, 0x500, 0x4B8, 0x475, + 0x435, 0x3F8, 0x3BF, 0x38A, 0x356, 0x326, 0x2F9, 0x2CF, 0x2A6, 0x280, + 0x25C, 0x23A, 0x21A, 0x1FC, 0x1E0, 0x1C5, 0x1AB, 0x193, 0x17D, 0x167, + 0x153, 0x140, 0x12E, 0x11D, 0x10D, 0xFE, 0xF0, 0xE2, 0xD6, 0xCA, 0xBE, + 0xB4, 0xAA, 0xA0, 0x97, 0x8F, 0x87, 0x7F, 0x78, 0x70, 0x60, 0x50, 0x40, + 0x30, 0x20, 0x10, 0, 0, 0x20, 0x2020, 0x2020, 0x2020, 0x2020, 0x3030, + 0x3030, 0x3020, 0x2020, 0x2020, 0x2020, 0x2020, 0x2020, 0x2020, 0x2020, + 0x2020, 0x2090, 0x4040, 0x4040, 0x4040, 0x4040, 0x4040, 0x4040, 0x4040, + 0x400C, 0xC0C, 0xC0C, 0xC0C, 0xC0C, 0xC40, 0x4040, 0x4040, 0x4040, 0x909, + 0x909, 0x909, 0x101, 0x101, 0x101, 0x101, 0x101, 0x101, 0x101, 0x101, 0x101, + 0x101, 0x4040, 0x4040, 0x4040, 0xA0A, 0xA0A, 0xA0A, 0x202, 0x202, 0x202, + 0x202, 0x202, 0x202, 0x202, 0x202, 0x202, 0x202, 0x4040, 0x4040, 0x2000}; +const uint8 Infogrames::tickCount[] = {2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96}; + +Infogrames::Infogrames(GobEngine *vm, bool stereo) : + Paula(vm, stereo, vm->_mixer->getOutputRate()/80) { + _instruments = 0; + _data = 0; + _repCount = -1; + + reset(); +} + +Infogrames::~Infogrames() { + if (_data) + delete[] _data; +} + +void Infogrames::init() { + int i; + + _volume = 0; + _period = 0; + _sample = 0; + _speedCounter = _speed; + _newVol = 0x3F; + + for (i = 0; i < 4; i++) { + _chn[i].cmds = 0; + _chn[i].cmdBlocks = 0; + _chn[i].volSlide.finetuneNeg = 0; + _chn[i].volSlide.finetunePos = 0; + _chn[i].volSlide.data = 0; + _chn[i].volSlide.amount = 0; + _chn[i].volSlide.dataOffset = 0; + _chn[i].volSlide.flags = 0; + _chn[i].volSlide.curDelay1 = 0; + _chn[i].volSlide.curDelay2 = 0; + _chn[i].periodSlide.finetuneNeg = 0; + _chn[i].periodSlide.finetunePos = 0; + _chn[i].periodSlide.data = 0; + _chn[i].periodSlide.amount = 0; + _chn[i].periodSlide.dataOffset = 0; + _chn[i].periodSlide.flags = 0; + _chn[i].periodSlide.curDelay1 = 0; + _chn[i].periodSlide.curDelay2 = 0; + _chn[i].curPeriod = 0; + _chn[i].period = 0; + _chn[i].curCmdBlock = 0; + _chn[i].flags = 0; + _chn[i].ticks = 0; + _chn[i].tickCount = 0; + _chn[i].periodMod = 0; + _chn[i].field_2B = 0; + _chn[i].field_2C = 0; + _chn[i].field_2F = 0; + } + + for (i = 0; i < 4; i++) { + _chn[i].flags = 0x81; + _chn[i].field_2B = 0x3F; + _chn[i].field_2F = 0x3F; + } +} + +void Infogrames::reset() { + int i; + + init(); + + _volSlideBlocks = 0; + _periodSlideBlocks = 0; + _subSong = 0; + _cmdBlocks = 0; + _speedCounter = 0; + _speed = 0; + _newVol = 0; + _field_1E = 8; + + for (i = 0; i < 4; i++) + _chn[i].cmdBlockIndices = 0; +} + +bool Infogrames::load(Common::SeekableReadStream &dum) { + int subSong = 0; + int i; + uint32 size; + + size = dum.size(); + if (size < 20) + return false; + + _data = new uint8[size]; + dum.seek(0); + dum.read(_data, size); + + Common::MemoryReadStream dataStr(_data, size); + + dataStr.seek(subSong * 2); + dataStr.seek(dataStr.readUint16BE()); + _subSong = _data + dataStr.pos(); + if (_subSong > (_data + size)) + return false; + + _speedCounter = dataStr.readUint16BE(); + _speed = _speedCounter; + _volSlideBlocks = _subSong + dataStr.readUint16BE(); + _periodSlideBlocks = _subSong + dataStr.readUint16BE(); + for (i = 0; i < 4; i++) { + _chn[i].cmdBlockIndices = _subSong + dataStr.readUint16BE(); + _chn[i].flags = 0x81; + _chn[i].field_2B = 0x3F; + _chn[i].field_2F = 0x3F; + } + _cmdBlocks = _data + dataStr.pos() + 2; + _newVol = 0x3F; + + if ((_volSlideBlocks > (_data + size)) || + (_periodSlideBlocks > (_data + size)) || + (_chn[0].cmdBlockIndices > (_data + size)) || + (_chn[1].cmdBlockIndices > (_data + size)) || + (_chn[2].cmdBlockIndices > (_data + size)) || + (_chn[3].cmdBlockIndices > (_data + size)) || + (_cmdBlocks > (_data + size))) + return false; + + _end = false; + return true; +} + +void Infogrames::unload(bool destroyInstruments) { + stopPlay(); + + if (_data) + delete[] _data; + _data = 0; + + clearVoices(); + reset(); + + if (destroyInstruments) { + if (_instruments) + delete _instruments; + _instruments = 0; + } + + _end = true; +} + +void Infogrames::getNextSample(Channel &chn) { + byte *data; + byte cmdBlock; + uint16 cmd; + bool cont = false; + + if (chn.flags & 64) + return; + + if (chn.field_2B != chn.field_2F) { + chn.field_2C++; + if (chn.field_2C > _field_1E) { + chn.field_2C = 0; + if (chn.field_2F <= chn.field_2B) + chn.field_2B--; + else + chn.field_2B++; + } + } + + if (chn.flags & 1) { + chn.flags &= ~1; + chn.cmdBlocks = chn.cmdBlockIndices; + chn.curCmdBlock = 0; + } else { + chn.flags &= ~1; + if (_speedCounter == 0) + chn.ticks--; + if (chn.ticks != 0) { + _volume = MAX(0, tune(chn.volSlide, 0) - (0x3F - MAX(chn.field_2B, _newVol))); + _period = tune(chn.periodSlide, chn.curPeriod); + return; + } else { + chn.ticks = chn.tickCount; + cont = true; + } + } + + while (1) { + while (cont || ((cmdBlock = *chn.cmdBlocks) != 0xFF)) { + if (!cont) { + chn.cmdBlocks++; + chn.curCmdBlock++; + chn.cmds = _subSong + READ_BE_UINT16(_cmdBlocks + (cmdBlock * 2)); + } else + cont = false; + while ((cmd = *chn.cmds) != 0xFF) { + chn.cmds++; + if (cmd & 128) + { + switch (cmd & 0xE0) { + case 0x80: // 100xxxxx - Set ticks + chn.ticks = tickCount[cmd & 0xF]; + chn.tickCount = tickCount[cmd & 0xF]; + break; + case 0xA0: // 101xxxxx - Set sample + _sample = cmd & 0x1F; + break; + case 0xC0: // 110xxxxx - Set volume slide/finetune + data = _volSlideBlocks + (cmd & 0x1F) * 13; + chn.volSlide.flags = (*data & 0x80) | 1; + chn.volSlide.amount = *data++ & 0x7F; + chn.volSlide.data = data; + chn.volSlide.dataOffset = 0; + chn.volSlide.finetunePos = 0; + chn.volSlide.finetuneNeg = 0; + chn.volSlide.curDelay1 = 0; + chn.volSlide.curDelay2 = 0; + break; + case 0xE0: // 111xxxxx - Extended + switch(cmd & 0x1F) { + case 0: // Set period modifier + chn.periodMod = (int8) *chn.cmds++; + break; + case 1: // Set continuous period slide + chn.periodSlide.data = _periodSlideBlocks + *chn.cmds++ * 13 + 1; + chn.periodSlide.amount = 0; + chn.periodSlide.dataOffset = 0; + chn.periodSlide.finetunePos = 0; + chn.periodSlide.finetuneNeg = 0; + chn.periodSlide.curDelay1 = 0; + chn.periodSlide.curDelay2 = 0; + chn.periodSlide.flags = 0x81; + break; + case 2: // Set non-continuous period slide + chn.periodSlide.data = _periodSlideBlocks + *chn.cmds++ * 13 + 1; + chn.periodSlide.amount = 0; + chn.periodSlide.dataOffset = 0; + chn.periodSlide.finetunePos = 0; + chn.periodSlide.finetuneNeg = 0; + chn.periodSlide.curDelay1 = 0; + chn.periodSlide.curDelay2 = 0; + chn.periodSlide.flags = 1; + break; + case 3: // NOP + break; + default: + warning("Unknown Infogrames command: %X", cmd); + } + break; + } + } else { // 0xxxxxxx - Set period + if (cmd != 0) + cmd += chn.periodMod; + chn.curPeriod = periods[cmd]; + chn.period = periods[cmd]; + chn.volSlide.dataOffset = 0; + chn.volSlide.finetunePos = 0; + chn.volSlide.finetuneNeg = 0; + chn.volSlide.curDelay1 = 0; + chn.volSlide.curDelay2 = 0; + chn.volSlide.flags |= 1; + chn.volSlide.flags &= ~4; + chn.periodSlide.dataOffset = 0; + chn.periodSlide.finetunePos = 0; + chn.periodSlide.finetuneNeg = 0; + chn.periodSlide.curDelay1 = 0; + chn.periodSlide.curDelay2 = 0; + chn.periodSlide.flags |= 1; + chn.periodSlide.flags &= ~4; + _volume = MAX(0, tune(chn.volSlide, 0) - (0x3F - MAX(chn.field_2B, _newVol))); + _period = tune(chn.periodSlide, chn.curPeriod); + return; + } + } + } + if (chn.flags & 32) { + chn.cmdBlocks = chn.cmdBlockIndices; + chn.curCmdBlock = 0; + } else { + chn.flags |= 0x40; + _volume = 0; + return; + } + } +} + +int16 Infogrames::tune(Slide &slide, int16 start) const { + byte *data; + uint8 off; + + data = slide.data + slide.dataOffset; + + if (slide.flags & 1) + slide.finetunePos += (int8) data[1]; + slide.flags &= ~1; + + start += slide.finetunePos - slide.finetuneNeg; + if (start < 0) + start = 0; + + if (slide.flags & 4) + return start; + + slide.curDelay1++; + if (slide.curDelay1 != data[2]) + return start; + slide.curDelay2++; + slide.curDelay1 = 0; + if (slide.curDelay2 == data[0]) { + slide.curDelay2 = 0; + off = slide.dataOffset + 3; + if (off == 12) { + if (slide.flags == 0) { + slide.flags |= 4; + return start; + } else { + slide.curDelay2 = 0; + slide.finetuneNeg += slide.amount; + off = 3; + } + } + slide.dataOffset = off; + } + slide.flags |= 1; + return start; +} + +void Infogrames::interrupt() { + int chn; + + if (!_data) { + clearVoices(); + return; + } + + _speedCounter--; + _sample = 0xFF; + for (chn = 0; chn < 4; chn++) { + _volume = 0; + _period = 0; + getNextSample(_chn[chn]); + _voice[chn].volume = _volume; + _voice[chn].period = _period; + if ((_sample != 0xFF) && (_sample < _instruments->_count)) { + _voice[chn].data = _instruments->_samples[_sample].data; + _voice[chn].length = _instruments->_samples[_sample].length; + _voice[chn].dataRepeat = _instruments->_samples[_sample].dataRepeat; + _voice[chn].lengthRepeat = _instruments->_samples[_sample].lengthRepeat; + _voice[chn].offset = 0; + _sample = 0xFF; + } + } + if (_speedCounter == 0) + _speedCounter = _speed; + + // End reached? + if ((_chn[0].flags & 64) && (_chn[1].flags & 64) && + (_chn[2].flags & 64) && (_chn[3].flags & 64)) { + if (_repCount > 0) { + _repCount--; + init(); + } else if (_repCount == -1) + init(); + else + _end = true; + } +} + +const char *Adlib::_tracks[][2] = { {"avt00.tot", "mine"}, {"avt001.tot", "nuit"}, {"avt002.tot", "campagne"}, @@ -57,7 +632,7 @@ const char *Music::_tracks[][2] = { {"avt022.tot", "zombie"} }; -const char *Music::_trackFiles[] = { +const char *Adlib::_trackFiles[] = { // "musmac1.adl", // TODO: This track isn't played correctly at all yet "musmac2.adl", "musmac3.adl", @@ -66,14 +641,14 @@ const char *Music::_trackFiles[] = { "musmac6.adl" }; -const unsigned char Music::_operators[] = {0, 1, 2, 8, 9, 10, 16, 17, 18}; -const unsigned char Music::_volRegNums[] = { +const unsigned char Adlib::_operators[] = {0, 1, 2, 8, 9, 10, 16, 17, 18}; +const unsigned char Adlib::_volRegNums[] = { 3, 4, 5, 11, 12, 13, 19, 20, 21 }; -Music::Music(GobEngine *vm) : _vm(vm) { +Adlib::Adlib(GobEngine *vm) : _vm(vm) { int i; _index = -1; @@ -96,14 +671,14 @@ Music::Music(GobEngine *vm) : _vm(vm) { setFreqs(); } -Music::~Music(void) { +Adlib::~Adlib(void) { OPLDestroy(_opl); if (_data && _needFree) delete[] _data; _vm->_mixer->setupPremix(0); } -void Music::premixerCall(int16 *buf, uint len) { +void Adlib::premixerCall(int16 *buf, uint len) { _mutex.lock(); if (!_playing) { memset(buf, 0, 2 * len * sizeof(int16)); @@ -161,12 +736,12 @@ void Music::premixerCall(int16 *buf, uint len) { _mutex.unlock(); } -void Music::writeOPL(byte reg, byte val) { +void Adlib::writeOPL(byte reg, byte val) { debugC(6, kDebugMusic, "writeOPL(%02X, %02X)", reg, val); OPLWriteReg(_opl, reg, val); } -void Music::setFreqs(void) { +void Adlib::setFreqs(void) { byte lin; byte col; long val = 0; @@ -192,7 +767,7 @@ void Music::setFreqs(void) { } } -void Music::reset() { +void Adlib::reset() { OPLResetChip(_opl); _samplesTillPoll = 0; @@ -209,13 +784,13 @@ void Music::reset() { writeOPL(0x01, 0x20); } -void Music::setVoices() { +void Adlib::setVoices() { // Definitions of the 9 instruments for (int i = 0; i < 9; i++) setVoice(i, i, true); } -void Music::setVoice(byte voice, byte instr, bool set) { +void Adlib::setVoice(byte voice, byte instr, bool set) { int i; int j; uint16 strct[27]; @@ -250,7 +825,7 @@ void Music::setVoice(byte voice, byte instr, bool set) { } } -void Music::setKey(byte voice, byte note, bool on, bool spec) { +void Adlib::setKey(byte voice, byte note, bool on, bool spec) { short freq = 0; short octa = 0; @@ -316,12 +891,12 @@ void Music::setKey(byte voice, byte note, bool on, bool spec) { warning("Voice %d, note %02X unknown\n", voice, note); } -void Music::setVolume(byte voice, byte volume) { +void Adlib::setVolume(byte voice, byte volume) { volume = 0x3F - (volume * 0x7E + 0x7F) / 0xFE; writeOPL(0x40 + _volRegNums[voice], volume); } -void Music::pollMusic(void) { +void Adlib::pollMusic(void) { unsigned char instr; byte channel; byte note; @@ -425,14 +1000,14 @@ void Music::pollMusic(void) { _samplesTillPoll = tempo * (_rate / 1000); } -void Music::startPlay(void) { +void Adlib::startPlay(void) { if (!_data) return; _playing = true; } -void Music::playBgMusic(void) { +void Adlib::playBgMusic(void) { for (int i = 0; i < ARRAYSIZE(_tracks); i++) if (!scumm_stricmp(_vm->_game->_curTotFile, _tracks[i][0])) { playTrack(_tracks[i][1]); @@ -440,19 +1015,19 @@ void Music::playBgMusic(void) { } } -void Music::playTrack(const char *trackname) { +void Adlib::playTrack(const char *trackname) { if (_playing) return; - debugC(1, kDebugMusic, "Music::playTrack(%s)", trackname); - unloadMusic(); - loadMusic(_trackFiles[_vm->_util->getRandom(ARRAYSIZE(_trackFiles))]); + debugC(1, kDebugMusic, "Adlib::playTrack(%s)", trackname); + unload(); + load(_trackFiles[_vm->_util->getRandom(ARRAYSIZE(_trackFiles))]); startPlay(); } -bool Music::loadMusic(const char *filename) { +bool Adlib::load(const char *filename) { Common::File song; - unloadMusic(); + unload(); song.open(filename); if (!song.isOpen()) return false; @@ -470,8 +1045,8 @@ bool Music::loadMusic(const char *filename) { return true; } -void Music::loadFromMemory(byte *data, int index) { - unloadMusic(); +void Adlib::load(byte *data, int index) { + unload(); _repCount = 0; _dataSize = (uint32) -1; @@ -483,7 +1058,7 @@ void Music::loadFromMemory(byte *data, int index) { _playPos = _data + 3 + (_data[1] + 1) * 0x38; } -void Music::unloadMusic(void) { +void Adlib::unload(void) { _playing = false; _index = -1; diff --git a/engines/gob/music.h b/engines/gob/music.h index f2db978f75..89d8e2933c 100644 --- a/engines/gob/music.h +++ b/engines/gob/music.h @@ -30,29 +30,194 @@ #include "common/mutex.h" #include "gob/gob.h" +#include "gob/util.h" namespace Gob { class GobEngine; -class Music : public Audio::AudioStream { +// Emulation of the "Paula" Amiga music chip +class Paula: public Audio::AudioStream { public: - Music(GobEngine *vm); - virtual ~Music(); + Paula(GobEngine *vm, bool stereo = false, int intFreq = 0); + ~Paula(); + + bool playing() const { return _playing; } + void setInterruptFreq(int freq) { _intFreq = freq; } + void clearVoice(int voice); + void clearVoices() { int i; for (i = 0; i < 4; i++) clearVoice(i); } + virtual void startPlay(void) {} + virtual void stopPlay(void) {} + virtual void pausePlay(bool pause) {} + +// AudioStream API + int readBuffer(int16 *buffer, const int numSamples); + bool isStereo() const { return _stereo; } + bool endOfData() const { return _end; } + int getRate() const { return _rate; } + +protected: + struct Channel { + int8 *data; + int8 *dataRepeat; + uint32 length; + uint32 lengthRepeat; + int16 period; + byte volume; + double offset; + byte panning; // For stereo mixing; 0: left, 1: right + } _voice[4]; + + int _rate; + int _intFreq; + int _curInt; + bool _stereo; + bool _end; + bool _playing; + + GobEngine *_vm; + + void mix(int16 *&buf, int8 data, int voice) { + if (_stereo) { + *buf++ += _voice[voice].panning == 0 ? 2 * _voice[voice].volume * data : 0; + *buf++ += _voice[voice].panning == 1 ? 2 * _voice[voice].volume * data : 0; + } else + *buf++ += _voice[voice].volume * data; + } + virtual void interrupt(void) {}; +}; + +class Infogrames : public Paula { +public: + class Instruments { + public: + struct Sample { + int8 *data; + int8 *dataRepeat; + uint32 length; + uint32 lengthRepeat; + } _samples[32]; + + uint8 _count; + int8 *_sampleData; + + Instruments(); + ~Instruments(); + + bool load(Common::SeekableReadStream &ins); + bool load(const char *ins) { + Common::File f; + + if (f.open(ins)) + return load(f); + return false; + } + }; + + Infogrames(GobEngine *vm, bool stereo = false); + ~Infogrames(); + + Instruments *getInstruments(void) const { return _instruments; } + bool getRepeating(void) const { return _repCount != 0; } + void setRepeating (int32 repCount) { _repCount = repCount; } + void restart(void) { if (!_data || !_instruments) return; init(); _end = false; } + virtual void startPlay(void) { if (_data && _instruments) { restart(); _playing = true; } } + virtual void stopPlay(void) { _playing = false; } + virtual void pausePlay(bool pause) { _playing = !pause; } + + bool load(Common::SeekableReadStream &dum); + bool load(const char *dum) { + Common::File f; + + if (f.open(dum)) + return load(f); + return false; + } + void unload(bool destroyInstruments = false); + template<typename T> bool loadInstruments(T ins) { + unload(true); + _instruments = new Instruments(); + if (!_instruments->load(ins)) { + delete _instruments; + _instruments = 0; + return false; + } + return true; + } + +protected: + Instruments *_instruments; + + static const uint8 tickCount[]; + static const uint16 periods[]; + byte *_data; + int32 _repCount; + + uint16 _volume; + int16 _period; + byte *_volSlideBlocks; + byte *_periodSlideBlocks; + byte *_subSong; + byte *_cmdBlocks; + uint8 _sample; + uint8 _speedCounter; + uint8 _speed; + uint8 _newVol; + uint8 _field_1E; + + struct Slide { + int16 finetuneNeg; + int16 finetunePos; + byte *data; + int8 amount; + uint8 dataOffset; + uint8 flags; // 0: Apply finetune modifier, 2: Don't slide, 7: Continuous + uint8 curDelay1; + uint8 curDelay2; + }; + struct Channel { + byte *cmdBlockIndices; + byte *cmds; + byte *cmdBlocks; + Slide volSlide; + Slide periodSlide; + int16 curPeriod; + int16 period; + uint16 curCmdBlock; + uint16 flags; // 0: Need init, 5: Loop cmdBlocks, 6: Ignore channel + uint8 ticks; + uint8 tickCount; + int8 periodMod; + uint8 field_2B; + uint8 field_2C; + uint8 field_2F; + } _chn[4]; + + void init(void); + void reset(void); + void getNextSample(Channel &chn); + int16 tune(Slide &slide, int16 start) const; + virtual void interrupt(void); +}; + +class Adlib : public Audio::AudioStream { +public: + Adlib(GobEngine *vm); + virtual ~Adlib(); void lock() { _mutex.lock(); } void unlock() { _mutex.unlock(); } - bool playing() { return _playing; } - bool getRepeating(void) { return _repCount != 0; } + bool playing() const { return _playing; } + bool getRepeating(void) const { return _repCount != 0; } void setRepeating (int32 repCount) { _repCount = repCount; } - int getIndex(void) { return _index; } + int getIndex(void) const { return _index; } virtual void startPlay(void); virtual void stopPlay(void) { _mutex.lock(); _playing = false; _mutex.unlock(); } virtual void playTrack(const char *trackname); virtual void playBgMusic(void); - virtual bool loadMusic(const char *filename); - virtual void loadFromMemory(byte *data, int index=-1); - virtual void unloadMusic(void); + virtual bool load(const char *filename); + virtual void load(byte *data, int index=-1); + virtual void unload(void); // AudioStream API int readBuffer(int16 *buffer, const int numSamples) { @@ -100,22 +265,19 @@ protected: void pollMusic(void); }; -class Music_Dummy: public Music { +class Adlib_Dummy: public Adlib { public: - Music_Dummy(GobEngine *vm) : Music(vm) { - _vm->_mixer->setupPremix(0); - OPLDestroy(_opl); - } + Adlib_Dummy(GobEngine *vm) : Adlib(vm) {} virtual void startPlay(void) {}; virtual void stopPlay(void) {}; virtual void playTrack(const char *trackname) {}; - virtual void playBgMusic(void) {}; - virtual bool loadMusic(const char *filename) { return true; }; - virtual void loadFromMemory(byte *data) {}; - virtual void unloadMusic(void) {}; + virtual void playBgAdlib(void) {}; + virtual bool load(const char *filename) { return true; } + virtual void load(byte *data, int index=-1) {} + virtual void unload(void) {}; - virtual ~Music_Dummy() {}; + virtual ~Adlib_Dummy() {}; }; } // End of namespace Gob |