1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-16 18:01:40 +00:00

Sound refactor and improvements (#12764)

This commit is contained in:
DS 2023-06-16 20:15:21 +02:00 committed by GitHub
parent 8e1af25738
commit edcbfa31c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 2802 additions and 1211 deletions

View file

@ -32,7 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiMainMenu.h"
#include "sound.h"
#include "client/sound_openal.h"
#include "client/clouds.h"
#include "httpfetch.h"
#include "log.h"
#include "client/fontengine.h"
@ -97,28 +96,15 @@ video::ITexture *MenuTextureSource::getTexture(const std::string &name, u32 *id)
/******************************************************************************/
/** MenuMusicFetcher */
/******************************************************************************/
void MenuMusicFetcher::fetchSounds(const std::string &name,
std::set<std::string> &dst_paths,
std::set<std::string> &dst_datas)
void MenuMusicFetcher::addThePaths(const std::string &name,
std::vector<std::string> &paths)
{
if(m_fetched.count(name))
return;
m_fetched.insert(name);
std::vector<fs::DirListNode> list;
// Reusable local function
auto add_paths = [&dst_paths](const std::string name, const std::string base = "") {
dst_paths.insert(base + name + ".ogg");
for (int i = 0; i < 10; i++)
dst_paths.insert(base + name + "." + itos(i) + ".ogg");
};
// Allow full paths
if (name.find(DIR_DELIM_CHAR) != std::string::npos) {
add_paths(name);
addAllAlternatives(name, paths);
} else {
std::string share_prefix = porting::path_share + DIR_DELIM;
add_paths(name, share_prefix + "sounds" + DIR_DELIM);
std::string user_prefix = porting::path_user + DIR_DELIM;
add_paths(name, user_prefix + "sounds" + DIR_DELIM);
addAllAlternatives(porting::path_share + DIR_DELIM + "sounds" + DIR_DELIM + name, paths);
addAllAlternatives(porting::path_user + DIR_DELIM + "sounds" + DIR_DELIM + name, paths);
}
}
@ -151,8 +137,10 @@ GUIEngine::GUIEngine(JoystickController *joystick,
// create soundmanager
#if USE_SOUND
if (g_settings->getBool("enable_sound") && g_sound_manager_singleton.get())
m_sound_manager.reset(createOpenALSoundManager(g_sound_manager_singleton.get(), &m_soundfetcher));
if (g_settings->getBool("enable_sound") && g_sound_manager_singleton.get()) {
m_sound_manager = createOpenALSoundManager(g_sound_manager_singleton.get(),
std::make_unique<MenuMusicFetcher>());
}
#endif
if (!m_sound_manager)
m_sound_manager = std::make_unique<DummySoundManager>();
@ -318,11 +306,12 @@ void GUIEngine::run()
/******************************************************************************/
GUIEngine::~GUIEngine()
{
m_sound_manager.reset();
infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
// deinitialize script first. gc destructors might depend on other stuff
infostream << "GUIEngine: Deinitializing scripting" << std::endl;
m_script.reset();
m_sound_manager.reset();
m_irr_toplefttext->setText(L"");
//clean up texture pointers
@ -608,16 +597,3 @@ void GUIEngine::updateTopLeftTextSize()
m_irr_toplefttext = gui::StaticText::add(m_rendering_engine->get_gui_env(),
m_toplefttext, rect, false, true, 0, -1);
}
/******************************************************************************/
s32 GUIEngine::playSound(const SimpleSoundSpec &spec)
{
s32 handle = m_sound_manager->playSound(spec);
return handle;
}
/******************************************************************************/
void GUIEngine::stopSound(s32 handle)
{
m_sound_manager->stopSound(handle);
}

View file

@ -115,30 +115,20 @@ private:
std::vector<video::ITexture*> m_to_delete;
};
/** GUIEngine specific implementation of OnDemandSoundFetcher */
class MenuMusicFetcher: public OnDemandSoundFetcher
/** GUIEngine specific implementation of SoundFallbackPathProvider */
class MenuMusicFetcher final : public SoundFallbackPathProvider
{
public:
/**
* get sound file paths according to sound name
* @param name sound name
* @param dst_paths receives possible paths to sound files
* @param dst_datas receives binary sound data (not used here)
*/
void fetchSounds(const std::string &name,
std::set<std::string> &dst_paths,
std::set<std::string> &dst_datas);
private:
/** set of fetched sound names */
std::set<std::string> m_fetched;
protected:
void addThePaths(const std::string &name,
std::vector<std::string> &paths) override;
};
/** implementation of main menu based uppon formspecs */
class GUIEngine {
/** grant ModApiMainMenu access to private members */
friend class ModApiMainMenu;
friend class ModApiSound;
friend class ModApiMainMenuSound;
friend class MainMenuSoundHandle;
public:
/**
@ -197,8 +187,6 @@ private:
MainMenuData *m_data = nullptr;
/** texture source */
std::unique_ptr<ISimpleTextureSource> m_texture_source;
/** sound fetcher, used by sound manager*/
MenuMusicFetcher m_soundfetcher{};
/** sound manager*/
std::unique_ptr<ISoundManager> m_sound_manager;
@ -296,11 +284,4 @@ private:
bool m_clouds_enabled = true;
/** data used to draw clouds */
clouddata m_cloud;
/** start playing a sound and return handle */
s32 playSound(const SimpleSoundSpec &spec);
/** stop playing a sound started with playSound() */
void stopSound(s32 handle);
};

View file

@ -4816,7 +4816,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
if ((s.ftype == f_TabHeader) &&
(s.fid == event.GUIEvent.Caller->getID())) {
if (!s.sound.empty() && m_sound_manager)
m_sound_manager->playSound(SimpleSoundSpec(s.sound, 1.0f));
m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
s.send = true;
acceptInput();
s.send = false;
@ -4861,7 +4861,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
if (s.ftype == f_Button || s.ftype == f_CheckBox) {
if (!s.sound.empty() && m_sound_manager)
m_sound_manager->playSound(SimpleSoundSpec(s.sound, 1.0f));
m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
s.send = true;
if (s.is_exit) {
@ -4886,7 +4886,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
}
}
if (!s.sound.empty() && m_sound_manager)
m_sound_manager->playSound(SimpleSoundSpec(s.sound, 1.0f));
m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
s.send = true;
acceptInput(quit_mode_no);
@ -4904,7 +4904,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
s.fdefault.clear();
} else if (s.ftype == f_Unknown || s.ftype == f_HyperText) {
if (!s.sound.empty() && m_sound_manager)
m_sound_manager->playSound(SimpleSoundSpec(s.sound, 1.0f));
m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
s.send = true;
acceptInput();
s.send = false;