Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

View File

@@ -0,0 +1,182 @@
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "ultima/ultima4/sound/music.h"
#include "ultima/ultima4/sound/sound.h"
#include "ultima/ultima4/core/config.h"
#include "ultima/ultima4/core/settings.h"
#include "ultima/ultima4/core/utils.h"
#include "ultima/ultima4/events/event_handler.h"
#include "ultima/ultima4/game/context.h"
#include "ultima/ultima4/map/location.h"
#include "ultima/ultima4/ultima4.h"
#include "audio/decoders/mp3.h"
#include "audio/mods/mod_xm_s3m.h"
#include "audio/midiparser.h"
namespace Ultima {
namespace Ultima4 {
Music *g_music;
Music::Music(Audio::Mixer *mixer) :
Audio::MidiPlayer(), _mixer(mixer), _introMid(TOWNS) {
g_music = this;
Audio::MidiPlayer::createDriver();
int ret = _driver->open();
if (ret == 0) {
if (_nativeMT32)
_driver->sendMT32Reset();
else
_driver->sendGMReset();
_driver->setTimerCallback(this, &timerCallback);
}
_filenames.reserve(MAX);
_filenames.push_back(""); // filename for MUSIC_NONE;
/*
* load music track filenames from xml config file
*/
const Config *config = Config::getInstance();
Std::vector<ConfigElement> musicConfs = config->getElement("music").getChildren();
for (const auto &m : musicConfs) {
if (m.getName() != "track")
continue;
_filenames.push_back(m.getString("file"));
}
}
Music::~Music() {
stop();
g_music = nullptr;
}
void Music::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 (_channelsTable[channel])
_channelsTable[channel]->volume(_channelsVolume[channel] * _masterVolume / 255);
}
if (_channelsTable[channel])
_channelsTable[channel]->send(b);
}
void Music::playMusic(Type music) {
playMusic(_filenames[music]);
}
void Music::playMapMusic() {
playMusic(g_context->_location->_map->_music);
}
void Music::playMusic(const Common::String &filename) {
stop();
// First try opening the file with whatever filename is provided
if (startMusic(filename))
return;
// TODO: Since the player doesn't yet support xu4 .it files,
// try starting the file with other extensions - which some have
const char *const EXTENSIONS[2] = { ".mp3", ".mid" };
for (int idx = 0; idx < 2; ++idx) {
size_t dotIndex = filename.findLastOf('.');
Common::String fname = (dotIndex != Common::String::npos) ?
Common::String(filename.c_str(), dotIndex) + EXTENSIONS[idx] :
filename + EXTENSIONS[idx];
if (startMusic(fname))
return;
}
// At this point, we couldn't open the given music file
warning("No support for playing music file - %s", filename.c_str());
}
bool Music::startMusic(const Common::String &filename) {
Common::File musicFile;
if (!musicFile.open(Common::Path(Common::String::format("data/mid/%s", filename.c_str()))))
// No such file exists
return false;
#ifdef USE_MAD
if (filename.hasSuffixIgnoreCase(".mp3")) {
Audio::SeekableAudioStream *audioStream = Audio::makeMP3Stream(
musicFile.readStream(musicFile.size()), DisposeAfterUse::YES);
_mixer->playStream(Audio::Mixer::kMusicSoundType,
&_soundHandle, audioStream);
return true;
} else
#endif
if (filename.hasSuffixIgnoreCase(".mid")) {
// Load MIDI resource data
int midiMusicSize = musicFile.size();
free(_midiData);
_midiData = (byte *)malloc(midiMusicSize);
musicFile.read(_midiData, midiMusicSize);
musicFile.close();
MidiParser *parser = MidiParser::createParser_SMF();
if (parser->loadMusic(_midiData, midiMusicSize)) {
parser->setTrack(0);
parser->setMidiDriver(this);
parser->setTimerRate(_driver->getBaseTempo());
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
_parser = parser;
syncVolume();
_isLooping = false;
_isPlaying = true;
return true;
} else {
delete parser;
return false;
}
} else {
return false;
}
}
void Music::stop() {
_mixer->stopHandle(_soundHandle);
Audio::MidiPlayer::stop();
}
void Music::introSwitch(int n) {
if (n > NONE &&n < MAX) {
_introMid = static_cast<Type>(n);
intro();
}
}
} // End of namespace Ultima4
} // End of namespace Ultima

View File

@@ -0,0 +1,151 @@
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ULTIMA4_SOUND_MUSIC_H
#define ULTIMA4_SOUND_MUSIC_H
#include "ultima/shared/std/containers.h"
#include "audio/audiostream.h"
#include "audio/midiplayer.h"
#include "audio/mixer.h"
namespace Ultima {
namespace Ultima4 {
#define CAMP_FADE_OUT_TIME 1000
#define CAMP_FADE_IN_TIME 0
#define INN_FADE_OUT_TIME 1000
#define INN_FADE_IN_TIME 5000
#define NLOOPS -1
class Music : public Audio::MidiPlayer {
public:
enum Type {
NONE,
OUTSIDE,
TOWNS,
SHRINES,
SHOPPING,
RULEBRIT,
FANFARE,
DUNGEON,
COMBAT,
CASTLES,
MAX
};
Type _introMid;
private:
Audio::Mixer *_mixer;
Audio::SoundHandle _soundHandle;
Std::vector<Common::String> _filenames;
/**
* Play a given music file if is exists
*/
bool startMusic(const Common::String &filename);
protected:
// Overload Audio::MidiPlayer method
void sendToChannel(byte channel, uint32 b) override;
public:
Music(Audio::Mixer *mixer);
~Music() override;
/**
* Play music
*/
void playMusic(const Common::String &filename);
/**
* Play music of a given type
*/
void playMusic(Type music);
/**
* Play the designated music for the current map
*/
void playMapMusic();
void stop() override;
/**
* Fade out the music
*/
void fadeOut(int msecs) {
// TODO
}
/**
* Fade in the music
*/
void fadeIn(int msecs, bool loadFromMap) {
// TODO
}
/**
* Music when you talk to Lord British
*/
void lordBritish() {
playMusic(RULEBRIT);
}
/**
* Music when you talk to Hawkwind
*/
void hawkwind() {
playMusic(SHOPPING);
}
/**
* Music that plays while camping
*/
void camp() {
fadeOut(1000);
}
/**
* Music when talking to a vendor
*/
void shopping() {
playMusic(SHOPPING);
}
void intro() {
#ifdef IOS_ULTIMA4
_on = true; // Force iOS to turn this back on from going in the background.
#endif
playMusic(_introMid);
}
/**
* Cycle through the introduction music
*/
void introSwitch(int n);
};
extern Music *g_music;
} // End of namespace Ultima4
} // End of namespace Ultima
#endif

View File

@@ -0,0 +1,173 @@
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "ultima/ultima4/sound/sound.h"
#include "ultima/ultima4/core/config.h"
#include "ultima/ultima4/sound/music.h"
#include "ultima/ultima4/core/settings.h"
#include "ultima/ultima4/core/utils.h"
#include "audio/audiostream.h"
#include "audio/decoders/flac.h"
#include "audio/decoders/mp3.h"
#include "audio/decoders/voc.h"
#include "audio/decoders/vorbis.h"
#include "audio/decoders/wave.h"
#include "common/file.h"
namespace Ultima {
namespace Ultima4 {
SoundManager *g_sound;
void soundPlay(Sound sound, bool onlyOnce, int specificDurationInTicks) {
g_sound->play(sound, onlyOnce, specificDurationInTicks);
}
void soundStop(int channel) {
g_sound->stop(channel);
}
/*-------------------------------------------------------------------*/
SoundManager::SoundManager(Audio::Mixer *mixer) : _mixer(mixer) {
g_sound = this;
// Load sound track filenames from xml config file
const Config *config = Config::getInstance();
_soundFilenames.reserve(SOUND_MAX);
_sounds.resize(SOUND_MAX);
Std::vector<ConfigElement> soundConfs = config->getElement("sound").getChildren();
for (const auto &s : soundConfs) {
if (s.getName() != "track")
continue;
_soundFilenames.push_back(Common::Path(s.getString("file")));
}
}
SoundManager::~SoundManager() {
g_sound = nullptr;
_mixer->stopHandle(_soundHandle);
for (uint idx = 0; idx < _sounds.size(); ++idx)
delete _sounds[idx];
}
bool SoundManager::load(Sound sound) {
assertMsg(sound < SOUND_MAX, "Attempted to load an invalid sound");
if (_sounds[sound] == nullptr) {
Common::Path pathname("data/sound/");
pathname.joinInPlace(_soundFilenames[sound]);
Common::String basename = pathname.baseName();
if (!basename.empty() && !basename.hasSuffix("/"))
return load_sys(sound, pathname);
}
return true;
}
void SoundManager::play(Sound sound, bool onlyOnce, int specificDurationInTicks) {
assertMsg(sound < SOUND_MAX, "Attempted to play an invalid sound");
if (_sounds[sound] == nullptr) {
if (!load(sound)) {
return;
}
}
play_sys(sound, onlyOnce, specificDurationInTicks);
}
void SoundManager::stop(int channel) {
stop_sys(channel);
}
bool SoundManager::load_sys(Sound sound, const Common::Path &filename) {
Common::File f;
if (!f.open(filename))
return false;
Audio::SeekableAudioStream *audioStream = nullptr;
Common::String baseName(filename.baseName());
#ifdef USE_FLAC
if (baseName.hasSuffixIgnoreCase(".fla"))
audioStream = Audio::makeFLACStream(f.readStream(f.size()), DisposeAfterUse::YES);
#endif
#ifdef USE_VORBIS
if (baseName.hasSuffixIgnoreCase(".ogg"))
audioStream = Audio::makeVorbisStream(f.readStream(f.size()), DisposeAfterUse::YES);
#endif
#ifdef USE_MAD
if (baseName.hasSuffixIgnoreCase(".mp3"))
audioStream = Audio::makeMP3Stream(f.readStream(f.size()), DisposeAfterUse::YES);
#endif
if (baseName.hasSuffixIgnoreCase(".wav"))
audioStream = Audio::makeWAVStream(f.readStream(f.size()), DisposeAfterUse::YES);
if (baseName.hasSuffixIgnoreCase(".voc"))
audioStream = Audio::makeVOCStream(f.readStream(f.size()), DisposeAfterUse::YES);
_sounds[sound] = audioStream;
return audioStream != nullptr;
}
void SoundManager::play_sys(Sound sound, bool onlyOnce, int specificDurationMilli) {
// Don't allow once only sounds if another sound is already playing
if (onlyOnce && _mixer->isSoundHandleActive(_soundHandle))
return;
// Ensure sound is stopped, and rewinded
_mixer->stopHandle(_soundHandle);
_sounds[sound]->rewind();
if (specificDurationMilli == -1) {
// Play a single sound effect
_mixer->playStream(Audio::Mixer::kSFXSoundType,
&_soundHandle, _sounds[sound], -1, Audio::Mixer::kMaxChannelVolume,
0, DisposeAfterUse::NO);
} else {
// Play a sound effect, looping if necessary, for a given duration
// TODO: Better handle cases where a number of loops won't fit
// exactly to give a desired duration
int duration = _sounds[sound]->getLength().msecs();
int loops = (specificDurationMilli + duration - 1) / duration;
assert(loops >= 0);
Audio::AudioStream *audioStream = new Audio::LoopingAudioStream(
_sounds[sound], loops, DisposeAfterUse::NO);
_mixer->playStream(Audio::Mixer::kSFXSoundType,
&_soundHandle, audioStream, -1, Audio::Mixer::kMaxChannelVolume,
0, DisposeAfterUse::NO);
}
}
void SoundManager::stop_sys(int channel) {
// Channel 1 is the dummy channel number used for sound effects
if (channel == 1)
_mixer->stopHandle(_soundHandle);
}
} // End of namespace Ultima4
} // End of namespace Ultima

View File

@@ -0,0 +1,97 @@
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ULTIMA4_SOUND_H
#define ULTIMA4_SOUND_H
#include "ultima/shared/std/containers.h"
#include "audio/audiostream.h"
#include "audio/mixer.h"
#include "common/str.h"
namespace Ultima {
namespace Ultima4 {
enum Sound {
SOUND_TITLE_FADE, // the intro title fade
SOUND_WALK_NORMAL, // walk, world and town
SOUND_WALK_SLOWED, // walk, slow progress
SOUND_WALK_COMBAT, // walk, combat
SOUND_BLOCKED, // location blocked
SOUND_ERROR, // error/bad command
SOUND_PC_ATTACK, // PC attacks
SOUND_PC_STRUCK, // PC damaged
SOUND_NPC_ATTACK, // NPC attacks
SOUND_NPC_STRUCK, // NPC damaged
SOUND_ACID, // effect, acid damage
SOUND_SLEEP, // effect, sleep
SOUND_POISON_EFFECT, // effect, poison
SOUND_POISON_DAMAGE, // damage, poison
SOUND_EVADE, // trap evaded
SOUND_FLEE, // flee combat
SOUND_ITEM_STOLEN, // item was stolen from a PC, food or gold
SOUND_LBHEAL, // LB heals party
SOUND_LEVELUP, // PC level up
SOUND_MOONGATE, // moongate used
SOUND_CANNON,
SOUND_RUMBLE,
SOUND_PREMAGIC_MANA_JUMBLE,
SOUND_MAGIC,
SOUND_WHIRLPOOL,
SOUND_STORM,
// SOUND_MISSED,
// SOUND_CREATUREATTACK,
// SOUND_PLAYERHIT,
SOUND_MAX
};
void soundPlay(Sound sound, bool onlyOnce = true, int specificDurationInTicks = -1);
void soundStop(int channel = 1);
class SoundManager {
private:
Audio::Mixer *_mixer;
Audio::SoundHandle _soundHandle;
Std::vector<Common::Path> _soundFilenames;
Std::vector<Audio::SeekableAudioStream *> _sounds;
private:
bool load(Sound sound);
void play_sys(Sound sound, bool onlyOnce, int specificDurationMilli);
bool load_sys(Sound sound, const Common::Path &filename);
void stop_sys(int channel);
public:
SoundManager(Audio::Mixer *mixer);
~SoundManager();
void play(Sound sound, bool onlyOnce = true, int specificDurationInTicks = -1);
void stop(int channel = 1);
};
extern SoundManager *g_sound;
} // End of namespace Ultima4
} // End of namespace Ultima
#endif