Cleanup and simplification of the music playing code

svn-id: r31691
This commit is contained in:
Filippos Karapetis 2008-04-24 14:12:08 +00:00
parent 551bd59fa8
commit 068372ac94
4 changed files with 53 additions and 166 deletions

View File

@ -97,9 +97,9 @@ MadeEngine::MadeEngine(OSystem *syst, const MadeGameDescription *gameDesc) : Eng
if (native_mt32)
driver->property(MidiDriver::PROP_CHANNEL_MASK, 0x03FE);
_music = new Music(driver, _musicVolume);
_music = new MusicPlayer(driver);
_music->setNativeMT32(native_mt32);
_music->setAdlib(adlib);
//_music->setAdlib(adlib);
_musicVolume = ConfMan.getInt("music_volume");

View File

@ -61,7 +61,7 @@ class PmvPlayer;
class Screen;
class ScriptInterpreter;
class GameDatabase;
class Music;
class MusicPlayer;
class MadeEngine : public ::Engine {
int _gameId;
@ -94,7 +94,7 @@ public:
Screen *_screen;
GameDatabase *_dat;
ScriptInterpreter *_script;
Music *_music;
MusicPlayer *_music;
int _eventMouseX, _eventMouseY;
uint16 _eventKey;

View File

@ -41,12 +41,15 @@ MusicPlayer::MusicPlayer(MidiDriver *driver) : _parser(0), _driver(driver), _loo
memset(_channel, 0, sizeof(_channel));
_masterVolume = 0;
this->open();
_xmidiParser = MidiParser::createParser_XMIDI();
}
MusicPlayer::~MusicPlayer() {
_driver->setTimerCallback(NULL, NULL);
stopMusic();
stop();
this->close();
_xmidiParser->setMidiDriver(NULL);
delete _xmidiParser;
}
void MusicPlayer::setVolume(int volume) {
@ -78,7 +81,7 @@ int MusicPlayer::open() {
}
void MusicPlayer::close() {
stopMusic();
stop();
if (_driver)
_driver->close();
_driver = 0;
@ -121,7 +124,7 @@ void MusicPlayer::metaEvent(byte type, byte *data, uint16 length) {
if (_looping)
_parser->jumpToTick(0);
else
stopMusic();
stop();
break;
default:
//warning("Unhandled meta event: %02x", type);
@ -137,11 +140,40 @@ void MusicPlayer::onTimer(void *refCon) {
music->_parser->onTimer();
}
void MusicPlayer::playMusic() {
_isPlaying = true;
void MusicPlayer::play(XmidiResource *midiResource, MusicFlags flags) {
byte *resourceData;
size_t resourceSize;
if (_isPlaying)
return;
stop();
// Load MIDI/XMI resource data
_isGM = true;
resourceSize = midiResource->getSize();
resourceData = new byte[resourceSize];
memcpy(resourceData, midiResource->getData(), resourceSize);
if (_xmidiParser->loadMusic(resourceData, resourceSize)) {
MidiParser *parser = _xmidiParser;
parser->setTrack(0);
parser->setMidiDriver(this);
parser->setTimerRate(getBaseTempo());
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
_parser = parser;
setVolume(255);
_looping = flags & MUSIC_LOOP;
_isPlaying = true;
}
}
void MusicPlayer::stopMusic() {
void MusicPlayer::stop() {
Common::StackLock lock(_mutex);
_isPlaying = false;
@ -151,113 +183,14 @@ void MusicPlayer::stopMusic() {
}
}
Music::Music(MidiDriver *driver, int enabled) : _enabled(enabled), _adlib(false) {
_player = new MusicPlayer(driver);
_currentVolume = 0;
xmidiParser = MidiParser::createParser_XMIDI();
_songTableLen = 0;
_songTable = 0;
_midiMusicData = NULL;
void MusicPlayer::pause() {
setVolume(-1);
_isPlaying = false;
}
Music::~Music() {
delete _player;
xmidiParser->setMidiDriver(NULL);
delete xmidiParser;
free(_songTable);
if (_midiMusicData)
delete[] _midiMusicData;
}
void Music::setVolume(int volume) {
if (volume == -1) // Set Full volume
volume = 255;
_player->setVolume(volume);
_currentVolume = volume;
}
bool Music::isPlaying() {
return _player->isPlaying();
}
void Music::play(XmidiResource *midiResource, MusicFlags flags) {
MidiParser *parser = 0;
byte *resourceData;
size_t resourceSize;
debug(2, "Music::play %d", flags);
if (!_enabled || isPlaying()) {
return;
}
_player->stopMusic();
/*
if (!_vm->_musicVolume) {
return;
}
*/
if (flags == MUSIC_DEFAULT) {
flags = MUSIC_NORMAL;
}
// Load MIDI/XMI resource data
_player->setGM(true);
resourceSize = midiResource->getSize();
resourceData = new byte[resourceSize];
memcpy(resourceData, midiResource->getData(), resourceSize);
if (resourceSize < 4) {
error("Music::play() wrong music resource size");
}
if (xmidiParser->loadMusic(resourceData, resourceSize)) {
//_player->setGM(false);
parser = xmidiParser;
}
parser->setTrack(0);
parser->setMidiDriver(_player);
parser->setTimerRate(_player->getBaseTempo());
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
_player->_parser = parser;
//setVolume(_vm->_musicVolume == 10 ? 255 : _vm->_musicVolume * 25);
void MusicPlayer::resume() {
setVolume(255);
if (flags & MUSIC_LOOP)
_player->setLoop(true);
else
_player->setLoop(false);
_player->playMusic();
if (_midiMusicData)
delete[] _midiMusicData;
_midiMusicData = resourceData;
}
void Music::pause(void) {
_player->setVolume(-1);
_player->setPlaying(false);
}
void Music::resume(void) {
//_player->setVolume(_vm->_musicVolume == 10 ? 255 : _vm->_musicVolume * 25);
setVolume(255);
_player->setPlaying(true);
}
void Music::stop(void) {
_player->stopMusic();
_isPlaying = true;
}
} // End of namespace Made

View File

@ -28,12 +28,8 @@
#ifndef MADE_MUSIC_H
#define MADE_MUSIC_H
#include "sound/audiocd.h"
#include "sound/mididrv.h"
#include "sound/midiparser.h"
#include "sound/mp3.h"
#include "sound/vorbis.h"
#include "sound/flac.h"
#include "common/mutex.h"
#include "made/resource.h"
@ -42,8 +38,7 @@ namespace Made {
enum MusicFlags {
MUSIC_NORMAL = 0,
MUSIC_LOOP = 0x0001,
MUSIC_DEFAULT = 0xffff
MUSIC_LOOP = 1
};
class MusicPlayer : public MidiDriver {
@ -59,8 +54,10 @@ public:
void setNativeMT32(bool b) { _nativeMT32 = b; }
bool hasNativeMT32() { return _nativeMT32; }
void playMusic();
void stopMusic();
void play(XmidiResource *midiResource, MusicFlags flags = MUSIC_NORMAL);
void stop();
void pause();
void resume();
void setLoop(bool loop) { _looping = loop; }
void setPassThrough(bool b) { _passThrough = b; }
@ -89,6 +86,7 @@ protected:
MidiChannel *_channel[16];
MidiDriver *_driver;
MidiParser *_xmidiParser;
byte _channelVolume[16];
bool _nativeMT32;
bool _isGM;
@ -96,51 +94,7 @@ protected:
bool _isPlaying;
bool _looping;
bool _randomLoop;
byte _masterVolume;
byte *_musicData;
uint16 *_buf;
size_t _musicDataSize;
};
class Music {
public:
Music(MidiDriver *driver, int enabled);
~Music(void);
void setNativeMT32(bool b) { _player->setNativeMT32(b); }
bool hasNativeMT32() { return _player->hasNativeMT32(); }
void setAdlib(bool b) { _adlib = b; }
bool hasAdlib() { return _adlib; }
void setPassThrough(bool b) { _player->setPassThrough(b); }
bool isPlaying(void);
void play(XmidiResource *midiResource, MusicFlags flags = MUSIC_DEFAULT);
void pause(void);
void resume(void);
void stop(void);
void setVolume(int volume);
int getVolume() { return _currentVolume; }
int32 *_songTable;
int _songTableLen;
private:
MusicPlayer *_player;
uint32 _trackNumber;
int _enabled;
bool _adlib;
int _currentVolume;
MidiParser *xmidiParser;
byte *_midiMusicData;
};
} // End of namespace Made