HDB: Add Music/Song data and functions

This commit is contained in:
Nipun Garg 2019-07-26 06:38:04 +05:30 committed by Eugene Sandulenko
parent 3aa373ab2b
commit 2bb60c69cd
5 changed files with 199 additions and 19 deletions

View File

@ -32,8 +32,8 @@
#include "hdb/input.h"
#include "hdb/lua-script.h"
#include "hdb/map.h"
#include "hdb/sound.h"
#include "hdb/menu.h"
#include "hdb/sound.h"
#include "hdb/mpc.h"
#include "hdb/window.h"
@ -152,6 +152,11 @@ bool HDBGame::init() {
return true;
}
void HDBGame::initializePath(const Common::FSNode &gamePath) {
Engine::initializePath(gamePath);
SearchMan.addDirectory("music", gamePath.getChild("music"));
}
void HDBGame::changeGameState() {
switch (_gameState) {
@ -856,8 +861,6 @@ Common::Error HDBGame::run() {
// Initializes Graphics
initGraphics(kScreenWidth, kScreenHeight, &_format);
_sound->test();
start();
#if 0

View File

@ -26,6 +26,7 @@
#include "common/scummsys.h"
#include "common/system.h"
#include "common/savefile.h"
#include "common/fs.h"
#include "engines/engine.h"
@ -145,6 +146,7 @@ public:
~HDBGame();
virtual bool hasFeature(Engine::EngineFeature f) const;
virtual void initializePath(const Common::FSNode &gamePath);
virtual Common::Error run();

View File

@ -24,6 +24,8 @@
#define HDB_MENU_H
#include "common/events.h"
#include "hdb/ai.h"
#include "hdb/sound.h"
namespace HDB {

View File

@ -21,10 +21,13 @@
*/
#include "common/debug.h"
#include "common/file.h"
#include "common/fs.h"
#include "hdb/hdb.h"
#include "hdb/file-manager.h"
#include "hdb/mpc.h"
#include "hdb/menu.h"
#include "hdb/sound.h"
namespace HDB {
@ -1390,6 +1393,7 @@ const SoundLookUp soundList[] = {
Sound::Sound() {
_sfxVolume = 255;
_musicVolume = 255;
}
void Sound::test() {
@ -1402,7 +1406,7 @@ void Sound::test() {
}
bool Sound::init() {
warning("STUB: Initialize songs");
_song1.playing = _song2.playing = false;
//
// init sound caching system
@ -1443,6 +1447,22 @@ void Sound::loadSaveFile(Common::InSaveFile *in) {
}
}
void Sound::setMusicVolume(int volume) {
_musicVolume = volume;
if (_song1.playing) {
if (_song1.fadingIn)
_song1.fadeInVol = volume;
if (!_song1.fadingOut)
g_hdb->_mixer->setChannelVolume(*_song1.handle, volume);
}
if (_song2.playing) {
if (_song2.fadingIn)
_song1.fadeInVol = volume;
if (!_song2.fadingOut)
g_hdb->_mixer->setChannelVolume(*_song2.handle, volume);
}
}
bool Sound::playSound(int index) {
debug(9, "STUB: Play Sound");
return true;
@ -1488,32 +1508,154 @@ bool Sound::playVoice(int index, int actor) {
}
bool Sound::startMusic(SoundType song) {
warning("STUB: Start Music");
return true;
g_hdb->_menu->saveSong(song);
if (!_musicVolume)
return false;
return beginMusic(song, false, 0);
}
bool Sound::fadeInMusic(SoundType song, int ramp) {
warning("STUB: Fade In Music");
return true;
g_hdb->_menu->saveSong(song);
if (!_musicVolume)
return false;
stopMusic();
return beginMusic(song, false, ramp);
}
void Sound::fadeOutMusic(int ramp) {
warning("STUB: Fade Out Music");
return;
if (_song1.playing) {
_song1.fadeOutRamp = ramp;
_song1.fadingOut = true;
_song1.fadeOutVol = _musicVolume;
} else if (_song2.playing) {
_song2.fadeOutRamp = ramp;
_song2.fadingOut = true;
_song2.fadeOutVol = _musicVolume;
}
}
bool Sound::songPlaying(SoundType song) {
warning("STUB: Check if Song is playing");
return true;
if (_song1.playing && _song1.song == song)
return true;
if (_song2.playing && _song2.song == song)
return true;
return false;
}
bool Sound::stopChannel(int channel) {
debug(9, "STUB: Stop Channel");
return true;
}
void Sound::stopMusic() {
debug(9, "STUB: Stop Music");
if (_song1.playing) {
_song1.playing = false;
g_hdb->_mixer->stopHandle(*_song1.handle);
}
if (_song2.playing) {
_song2.playing = false;
g_hdb->_mixer->stopHandle(*_song2.handle);
}
}
bool Sound::beginMusic(SoundType song, bool fadeIn, int ramp) {
if (!_song1.playing) {
// Start fading out SONG2 if its playing
if (_song2.playing) {
_song2.fadeOutRamp = ramp;
_song2.fadingOut = true;
_song2.fadeOutVol = _musicVolume;
}
// Load up the song
#ifdef USE_MAD
Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(soundList[song].name);
if (stream == nullptr)
return false;
Audio::SeekableAudioStream *audioStream = Audio::makeMP3Stream(stream, DisposeAfterUse::YES);
Audio::AudioStream *loopingStream = new Audio::LoopingAudioStream(audioStream, 0, DisposeAfterUse::YES);
g_hdb->_mixer->setChannelVolume(*_song1.handle, _musicVolume);
// do we need to fade-in this song?
if (fadeIn) {
_song1.fadeInRamp = ramp;
_song1.fadingIn = true;
_song1.fadeInVol = 0;
g_hdb->_mixer->setChannelVolume(*_song1.handle, 0);
}
g_hdb->_mixer->playStream(Audio::Mixer::kMusicSoundType, _song1.handle, loopingStream);
_song1.playing = true;
#endif
} else if (!_song2.playing) {
// Start fading out SONG1 if its playing
if (_song1.playing) {
_song1.fadeOutRamp = ramp;
_song1.fadingOut = true;
_song1.fadeOutVol = _musicVolume;
}
// Load up the song
#ifdef USE_MAD
Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(soundList[song].name);
if (stream == nullptr)
return false;
Audio::SeekableAudioStream *audioStream = Audio::makeMP3Stream(stream, DisposeAfterUse::YES);
Audio::AudioStream *loopingStream = new Audio::LoopingAudioStream(audioStream, 0, DisposeAfterUse::YES);
g_hdb->_mixer->setChannelVolume(*_song2.handle, _musicVolume);
// do we need to fade-in this song?
if (fadeIn) {
_song2.fadeInRamp = ramp;
_song2.fadingIn = true;
_song2.fadeInVol = 0;
g_hdb->_mixer->setChannelVolume(*_song2.handle, 0);
}
g_hdb->_mixer->playStream(Audio::Mixer::kMusicSoundType, _song2.handle, loopingStream);
_song2.playing = true;
#endif
} else
return false;
return true;
}
void Sound::updateMusic() {
if (_song1.playing) {
if (_song1.fadingOut) {
_song1.fadeOutVol = 0;
_song1.playing = false;
g_hdb->_mixer->stopHandle(*_song1.handle);
} else if (_song1.fadingIn) {
_song1.fadeInVol = _musicVolume;
_song1.fadingIn = false;
}
}
if (_song2.playing) {
if (_song2.fadingOut) {
_song2.fadeOutVol = 0;
_song2.playing = false;
g_hdb->_mixer->stopHandle(*_song2.handle);
} else if (_song2.fadingIn) {
_song2.fadeInVol = _musicVolume;
_song2.fadingIn = false;
}
}
}
int Sound::registerSound(const char *name) {
@ -1560,7 +1702,12 @@ int Sound::getSNDIndex(const char *name) {
}
SoundType Sound::whatSongIsPlaying() {
debug(9, "STUB: whatSongIsPlaying");
if (_song1.playing)
return _song1.song;
if (_song2.playing)
return _song2.song;
return SONG_NONE;
}

View File

@ -1436,6 +1436,28 @@ struct SoundCache {
SoundCache() : loaded(0), size(0), name(nullptr), luaName(nullptr), ext(0) {}
};
struct Song {
bool playing;
SoundType song;
const char *sndMusic;
Audio::SoundHandle *handle;
bool fadingOut;
int fadeOutVol;
int fadeOutRamp;
bool fadingIn;
int fadeInVol;
int fadeInRamp;
Song() : playing(false), song(SONG_NONE), sndMusic(nullptr), handle(new Audio::SoundHandle()),
fadingOut(false), fadeOutVol(0), fadeOutRamp(0),
fadingIn(false), fadeInVol(0), fadeInRamp(0) {}
~Song() {
delete handle;
}
};
class Sound {
public:
@ -1446,12 +1468,9 @@ public:
bool init();
void save(Common::OutSaveFile *out);
void loadSaveFile(Common::InSaveFile *in);
void setMusicVolume(int value) {
//debug(9, "STUB: Add Music System Variables");
}
void setMusicVolume(int value);
int getMusicVolume() {
//debug(9, "STUB: Add Music System Variables");
return 1;
return _musicVolume;
}
void setSFXVolume(int value) {
_sfxVolume = value;
@ -1476,6 +1495,8 @@ public:
bool fadeInMusic(SoundType song, int ramp);
void fadeOutMusic(int ramp);
void stopMusic();
bool beginMusic(SoundType song, bool fadeIn, int ramp);
void updateMusic();
bool songPlaying(SoundType song);
bool stopChannel(int channel);
int registerSound(const char *name);
@ -1503,6 +1524,11 @@ public:
int _voicesOn;
byte _voicePlayed[NUM_VOICES];
// Music System Variables
Song _song1, _song2;
int _musicVolume;
// Sound Caching System Variables
SoundCache _soundCache[kMaxSounds];