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 #include "common/debug.h"
23 #include "common/memstream.h"
24 #include "common/stream.h"
25 #include "audio/midiparser.h"
26 #include "audio/soundfont/rawfile.h"
27 #include "audio/soundfont/vab/vab.h"
28 #include "audio/soundfont/vgmcoll.h"
29 #include "midimusicplayer.h"
30
31
32 namespace Dragons {
33
MidiMusicPlayer(BigfileArchive * bigFileArchive)34 MidiMusicPlayer::MidiMusicPlayer(BigfileArchive *bigFileArchive): _midiDataSize(0) {
35 _midiData = nullptr;
36 MidiPlayer::createDriver(MDT_PREFER_FLUID | MDT_MIDI);
37
38 if (_driver->acceptsSoundFontData()) {
39 _driver->setEngineSoundFont(loadSoundFont(bigFileArchive));
40 } else {
41 //If the selected driver doesn't support loading soundfont we should assume we got a fluid Synth V1 and reload
42 delete _driver;
43 MidiPlayer::createDriver();
44 }
45
46 int ret = _driver->open();
47 if (ret == 0) {
48 if (_nativeMT32)
49 _driver->sendMT32Reset();
50 else
51 _driver->sendGMReset();
52
53 _driver->setTimerCallback(this, &timerCallback);
54 }
55 }
56
~MidiMusicPlayer()57 MidiMusicPlayer::~MidiMusicPlayer() {
58 if (isPlaying()) {
59 stop();
60 }
61 }
62
playSong(Common::SeekableReadStream * seqData)63 void MidiMusicPlayer::playSong(Common::SeekableReadStream *seqData) {
64 Common::StackLock lock(_mutex);
65
66 if (isPlaying()) {
67 stop();
68 }
69
70 if (seqData->readUint32LE() != MKTAG('S', 'E', 'Q', 'p'))
71 error("Failed to find SEQp tag");
72
73 // Make sure we don't have a SEP file (with multiple SEQ's inside)
74 if (seqData->readUint32BE() != 1)
75 error("Can only play SEQ files, not SEP");
76
77 uint16 ppqn = seqData->readUint16BE();
78 uint32 tempo = seqData->readUint16BE() << 8;
79 tempo |= seqData->readByte();
80 /* uint16 beat = */ seqData->readUint16BE();
81
82 // SEQ is directly based on SMF and we'll use that to our advantage here
83 // and convert to SMF and then use the SMF MidiParser.
84
85 // Calculate the SMF size we'll need
86 uint32 dataSize = seqData->size() - 15;
87 uint32 actualSize = dataSize + 7 + 22;
88
89 // Resize the buffer if necessary
90 byte *midiData = resizeMidiBuffer(actualSize);
91
92 // Now construct the header
93 WRITE_BE_UINT32(midiData, MKTAG('M', 'T', 'h', 'd'));
94 WRITE_BE_UINT32(midiData + 4, 6); // header size
95 WRITE_BE_UINT16(midiData + 8, 0); // type 0
96 WRITE_BE_UINT16(midiData + 10, 1); // one track
97 WRITE_BE_UINT16(midiData + 12, ppqn);
98 WRITE_BE_UINT32(midiData + 14, MKTAG('M', 'T', 'r', 'k'));
99 WRITE_BE_UINT32(midiData + 18, dataSize + 7); // SEQ data size + tempo change event size
100
101 // Add in a fake tempo change event
102 WRITE_BE_UINT32(midiData + 22, 0x00FF5103); // no delta, meta event, tempo change, param size = 3
103 WRITE_BE_UINT16(midiData + 26, tempo >> 8);
104 midiData[28] = tempo & 0xFF;
105
106 // Now copy in the rest of the events
107 seqData->read(midiData + 29, dataSize);
108
109 MidiParser *parser = MidiParser::createParser_SMF();
110 if (parser->loadMusic(midiData, actualSize)) {
111 parser->setTrack(0);
112 parser->setMidiDriver(this);
113 parser->setTimerRate(getBaseTempo());
114 parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
115 parser->property(MidiParser::mpSendSustainOffOnNotesOff, 1);
116
117 _parser = parser;
118
119 _isLooping = true;
120 _isPlaying = true;
121 } else {
122 delete parser;
123 }
124 }
125
resizeMidiBuffer(uint32 desiredSize)126 byte *MidiMusicPlayer::resizeMidiBuffer(uint32 desiredSize) {
127 if (_midiData == nullptr) {
128 _midiData = (byte *)malloc(desiredSize);
129 _midiDataSize = desiredSize;
130 } else {
131 if (desiredSize > _midiDataSize) {
132 _midiData = (byte *)realloc(_midiData, desiredSize);
133 _midiDataSize = desiredSize;
134 }
135 }
136 return _midiData;
137 }
138
setVolume(int volume)139 void MidiMusicPlayer::setVolume(int volume) {
140 // _vm->_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, volume); TODO do we need this?
141 MidiPlayer::setVolume(volume);
142 }
143
sendToChannel(byte channel,uint32 b)144 void MidiMusicPlayer::sendToChannel(byte channel, uint32 b) {
145 if (!_channelsTable[channel]) {
146 _channelsTable[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
147 // If a new channel is allocated during the playback, make sure
148 // its volume is correctly initialized.
149 if (_channelsTable[channel])
150 _channelsTable[channel]->volume(_channelsVolume[channel] * _masterVolume / 255);
151 }
152
153 if (_channelsTable[channel])
154 _channelsTable[channel]->send(b);
155 }
156
loadSoundFont(BigfileArchive * bigFileArchive)157 Common::SeekableReadStream *MidiMusicPlayer::loadSoundFont(BigfileArchive *bigFileArchive) {
158 uint32 headSize, bodySize;
159 byte *headData = bigFileArchive->load("musx.vh", headSize);
160 byte *bodyData = bigFileArchive->load("musx.vb", bodySize);
161
162 byte *vabData = (byte *)malloc(headSize + bodySize);
163
164 memcpy(vabData, headData, headSize);
165 memcpy(vabData + headSize, bodyData, bodySize);
166
167 free(headData);
168 free(bodyData);
169
170 MemFile *memFile = new MemFile(vabData, headSize + bodySize);
171 debug("Loading soundfont2 from musx vab file.");
172 Vab *vab = new Vab(memFile, 0);
173 vab->LoadVGMFile();
174 VGMColl vabCollection;
175 SF2File *file = vabCollection.CreateSF2File(vab);
176 const byte *bytes = (const byte *)file->SaveToMem();
177 uint32 size = file->GetSize();
178
179 delete file;
180 delete vab;
181 delete memFile;
182
183 return new Common::MemoryReadStream(bytes, size, DisposeAfterUse::YES);
184 }
185
186 } // End of namespace Dragons
187