1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/scummsys.h"
24 
25 #include "zvision/scripting/effects/syncsound_effect.h"
26 
27 #include "zvision/zvision.h"
28 #include "zvision/scripting/script_manager.h"
29 #include "zvision/graphics/render_manager.h"
30 #include "zvision/sound/zork_raw.h"
31 
32 #include "common/stream.h"
33 #include "common/file.h"
34 #include "audio/decoders/wave.h"
35 
36 namespace ZVision {
37 
SyncSoundNode(ZVision * engine,uint32 key,Common::String & filename,int32 syncto)38 SyncSoundNode::SyncSoundNode(ZVision *engine, uint32 key, Common::String &filename, int32 syncto)
39 	: ScriptingEffect(engine, key, SCRIPTING_EFFECT_AUDIO) {
40 	_syncto = syncto;
41 	_sub = NULL;
42 
43 	Audio::RewindableAudioStream *audioStream = NULL;
44 
45 	if (filename.contains(".wav")) {
46 		Common::File *file = new Common::File();
47 		if (_engine->getSearchManager()->openFile(*file, filename)) {
48 			audioStream = Audio::makeWAVStream(file, DisposeAfterUse::YES);
49 		}
50 	} else {
51 		audioStream = makeRawZorkStream(filename, _engine);
52 	}
53 
54 	_engine->_mixer->playStream(Audio::Mixer::kPlainSoundType, &_handle, audioStream);
55 
56 	Common::String subname = filename;
57 	subname.setChar('s', subname.size() - 3);
58 	subname.setChar('u', subname.size() - 2);
59 	subname.setChar('b', subname.size() - 1);
60 
61 	if (_engine->getSearchManager()->hasFile(subname))
62 		_sub = new Subtitle(_engine, subname);
63 }
64 
~SyncSoundNode()65 SyncSoundNode::~SyncSoundNode() {
66 	_engine->_mixer->stopHandle(_handle);
67 	if (_sub)
68 		delete _sub;
69 }
70 
process(uint32 deltaTimeInMillis)71 bool SyncSoundNode::process(uint32 deltaTimeInMillis) {
72 	if (! _engine->_mixer->isSoundHandleActive(_handle))
73 		return stop();
74 	else {
75 
76 		if (_engine->getScriptManager()->getSideFX(_syncto) == NULL)
77 			return stop();
78 
79 		if (_sub && _engine->getScriptManager()->getStateValue(StateKey_Subtitles) == 1)
80 			_sub->process(_engine->_mixer->getSoundElapsedTime(_handle) / 100);
81 	}
82 	return false;
83 }
84 
85 } // End of namespace ZVision
86