2009-10-21 08:41:57 +00:00
|
|
|
/* 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.
|
|
|
|
*
|
2009-10-30 05:21:44 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2009-10-21 08:41:57 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// MIDI and digital music class
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/mididrv.h"
|
|
|
|
#include "audio/midiparser.h"
|
2009-10-21 08:41:57 +00:00
|
|
|
#include "common/config-manager.h"
|
|
|
|
#include "common/file.h"
|
|
|
|
|
|
|
|
#include "draci/draci.h"
|
|
|
|
#include "draci/music.h"
|
|
|
|
|
|
|
|
namespace Draci {
|
|
|
|
|
2011-03-24 16:38:31 +01:00
|
|
|
MusicPlayer::MusicPlayer(const char *pathMask) : _pathMask(pathMask), _isGM(false), _track(-1) {
|
2011-03-23 16:14:39 +01:00
|
|
|
|
|
|
|
MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_GM);
|
|
|
|
_nativeMT32 = ((MidiDriver::getMusicType(dev) == MT_MT32) || ConfMan.getBool("native_mt32"));
|
|
|
|
//bool adlib = (MidiDriver::getMusicType(dev) == MT_ADLIB);
|
|
|
|
|
|
|
|
_driver = MidiDriver::createMidi(dev);
|
|
|
|
assert(_driver);
|
|
|
|
if (_nativeMT32)
|
|
|
|
_driver->property(MidiDriver::PROP_CHANNEL_MASK, 0x03FE);
|
|
|
|
|
2009-10-21 08:41:57 +00:00
|
|
|
_smfParser = MidiParser::createParser_SMF();
|
|
|
|
_midiMusicData = NULL;
|
2011-03-23 16:14:39 +01:00
|
|
|
|
|
|
|
int ret = _driver->open();
|
|
|
|
if (ret == 0) {
|
|
|
|
if (_nativeMT32)
|
|
|
|
_driver->sendMT32Reset();
|
|
|
|
else
|
|
|
|
_driver->sendGMReset();
|
|
|
|
|
|
|
|
// TODO: Load cmf.ins with the instrument table. It seems that an
|
|
|
|
// interface for such an operation is supported for AdLib. Maybe for
|
|
|
|
// this card, setting instruments is necessary.
|
|
|
|
|
|
|
|
_driver->setTimerCallback(this, &onTimer);
|
|
|
|
}
|
2009-10-21 08:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MusicPlayer::~MusicPlayer() {
|
|
|
|
_driver->setTimerCallback(NULL, NULL);
|
|
|
|
stop();
|
2011-03-23 16:14:39 +01:00
|
|
|
if (_driver) {
|
|
|
|
_driver->close();
|
|
|
|
delete _driver;
|
|
|
|
_driver = 0;
|
|
|
|
}
|
2009-10-21 08:41:57 +00:00
|
|
|
_smfParser->setMidiDriver(NULL);
|
|
|
|
delete _smfParser;
|
2009-10-26 22:04:03 +00:00
|
|
|
delete[] _midiMusicData;
|
2009-10-21 08:41:57 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 16:38:31 +01:00
|
|
|
void MusicPlayer::sendToChannel(byte channel, uint32 b) {
|
|
|
|
if (!_channelsTable[channel]) {
|
|
|
|
_channelsTable[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
|
2009-11-08 22:48:27 +00:00
|
|
|
// If a new channel is allocated during the playback, make sure
|
|
|
|
// its volume is correctly initialized.
|
2011-03-24 16:38:31 +01:00
|
|
|
if (_channelsTable[channel])
|
|
|
|
_channelsTable[channel]->volume(_channelsVolume[channel] * _masterVolume / 255);
|
2009-10-26 09:03:54 +00:00
|
|
|
}
|
2009-10-21 08:41:57 +00:00
|
|
|
|
2011-03-24 16:38:31 +01:00
|
|
|
if (_channelsTable[channel])
|
|
|
|
_channelsTable[channel]->send(b);
|
2009-10-21 08:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::onTimer(void *refCon) {
|
|
|
|
MusicPlayer *music = (MusicPlayer *)refCon;
|
|
|
|
Common::StackLock lock(music->_mutex);
|
|
|
|
|
2009-10-26 09:03:54 +00:00
|
|
|
if (music->_parser)
|
2009-10-21 08:41:57 +00:00
|
|
|
music->_parser->onTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::playSMF(int track, bool loop) {
|
2009-10-22 06:21:39 +00:00
|
|
|
if (_isPlaying && track == _track) {
|
|
|
|
debugC(2, kDraciSoundDebugLevel, "Already plaing track %d", track);
|
2009-10-21 08:41:57 +00:00
|
|
|
return;
|
2009-10-22 06:21:39 +00:00
|
|
|
}
|
2009-10-21 08:41:57 +00:00
|
|
|
|
|
|
|
stop();
|
|
|
|
|
|
|
|
_isGM = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Load MIDI resource data
|
|
|
|
Common::File musicFile;
|
|
|
|
char musicFileName[40];
|
|
|
|
sprintf(musicFileName, _pathMask.c_str(), track);
|
|
|
|
musicFile.open(musicFileName);
|
2009-10-22 06:21:39 +00:00
|
|
|
if (!musicFile.isOpen()) {
|
|
|
|
debugC(2, kDraciSoundDebugLevel, "Cannot open track %d", track);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-21 08:41:57 +00:00
|
|
|
int midiMusicSize = musicFile.size();
|
2009-10-26 22:04:03 +00:00
|
|
|
delete[] _midiMusicData;
|
2009-10-21 08:41:57 +00:00
|
|
|
_midiMusicData = new byte[midiMusicSize];
|
|
|
|
musicFile.read(_midiMusicData, midiMusicSize);
|
|
|
|
musicFile.close();
|
|
|
|
|
|
|
|
if (_smfParser->loadMusic(_midiMusicData, midiMusicSize)) {
|
|
|
|
MidiParser *parser = _smfParser;
|
|
|
|
parser->setTrack(0);
|
|
|
|
parser->setMidiDriver(this);
|
2011-03-23 15:23:26 +01:00
|
|
|
parser->setTimerRate(_driver->getBaseTempo());
|
2009-10-21 08:41:57 +00:00
|
|
|
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
|
|
|
|
|
|
|
|
_parser = parser;
|
|
|
|
|
2009-10-22 06:21:39 +00:00
|
|
|
syncVolume();
|
2009-10-21 08:41:57 +00:00
|
|
|
|
2011-03-24 16:38:31 +01:00
|
|
|
_isLooping = loop;
|
2009-10-21 08:41:57 +00:00
|
|
|
_isPlaying = true;
|
|
|
|
_track = track;
|
2009-10-22 06:21:39 +00:00
|
|
|
debugC(2, kDraciSoundDebugLevel, "Playing track %d", track);
|
|
|
|
} else {
|
|
|
|
debugC(2, kDraciSoundDebugLevel, "Cannot play track %d", track);
|
2009-10-21 08:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::stop() {
|
2011-03-24 16:38:31 +01:00
|
|
|
Audio::MidiPlayer::stop();
|
2009-10-22 06:21:39 +00:00
|
|
|
debugC(2, kDraciSoundDebugLevel, "Stopping track %d", _track);
|
2009-11-12 22:52:00 +00:00
|
|
|
_track = -1;
|
2009-10-22 07:34:43 +00:00
|
|
|
}
|
2009-10-21 08:41:57 +00:00
|
|
|
|
|
|
|
} // End of namespace Draci
|