1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "groovie/music.h"
#include "groovie/resource.h"
namespace Groovie {
MusicPlayer::MusicPlayer(GroovieEngine *vm) :
_vm(vm), _midiParser(NULL), _data(NULL), _driver(NULL),
_backgroundFileRef(0), _gameVolume(100) {
// Create the parser
_midiParser = MidiParser::createParser_XMIDI();
// Create the driver
int driver = detectMusicDriver(MDT_MIDI | MDT_ADLIB | MDT_PREFER_MIDI);
_driver = createMidi(driver);
this->open();
// Initialize the channel volumes
for (int i = 0; i < 0x10; i++) {
_chanVolumes[i] = 0x7F;
}
// Set the parser's driver
_midiParser->setMidiDriver(this);
// Set the timer rate
_midiParser->setTimerRate(_driver->getBaseTempo());
}
MusicPlayer::~MusicPlayer() {
_driver->setTimerCallback(NULL, NULL);
Common::StackLock lock(_mutex);
// Unload the parser
unload();
delete _midiParser;
// Unload the MIDI Driver
_driver->close();
delete _driver;
}
void MusicPlayer::playSong(uint16 fileref) {
Common::StackLock lock(_mutex);
// Play the referenced file once
play(fileref, false);
}
void MusicPlayer::setBackgroundSong(uint16 fileref) {
Common::StackLock lock(_mutex);
debugC(1, kGroovieDebugMIDI | kGroovieDebugAll, "Groovie::Music: Changing the background song: %04X", fileref);
_backgroundFileRef = fileref;
}
void MusicPlayer::setUserVolume(uint16 volume) {
Common::StackLock lock(_mutex);
// Save the new user volume
_userVolume = volume;
if (_userVolume > 0x100)
_userVolume = 0x100;
// Apply it to all the channels
for (int i = 0; i < 0x10; i++) {
updateChanVolume(i);
}
//FIXME: AdlibPercussionChannel::controlChange() is empty
//(can't set the volume for the percusion channel)
}
void MusicPlayer::setGameVolume(uint16 volume, uint16 time) {
Common::StackLock lock(_mutex);
debugC(1, kGroovieDebugMIDI | kGroovieDebugAll, "Groovie::Music: Setting game volume from %d to %d in %dms", _gameVolume, volume, time);
// Save the start parameters of the fade
_fadingStartTime = _vm->_system->getMillis();
_fadingStartVolume = _gameVolume;
_fadingDuration = time;
// Save the new game volume
_fadingEndVolume = volume;
if (_fadingEndVolume > 100)
_fadingEndVolume = 100;
}
void MusicPlayer::applyFading() {
Common::StackLock lock(_mutex);
// Calculate the passed time
uint32 time = _vm->_system->getMillis() - _fadingStartTime;
if (time >= _fadingDuration) {
// If we were fading to 0, stop the playback and restore the volume
if (_fadingEndVolume == 0) {
unload();
_fadingEndVolume = 100;
}
// Set the end volume
_gameVolume = _fadingEndVolume;
} else {
// Calculate the interpolated volume for the current time
_gameVolume = (_fadingStartVolume * (_fadingDuration - time) +
_fadingEndVolume * time) / _fadingDuration;
}
// Apply the new volume to all the channels
for (int i = 0; i < 0x10; i++) {
updateChanVolume(i);
}
}
void MusicPlayer::updateChanVolume(byte channel) {
// Generate a MIDI Control change message for the volume
uint32 b = 0x7B0;
// Specify the channel
b |= (channel & 0xF);
// Scale by the user and game volumes
uint32 val = (_chanVolumes[channel] * _userVolume * _gameVolume) / 0x100 / 100;
val &= 0x7F;
// Send it to the driver
_driver->send(b | (val << 16));
}
bool MusicPlayer::play(uint16 fileref, bool loop) {
// Unload the previous song
unload();
// Set the looping option
_midiParser->property(MidiParser::mpAutoLoop, loop);
// Load the new file
return load(fileref);
}
bool MusicPlayer::load(uint16 fileref) {
debugC(1, kGroovieDebugMIDI | kGroovieDebugAll, "Groovie::Music: Starting the playback of song: %04X", fileref);
// Open the song resource
Common::SeekableReadStream *xmidiFile = _vm->_resMan->open(fileref);
if (!xmidiFile) {
error("Groovie::Music: Couldn't resource 0x%04X", fileref);
return false;
}
// Read the whole file to memory
int length = xmidiFile->size();
_data = new byte[length];
xmidiFile->read(_data, length);
delete xmidiFile;
// Start parsing the data
if (!_midiParser->loadMusic(_data, length)) {
error("Groovie::Music: Invalid XMI file");
return false;
}
// Activate the timer source
_driver->setTimerCallback(this, &onTimer);
return true;
}
void MusicPlayer::unload() {
debugC(1, kGroovieDebugMIDI | kGroovieDebugAll, "Groovie::Music: Stopping the playback");
// Unload the parser
_midiParser->unloadMusic();
// Unload the xmi file
delete[] _data;
_data = NULL;
}
int MusicPlayer::open() {
// Don't ever call open without first setting the output driver!
if (!_driver)
return 255;
int ret = _driver->open();
if (ret)
return ret;
return 0;
}
void MusicPlayer::close() {}
void MusicPlayer::send(uint32 b) {
if ((b & 0xFFF0) == 0x07B0) { // Volume change
// Save the specific channel volume
byte chan = b & 0xF;
_chanVolumes[chan] = (b >> 16) & 0x7F;
// Send the updated value
updateChanVolume(chan);
return;
}
_driver->send(b);
}
void MusicPlayer::metaEvent(byte type, byte *data, uint16 length) {
switch (type) {
case 0x2F:
// End of Track, play the background song
debugC(1, kGroovieDebugMIDI | kGroovieDebugAll, "Groovie::Music: End of song");
if (_backgroundFileRef) {
play(_backgroundFileRef, true);
}
break;
default:
_driver->metaEvent(type, data, length);
break;
}
}
void MusicPlayer::onTimer(void *refCon) {
MusicPlayer *music = (MusicPlayer *)refCon;
Common::StackLock lock(music->_mutex);
// Apply the game volume fading
if (music->_gameVolume != music->_fadingEndVolume) {
// Apply the next step of the fading
music->applyFading();
}
// TODO: We really only need to call this while music is playing.
music->_midiParser->onTimer();
}
void MusicPlayer::setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) {
_driver->setTimerCallback(timer_param, timer_proc);
}
uint32 MusicPlayer::getBaseTempo(void) {
return _driver->getBaseTempo();
}
MidiChannel *MusicPlayer::allocateChannel() {
return _driver->allocateChannel();
}
MidiChannel *MusicPlayer::getPercussionChannel() {
return _driver->getPercussionChannel();
}
} // End of Groovie namespace
|