scummvm/scumm/imuse_digi/dimuse_sndmgr.cpp

531 lines
17 KiB
C++
Raw Normal View History

/* ScummVM - Scumm Interpreter
* Copyright (C) 2001-2004 The ScummVM project
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
2004-05-23 19:44:10 +00:00
* $Header$
*/
#include "stdafx.h"
#include "common/scummsys.h"
#include "common/util.h"
#include "sound/voc.h"
#include "scumm/scumm.h"
#include "scumm/imuse_digi/dimuse.h"
#include "scumm/imuse_digi/dimuse_sndmgr.h"
#include "scumm/imuse_digi/dimuse_bndmgr.h"
namespace Scumm {
ImuseDigiSndMgr::ImuseDigiSndMgr(ScummEngine *scumm) {
2004-01-18 11:18:46 +00:00
for (int l = 0; l < MAX_IMUSE_SOUNDS; l++) {
memset(&_sounds[l], 0, sizeof(soundStruct));
}
_vm = scumm;
_disk = 0;
2004-01-07 04:50:38 +00:00
_cacheBundleDir = new BundleDirCache();
BundleCodecs::initializeImcTables();
}
ImuseDigiSndMgr::~ImuseDigiSndMgr() {
for (int l = 0; l < MAX_IMUSE_SOUNDS; l++) {
2004-01-18 09:35:05 +00:00
closeSound(&_sounds[l]);
}
#ifdef __PALM_OS__
BundleCodecs::releaseImcTables();
#endif
}
void ImuseDigiSndMgr::countElements(byte *ptr, int &numRegions, int &numJumps, int &numSyncs) {
uint32 tag;
int32 size = 0;
do {
tag = READ_BE_UINT32(ptr); ptr += 4;
switch(tag) {
case MKID_BE('TEXT'):
case MKID_BE('STOP'):
case MKID_BE('FRMT'):
case MKID_BE('DATA'):
size = READ_BE_UINT32(ptr); ptr += size + 4;
break;
case MKID_BE('REGN'):
numRegions++;
size = READ_BE_UINT32(ptr); ptr += size + 4;
break;
case MKID_BE('JUMP'):
numJumps++;
size = READ_BE_UINT32(ptr); ptr += size + 4;
break;
case MKID_BE('SYNC'):
numSyncs++;
size = READ_BE_UINT32(ptr); ptr += size + 4;
break;
default:
error("ImuseDigiSndMgr::countElements() Unknown sfx header '%s'", tag2str(tag));
}
} while (tag != MKID_BE('DATA'));
}
void ImuseDigiSndMgr::prepareSound(byte *ptr, int slot) {
if (READ_UINT32(ptr) == MKID('Crea')) {
bool quit = false;
int len;
int32 offset = READ_LE_UINT16(ptr + 20);
int16 code = READ_LE_UINT16(ptr + 24);
_sounds[slot].region = (_region *)malloc(sizeof(_region) * 70);
_sounds[slot].jump = (_jump *)malloc(sizeof(_jump));
_sounds[slot].resPtr = ptr;
_sounds[slot].bits = 8;
_sounds[slot].channels = 1;
while (!quit) {
len = READ_LE_UINT32(ptr + offset);
code = len & 0xFF;
if ((code != 0) && (code != 1) && (code != 6) && (code != 7)) {
// try again with 2 bytes forward (workaround for some FT sounds (ex.362, 363)
offset += 2;
len = READ_LE_UINT32(ptr + offset);
code = len & 0xFF;
if ((code != 0) && (code != 1) && (code != 6) && (code != 7)) {
error("Invalid code in VOC file : %d", code);
}
}
offset += 4;
len >>= 8;
switch(code) {
case 0:
quit = true;
break;
case 1:
{
int time_constant = ptr[offset];
offset += 2;
len -= 2;
_sounds[slot].freq = getSampleRateFromVOCRate(time_constant);
_sounds[slot].region[_sounds[slot].numRegions].offset = offset;
_sounds[slot].region[_sounds[slot].numRegions].length = len;
_sounds[slot].numRegions++;
}
break;
case 6: // begin of loop
_sounds[slot].jump[0].dest = offset + 8;
_sounds[slot].jump[0].hookId = 0;
_sounds[slot].jump[0].fadeDelay = 0;
break;
case 7: // end of loop
_sounds[slot].jump[0].offset = offset - 4;
_sounds[slot].numJumps++;
_sounds[slot].region[_sounds[slot].numRegions].offset = offset - 4;
_sounds[slot].region[_sounds[slot].numRegions].length = 0;
_sounds[slot].numRegions++;
break;
default:
error("Invalid code in VOC file : %d", code);
quit = true;
break;
}
offset += len;
}
} else if (READ_UINT32(ptr) == MKID('iMUS')) {
uint32 tag;
int32 size = 0;
byte *s_ptr = ptr;
ptr += 16;
int curIndexRegion = 0;
int curIndexJump = 0;
int curIndexSync = 0;
_sounds[slot].numRegions = 0;
_sounds[slot].numJumps = 0;
_sounds[slot].numSyncs = 0;
countElements(ptr, _sounds[slot].numRegions, _sounds[slot].numJumps, _sounds[slot].numSyncs);
_sounds[slot].region = (_region *)malloc(sizeof(_region) * _sounds[slot].numRegions);
_sounds[slot].jump = (_jump *)malloc(sizeof(_jump) * _sounds[slot].numJumps);
_sounds[slot].sync = (_sync *)malloc(sizeof(_sync) * _sounds[slot].numSyncs);
do {
tag = READ_BE_UINT32(ptr); ptr += 4;
switch(tag) {
case MKID_BE('FRMT'):
ptr += 12;
_sounds[slot].bits = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].freq = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].channels = READ_BE_UINT32(ptr); ptr += 4;
break;
case MKID_BE('TEXT'):
case MKID_BE('STOP'):
2004-01-18 16:59:54 +00:00
size = READ_BE_UINT32(ptr); ptr += size + 4;
break;
case MKID_BE('REGN'):
ptr += 4;
_sounds[slot].region[curIndexRegion].offset = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].region[curIndexRegion].length = READ_BE_UINT32(ptr); ptr += 4;
curIndexRegion++;
break;
case MKID_BE('JUMP'):
ptr += 4;
_sounds[slot].jump[curIndexJump].offset = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].jump[curIndexJump].dest = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].jump[curIndexJump].hookId = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].jump[curIndexJump].fadeDelay = READ_BE_UINT32(ptr); ptr += 4;
curIndexJump++;
break;
case MKID_BE('SYNC'):
size = READ_BE_UINT32(ptr); ptr += 4;
_sounds[slot].sync[curIndexSync].size = size;
_sounds[slot].sync[curIndexSync].ptr = (byte *)malloc(size);
memcpy(_sounds[slot].sync[curIndexSync].ptr, ptr, size);
curIndexSync++;
ptr += size;
break;
case MKID_BE('DATA'):
ptr += 4;
break;
default:
error("ImuseDigiSndMgr::prepareSound(%d/%s) Unknown sfx header '%s'", _sounds[slot].soundId, _sounds[slot].name, tag2str(tag));
}
} while (tag != MKID_BE('DATA'));
_sounds[slot].offsetData = ptr - s_ptr;
} else {
error("ImuseDigiSndMgr::prepareSound(): Unknown sound format");
}
}
int ImuseDigiSndMgr::allocSlot() {
for (int l = 0; l < MAX_IMUSE_SOUNDS; l++) {
if (!_sounds[l].inUse) {
_sounds[l].inUse = true;
return l;
}
}
return -1;
}
bool ImuseDigiSndMgr::openMusicBundle(int slot, int disk) {
bool result = false;
_sounds[slot].bundle = new BundleMgr(_cacheBundleDir);
if (_vm->_gameId == GID_CMI) {
if (_vm->_features & GF_DEMO) {
result = _sounds[slot].bundle->openFile("music.bun", _vm->getGameDataPath());
} else {
char musicfile[20];
if (disk == -1)
sprintf(musicfile, "musdisk%d.bun", _vm->VAR(_vm->VAR_CURRENTDISK));
else
sprintf(musicfile, "musdisk%d.bun", disk);
// if (_disk != _vm->VAR(_vm->VAR_CURRENTDISK)) {
// _vm->_imuseDigital->parseScriptCmds(0x1000, 0, 0, 0, 0, 0, 0, 0);
// _vm->_imuseDigital->parseScriptCmds(0x2000, 0, 0, 0, 0, 0, 0, 0);
// _vm->_imuseDigital->stopAllSounds();
// _sounds[slot].bundle->closeFile();
// }
result = _sounds[slot].bundle->openFile(musicfile, _vm->getGameDataPath());
if (result == false)
result = _sounds[slot].bundle->openFile("music.bun", _vm->getGameDataPath());
_disk = (byte)_vm->VAR(_vm->VAR_CURRENTDISK);
}
} else if (_vm->_gameId == GID_DIG)
result = _sounds[slot].bundle->openFile("digmusic.bun", _vm->getGameDataPath());
else
error("ImuseDigiSndMgr::openMusicBundle() Don't know which bundle file to load");
return result;
}
bool ImuseDigiSndMgr::openVoiceBundle(int slot, int disk) {
bool result = false;
_sounds[slot].bundle = new BundleMgr(_cacheBundleDir);
if (_vm->_gameId == GID_CMI) {
if (_vm->_features & GF_DEMO) {
result = _sounds[slot].bundle->openFile("voice.bun", _vm->getGameDataPath());
} else {
char voxfile[20];
if (disk == -1)
sprintf(voxfile, "voxdisk%d.bun", _vm->VAR(_vm->VAR_CURRENTDISK));
else
sprintf(voxfile, "voxdisk%d.bun", disk);
// if (_disk != _vm->VAR(_vm->VAR_CURRENTDISK)) {
// _vm->_imuseDigital->parseScriptCmds(0x1000, 0, 0, 0, 0, 0, 0, 0);
// _vm->_imuseDigital->parseScriptCmds(0x2000, 0, 0, 0, 0, 0, 0, 0);
// _vm->_imuseDigital->stopAllSounds();
// _sounds[slot].bundle->closeFile();
// }
result = _sounds[slot].bundle->openFile(voxfile, _vm->getGameDataPath());
if (result == false)
result = _sounds[slot].bundle->openFile("voice.bun", _vm->getGameDataPath());
_disk = (byte)_vm->VAR(_vm->VAR_CURRENTDISK);
}
} else if (_vm->_gameId == GID_DIG)
result = _sounds[slot].bundle->openFile("digvoice.bun", _vm->getGameDataPath());
else
error("ImuseDigiSndMgr::openVoiceBundle() Don't know which bundle file to load");
return result;
}
ImuseDigiSndMgr::soundStruct *ImuseDigiSndMgr::openSound(int32 soundId, const char *soundName, int soundType, int volGroupId, int disk) {
assert(soundId >= 0);
assert(soundType);
int slot = allocSlot();
if (slot == -1) {
error("ImuseDigiSndMgr::openSound() can't alloc free sound slot");
}
bool result = false;
byte *ptr = NULL;
if (soundName[0] == 0) {
_sounds[slot].name[0] = 0;
if ((soundType == IMUSE_RESOURCE)) {
_vm->ensureResourceLoaded(rtSound, soundId);
_vm->lock(rtSound, soundId);
ptr = _vm->getResourceAddress(rtSound, soundId);
if (ptr == NULL) {
closeSound(&_sounds[slot]);
return NULL;
}
_sounds[slot].resPtr = ptr;
_sounds[slot].soundId = soundId;
_sounds[slot].type = soundType;
_sounds[slot].volGroupId = volGroupId;
result = true;
} else if (soundType == IMUSE_BUNDLE) {
bool header_outside = ((_vm->_gameId == GID_CMI) && !(_vm->_features & GF_DEMO));
if (volGroupId == IMUSE_VOLGRP_VOICE)
result = openVoiceBundle(slot, disk);
else if (volGroupId == IMUSE_VOLGRP_MUSIC)
result = openMusicBundle(slot, disk);
else
error("ImuseDigiSndMgr::openSound() Don't know how load sound: %d", soundId);
if (!result) {
closeSound(&_sounds[slot]);
return NULL;
}
_sounds[slot].bundle->decompressSampleByIndex(soundId, 0, 0x2000, &ptr, 0, header_outside);
_sounds[slot].soundId = soundId;
_sounds[slot].type = soundType;
_sounds[slot].volGroupId = volGroupId;
} else {
error("ImuseDigiSndMgr::openSound() Don't know how load sound: %d", soundId);
}
2004-01-15 20:37:26 +00:00
} else {
if (soundType == IMUSE_BUNDLE) {
bool header_outside = ((_vm->_gameId == GID_CMI) && !(_vm->_features & GF_DEMO));
if (volGroupId == IMUSE_VOLGRP_VOICE)
result = openVoiceBundle(slot, disk);
else if (volGroupId == IMUSE_VOLGRP_MUSIC)
result = openMusicBundle(slot, disk);
else
error("ImuseDigiSndMgr::openSound() Don't know how load sound: %d", soundId);
if (!result) {
closeSound(&_sounds[slot]);
return NULL;
}
_sounds[slot].bundle->decompressSampleByName(soundName, 0, 0x2000, &ptr, header_outside);
strcpy(_sounds[slot].name, soundName);
_sounds[slot].soundId = soundId;
_sounds[slot].type = soundType;
_sounds[slot].volGroupId = volGroupId;
} else {
error("ImuseDigiSndMgr::openSound() Don't know how load sound: %s", soundName);
}
}
if (result) {
if (ptr == NULL) {
closeSound(&_sounds[slot]);
return NULL;
}
_sounds[slot].disk = _disk;
prepareSound(ptr, slot);
2004-01-07 21:02:48 +00:00
return &_sounds[slot];
}
return NULL;
}
2004-01-07 21:02:48 +00:00
void ImuseDigiSndMgr::closeSound(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
if (soundHandle->resPtr) {
bool found = false;
for (int l = 0; l < MAX_IMUSE_SOUNDS; l++) {
if ((_sounds[l].soundId == soundHandle->soundId) && (&_sounds[l] != soundHandle))
found = true;
}
if (!found)
_vm->unlock(rtSound, soundHandle->soundId);
}
2004-04-10 00:17:42 +00:00
delete soundHandle->bundle;
for (int r = 0; r < soundHandle->numSyncs; r++)
free(soundHandle->sync[r].ptr);
free(soundHandle->region);
free(soundHandle->jump);
free(soundHandle->sync);
memset(soundHandle, 0, sizeof(soundStruct));
}
ImuseDigiSndMgr::soundStruct *ImuseDigiSndMgr::cloneSound(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
return openSound(soundHandle->soundId, soundHandle->name, soundHandle->type, soundHandle->volGroupId, soundHandle->disk);
}
2004-01-07 21:02:48 +00:00
bool ImuseDigiSndMgr::checkForProperHandle(soundStruct *soundHandle) {
for (int l = 0; l < MAX_IMUSE_SOUNDS; l++) {
if (soundHandle == &_sounds[l])
return true;
}
return false;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getFreq(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
return soundHandle->freq;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getBits(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
return soundHandle->bits;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getChannels(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
return soundHandle->channels;
}
2004-01-07 21:02:48 +00:00
bool ImuseDigiSndMgr::isEndOfRegion(soundStruct *soundHandle, int region) {
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
assert(region >= 0 && region < soundHandle->numRegions);
return soundHandle->endFlag;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getNumRegions(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
return soundHandle->numRegions;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getNumJumps(soundStruct *soundHandle) {
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
return soundHandle->numJumps;
}
int ImuseDigiSndMgr::getRegionOffset(soundStruct *soundHandle, int region) {
debug(5, "getRegionOffset() region:%d");
assert(soundHandle && checkForProperHandle(soundHandle));
assert(region >= 0 && region < soundHandle->numRegions);
return soundHandle->region[region].offset;
}
2004-04-09 21:44:29 +00:00
int ImuseDigiSndMgr::getJumpIdByRegionAndHookId(soundStruct *soundHandle, int region, int hookId) {
debug(5, "getJumpIdByRegionAndHookId() region:%d, hookId:%d", region, hookId);
assert(soundHandle && checkForProperHandle(soundHandle));
assert(region >= 0 && region < soundHandle->numRegions);
int32 offset = soundHandle->region[region].offset;
2004-01-07 21:02:48 +00:00
for (int l = 0; l < soundHandle->numJumps; l++) {
if (offset == soundHandle->jump[l].offset) {
2004-04-09 21:44:29 +00:00
if (soundHandle->jump[l].hookId == hookId)
return l;
}
}
return -1;
}
void ImuseDigiSndMgr::getSyncSizeAndPtrById(soundStruct *soundHandle, int number, int32 &sync_size, byte **sync_ptr) {
2004-01-09 22:10:32 +00:00
assert(soundHandle && checkForProperHandle(soundHandle));
assert(number >= 0);
if (number < soundHandle->numSyncs) {
sync_size = soundHandle->sync[number].size;
*sync_ptr = soundHandle->sync[number].ptr;
} else {
sync_size = 0;
*sync_ptr = NULL;
}
2004-01-09 22:10:32 +00:00
}
int ImuseDigiSndMgr::getRegionIdByJumpId(soundStruct *soundHandle, int jumpId) {
debug(5, "getRegionIdByJumpId() jumpId:%d", jumpId);
assert(soundHandle && checkForProperHandle(soundHandle));
assert(jumpId >= 0 && jumpId < soundHandle->numJumps);
int32 dest = soundHandle->jump[jumpId].dest;
for (int l = 0; l < soundHandle->numRegions; l++) {
if (dest == soundHandle->region[l].offset) {
return l;
}
}
return -1;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getJumpHookId(soundStruct *soundHandle, int number) {
debug(5, "getJumpHookId() number:%d", number);
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
assert(number >= 0 && number < soundHandle->numJumps);
return soundHandle->jump[number].hookId;
}
2004-01-07 21:02:48 +00:00
int ImuseDigiSndMgr::getJumpFade(soundStruct *soundHandle, int number) {
debug(5, "getJumpFade() number:%d", number);
assert(soundHandle && checkForProperHandle(soundHandle));
2004-01-07 21:02:48 +00:00
assert(number >= 0 && number < soundHandle->numJumps);
return soundHandle->jump[number].fadeDelay;
}
2004-01-07 21:02:48 +00:00
int32 ImuseDigiSndMgr::getDataFromRegion(soundStruct *soundHandle, int region, byte **buf, int32 offset, int32 size) {
debug(5, "getDataFromRegion() region:%d, offset:%d, size:%d, numRegions:%d", region, offset, size, soundHandle->numRegions);
assert(soundHandle && checkForProperHandle(soundHandle));
assert(buf && offset >= 0 && size >= 0);
2004-01-07 21:02:48 +00:00
assert(region >= 0 && region < soundHandle->numRegions);
2004-01-07 21:02:48 +00:00
int32 region_offset = soundHandle->region[region].offset;
int32 region_length = soundHandle->region[region].length;
int32 offset_data = soundHandle->offsetData;
int32 start = region_offset - offset_data;
if (offset + size + offset_data > region_length) {
size = region_length - offset;
2004-01-07 21:02:48 +00:00
soundHandle->endFlag = true;
} else {
2004-01-07 21:02:48 +00:00
soundHandle->endFlag = false;
}
2004-01-07 21:02:48 +00:00
int header_size = soundHandle->offsetData;
bool header_outside = ((_vm->_gameId == GID_CMI) && !(_vm->_features & GF_DEMO));
if (soundHandle->bundle) {
size = soundHandle->bundle->decompressSampleByCurIndex(start + offset, size, buf, header_size, header_outside);
2004-01-07 21:02:48 +00:00
} else if (soundHandle->resPtr) {
*buf = (byte *)malloc(size);
2004-01-07 21:02:48 +00:00
memcpy(*buf, soundHandle->resPtr + start + offset + header_size, size);
}
return size;
}
} // End of namespace Scumm