diff options
Diffstat (limited to 'engines/hugo/sound.cpp')
-rw-r--r-- | engines/hugo/sound.cpp | 87 |
1 files changed, 23 insertions, 64 deletions
diff --git a/engines/hugo/sound.cpp b/engines/hugo/sound.cpp index 04da0c89ee..24359a2c54 100644 --- a/engines/hugo/sound.cpp +++ b/engines/hugo/sound.cpp @@ -47,58 +47,46 @@ namespace Hugo { -MidiPlayer::MidiPlayer(MidiDriver *driver) - : _midiData(0) { +MidiPlayer::MidiPlayer() { + MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_GM); + _driver = MidiDriver::createMidi(dev); assert(_driver); _paused = false; -} -MidiPlayer::~MidiPlayer() { - stop(); - Common::StackLock lock(_mutex); - _driver->setTimerCallback(0, 0); - _driver->close(); - delete _driver; - _driver = 0; - if (_parser) - _parser->setMidiDriver(0); - delete _parser; + int ret = _driver->open(); + if (ret == 0) { + _driver->sendGMReset(); + + _driver->setTimerCallback(this, &timerCallback); + } } void MidiPlayer::play(uint8 *stream, uint16 size) { debugC(3, kDebugMusic, "MidiPlayer::play"); - if (!stream) { - stop(); - return; - } + + Common::StackLock lock(_mutex); stop(); + if (!stream) + return; + _midiData = (uint8 *)malloc(size); if (_midiData) { memcpy(_midiData, stream, size); - Common::StackLock lock(_mutex); syncVolume(); // FIXME: syncVolume calls setVolume which in turn also locks the mutex! ugh + + _parser = MidiParser::createParser_SMF(); _parser->loadMusic(_midiData, size); _parser->setTrack(0); + _parser->setMidiDriver(this); + _parser->setTimerRate(_driver->getBaseTempo()); _isLooping = false; _isPlaying = true; } } -void MidiPlayer::stop() { - debugC(3, kDebugMusic, "MidiPlayer::stop"); - - Common::StackLock lock(_mutex); - if (_isPlaying) { - _isPlaying = false; - _parser->unloadMusic(); - free(_midiData); - _midiData = 0; - } -} - void MidiPlayer::pause(bool p) { _paused = p; @@ -109,34 +97,14 @@ void MidiPlayer::pause(bool p) { } } -void MidiPlayer::updateTimer() { - if (_paused) { - return; - } - +void MidiPlayer::onTimer() { Common::StackLock lock(_mutex); - if (_isPlaying) { + + if (!_paused && _isPlaying && _parser) { _parser->onTimer(); } } -int MidiPlayer::open() { - if (!_driver) - return 255; - int ret = _driver->open(); - if (ret) - return ret; - - _driver->sendGMReset(); - - _parser = MidiParser::createParser_SMF(); - _parser->setMidiDriver(this); - _parser->setTimerRate(_driver->getBaseTempo()); - _driver->setTimerCallback(this, &timerCallback); - - return 0; -} - void MidiPlayer::sendToChannel(byte channel, uint32 b) { if (!_channelsTable[channel]) { _channelsTable[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel(); @@ -151,17 +119,8 @@ void MidiPlayer::sendToChannel(byte channel, uint32 b) { } -void MidiPlayer::timerCallback(void *p) { - MidiPlayer *player = (MidiPlayer *)p; - - player->updateTimer(); -} - SoundHandler::SoundHandler(HugoEngine *vm) : _vm(vm) { - MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_GM); - MidiDriver *driver = MidiDriver::createMidi(dev); - - _midiPlayer = new MidiPlayer(driver); + _midiPlayer = new MidiPlayer(); _speakerStream = new Audio::PCSpeaker(_vm->_mixer->getOutputRate()); _vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, &_speakerHandle, _speakerStream, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true); @@ -263,7 +222,7 @@ void SoundHandler::playSound(int16 sound, const byte priority) { * Initialize for MCI sound and midi */ void SoundHandler::initSound() { - _midiPlayer->open(); + //_midiPlayer->open(); } void SoundHandler::syncVolume() { |