From 494ac0ccdf545e8055c83c22b8b2fbf9e045b32a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 3 Dec 2012 17:10:39 +0300 Subject: Rename sound.cpp/h to soundmanager. --- src/CMakeLists.txt | 4 +- src/Makefile.am | 4 +- src/actionmanager.cpp | 2 +- src/actorsprite.cpp | 2 +- src/being.cpp | 2 +- src/client.cpp | 4 +- src/effectmanager.cpp | 2 +- src/game.cpp | 2 +- src/gui/chatwindow.cpp | 2 +- src/gui/confirmdialog.cpp | 2 +- src/gui/npcdialog.cpp | 2 +- src/gui/okdialog.cpp | 2 +- src/gui/questswindow.cpp | 2 +- src/gui/quitdialog.cpp | 2 +- src/gui/setup_audio.cpp | 2 +- src/gui/shopwindow.cpp | 2 +- src/gui/widgets/chattab.cpp | 2 +- src/gui/widgets/guildchattab.cpp | 2 +- src/gui/widgets/setupitem.cpp | 2 +- src/gui/widgets/window.cpp | 2 +- src/localplayer.cpp | 2 +- src/net/ea/gui/guildtab.cpp | 2 +- src/net/ea/gui/partytab.cpp | 2 +- src/sound.cpp | 417 --------------------------------------- src/sound.h | 176 ----------------- src/soundmanager.cpp | 417 +++++++++++++++++++++++++++++++++++++++ src/soundmanager.h | 176 +++++++++++++++++ src/statuseffect.cpp | 2 +- src/test/testlauncher.cpp | 2 +- 29 files changed, 621 insertions(+), 621 deletions(-) delete mode 100644 src/sound.cpp delete mode 100644 src/sound.h create mode 100644 src/soundmanager.cpp create mode 100644 src/soundmanager.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 944423738..3cbf014a9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -635,8 +635,8 @@ SET(SRCS shopitem.h simpleanimation.cpp simpleanimation.h - sound.cpp - sound.h + soundmanager.cpp + soundmanager.h sprite.h statuseffect.cpp statuseffect.h diff --git a/src/Makefile.am b/src/Makefile.am index 1f1dd23a0..a02c5da69 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -639,8 +639,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ shopitem.h \ simpleanimation.cpp \ simpleanimation.h \ - sound.cpp \ - sound.h \ + soundmanager.cpp \ + soundmanager.h \ sprite.h \ statuseffect.cpp \ statuseffect.h \ diff --git a/src/actionmanager.cpp b/src/actionmanager.cpp index 3cb49f340..dd9239c12 100644 --- a/src/actionmanager.cpp +++ b/src/actionmanager.cpp @@ -31,7 +31,7 @@ #include "localplayer.h" #include "playerinfo.h" #include "playerrelations.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/botcheckerwindow.h" #include "gui/buyselldialog.h" diff --git a/src/actorsprite.cpp b/src/actorsprite.cpp index edc954d28..f436047e5 100644 --- a/src/actorsprite.cpp +++ b/src/actorsprite.cpp @@ -28,7 +28,7 @@ #include "imagesprite.h" #include "localplayer.h" #include "simpleanimation.h" -#include "sound.h" +#include "soundmanager.h" #include "statuseffect.h" #include "gui/theme.h" diff --git a/src/being.cpp b/src/being.cpp index fe84cdaaf..c425f89a4 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -35,7 +35,7 @@ #include "party.h" #include "playerrelations.h" #include "simpleanimation.h" -#include "sound.h" +#include "soundmanager.h" #include "text.h" #include "gui/equipmentwindow.h" diff --git a/src/client.cpp b/src/client.cpp index 6ea0ad11f..1265d048f 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -41,7 +41,7 @@ #include "keyboardconfig.h" #include "party.h" #include "playerrelations.h" -#include "sound.h" +#include "soundmanager.h" #include "statuseffect.h" #include "units.h" #include "touchmanager.h" @@ -146,7 +146,7 @@ KeyboardConfig keyboard; UserPalette *userPalette = nullptr; Graphics *mainGraphics = nullptr; -Sound sound; +SoundManager sound; int openGLMode = 0; static uint32_t nextTick(uint32_t interval, void *param A_UNUSED); diff --git a/src/effectmanager.cpp b/src/effectmanager.cpp index f80a16972..aea3fed3c 100644 --- a/src/effectmanager.cpp +++ b/src/effectmanager.cpp @@ -25,7 +25,7 @@ #include "being.h" #include "logger.h" #include "particle.h" -#include "sound.h" +#include "soundmanager.h" #include "debug.h" diff --git a/src/game.cpp b/src/game.cpp index 8716ba0e4..f2b88769f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -40,7 +40,7 @@ #include "localplayer.h" #include "particle.h" #include "playerinfo.h" -#include "sound.h" +#include "soundmanager.h" #include "spellshortcut.h" #include "gui/botcheckerwindow.h" diff --git a/src/gui/chatwindow.cpp b/src/gui/chatwindow.cpp index 9ba871575..7919fff24 100644 --- a/src/gui/chatwindow.cpp +++ b/src/gui/chatwindow.cpp @@ -34,7 +34,7 @@ #include "party.h" #include "playerinfo.h" #include "spellshortcut.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/gui.h" #include "gui/setup.h" diff --git a/src/gui/confirmdialog.cpp b/src/gui/confirmdialog.cpp index 65fdb9fd1..0ec54c5d9 100644 --- a/src/gui/confirmdialog.cpp +++ b/src/gui/confirmdialog.cpp @@ -22,7 +22,7 @@ #include "gui/confirmdialog.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/gui.h" diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index 1eb5e9800..f552deb60 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -25,7 +25,7 @@ #include "being.h" #include "configuration.h" #include "client.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/gui.h" #include "gui/sdlfont.h" diff --git a/src/gui/okdialog.cpp b/src/gui/okdialog.cpp index 5d0f2a01f..ad379e656 100644 --- a/src/gui/okdialog.cpp +++ b/src/gui/okdialog.cpp @@ -22,7 +22,7 @@ #include "gui/okdialog.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/gui.h" diff --git a/src/gui/questswindow.cpp b/src/gui/questswindow.cpp index 3991c8172..16fe7190b 100644 --- a/src/gui/questswindow.cpp +++ b/src/gui/questswindow.cpp @@ -21,7 +21,7 @@ #include "gui/questswindow.h" #include "configuration.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/gui.h" #include "gui/sdlfont.h" diff --git a/src/gui/quitdialog.cpp b/src/gui/quitdialog.cpp index f2c6602cd..4e5cb4a05 100644 --- a/src/gui/quitdialog.cpp +++ b/src/gui/quitdialog.cpp @@ -26,7 +26,7 @@ #include "game.h" #include "keydata.h" #include "keyevent.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/chatwindow.h" #include "gui/npcdialog.h" diff --git a/src/gui/setup_audio.cpp b/src/gui/setup_audio.cpp index 9ee897cd4..15fd23d33 100644 --- a/src/gui/setup_audio.cpp +++ b/src/gui/setup_audio.cpp @@ -25,7 +25,7 @@ #include "main.h" #include "configuration.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/viewport.h" diff --git a/src/gui/shopwindow.cpp b/src/gui/shopwindow.cpp index a9afab262..8be665d08 100644 --- a/src/gui/shopwindow.cpp +++ b/src/gui/shopwindow.cpp @@ -49,7 +49,7 @@ #include "playerinfo.h" #include "playerrelations.h" #include "shopitem.h" -#include "sound.h" +#include "soundmanager.h" #include "units.h" #include "net/net.h" diff --git a/src/gui/widgets/chattab.cpp b/src/gui/widgets/chattab.cpp index 4e6e7c522..d2d66e681 100644 --- a/src/gui/widgets/chattab.cpp +++ b/src/gui/widgets/chattab.cpp @@ -28,7 +28,7 @@ #include "commandhandler.h" #include "configuration.h" #include "localplayer.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/helpwindow.h" diff --git a/src/gui/widgets/guildchattab.cpp b/src/gui/widgets/guildchattab.cpp index 442bb44e2..5d60c2a29 100644 --- a/src/gui/widgets/guildchattab.cpp +++ b/src/gui/widgets/guildchattab.cpp @@ -26,7 +26,7 @@ #include "guild.h" #include "guildmanager.h" #include "localplayer.h" -#include "sound.h" +#include "soundmanager.h" #include "resources/iteminfo.h" #include "resources/itemdb.h" diff --git a/src/gui/widgets/setupitem.cpp b/src/gui/widgets/setupitem.cpp index 3a273e419..cb6dc378a 100644 --- a/src/gui/widgets/setupitem.cpp +++ b/src/gui/widgets/setupitem.cpp @@ -22,7 +22,7 @@ #include "configuration.h" #include "main.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/editdialog.h" #include "gui/gui.h" diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index 8c1f63e29..83ae2e42b 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -25,7 +25,7 @@ #include "client.h" #include "configuration.h" #include "graphicsvertexes.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/gui.h" #include "gui/viewport.h" diff --git a/src/localplayer.cpp b/src/localplayer.cpp index d387cdd40..3ec5c6b71 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -36,7 +36,7 @@ #include "playerinfo.h" #include "playerrelations.h" #include "simpleanimation.h" -#include "sound.h" +#include "soundmanager.h" #include "statuseffect.h" #include "dropshortcut.h" diff --git a/src/net/ea/gui/guildtab.cpp b/src/net/ea/gui/guildtab.cpp index 7d7c5119e..d1aef6b12 100644 --- a/src/net/ea/gui/guildtab.cpp +++ b/src/net/ea/gui/guildtab.cpp @@ -25,7 +25,7 @@ #include "chatlogger.h" #include "guild.h" #include "localplayer.h" -#include "sound.h" +#include "soundmanager.h" #include "net/net.h" diff --git a/src/net/ea/gui/partytab.cpp b/src/net/ea/gui/partytab.cpp index e5b2c586b..5a6615038 100644 --- a/src/net/ea/gui/partytab.cpp +++ b/src/net/ea/gui/partytab.cpp @@ -26,7 +26,7 @@ #include "commandhandler.h" #include "localplayer.h" #include "party.h" -#include "sound.h" +#include "soundmanager.h" #include "net/net.h" #include "net/partyhandler.h" diff --git a/src/sound.cpp b/src/sound.cpp deleted file mode 100644 index a316abc9d..000000000 --- a/src/sound.cpp +++ /dev/null @@ -1,417 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011-2012 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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 2 of the License, or - * 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 . - */ - -#include "sound.h" - -#include "configuration.h" -#include "localplayer.h" - -#include "resources/sdlmusic.h" -#include "resources/resourcemanager.h" -#include "resources/soundeffect.h" - -#include - -#include "debug.h" - -/** - * This will be set to true, when a music can be freed after a fade out - * Currently used by fadeOutCallBack() - */ -static bool sFadingOutEnded = false; - -/** - * Callback used at end of fadeout. - * It is called by Mix_MusicFadeFinished(). - */ -static void fadeOutCallBack() -{ - sFadingOutEnded = true; -} - -Sound::Sound(): - mInstalled(false), - mSfxVolume(100), - mMusicVolume(60), - mMusic(nullptr), - mPlayBattle(false), - mPlayGui(false), - mPlayMusic(false), - mGuiChannel(-1) -{ - // This set up our callback function used to - // handle fade outs endings. - sFadingOutEnded = false; - Mix_HookMusicFinished(fadeOutCallBack); -} - -Sound::~Sound() -{ - config.removeListeners(this); - - // Unlink the callback function. - Mix_HookMusicFinished(nullptr); -} - -void Sound::optionChanged(const std::string &value) -{ - if (value == "playBattleSound") - mPlayBattle = config.getBoolValue("playBattleSound"); - else if (value == "playGuiSound") - mPlayGui = config.getBoolValue("playGuiSound"); - else if (value == "playMusic") - mPlayMusic = config.getBoolValue("playMusic"); - else if (value == "sfxVolume") - setSfxVolume(config.getIntValue("sfxVolume")); - else if (value == "musicVolume") - setMusicVolume(config.getIntValue("musicVolume")); -} - -void Sound::init() -{ - // Don't initialize sound engine twice - if (mInstalled) - return; - - logger->log1("Sound::init() Initializing sound..."); - - mPlayBattle = config.getBoolValue("playBattleSound"); - mPlayGui = config.getBoolValue("playGuiSound"); - mPlayMusic = config.getBoolValue("playMusic"); - config.addListener("playBattleSound", this); - config.addListener("playGuiSound", this); - config.addListener("playMusic", this); - config.addListener("sfxVolume", this); - config.addListener("musicVolume", this); - - if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) - { - logger->log1("Sound::init() Failed to initialize audio subsystem"); - return; - } - - const size_t audioBuffer = 4096; - int channels = config.getIntValue("audioChannels"); - switch (channels) - { - case 3: - channels = 4; - break; - case 4: - channels = 6; - break; - default: - break; - } - - const int res = Mix_OpenAudio(config.getIntValue("audioFrequency"), - MIX_DEFAULT_FORMAT, channels, audioBuffer); - - if (res < 0) - { - logger->log("Sound::init Could not initialize audio: %s", - Mix_GetError()); - if (Mix_OpenAudio(22010, MIX_DEFAULT_FORMAT, 2, audioBuffer) < 0) - return; - logger->log("Fallback to stereo audio"); - } - - Mix_AllocateChannels(16); - Mix_VolumeMusic(mMusicVolume); - Mix_Volume(-1, mSfxVolume); - - info(); - - mInstalled = true; - - if (!mCurrentMusicFile.empty() && mPlayMusic) - playMusic(mCurrentMusicFile); -} - -void Sound::info() const -{ - SDL_version compiledVersion; - const SDL_version *linkedVersion; - char driver[40] = "Unknown"; - const char *format = "Unknown"; - int rate = 0; - uint16_t audioFormat = 0; - int channels = 0; - - MIX_VERSION(&compiledVersion); - linkedVersion = Mix_Linked_Version(); - - SDL_AudioDriverName(driver, 40); - - Mix_QuerySpec(&rate, &audioFormat, &channels); - switch (audioFormat) - { - case AUDIO_U8: format = "U8"; break; - case AUDIO_S8: format = "S8"; break; - case AUDIO_U16LSB: format = "U16LSB"; break; - case AUDIO_S16LSB: format = "S16LSB"; break; - case AUDIO_U16MSB: format = "U16MSB"; break; - case AUDIO_S16MSB: format = "S16MSB"; break; - default: break; - } - - logger->log("Sound::info() SDL_mixer: %i.%i.%i (compiled)", - compiledVersion.major, - compiledVersion.minor, - compiledVersion.patch); - if (linkedVersion) - { - logger->log("Sound::info() SDL_mixer: %i.%i.%i (linked)", - linkedVersion->major, - linkedVersion->minor, - linkedVersion->patch); - } - else - { - logger->log1("Sound::info() SDL_mixer: unknown"); - } - logger->log("Sound::info() Driver: %s", driver); - logger->log("Sound::info() Format: %s", format); - logger->log("Sound::info() Rate: %i", rate); - logger->log("Sound::info() Channels: %i", channels); -} - -void Sound::setMusicVolume(const int volume) -{ - mMusicVolume = volume; - - if (mInstalled) - Mix_VolumeMusic(mMusicVolume); -} - -void Sound::setSfxVolume(const int volume) -{ - mSfxVolume = volume; - - if (mInstalled) - Mix_Volume(-1, mSfxVolume); -} - -static SDLMusic *loadMusic(const std::string &fileName) -{ - ResourceManager *const resman = ResourceManager::getInstance(); - return resman->getMusic(paths.getStringValue("music") + fileName); -} - -void Sound::playMusic(const std::string &fileName) -{ - mCurrentMusicFile = fileName; - - if (!mInstalled || !mPlayMusic) - return; - - haltMusic(); - - if (!fileName.empty()) - { - mMusic = loadMusic(fileName); - if (mMusic) - mMusic->play(); - } -} - -void Sound::stopMusic() -{ - haltMusic(); -} - -void Sound::fadeInMusic(const std::string &fileName, const int ms) -{ - mCurrentMusicFile = fileName; - - if (!mInstalled || !mPlayMusic) - return; - - haltMusic(); - - if (!fileName.empty()) - { - mMusic = loadMusic(fileName); - if (mMusic) - mMusic->play(-1, ms); - } -} - -void Sound::fadeOutMusic(const int ms) -{ - mCurrentMusicFile.clear(); - - if (!mInstalled) - return; - - logger->log("Sound::fadeOutMusic() Fading-out (%i ms)", ms); - - if (mMusic) - { - Mix_FadeOutMusic(ms); - // Note: The fadeOutCallBack handler will take care about freeing - // the music file at fade out ending. - } - else - { - sFadingOutEnded = true; - } -} - -void Sound::fadeOutAndPlayMusic(const std::string &fileName, const int ms) -{ - mNextMusicFile = fileName; - fadeOutMusic(ms); -} - -void Sound::logic() -{ - BLOCK_START("Sound::logic") - if (sFadingOutEnded) - { - if (mMusic) - { - mMusic->decRef(); - mMusic = nullptr; - } - sFadingOutEnded = false; - - if (!mNextMusicFile.empty()) - { - playMusic(mNextMusicFile); - mNextMusicFile.clear(); - } - } - BLOCK_END("Sound::logic") -} - -void Sound::playSfx(const std::string &path, const int x, const int y) const -{ - if (!mInstalled || path.empty() || !mPlayBattle) - return; - - std::string tmpPath; - if (!path.compare(0, 4, "sfx/")) - tmpPath = path; - else - tmpPath = paths.getValue("sfx", "sfx/") + path; - ResourceManager *const resman = ResourceManager::getInstance(); - SoundEffect *const sample = resman->getSoundEffect(tmpPath); - if (sample) - { - logger->log("Sound::playSfx() Playing: %s", path.c_str()); - int vol = 120; - if (player_node && (x > 0 || y > 0)) - { - int dx = player_node->getTileX() - x; - int dy = player_node->getTileY() - y; - if (dx < 0) - dx = -dx; - if (dy < 0) - dy = -dy; - const int dist = dx > dy ? dx : dy; - if (dist * 8 > vol) - return; - - vol -= dist * 8; - } - sample->play(0, vol); - } -} - -void Sound::playGuiSound(const std::string &name) -{ - playGuiSfx(branding.getStringValue("systemsounds") - + config.getStringValue(name) + ".ogg"); -} - -void Sound::playGuiSfx(const std::string &path) -{ - if (!mInstalled || path.empty() || !mPlayGui) - return; - - std::string tmpPath; - if (!path.compare(0, 4, "sfx/")) - tmpPath = path; - else - tmpPath = paths.getValue("sfx", "sfx/") + path; - ResourceManager *const resman = ResourceManager::getInstance(); - SoundEffect *const sample = resman->getSoundEffect(tmpPath); - if (sample) - { - logger->log("Sound::playGuiSfx() Playing: %s", path.c_str()); - const int ret = sample->play(0, 120, mGuiChannel); - if (ret != -1) - mGuiChannel = ret; - } -} - -void Sound::close() -{ - if (!mInstalled) - return; - - haltMusic(); - logger->log1("Sound::close() Shutting down sound..."); - Mix_CloseAudio(); - - mInstalled = false; -} - -void Sound::haltMusic() -{ - if (!mMusic) - return; - - Mix_HaltMusic(); - if (mMusic) - { - mMusic->decRef(); - mMusic = nullptr; - } -} - -void Sound::changeAudio() -{ - if (mInstalled) - close(); - else - init(); -} - -void Sound::volumeOff() const -{ - if (mInstalled) - { - Mix_VolumeMusic(0); - Mix_Volume(-1, 0); - } -} - -void Sound::volumeRestore() -{ - if (mInstalled) - { - Mix_VolumeMusic(mMusicVolume); - Mix_Volume(-1, mSfxVolume); - } -} diff --git a/src/sound.h b/src/sound.h deleted file mode 100644 index 12b75a86b..000000000 --- a/src/sound.h +++ /dev/null @@ -1,176 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011-2012 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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 2 of the License, or - * 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 . - */ - -#ifndef SOUND_H -#define SOUND_H - -#include - -#include "configlistener.h" - -#include - -#include "localconsts.h" - -class SDLMusic; - -static const std::string SOUND_INFO = "soundinfo"; -static const std::string SOUND_ERROR = "sounderror"; -static const std::string SOUND_REQUEST = "soundrequest"; -static const std::string SOUND_TRADE = "soundtrade"; -static const std::string SOUND_WHISPER = "soundwhisper"; -static const std::string SOUND_HIGHLIGHT = "soundhighlight"; -static const std::string SOUND_GLOBAL = "soundglobal"; -static const std::string SOUND_GUILD = "soundguild"; -static const std::string SOUND_SHOW_WINDOW = "soundshowwindow"; -static const std::string SOUND_HIDE_WINDOW = "soundhidewindow"; - -/** Sound engine - * - * \ingroup CORE - */ -class Sound final : public ConfigListener -{ - public: - Sound(); - - A_DELETE_COPY(Sound) - - ~Sound(); - - void optionChanged(const std::string &value) override; - - /** - * Installs the sound engine. - */ - void init(); - - /** - * Removes all sound functionalities. - */ - void close(); - - /** - * Starts background music. - * - * @param fileName The name of the music file. - */ - void playMusic(const std::string &fileName); - - /** - * Stops currently running background music track. - */ - void stopMusic(); - - /** - * Fades in background music. - * - * @param fileName The name of the music file. - * @param ms Duration of fade-in effect (ms) - */ - void fadeInMusic(const std::string &fileName, const int ms = 1000); - - /** - * Fades out currently running background music track. - * - * @param ms Duration of fade-out effect (ms) - */ - void fadeOutMusic(const int ms = 1000); - - /** - * Fades out a background music and play a new one. - * - * @param fileName The name of the music file. - * @param ms Duration of fade-out effect (ms) - */ - void fadeOutAndPlayMusic(const std::string &fileName, - const int ms = 1000); - - int getMaxVolume() const A_WARN_UNUSED - { return MIX_MAX_VOLUME; } - - void setMusicVolume(const int volume); - - void setSfxVolume(const int volume); - - /** - * Plays an item. - * - * @param path The resource path to the sound file. - */ - void playSfx(const std::string &path, const int x = 0, - const int y = 0) const; - - /** - * Plays an item for gui. - * - * @param path The resource path to the sound file. - */ - void playGuiSfx(const std::string &path); - - void playGuiSound(const std::string &name); - - void changeAudio(); - - void volumeOff() const; - - void volumeRestore(); - - std::string getCurrentMusicFile() const A_WARN_UNUSED - { return mCurrentMusicFile; } - - /** - * The sound logic. - * Currently used to check whether the music file can be freed after - * a fade out, and whether new music has to be played. - */ - void logic(); - - private: - /** Logs various info about sound device. */ - void info() const; - - /** Halts and frees currently playing music. */ - void haltMusic(); - - /** - * When calling fadeOutAndPlayMusic(), - * the music file below will then be played - */ - std::string mNextMusicFile; - - bool mInstalled; - - int mSfxVolume; - int mMusicVolume; - - std::string mCurrentMusicFile; - SDLMusic *mMusic; - bool mPlayBattle; - bool mPlayGui; - bool mPlayMusic; - int mGuiChannel; -}; - -extern Sound sound; - -#endif diff --git a/src/soundmanager.cpp b/src/soundmanager.cpp new file mode 100644 index 000000000..9a3bc3185 --- /dev/null +++ b/src/soundmanager.cpp @@ -0,0 +1,417 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2012 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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 2 of the License, or + * 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 . + */ + +#include "soundmanager.h" + +#include "configuration.h" +#include "localplayer.h" + +#include "resources/sdlmusic.h" +#include "resources/resourcemanager.h" +#include "resources/soundeffect.h" + +#include + +#include "debug.h" + +/** + * This will be set to true, when a music can be freed after a fade out + * Currently used by fadeOutCallBack() + */ +static bool sFadingOutEnded = false; + +/** + * Callback used at end of fadeout. + * It is called by Mix_MusicFadeFinished(). + */ +static void fadeOutCallBack() +{ + sFadingOutEnded = true; +} + +SoundManager::SoundManager(): + mInstalled(false), + mSfxVolume(100), + mMusicVolume(60), + mMusic(nullptr), + mPlayBattle(false), + mPlayGui(false), + mPlayMusic(false), + mGuiChannel(-1) +{ + // This set up our callback function used to + // handle fade outs endings. + sFadingOutEnded = false; + Mix_HookMusicFinished(fadeOutCallBack); +} + +SoundManager::~SoundManager() +{ + config.removeListeners(this); + + // Unlink the callback function. + Mix_HookMusicFinished(nullptr); +} + +void SoundManager::optionChanged(const std::string &value) +{ + if (value == "playBattleSound") + mPlayBattle = config.getBoolValue("playBattleSound"); + else if (value == "playGuiSound") + mPlayGui = config.getBoolValue("playGuiSound"); + else if (value == "playMusic") + mPlayMusic = config.getBoolValue("playMusic"); + else if (value == "sfxVolume") + setSfxVolume(config.getIntValue("sfxVolume")); + else if (value == "musicVolume") + setMusicVolume(config.getIntValue("musicVolume")); +} + +void SoundManager::init() +{ + // Don't initialize sound engine twice + if (mInstalled) + return; + + logger->log1("SoundManager::init() Initializing sound..."); + + mPlayBattle = config.getBoolValue("playBattleSound"); + mPlayGui = config.getBoolValue("playGuiSound"); + mPlayMusic = config.getBoolValue("playMusic"); + config.addListener("playBattleSound", this); + config.addListener("playGuiSound", this); + config.addListener("playMusic", this); + config.addListener("sfxVolume", this); + config.addListener("musicVolume", this); + + if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) + { + logger->log1("SoundManager::init() Failed to initialize audio subsystem"); + return; + } + + const size_t audioBuffer = 4096; + int channels = config.getIntValue("audioChannels"); + switch (channels) + { + case 3: + channels = 4; + break; + case 4: + channels = 6; + break; + default: + break; + } + + const int res = Mix_OpenAudio(config.getIntValue("audioFrequency"), + MIX_DEFAULT_FORMAT, channels, audioBuffer); + + if (res < 0) + { + logger->log("SoundManager::init Could not initialize audio: %s", + Mix_GetError()); + if (Mix_OpenAudio(22010, MIX_DEFAULT_FORMAT, 2, audioBuffer) < 0) + return; + logger->log("Fallback to stereo audio"); + } + + Mix_AllocateChannels(16); + Mix_VolumeMusic(mMusicVolume); + Mix_Volume(-1, mSfxVolume); + + info(); + + mInstalled = true; + + if (!mCurrentMusicFile.empty() && mPlayMusic) + playMusic(mCurrentMusicFile); +} + +void SoundManager::info() const +{ + SDL_version compiledVersion; + const SDL_version *linkedVersion; + char driver[40] = "Unknown"; + const char *format = "Unknown"; + int rate = 0; + uint16_t audioFormat = 0; + int channels = 0; + + MIX_VERSION(&compiledVersion); + linkedVersion = Mix_Linked_Version(); + + SDL_AudioDriverName(driver, 40); + + Mix_QuerySpec(&rate, &audioFormat, &channels); + switch (audioFormat) + { + case AUDIO_U8: format = "U8"; break; + case AUDIO_S8: format = "S8"; break; + case AUDIO_U16LSB: format = "U16LSB"; break; + case AUDIO_S16LSB: format = "S16LSB"; break; + case AUDIO_U16MSB: format = "U16MSB"; break; + case AUDIO_S16MSB: format = "S16MSB"; break; + default: break; + } + + logger->log("SoundManager::info() SDL_mixer: %i.%i.%i (compiled)", + compiledVersion.major, + compiledVersion.minor, + compiledVersion.patch); + if (linkedVersion) + { + logger->log("SoundManager::info() SDL_mixer: %i.%i.%i (linked)", + linkedVersion->major, + linkedVersion->minor, + linkedVersion->patch); + } + else + { + logger->log1("SoundManager::info() SDL_mixer: unknown"); + } + logger->log("SoundManager::info() Driver: %s", driver); + logger->log("SoundManager::info() Format: %s", format); + logger->log("SoundManager::info() Rate: %i", rate); + logger->log("SoundManager::info() Channels: %i", channels); +} + +void SoundManager::setMusicVolume(const int volume) +{ + mMusicVolume = volume; + + if (mInstalled) + Mix_VolumeMusic(mMusicVolume); +} + +void SoundManager::setSfxVolume(const int volume) +{ + mSfxVolume = volume; + + if (mInstalled) + Mix_Volume(-1, mSfxVolume); +} + +static SDLMusic *loadMusic(const std::string &fileName) +{ + ResourceManager *const resman = ResourceManager::getInstance(); + return resman->getMusic(paths.getStringValue("music") + fileName); +} + +void SoundManager::playMusic(const std::string &fileName) +{ + mCurrentMusicFile = fileName; + + if (!mInstalled || !mPlayMusic) + return; + + haltMusic(); + + if (!fileName.empty()) + { + mMusic = loadMusic(fileName); + if (mMusic) + mMusic->play(); + } +} + +void SoundManager::stopMusic() +{ + haltMusic(); +} + +void SoundManager::fadeInMusic(const std::string &fileName, const int ms) +{ + mCurrentMusicFile = fileName; + + if (!mInstalled || !mPlayMusic) + return; + + haltMusic(); + + if (!fileName.empty()) + { + mMusic = loadMusic(fileName); + if (mMusic) + mMusic->play(-1, ms); + } +} + +void SoundManager::fadeOutMusic(const int ms) +{ + mCurrentMusicFile.clear(); + + if (!mInstalled) + return; + + logger->log("SoundManager::fadeOutMusic() Fading-out (%i ms)", ms); + + if (mMusic) + { + Mix_FadeOutMusic(ms); + // Note: The fadeOutCallBack handler will take care about freeing + // the music file at fade out ending. + } + else + { + sFadingOutEnded = true; + } +} + +void SoundManager::fadeOutAndPlayMusic(const std::string &fileName, const int ms) +{ + mNextMusicFile = fileName; + fadeOutMusic(ms); +} + +void SoundManager::logic() +{ + BLOCK_START("SoundManager::logic") + if (sFadingOutEnded) + { + if (mMusic) + { + mMusic->decRef(); + mMusic = nullptr; + } + sFadingOutEnded = false; + + if (!mNextMusicFile.empty()) + { + playMusic(mNextMusicFile); + mNextMusicFile.clear(); + } + } + BLOCK_END("SoundManager::logic") +} + +void SoundManager::playSfx(const std::string &path, const int x, const int y) const +{ + if (!mInstalled || path.empty() || !mPlayBattle) + return; + + std::string tmpPath; + if (!path.compare(0, 4, "sfx/")) + tmpPath = path; + else + tmpPath = paths.getValue("sfx", "sfx/") + path; + ResourceManager *const resman = ResourceManager::getInstance(); + SoundEffect *const sample = resman->getSoundEffect(tmpPath); + if (sample) + { + logger->log("SoundManager::playSfx() Playing: %s", path.c_str()); + int vol = 120; + if (player_node && (x > 0 || y > 0)) + { + int dx = player_node->getTileX() - x; + int dy = player_node->getTileY() - y; + if (dx < 0) + dx = -dx; + if (dy < 0) + dy = -dy; + const int dist = dx > dy ? dx : dy; + if (dist * 8 > vol) + return; + + vol -= dist * 8; + } + sample->play(0, vol); + } +} + +void SoundManager::playGuiSound(const std::string &name) +{ + playGuiSfx(branding.getStringValue("systemsounds") + + config.getStringValue(name) + ".ogg"); +} + +void SoundManager::playGuiSfx(const std::string &path) +{ + if (!mInstalled || path.empty() || !mPlayGui) + return; + + std::string tmpPath; + if (!path.compare(0, 4, "sfx/")) + tmpPath = path; + else + tmpPath = paths.getValue("sfx", "sfx/") + path; + ResourceManager *const resman = ResourceManager::getInstance(); + SoundEffect *const sample = resman->getSoundEffect(tmpPath); + if (sample) + { + logger->log("SoundManager::playGuiSfx() Playing: %s", path.c_str()); + const int ret = sample->play(0, 120, mGuiChannel); + if (ret != -1) + mGuiChannel = ret; + } +} + +void SoundManager::close() +{ + if (!mInstalled) + return; + + haltMusic(); + logger->log1("SoundManager::close() Shutting down sound..."); + Mix_CloseAudio(); + + mInstalled = false; +} + +void SoundManager::haltMusic() +{ + if (!mMusic) + return; + + Mix_HaltMusic(); + if (mMusic) + { + mMusic->decRef(); + mMusic = nullptr; + } +} + +void SoundManager::changeAudio() +{ + if (mInstalled) + close(); + else + init(); +} + +void SoundManager::volumeOff() const +{ + if (mInstalled) + { + Mix_VolumeMusic(0); + Mix_Volume(-1, 0); + } +} + +void SoundManager::volumeRestore() +{ + if (mInstalled) + { + Mix_VolumeMusic(mMusicVolume); + Mix_Volume(-1, mSfxVolume); + } +} diff --git a/src/soundmanager.h b/src/soundmanager.h new file mode 100644 index 000000000..4ea36c27e --- /dev/null +++ b/src/soundmanager.h @@ -0,0 +1,176 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2012 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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 2 of the License, or + * 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 . + */ + +#ifndef SOUNDMANAGER_H +#define SOUNDMANAGER_H + +#include + +#include "configlistener.h" + +#include + +#include "localconsts.h" + +class SDLMusic; + +static const std::string SOUND_INFO = "soundinfo"; +static const std::string SOUND_ERROR = "sounderror"; +static const std::string SOUND_REQUEST = "soundrequest"; +static const std::string SOUND_TRADE = "soundtrade"; +static const std::string SOUND_WHISPER = "soundwhisper"; +static const std::string SOUND_HIGHLIGHT = "soundhighlight"; +static const std::string SOUND_GLOBAL = "soundglobal"; +static const std::string SOUND_GUILD = "soundguild"; +static const std::string SOUND_SHOW_WINDOW = "soundshowwindow"; +static const std::string SOUND_HIDE_WINDOW = "soundhidewindow"; + +/** SoundManager + * + * \ingroup CORE + */ +class SoundManager final : public ConfigListener +{ + public: + SoundManager(); + + A_DELETE_COPY(SoundManager) + + ~SoundManager(); + + void optionChanged(const std::string &value) override; + + /** + * Installs the sound engine. + */ + void init(); + + /** + * Removes all sound functionalities. + */ + void close(); + + /** + * Starts background music. + * + * @param fileName The name of the music file. + */ + void playMusic(const std::string &fileName); + + /** + * Stops currently running background music track. + */ + void stopMusic(); + + /** + * Fades in background music. + * + * @param fileName The name of the music file. + * @param ms Duration of fade-in effect (ms) + */ + void fadeInMusic(const std::string &fileName, const int ms = 1000); + + /** + * Fades out currently running background music track. + * + * @param ms Duration of fade-out effect (ms) + */ + void fadeOutMusic(const int ms = 1000); + + /** + * Fades out a background music and play a new one. + * + * @param fileName The name of the music file. + * @param ms Duration of fade-out effect (ms) + */ + void fadeOutAndPlayMusic(const std::string &fileName, + const int ms = 1000); + + int getMaxVolume() const A_WARN_UNUSED + { return MIX_MAX_VOLUME; } + + void setMusicVolume(const int volume); + + void setSfxVolume(const int volume); + + /** + * Plays an item. + * + * @param path The resource path to the sound file. + */ + void playSfx(const std::string &path, const int x = 0, + const int y = 0) const; + + /** + * Plays an item for gui. + * + * @param path The resource path to the sound file. + */ + void playGuiSfx(const std::string &path); + + void playGuiSound(const std::string &name); + + void changeAudio(); + + void volumeOff() const; + + void volumeRestore(); + + std::string getCurrentMusicFile() const A_WARN_UNUSED + { return mCurrentMusicFile; } + + /** + * The sound logic. + * Currently used to check whether the music file can be freed after + * a fade out, and whether new music has to be played. + */ + void logic(); + + private: + /** Logs various info about sound device. */ + void info() const; + + /** Halts and frees currently playing music. */ + void haltMusic(); + + /** + * When calling fadeOutAndPlayMusic(), + * the music file below will then be played + */ + std::string mNextMusicFile; + + bool mInstalled; + + int mSfxVolume; + int mMusicVolume; + + std::string mCurrentMusicFile; + SDLMusic *mMusic; + bool mPlayBattle; + bool mPlayGui; + bool mPlayMusic; + int mGuiChannel; +}; + +extern SoundManager sound; + +#endif diff --git a/src/statuseffect.cpp b/src/statuseffect.cpp index 82d77a14b..ef0a8ce10 100644 --- a/src/statuseffect.cpp +++ b/src/statuseffect.cpp @@ -23,7 +23,7 @@ #include "statuseffect.h" #include "logger.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/widgets/chattab.h" diff --git a/src/test/testlauncher.cpp b/src/test/testlauncher.cpp index 6d79f2e8e..2b1ea448a 100644 --- a/src/test/testlauncher.cpp +++ b/src/test/testlauncher.cpp @@ -26,7 +26,7 @@ #include "configuration.h" #include "graphics.h" #include "graphicsmanager.h" -#include "sound.h" +#include "soundmanager.h" #include "gui/theme.h" -- cgit v1.2.3-70-g09d2