Skip to content

Commit

Permalink
PRINCE: voice sample handling moved to prince module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Zbróg committed Nov 5, 2013
1 parent d6a76d9 commit 2b5b0b9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
51 changes: 50 additions & 1 deletion engines/prince/prince.cpp
Expand Up @@ -73,7 +73,7 @@ PrinceEngine::PrinceEngine(OSystem *syst, const PrinceGameDescription *gameDesc)
Engine(syst), _gameDescription(gameDesc), _graph(NULL), _script(NULL),
_locationNr(0), _debugger(NULL), _objectList(NULL), _mobList(NULL), _midiPlayer(NULL),
_cameraX(0), _newCameraX(0), _frameNr(0), _cursor1(NULL), _cursor2(NULL), _font(NULL),
_walizkaBmp(NULL), _roomBmp(NULL) {
_walizkaBmp(NULL), _roomBmp(NULL), _voiceStream(NULL) {

// Debug/console setup
DebugMan.addDebugChannel(DebugChannel::kScript, "script", "Prince Script debug channel");
Expand Down Expand Up @@ -304,6 +304,55 @@ bool PrinceEngine::playNextFrame() {
return true;
}

void PrinceEngine::playSample(uint16 sampleId, uint16 loopType) {
if (_voiceStream) {

Audio::RewindableAudioStream *audioStream = Audio::makeWAVStream(_voiceStream, DisposeAfterUse::YES);
_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, audioStream, sampleId);
}
}

void PrinceEngine::stopSample(uint16 sampleId) {
_mixer->stopID(sampleId);
_voiceStream = NULL;
}

bool PrinceEngine::loadVoice(uint32 slot, const Common::String &streamName) {
debugEngine("Loading wav %s slot %d", streamName.c_str(), slot);

_voiceStream = SearchMan.createReadStreamForMember(streamName);
if (!_voiceStream) {
error("Can't open %s", streamName.c_str());
return false;
}

uint32 id = _voiceStream->readUint32LE();
if (id != 0x46464952) {
error("It's not RIFF file %s", streamName.c_str());
return false;
}

_voiceStream->skip(0x20);
id = _voiceStream->readUint32LE();
if (id != 0x61746164) {
error("No data section in %s id %04x", streamName.c_str(), id);
return false;
}

id = _voiceStream->readUint32LE();
debugEngine("SetVoice slot %d time %04x", slot, id);
id <<= 3;
id /= 22050;
id += 2;

_textSlots[slot]._time = id;

debugEngine("SetVoice slot %d time %04x", slot, id);
_voiceStream->seek(0);

return true;
}

bool PrinceEngine::loadAnim(uint16 animNr, bool loop) {
Common::String streamName = Common::String::format("AN%02d", animNr);
Common::SeekableReadStream * flicStream = SearchMan.createReadStreamForMember(streamName);
Expand Down
7 changes: 7 additions & 0 deletions engines/prince/prince.h
Expand Up @@ -101,6 +101,10 @@ class PrinceEngine : public Engine {

bool loadLocation(uint16 locationNr);
bool loadAnim(uint16 animNr, bool loop);
bool loadVoice(uint32 slot, const Common::String &name);

void playSample(uint16 sampleId, uint16 loopType);
void stopSample(uint16 sampleId);

virtual GUI::Debugger *getDebugger();

Expand Down Expand Up @@ -139,6 +143,9 @@ class PrinceEngine : public Engine {
MobList *_mobList;
MusicPlayer *_midiPlayer;

Audio::SoundHandle _soundHandle;
Common::SeekableReadStream *_voiceStream;

uint16 _cameraX;
uint16 _newCameraX;
uint16 _sceneWidth;
Expand Down
54 changes: 11 additions & 43 deletions engines/prince/script.cpp
Expand Up @@ -40,7 +40,7 @@ static const uint16 NUM_OPCODES = 144;

Script::Script(PrinceEngine *vm) :
_code(NULL), _stacktop(0), _vm(vm), _opcodeNF(false),
_waitFlag(0), _voiceStream(NULL), _result(true) {
_waitFlag(0), _result(true) {
}

Script::~Script() {
Expand Down Expand Up @@ -200,12 +200,7 @@ void Script::O_PLAYSAMPLE() {
uint16 sampleId = readScript16bits();
uint16 loopType = readScript16bits();
debugScript("O_PLAYSAMPLE sampleId %d loopType %d", sampleId, loopType);

if (_voiceStream) {

Audio::RewindableAudioStream *audioStream = Audio::makeWAVStream(_voiceStream, DisposeAfterUse::YES);
_vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, audioStream, sampleId);
}
_vm->playSample(sampleId, loopType);
}

void Script::O_PUTOBJECT() {
Expand Down Expand Up @@ -875,9 +870,7 @@ void Script::O_SHOWDIALOGBOX() {
void Script::O_STOPSAMPLE() {
uint16 slot = readScript16bits();
debugScript("O_STOPSAMPLE slot %d", slot);

_vm->_mixer->stopID(slot);
_voiceStream = NULL;
_vm->stopSample(slot);
}

void Script::O_BACKANIMRANGE() {
Expand Down Expand Up @@ -1065,39 +1058,14 @@ void Script::O_SKIPTEXT() {
}

void Script::SetVoice(uint32 slot) {

const uint16 VOICE_H_LINE = getFlagValue(Flags::VOICE_H_LINE);

const Common::String streamName = Common::String::format("%03d-%02d.WAV", _currentString, VOICE_H_LINE);
debugScript("Loading wav %s slot %d", streamName.c_str(), slot);

_voiceStream = SearchMan.createReadStreamForMember(streamName);
if (!_voiceStream) {
error("Can't open %s", streamName.c_str());
}
uint32 id = _voiceStream->readUint32LE();
if (id != 0x46464952) {
error("It's not RIFF file %s", streamName.c_str());
return;
}

_voiceStream->skip(0x20);
id = _voiceStream->readUint32LE();
if (id != 0x61746164) {
error("No data section in %s id %04x", streamName.c_str(), id);
return;
}

id = _voiceStream->readUint32LE();
debugScript("SetVoice slot %d time %04x", slot, id);
id <<= 3;
id /= 22050;
id += 2;

_vm->_textSlots[slot]._time = id;

debugScript("SetVoice slot %d time %04x", slot, id);
_voiceStream->seek(0);
_vm->loadVoice(
slot,
Common::String::format(
"%03d-%02d.WAV",
_currentString,
getFlagValue(Flags::VOICE_H_LINE)
)
);
}

void Script::O_SETVOICEH() {
Expand Down
2 changes: 0 additions & 2 deletions engines/prince/script.h
Expand Up @@ -74,11 +74,9 @@ class Script {
uint8 _stacktop;
uint8 _savedStacktop;
uint32 _waitFlag;
Audio::SoundHandle _soundHandle;

const byte *_string;
uint32 _currentString;
Common::SeekableReadStream *_voiceStream;
const char *_mode;

// Helper functions
Expand Down

0 comments on commit 2b5b0b9

Please sign in to comment.