From bfa7d895002198a364a599893953f911f2095ce6 Mon Sep 17 00:00:00 2001 From: Martin Kiewitz Date: Fri, 3 Jul 2015 00:52:36 +0200 Subject: AGOS: Accolade music: read driver data in factory moved driver reading code into factory of both Accolade music drivers --- engines/agos/drivers/accolade/adlib.cpp | 24 +++- engines/agos/drivers/accolade/driverfile.cpp | 166 +++++++++++++++++++++++++++ engines/agos/drivers/accolade/mididriver.h | 7 +- engines/agos/drivers/accolade/mt32.cpp | 24 +++- 4 files changed, 205 insertions(+), 16 deletions(-) create mode 100644 engines/agos/drivers/accolade/driverfile.cpp (limited to 'engines/agos/drivers/accolade') diff --git a/engines/agos/drivers/accolade/adlib.cpp b/engines/agos/drivers/accolade/adlib.cpp index a748ac2323..ed05fe19ac 100644 --- a/engines/agos/drivers/accolade/adlib.cpp +++ b/engines/agos/drivers/accolade/adlib.cpp @@ -862,12 +862,24 @@ bool MidiDriver_Accolade_AdLib::setupInstruments(byte *driverData, uint16 driver return true; } -MidiDriver *MidiDriver_Accolade_AdLib_create() { - return new MidiDriver_Accolade_AdLib(g_system->getMixer()); -} - -bool MidiDriver_Accolade_AdLib_setupInstruments(MidiDriver *driver, byte *driverData, uint16 driverDataSize, bool useMusicDrvFile) { - return static_cast(driver)->setupInstruments(driverData, driverDataSize, useMusicDrvFile); +MidiDriver *MidiDriver_Accolade_AdLib_create(Common::String driverFilename) { + byte *driverData = NULL; + uint16 driverDataSize = 0; + bool isMusicDrvFile = false; + + MidiDriver_Accolade_readDriver(driverFilename, MT_ADLIB, driverData, driverDataSize, isMusicDrvFile); + if (!driverData) + error("ACCOLADE-ADLIB: error during readDriver()"); + + MidiDriver_Accolade_AdLib *driver = new MidiDriver_Accolade_AdLib(g_system->getMixer()); + if (!driver) + return nullptr; + + if (!driver->setupInstruments(driverData, driverDataSize, isMusicDrvFile)) { + delete driver; + return nullptr; + } + return driver; } } // End of namespace AGOS diff --git a/engines/agos/drivers/accolade/driverfile.cpp b/engines/agos/drivers/accolade/driverfile.cpp new file mode 100644 index 0000000000..4ff2fd550f --- /dev/null +++ b/engines/agos/drivers/accolade/driverfile.cpp @@ -0,0 +1,166 @@ +/* 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 "agos/agos.h" +#include "audio/mididrv.h" +#include "common/error.h" +#include "common/file.h" + +namespace AGOS { + +// this reads and gets Accolade driver data +// we need it for channel mapping, instrument mapping and other things +// this driver data chunk gets passed to the actual music driver (MT32 / AdLib) +void MidiDriver_Accolade_readDriver(Common::String filename, MusicType requestedDriverType, byte *&driverData, uint16 &driverDataSize, bool &isMusicDrvFile) { + Common::File *driverStream = new Common::File(); + + isMusicDrvFile = false; + + if (!driverStream->open(filename)) { + error("%s: unable to open file", filename.c_str()); + } + + if (filename == "INSTR.DAT") { + // INSTR.DAT: used by Elvira 1 + uint32 streamSize = driverStream->size(); + uint32 streamLeft = streamSize; + uint16 skipChunks = 0; // 1 for MT32, 0 for AdLib + uint16 chunkSize = 0; + + switch (requestedDriverType) { + case MT_ADLIB: + skipChunks = 0; + break; + case MT_MT32: + skipChunks = 1; // Skip one entry for MT32 + break; + default: + assert(0); + break; + } + + do { + if (streamLeft < 2) + error("%s: unexpected EOF", filename.c_str()); + + chunkSize = driverStream->readUint16LE(); + streamLeft -= 2; + + if (streamLeft < chunkSize) + error("%s: unexpected EOF", filename.c_str()); + + if (skipChunks) { + // Skip the chunk + driverStream->skip(chunkSize); + streamLeft -= chunkSize; + + skipChunks--; + } + } while (skipChunks); + + // Seek over the ASCII string until there is a NUL terminator + byte curByte = 0; + + do { + if (chunkSize == 0) + error("%s: no actual instrument data found", filename.c_str()); + + curByte = driverStream->readByte(); + chunkSize--; + } while (curByte); + + driverDataSize = chunkSize; + + // Read the requested instrument data entry + driverData = new byte[driverDataSize]; + driverStream->read(driverData, driverDataSize); + + } else if (filename == "MUSIC.DRV") { + // MUSIC.DRV / used by Elvira 2 / Waxworks / Simon 1 demo + uint32 streamSize = driverStream->size(); + uint32 streamLeft = streamSize; + uint16 getChunk = 0; // 4 for MT32, 2 for AdLib + + switch (requestedDriverType) { + case MT_ADLIB: + getChunk = 2; + break; + case MT_MT32: + getChunk = 4; + break; + default: + assert(0); + break; + } + + if (streamLeft < 2) + error("%s: unexpected EOF", filename.c_str()); + + uint16 chunkCount = driverStream->readUint16LE(); + streamLeft -= 2; + + if (getChunk >= chunkCount) + error("%s: required chunk not available", filename.c_str()); + + uint16 headerOffset = 2 + (28 * getChunk); + streamLeft -= (28 * getChunk); + + if (streamLeft < 28) + error("%s: unexpected EOF", filename.c_str()); + + // Seek to required chunk + driverStream->seek(headerOffset); + driverStream->skip(20); // skip over name + streamLeft -= 20; + + uint16 musicDrvSignature = driverStream->readUint16LE(); + uint16 musicDrvType = driverStream->readUint16LE(); + uint16 chunkOffset = driverStream->readUint16LE(); + uint16 chunkSize = driverStream->readUint16LE(); + + // Security checks + if (musicDrvSignature != 0xFEDC) + error("%s: chunk signature mismatch", filename.c_str()); + if (musicDrvType != 1) + error("%s: not a music driver", filename.c_str()); + if (chunkOffset >= streamSize) + error("%s: driver chunk points outside of file", filename.c_str()); + + streamLeft = streamSize - chunkOffset; + if (streamLeft < chunkSize) + error("%s: driver chunk is larger than file", filename.c_str()); + + driverDataSize = chunkSize; + + // Read the requested instrument data entry + driverData = new byte[driverDataSize]; + + driverStream->seek(chunkOffset); + driverStream->read(driverData, driverDataSize); + isMusicDrvFile = true; + } + + driverStream->close(); + delete driverStream; +} + +} // End of namespace AGOS diff --git a/engines/agos/drivers/accolade/mididriver.h b/engines/agos/drivers/accolade/mididriver.h index 96637ed1f2..253fb6b736 100644 --- a/engines/agos/drivers/accolade/mididriver.h +++ b/engines/agos/drivers/accolade/mididriver.h @@ -34,11 +34,10 @@ namespace AGOS { #define AGOS_MIDI_KEYNOTE_COUNT 64 -extern MidiDriver *MidiDriver_Accolade_AdLib_create(); -extern bool MidiDriver_Accolade_AdLib_setupInstruments(MidiDriver *driver, byte *instrDATData, uint16 instrDATDataSize, bool useMusicDrvFile); +extern void MidiDriver_Accolade_readDriver(Common::String filename, MusicType requestedDriverType, byte *&driverData, uint16 &driverDataSize, bool &isMusicDrvFile); -extern MidiDriver *MidiDriver_Accolade_MT32_create(); -extern bool MidiDriver_Accolade_MT32_setupInstruments(MidiDriver *driver, byte *instrDATData, uint16 instrDATDataSize, bool useMusicDrvFile); +extern MidiDriver *MidiDriver_Accolade_AdLib_create(Common::String driverFilename); +extern MidiDriver *MidiDriver_Accolade_MT32_create(Common::String driverFilename); } // End of namespace AGOS diff --git a/engines/agos/drivers/accolade/mt32.cpp b/engines/agos/drivers/accolade/mt32.cpp index f863ffba4c..2a023297ee 100644 --- a/engines/agos/drivers/accolade/mt32.cpp +++ b/engines/agos/drivers/accolade/mt32.cpp @@ -254,12 +254,24 @@ bool MidiDriver_Accolade_MT32::setupInstruments(byte *driverData, uint16 driverD return true; } -MidiDriver *MidiDriver_Accolade_MT32_create() { - return new MidiDriver_Accolade_MT32(); -} - -bool MidiDriver_Accolade_MT32_setupInstruments(MidiDriver *driver, byte *instrumentData, uint16 instrumentDataSize, bool useMusicDrvFile) { - return static_cast(driver)->setupInstruments(instrumentData, instrumentDataSize, useMusicDrvFile); +MidiDriver *MidiDriver_Accolade_MT32_create(Common::String driverFilename) { + byte *driverData = NULL; + uint16 driverDataSize = 0; + bool isMusicDrvFile = false; + + MidiDriver_Accolade_readDriver(driverFilename, MT_MT32, driverData, driverDataSize, isMusicDrvFile); + if (!driverData) + error("ACCOLADE-ADLIB: error during readDriver()"); + + MidiDriver_Accolade_MT32 *driver = new MidiDriver_Accolade_MT32(); + if (!driver) + return nullptr; + + if (!driver->setupInstruments(driverData, driverDataSize, isMusicDrvFile)) { + delete driver; + return nullptr; + } + return driver; } } // End of namespace AGOS -- cgit v1.2.3