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,272 @@
/* 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 "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/raw.h"
#include "m4/platform/sound/digi.h"
#include "m4/adv_r/adv_file.h"
#include "m4/core/imath.h"
#include "m4/fileio/extensions.h"
#include "m4/vars.h"
#include "m4/m4.h"
namespace M4 {
namespace Sound {
Digi::~Digi() {
unload_sounds();
}
void Digi::loadFootstepSounds(const char **names) {
if (!_sounds.empty())
unload_sounds();
if (names) {
for (; *names; ++names)
preload(*names, true, NOWHERE);
}
}
void Digi::unload_sounds() {
_mixer->stopAll();
for (auto it = _sounds.begin(); it != _sounds.end(); ++it) {
rtoss(it->_value._filename);
free(it->_value._data);
}
_sounds.clear();
}
bool Digi::preload(const Common::String &name, bool isFootsteps, int roomNum) {
MemHandle workHandle;
int32 assetSize;
if (_sounds.contains(name))
return true;
// Load in the sound
Common::String fileName = expand_name_2_RAW(name, roomNum);
if ((workHandle = rget(fileName, &assetSize)) == nullptr)
error("Could not find sound - %s", fileName.c_str());
HLock(workHandle);
const byte *pSrc = (byte *)*workHandle;
byte *pDest = (byte *)malloc(assetSize);
Common::copy(pSrc, pSrc + assetSize, pDest);
HUnLock(workHandle);
_sounds[name] = DigiEntry(fileName, pDest, assetSize);
_sounds[name]._isFootsteps = isFootsteps;
return false;
}
void Digi::unload(const Common::String &name) {
if (_sounds.contains(name)) {
// Stop it if it's playing
for (int channel = 0; channel < MAX_CHANNELS; ++channel) {
if (_channels[channel]._name == name)
stop(channel, true);
}
// Remove the underlying resource
if (!_sounds[name]._filename.empty() && !_sounds[name]._isFootsteps) {
rtoss(_sounds[name]._filename);
_sounds[name]._filename.clear();
// Delete the sound entry
free(_sounds[name]._data);
_sounds.erase(name);
}
}
}
void Digi::task() {
// No implementation
}
int32 Digi::play(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num) {
return play(name, channel, vol, trigger, room_num, false);
}
int32 Digi::play_loop(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num) {
return play(name, channel, vol, trigger, room_num, true);
}
int32 Digi::play(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num, bool loop) {
assert(channel < 4);
// Assure no prior sound for the channel is playing
stop(channel);
if (!loop)
g_engine->drawSubtitle(name);
// Load in the new sound
preload(name, false, room_num);
DigiEntry &entry = _sounds[name];
Channel &c = _channels[channel];
// Create new audio stream
Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
Audio::makeRawStream(entry._data, entry._size, 11025, Audio::FLAG_UNSIGNED,
DisposeAfterUse::NO),
loop ? 0 : 1);
_mixer->playStream(Audio::Mixer::kSFXSoundType, &c._soundHandle, stream,
-1, vol);
if (trigger < 0 || trigger > 32767)
trigger = -1;
c._trigger = kernel_trigger_create(trigger);
c._name = name;
return 0;
}
void Digi::playFootsteps() {
// Get a list of the walking sounds
Common::Array<Common::String> names;
for (auto it = _sounds.begin(); it != _sounds.end(); ++it) {
if (it->_value._isFootsteps)
names.push_back(it->_key);
}
if (!names.empty()) {
play(names[imath_ranged_rand(0, (int)names.size() - 1)].c_str(),
1, 100, NO_TRIGGER, GLOBAL_SCENE);
}
}
void Digi::stop(uint channel, bool calledFromUnload) {
assert(channel < 4);
Channel &c = _channels[channel];
if (!c._name.empty()) {
Common::String name = c._name;
_mixer->stopHandle(c._soundHandle);
c._trigger = -1;
c._name.clear();
g_engine->clearSubtitle();
if (!calledFromUnload) {
digi_unload(name);
}
}
}
void Digi::flush_mem() {
unload_sounds();
}
void Digi::read_another_chunk() {
// For ScummVM, the audio data is completely loaded for each sound. But we still
// need to check whether a sound has finished so it's trigger can be dispatched
for (int channel = 0; channel < MAX_CHANNELS; ++channel) {
Channel &c = _channels[channel];
// Check if the channel has a sound playing that finished
if (c._trigger != -1 && !_mixer->isSoundHandleActive(c._soundHandle)) {
int trigger = c._trigger;
c._trigger = -1;
stop(channel);
// Dispatch the trigger
kernel_trigger_dispatchx(trigger);
}
}
}
bool Digi::play_state(int channel) const {
return _mixer->isSoundHandleActive(_channels[channel]._soundHandle);
}
void Digi::change_volume(int channel, int vol) {
_mixer->setChannelVolume(_channels[channel]._soundHandle, vol);
}
int32 Digi::ticks_to_play(const char *name, int roomNum) {
// Get the file and retrieve it's size
Common::String filename = expand_name_2_RAW(name, roomNum);
SysFile sf(filename);
double size = sf.size();
sf.close();
term_message(" digi_ticks_to_play");
term_message(" %s", filename.c_str());
term_message(" size = %f, room = %d", size, roomNum);
return (int32)floor(size * 0.000090702946 * 60.0);
}
void Digi::change_panning(int val1, int val2) {
if (_G(game).room_id != _panningTodoRoom) {
_panningTodoRoom = _G(game).room_id;
warning("TODO: digi_change_panning");
}
}
} // namespace Sound
bool digi_preload(const Common::String &name, int roomNum) {
return _G(digi).preload(name, false, roomNum);
}
void digi_unload(const Common::String &name) {
_G(digi).unload(name);
}
int32 digi_play(const char *name, uint channel, int32 vol, int32 trigger, int32 room_num) {
return _G(digi).play(name, channel, vol, trigger, room_num);
}
int32 digi_play_loop(const char *name, uint channel, int32 vol, int32 trigger, int32 room_num) {
return _G(digi).play_loop(name, channel, vol, trigger, room_num);
}
void digi_read_another_chunk() {
return _G(digi).read_another_chunk();
}
void digi_stop(int slot) {
_G(digi).stop(slot);
}
bool digi_play_state(int channel) {
return _G(digi).play_state(channel);
}
void digi_change_volume(int channel, int vol) {
_G(digi).change_volume(channel, vol);
}
int32 digi_ticks_to_play(const char *name, int roomNum) {
return _G(digi).ticks_to_play(name, roomNum);
}
void digi_change_panning(int val1, int val2) {
_G(digi).change_panning(val1, val2);
}
} // namespace M4

View File

@@ -0,0 +1,126 @@
/* 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 M4_PLATFORM_SOUND_DIGI_H
#define M4_PLATFORM_SOUND_DIGI_H
#include "audio/mixer.h"
#include "audio/audiostream.h"
#include "common/hashmap.h"
#include "m4/m4_types.h"
namespace M4 {
namespace Sound {
#define MAX_CHANNELS 4
/**
* M4 Digital player digivolves to ScummVM-digital player
*/
class Digi {
/**
* Digital sound entry
*/
struct DigiEntry {
Common::String _filename;
byte *_data = nullptr;
size_t _size = 0;
bool _isFootsteps = false;
DigiEntry() {}
DigiEntry(Common::String &name, byte *data, size_t size) :
_filename(name), _data(data), _size(size) {}
};
/**
* Sound channel
*/
struct Channel {
Audio::SoundHandle _soundHandle;
int _trigger = -1;
Common::String _name;
};
private:
Audio::Mixer *_mixer;
Channel _channels[4];
Common::HashMap<Common::String, DigiEntry> _sounds;
int _panningTodoRoom = 0;
int32 play(const Common::String &name, uint channel, int32 vol, int32 trigger,
int32 room_num, bool loop);
public:
Digi(Audio::Mixer *mixer) : _mixer(mixer) {}
~Digi();
/**
* Preload a digi sample into memory buffer for play back later.
*/
bool preload(const Common::String &name, bool isFootsteps, int roomNum = -1);
/**
* A room can designate one or more sounds to be randomly played when
* the player walks around
*/
void loadFootstepSounds(const char **names);
void unload_sounds();
void unload(const Common::String &name);
void task();
// digi_play and digi_play_loop play a particular sound file in a given channel,
// at a particular volume. The room_num parameter tells us what directory the sound
// is stored in (all sounds are AIFFs). Trigger is an integer that is fed into
// kernel_dispatch_trigger when the sound has finished playing
// If the sound has been preloaded it will be played from memory, otherwise it will
// be streamed from disk
int32 play(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num = -1);
int32 play_loop(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num = -1);
void playFootsteps();
void stop(uint channel, bool calledFromUnload = false);
void flush_mem();
void read_another_chunk();
bool play_state(int channel) const;
void change_volume(int channel, int vol);
int32 ticks_to_play(const char *name, int roomNum = -1);
void change_panning(int val1, int val2);
};
} // namespace Sound
bool digi_preload(const Common::String &name, int roomNum = -1);
void digi_unload(const Common::String &name);
int32 digi_play(const char *name, uint channel, int32 vol = 255,
int32 trigger = -1, int32 room_num = -1);
int32 digi_play_loop(const char *name, uint channel, int32 vol = 255,
int32 trigger = -1, int32 room_num = -1);
void digi_read_another_chunk();
void digi_stop(int channel);
bool digi_play_state(int channel);
void digi_change_volume(int channel, int vol);
int32 digi_ticks_to_play(const char *name, int roomNum = -1);
void digi_change_panning(int val1, int val2);
} // namespace M4
#endif

View File

@@ -0,0 +1,310 @@
/* 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 "m4/platform/sound/midi.h"
#include "m4/adv_r/adv_file.h"
#include "m4/vars.h"
#include "common/config-manager.h"
#include "audio/adlib_hmisos.h"
#include "audio/adlib_ms.h"
#include "audio/fmopl.h"
#include "audio/midiparser.h"
#include "audio/midiparser_hmp.h"
#include "audio/mt32gm.h"
namespace M4 {
namespace Sound {
int Midi::_midiEndTrigger;
Midi::Midi() {
_driver = nullptr;
_paused = false;
_deviceType = MT_NULL;
_midiParser = nullptr;
_midiData = nullptr;
}
Midi::~Midi() {
stop();
if (_driver != nullptr) {
_driver->setTimerCallback(nullptr, nullptr);
_driver->close();
}
Common::StackLock lock(_mutex);
if (_midiParser != nullptr)
delete _midiParser;
if (_midiData != nullptr)
delete[] _midiData;
if (_driver != nullptr) {
delete _driver;
_driver = nullptr;
}
}
int Midi::open() {
assert(_driver == nullptr);
// Check the type of device that the user has configured.
MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_GM);
_deviceType = MidiDriver::getMusicType(dev);
if (_deviceType == MT_GM && ConfMan.getBool("native_mt32"))
_deviceType = MT_MT32;
OPL::Config::OplType oplType;
switch (_deviceType) {
case MT_ADLIB:
oplType = MidiDriver_ADLIB_HMISOS::detectOplType(OPL::Config::kOpl3) ? OPL::Config::kOpl3 : OPL::Config::kOpl2;
MidiDriver_ADLIB_HMISOS *adLibDriver;
adLibDriver = new MidiDriver_ADLIB_HMISOS(oplType);
_driver = adLibDriver;
Common::SeekableReadStream *instrumentBankStream;
instrumentBankStream = SearchMan.createReadStreamForMember(Common::Path("MELODIC.BNK"));
Common::SeekableReadStream *rhythmBankStream;
rhythmBankStream = SearchMan.createReadStreamForMember(Common::Path("DRUM.BNK"));
adLibDriver->loadInstrumentBanks(instrumentBankStream, rhythmBankStream);
break;
case MT_GM:
case MT_MT32:
_driver = new MidiDriver_MT32GM(MusicType::MT_GM);
break;
default:
_driver = new MidiDriver_NULL_Multisource();
break;
}
_midiParser = new MidiParser_HMP(0);
_driver->property(MidiDriver::PROP_USER_VOLUME_SCALING, true);
// Riddle's MIDI data does not consistently set values for every controller
// at the start of every track
_driver->setControllerDefault(MidiDriver_Multisource::CONTROLLER_DEFAULT_PITCH_BEND);
_driver->setControllerDefault(MidiDriver_Multisource::CONTROLLER_DEFAULT_MODULATION);
_driver->setControllerDefault(MidiDriver_Multisource::CONTROLLER_DEFAULT_PANNING);
_driver->setControllerDefault(MidiDriver_Multisource::CONTROLLER_DEFAULT_REVERB);
_driver->setControllerDefault(MidiDriver_Multisource::CONTROLLER_DEFAULT_CHORUS);
_midiParser->property(MidiParser::mpDisableAutoStartPlayback, true);
// Riddle's MIDI data uses sustain
_midiParser->property(MidiParser::mpSendSustainOffOnNotesOff, true);
// Open the MIDI driver.
int returnCode = _driver->open();
if (returnCode != 0)
error("Midi::open - Failed to open MIDI music driver - error code %d.", returnCode);
syncSoundSettings();
// Connect the driver and the parser.
_midiParser->setMidiDriver(_driver);
_midiParser->setTimerRate(_driver->getBaseTempo());
_driver->setTimerCallback(this, &onTimer);
return 0;
}
void Midi::load(byte* in, int32 size) {
Common::StackLock lock(_mutex);
if (_midiParser == nullptr)
return;
_midiParser->unloadMusic();
if (_midiData != nullptr)
delete[] _midiData;
_midiData = new byte[size];
Common::copy(in, in + size, _midiData);
_midiParser->loadMusic(_midiData, size);
}
void Midi::play() {
Common::StackLock lock(_mutex);
if (_midiParser == nullptr || _driver == nullptr)
return;
_midiParser->startPlaying();
}
void Midi::pause(bool pause) {
if (_paused == pause || _driver == nullptr)
return;
_paused = pause;
if (_midiParser != nullptr) {
Common::StackLock lock(_mutex);
if (_paused) {
_midiParser->pausePlaying();
} else {
_midiParser->resumePlaying();
}
}
}
void Midi::stop() {
Common::StackLock lock(_mutex);
if (_midiParser != nullptr) {
_midiParser->stopPlaying();
if (_driver != nullptr)
_driver->deinitSource(0);
}
}
bool Midi::isPlaying() {
Common::StackLock lock(_mutex);
return _midiParser->isPlaying();
}
void Midi::startFade(uint16 duration, uint16 targetVolume) {
if (_driver == nullptr || _midiParser == nullptr || !_midiParser->isPlaying())
return;
_driver->startFade(0, duration, targetVolume);
}
bool Midi::isFading() {
return _driver->isFading(0);
}
void Midi::syncSoundSettings() {
if (_driver != nullptr)
_driver->syncSoundSettings();
}
void Midi::midi_play(const char *name, int volume, bool loop, int trigger, int roomNum) {
if (_driver == nullptr || _midiParser == nullptr)
return;
_midiEndTrigger = trigger;
// Load in the resource
Common::String fileName = expand_name_2_HMP(name, roomNum);
int32 assetSize;
MemHandle workHandle = rget(fileName, &assetSize);
if (workHandle == nullptr)
error("Could not find music - %s", fileName.c_str());
HLock(workHandle);
/*
Common::DumpFile dump;
dump.open(fileName.c_str());
dump.write(*workHandle, assetSize);
dump.close();
*/
load((byte *)*workHandle, assetSize);
_midiParser->setTrack(0);
_midiParser->property(MidiParser::mpAutoLoop, loop ? 1 : 0);
// TODO Some calls use volume 0? What is that supposed to do?
_driver->setSourceVolume(0, volume);
play();
/*
#ifdef TODO
byte *pSrc = (byte *)*workHandle;
MidiParser *parser = MidiParser::createParser_SMF();
bool loaded = parser->loadMusic(pSrc, assetSize);
if (loaded) {
stop();
parser->setTrack(0);
parser->setMidiDriver(this);
parser->setTimerRate(_driver->getBaseTempo());
parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
_parser = parser;
_isLooping = false;
_isPlaying = true;
}
#else
// TODO: When music is properly implemented, trigger when music done
if (trigger != -1)
kernel_timing_trigger(10, trigger);
#endif
*/
HUnLock(workHandle);
rtoss(fileName);
}
void Midi::task() {
// No implementation
}
void Midi::loop() {
// No implementation
}
void Midi::midi_fade_volume(int targetVolume, int duration) {
uint16 durationMsec = duration * 1000 / 30;
startFade(durationMsec, targetVolume);
// TODO Should this stop playback when fade is completed?
// Should this call return after the fade has completed?
}
void Midi::onTimer(void* data) {
Midi *m = (Midi *)data;
Common::StackLock lock(m->_mutex);
if (m->_midiParser != nullptr) {
m->_midiParser->onTimer();
if (!m->_midiParser->isPlaying() && _midiEndTrigger >= 0) {
// FIXME Can this trigger a deadlock on the mutex?
kernel_timing_trigger(10, _midiEndTrigger);
_midiEndTrigger = -1;
}
}
}
} // namespace Sound
void midi_play(const char *name, int volume, bool loop, int trigger, int roomNum) {
_G(midi).midi_play(name, volume, loop, trigger, roomNum);
}
void midi_loop() {
_G(midi).loop();
}
void midi_stop() {
_G(midi).stop();
}
void midi_fade_volume(int targetVolume, int duration) {
_G(midi).midi_fade_volume(targetVolume, duration);
}
} // namespace M4

View File

@@ -0,0 +1,82 @@
/* 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 M4_SOUND_PLATFORM_MIDI_H
#define M4_SOUND_PLATFORM_MIDI_H
#include "m4/m4_types.h"
#include "audio/mididrv_ms.h"
#include "audio/midiparser.h"
namespace M4 {
namespace Sound {
class Midi {
private:
static int _midiEndTrigger;
Common::Mutex _mutex;
MusicType _deviceType;
MidiDriver_Multisource *_driver;
MidiParser *_midiParser;
byte *_midiData;
bool _paused;
protected:
static void onTimer(void *data);
public:
Midi();
~Midi();
int open();
void load(byte *in, int32 size);
void play();
void pause(bool pause);
void stop();
bool isPlaying();
void startFade(uint16 duration, uint16 targetVolume);
bool isFading();
void syncSoundSettings();
void midi_play(const char *name, int volume, bool loop, int trigger, int roomNum);
void task();
void loop();
void midi_fade_volume(int targetVolume, int duration);
};
} // namespace Sound
void midi_play(const char *name, int volume, bool loop, int trigger, int roomNum);
void midi_loop();
void midi_stop();
void midi_fade_volume(int targetVolume, int duration);
} // namespace M4
#endif