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 /*
24  * This code is based on original Sfinx source code
25  * Copyright (c) 1994-1997 Janus B. Wisniewski and L.K. Avalon
26  */
27 
28 #include "cge2/sound.h"
29 #include "common/memstream.h"
30 #include "audio/audiostream.h"
31 #include "audio/decoders/wave.h"
32 #include "audio/mididrv.h"
33 #include "audio/midiparser.h"
34 #include "cge2/cge2.h"
35 
36 namespace CGE2 {
37 
DataCk(byte * buf,int bufSize)38 DataCk::DataCk(byte *buf, int bufSize) {
39 	_buf = buf;
40 	_ckSize = bufSize;
41 }
42 
~DataCk()43 DataCk::~DataCk() {
44 	free(_buf);
45 }
46 
Sound(CGE2Engine * vm)47 Sound::Sound(CGE2Engine *vm) : _vm(vm) {
48 	_audioStream = nullptr;
49 	_soundRepeatCount = 1;
50 	open();
51 }
52 
~Sound()53 Sound::~Sound() {
54 	close();
55 }
56 
close()57 void Sound::close() {
58 	_vm->_midiPlayer->killMidi();
59 }
60 
open()61 void Sound::open() {
62 	setRepeat(1);
63 	if (_vm->_commandHandlerTurbo != nullptr)
64 		_vm->switchSay();
65 	play(Audio::Mixer::kSFXSoundType, _vm->_fx->load(99, 99));
66 }
67 
setRepeat(int16 count)68 void Sound::setRepeat(int16 count) {
69 	_soundRepeatCount = count;
70 }
71 
getRepeat()72 int16 Sound::getRepeat() {
73 	return _soundRepeatCount;
74 }
75 
play(Audio::Mixer::SoundType soundType,DataCk * wav,int pan)76 void Sound::play(Audio::Mixer::SoundType soundType, DataCk *wav, int pan) {
77 	if (wav) {
78 		stop();
79 		_smpinf._saddr = &*(wav->addr());
80 		_smpinf._slen = (uint16)wav->size();
81 		_smpinf._span = pan;
82 		_smpinf._counter = getRepeat();
83 		sndDigiStart(&_smpinf, soundType);
84 	}
85 }
86 
sndDigiStart(SmpInfo * PSmpInfo,Audio::Mixer::SoundType soundType)87 void Sound::sndDigiStart(SmpInfo *PSmpInfo, Audio::Mixer::SoundType soundType) {
88 	// Create an audio stream wrapper for sound
89 	Common::MemoryReadStream *stream = new Common::MemoryReadStream(PSmpInfo->_saddr,
90 		PSmpInfo->_slen, DisposeAfterUse::NO);
91 	_audioStream = Audio::makeWAVStream(stream, DisposeAfterUse::YES);
92 
93 	// Decide which handle to use
94 	Audio::SoundHandle *handle = nullptr;
95 	switch (soundType) {
96 	case Audio::Mixer::kSFXSoundType:
97 		handle = &_sfxHandle;
98 		break;
99 	case Audio::Mixer::kSpeechSoundType:
100 		handle = &_speechHandle;
101 		break;
102 	default:
103 		error("Wrong sound type passed to sndDigiStart()");
104 	}
105 
106 	// Start the new sound
107 	_vm->_mixer->playStream(soundType, handle,
108 		Audio::makeLoopingAudioStream(_audioStream, (uint)PSmpInfo->_counter));
109 
110 	// CGE pan:
111 	// 8 = Center
112 	// Less = Left
113 	// More = Right
114 	_vm->_mixer->setChannelBalance(*handle, (int8)CLIP(((PSmpInfo->_span - 8) * 16), -127, 127));
115 }
116 
stop()117 void Sound::stop() {
118 	sndDigiStop(_sfxHandle);
119 	sndDigiStop(_speechHandle);
120 	_audioStream = nullptr;
121 }
122 
checkSoundHandles()123 void Sound::checkSoundHandles() {
124 	if (!_vm->_mixer->isSoundHandleActive(_speechHandle) && !_vm->_mixer->isSoundHandleActive(_sfxHandle))
125 		_smpinf._counter = 0;
126 }
127 
sndDigiStop(Audio::SoundHandle & handle)128 void Sound::sndDigiStop(Audio::SoundHandle &handle) {
129 	if (_vm->_mixer->isSoundHandleActive(handle))
130 		_vm->_mixer->stopHandle(handle);
131 }
132 
Fx(CGE2Engine * vm,int size)133 Fx::Fx(CGE2Engine *vm, int size) : _current(nullptr), _vm(vm) {
134 }
135 
~Fx()136 Fx::~Fx() {
137 	clear();
138 }
139 
clear()140 void Fx::clear() {
141 	if (_current)
142 		delete _current;
143 	_current = nullptr;
144 }
145 
name(int ref,int sub)146 Common::String Fx::name(int ref, int sub) {
147 	const char *fxname = "%.2dfx%.2d.WAV";
148 	const char *subName = "%.2dfx%.2d?.WAV";
149 	const char *p = (sub) ? subName : fxname;
150 	Common::String filename = Common::String::format(p, ref >> 8, ref & 0xFF);
151 	if (sub)
152 		filename.setChar('@' + sub, 6);
153 	return filename;
154 }
155 
exist(int ref,int sub)156 bool Fx::exist(int ref, int sub) {
157 	return _vm->_resman->exist(name(ref, sub).c_str());
158 }
159 
load(int ref,int sub)160 DataCk *Fx::load(int ref, int sub) {
161 	Common::String filename = name(ref, sub);
162 	EncryptedStream file(_vm, filename.c_str());
163 	clear();
164 	return (_current = loadWave(&file));
165 }
166 
loadWave(EncryptedStream * file)167 DataCk *Fx::loadWave(EncryptedStream *file) {
168 	byte *data = (byte *)malloc(file->size());
169 
170 	if (!data)
171 		return 0;
172 
173 	file->read(data, file->size());
174 
175 	return new DataCk(data, file->size());
176 }
177 
MusicPlayer(CGE2Engine * vm)178 MusicPlayer::MusicPlayer(CGE2Engine *vm) : _vm(vm) {
179 	_data = nullptr;
180 	_isGM = false;
181 
182 	MidiPlayer::createDriver();
183 
184 	int ret = _driver->open();
185 	if (ret == 0) {
186 		if (_nativeMT32)
187 			_driver->sendMT32Reset();
188 		else
189 			_driver->sendGMReset();
190 
191 		// TODO: Load cmf.ins with the instrument table.  It seems that an
192 		// interface for such an operation is supported for AdLib.  Maybe for
193 		// this card, setting instruments is necessary.
194 
195 		_driver->setTimerCallback(this, &timerCallback);
196 	}
197 	_dataSize = -1;
198 }
199 
~MusicPlayer()200 MusicPlayer::~MusicPlayer() {
201 	killMidi();
202 }
203 
killMidi()204 void MusicPlayer::killMidi() {
205 	Audio::MidiPlayer::stop();
206 
207 	free(_data);
208 	_data = nullptr;
209 }
210 
loadMidi(int ref)211 void MusicPlayer::loadMidi(int ref) {
212 	if (_vm->_midiNotify != nullptr)
213 		(_vm->*_vm->_midiNotify)();
214 
215 	// Work out the filename and check the given MIDI file exists
216 	Common::String filename = Common::String::format("%.2dSG%.2d.MID", ref >> 8, ref & 0xFF);
217 	if (!_vm->_resman->exist(filename.c_str()))
218 		return;
219 
220 	// Stop any currently playing MIDI file
221 	killMidi();
222 
223 	// Read in the data for the file
224 	EncryptedStream mid(_vm, filename.c_str());
225 	_dataSize = mid.size();
226 	_data = (byte *)malloc(_dataSize);
227 	mid.read(_data, _dataSize);
228 
229 	// Start playing the music
230 	sndMidiStart();
231 }
232 
sndMidiStart()233 void MusicPlayer::sndMidiStart() {
234 	_isGM = true;
235 
236 	MidiParser *parser = MidiParser::createParser_SMF();
237 	if (parser->loadMusic(_data, _dataSize)) {
238 		parser->setTrack(0);
239 		parser->setMidiDriver(this);
240 		parser->setTimerRate(_driver->getBaseTempo());
241 		parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
242 
243 		_parser = parser;
244 
245 		syncVolume();
246 
247 		// Al the tracks are supposed to loop
248 		_isLooping = true;
249 		_isPlaying = true;
250 	}
251 }
252 
send(uint32 b)253 void MusicPlayer::send(uint32 b) {
254 	if (((b & 0xF0) == 0xC0) && !_isGM && !_nativeMT32) {
255 		b = (b & 0xFFFF00FF) | MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8;
256 	}
257 
258 	Audio::MidiPlayer::send(b);
259 }
260 
sendToChannel(byte channel,uint32 b)261 void MusicPlayer::sendToChannel(byte channel, uint32 b) {
262 	if (!_channelsTable[channel]) {
263 		_channelsTable[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
264 		// If a new channel is allocated during the playback, make sure
265 		// its volume is correctly initialized.
266 		if (_channelsTable[channel])
267 			_channelsTable[channel]->volume(_channelsVolume[channel] * _masterVolume / 255);
268 	}
269 
270 	if (_channelsTable[channel])
271 		_channelsTable[channel]->send(b);
272 }
273 
274 } // End of namespace CGE2
275