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 // MIDI and digital music class
24
25 #include "audio/mididrv.h"
26 #include "audio/midiparser.h"
27 #include "common/debug.h"
28 #include "common/file.h"
29
30 #include "gnap/music.h"
31 #include "gnap/gnap.h"
32
33 namespace Gnap {
34
MusicPlayer(const char * filename)35 MusicPlayer::MusicPlayer(const char *filename) : _filename(filename) {
36
37 MidiPlayer::createDriver();
38
39 int ret = _driver->open();
40 if (ret == 0) {
41 if (_nativeMT32)
42 _driver->sendMT32Reset();
43 else
44 _driver->sendGMReset();
45
46 _driver->setTimerCallback(this, &timerCallback);
47 }
48 }
49
sendToChannel(byte channel,uint32 b)50 void MusicPlayer::sendToChannel(byte channel, uint32 b) {
51 if (!_channelsTable[channel]) {
52 _channelsTable[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
53 // If a new channel is allocated during the playback, make sure
54 // its volume is correctly initialized.
55 if (_channelsTable[channel])
56 _channelsTable[channel]->volume(_channelsVolume[channel] * _masterVolume / 255);
57 }
58
59 if (_channelsTable[channel])
60 _channelsTable[channel]->send(b);
61 }
62
playSMF(bool loop)63 void MusicPlayer::playSMF(bool loop) {
64 Common::StackLock lock(_mutex);
65
66 stop();
67
68 // Load MIDI resource data
69 Common::File musicFile;
70 musicFile.open(_filename);
71 if (!musicFile.isOpen()) {
72 debugC(2, kDebugMusic, "Cannot open music file %s", _filename.c_str());
73 return;
74 }
75 int midiMusicSize = musicFile.size();
76 free(_midiData);
77 _midiData = (byte *)malloc(midiMusicSize);
78 musicFile.read(_midiData, midiMusicSize);
79 musicFile.close();
80
81 MidiParser *parser = MidiParser::createParser_SMF();
82 if (parser->loadMusic(_midiData, midiMusicSize)) {
83 parser->setTrack(0);
84 parser->setMidiDriver(this);
85 parser->setTimerRate(_driver->getBaseTempo());
86 parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
87
88 _parser = parser;
89
90 syncVolume();
91
92 _isLooping = loop;
93 _isPlaying = true;
94 } else {
95 debugC(2, kDebugMusic, "Cannot play music file %s", _filename.c_str());
96 delete parser;
97 }
98 }
99
stop()100 void MusicPlayer::stop() {
101 Audio::MidiPlayer::stop();
102 }
103
104 } // End of namespace Gnap
105