DRACI: Change MusicPlayer to derive from Audio::MidiPlayer

This commit is contained in:
Max Horn 2011-03-24 16:38:31 +01:00
parent f4b30ecf6e
commit 4780eb7393
2 changed files with 14 additions and 126 deletions

View File

@ -36,7 +36,7 @@
namespace Draci {
MusicPlayer::MusicPlayer(const char *pathMask) : _parser(0), _driver(0), _pathMask(pathMask), _looping(false), _isPlaying(false), _isGM(false), _track(-1) {
MusicPlayer::MusicPlayer(const char *pathMask) : _pathMask(pathMask), _isGM(false), _track(-1) {
MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_GM);
_nativeMT32 = ((MidiDriver::getMusicType(dev) == MT_MT32) || ConfMan.getBool("native_mt32"));
@ -47,9 +47,6 @@ MusicPlayer::MusicPlayer(const char *pathMask) : _parser(0), _driver(0), _pathMa
if (_nativeMT32)
_driver->property(MidiDriver::PROP_CHANNEL_MASK, 0x03FE);
memset(_channel, 0, sizeof(_channel));
memset(_channelVolume, 127, sizeof(_channelVolume));
_masterVolume = 0;
_smfParser = MidiParser::createParser_SMF();
_midiMusicData = NULL;
@ -81,71 +78,17 @@ MusicPlayer::~MusicPlayer() {
delete[] _midiMusicData;
}
void MusicPlayer::setChannelVolume(int channel) {
int newVolume = _channelVolume[channel] * _masterVolume / 255;
debugC(3, kDraciSoundDebugLevel, "Music channel %d: volume %d->%d",
channel, _channelVolume[channel], newVolume);
_channel[channel]->volume(newVolume);
}
void MusicPlayer::setVolume(int volume) {
Common::StackLock lock(_mutex);
volume = CLIP(volume, 0, 255);
if (_masterVolume == volume)
return;
_masterVolume = volume;
for (int i = 0; i < 16; ++i) {
if (_channel[i]) {
setChannelVolume(i);
}
}
}
void MusicPlayer::send(uint32 b) {
byte channel = (byte)(b & 0x0F);
if ((b & 0xFFF0) == 0x07B0) {
// Adjust volume changes by master volume
byte volume = (byte)((b >> 16) & 0x7F);
_channelVolume[channel] = volume;
volume = volume * _masterVolume / 255;
b = (b & 0xFF00FFFF) | (volume << 16);
} else if ((b & 0xF0) == 0xC0 && !_isGM && !_nativeMT32) {
b = (b & 0xFFFF00FF) | MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8;
}
else if ((b & 0xFFF0) == 0x007BB0) {
//Only respond to All Notes Off if this channel
//has currently been allocated
if (!_channel[b & 0x0F])
return;
}
if (!_channel[channel]) {
_channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
void MusicPlayer::sendToChannel(byte channel, uint32 b) {
if (!_channelsTable[channel]) {
_channelsTable[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
// If a new channel is allocated during the playback, make sure
// its volume is correctly initialized.
if (_channel[channel])
setChannelVolume(channel);
if (_channelsTable[channel])
_channelsTable[channel]->volume(_channelsVolume[channel] * _masterVolume / 255);
}
if (_channel[channel])
_channel[channel]->send(b);
}
void MusicPlayer::metaEvent(byte type, byte *data, uint16 length) {
switch (type) {
case 0x2F: // End of Track
if (_looping)
_parser->jumpToTick(0);
else
stop();
break;
default:
//warning("Unhandled meta event: %02x", type);
break;
}
if (_channelsTable[channel])
_channelsTable[channel]->send(b);
}
void MusicPlayer::onTimer(void *refCon) {
@ -193,7 +136,7 @@ void MusicPlayer::playSMF(int track, bool loop) {
syncVolume();
_looping = loop;
_isLooping = loop;
_isPlaying = true;
_track = track;
debugC(2, kDraciSoundDebugLevel, "Playing track %d", track);
@ -203,39 +146,9 @@ void MusicPlayer::playSMF(int track, bool loop) {
}
void MusicPlayer::stop() {
Common::StackLock lock(_mutex);
if (!_isPlaying)
return;
Audio::MidiPlayer::stop();
debugC(2, kDraciSoundDebugLevel, "Stopping track %d", _track);
_track = -1;
_isPlaying = false;
if (_parser) {
_parser->unloadMusic();
_parser = NULL;
}
}
void MusicPlayer::pause() {
debugC(2, kDraciSoundDebugLevel, "Pausing track %d", _track);
setVolume(-1);
_isPlaying = false;
}
void MusicPlayer::resume() {
debugC(2, kDraciSoundDebugLevel, "Resuming track %d", _track);
syncVolume();
_isPlaying = true;
}
void MusicPlayer::syncVolume() {
int volume = ConfMan.getInt("music_volume");
if (ConfMan.getBool("mute")) {
volume = -1;
}
debugC(2, kDraciSoundDebugLevel, "Syncing music volume to %d", volume);
setVolume(volume);
}
} // End of namespace Draci

View File

@ -28,58 +28,33 @@
#ifndef DRACI_MUSIC_H
#define DRACI_MUSIC_H
#include "audio/mididrv.h"
#include "audio/midiparser.h"
#include "common/mutex.h"
#include "audio/midiplayer.h"
namespace Draci {
// Taken from MADE, which took it from SAGA.
class MusicPlayer : public MidiDriver_BASE {
class MusicPlayer : public Audio::MidiPlayer {
public:
MusicPlayer(const char *pathMask);
~MusicPlayer();
bool isPlaying() const { return _isPlaying; }
void setPlaying(bool playing) { _isPlaying = playing; }
void setVolume(int volume);
int getVolume() const { return _masterVolume; }
void syncVolume();
bool hasNativeMT32() const { return _nativeMT32; }
void playSMF(int track, bool loop);
void stop();
void pause();
void resume();
void setLoop(bool loop) { _looping = loop; }
void setGM(bool isGM) { _isGM = isGM; }
// MidiDriver_BASE interface implementation
virtual void send(uint32 b);
virtual void metaEvent(byte type, byte *data, uint16 length);
// Overload Audio::MidiPlayer method
virtual void sendToChannel(byte channel, uint32 b);
protected:
static void onTimer(void *data);
void setChannelVolume(int channel);
MidiParser *_parser;
Common::Mutex _mutex;
MidiChannel *_channel[16];
MidiDriver *_driver;
MidiParser *_smfParser;
Common::String _pathMask;
byte _channelVolume[16];
bool _nativeMT32;
bool _isGM;
bool _isPlaying;
bool _looping;
byte _masterVolume;
int _track;
byte *_midiMusicData;