Skip to content

Commit 3e88bb2

Browse files
ReimousTHhyperbx
andauthored
Implement audio volume settings (#85)
* SoundConfig * Implement Sonicteam::System::Singleton * Remove save data struct Not relevant to the PR and doesn't seem to be mapped correctly. * Save audio settings to config on exit * Use original game defaults for volume settings * Fix naming convention --------- Co-authored-by: Hyper <34012267+hyperbx@users.noreply.github.com>
1 parent 4ff6e36 commit 3e88bb2

7 files changed

Lines changed: 87 additions & 6 deletions

File tree

MarathonRecomp/api/Marathon.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "CSD/Platform/csdTexList.h"
1818
#include "Sonicteam/Actor.h"
1919
#include "Sonicteam/AppMarathon.h"
20+
#include "Sonicteam/AudioEngineXenon.h"
2021
#include "Sonicteam/Camera/CameraMode.h"
2122
#include "Sonicteam/Camera/Cameraman.h"
2223
#include "Sonicteam/Camera/SonicCamera.h"
@@ -48,12 +49,15 @@
4849
#include "Sonicteam/Player/State/Object2.h"
4950
#include "Sonicteam/Player/State/TailsContext.h"
5051
#include "Sonicteam/SoX/AI/StateMachine.h"
52+
#include "Sonicteam/SoX/Audio/IAudioEngine.h"
5153
#include "Sonicteam/SoX/Component.h"
5254
#include "Sonicteam/SoX/Engine/Doc.h"
5355
#include "Sonicteam/SoX/Engine/DocMode.h"
5456
#include "Sonicteam/SoX/Engine/Task.h"
5557
#include "Sonicteam/SoX/MessageReceiver.h"
5658
#include "Sonicteam/SoX/Object.h"
59+
#include "Sonicteam/System/CreateStatic.h"
60+
#include "Sonicteam/System/Singleton.h"
5761
#include "Sonicteam/TitleTask.h"
5862
#include "boost/smart_ptr/make_shared_object.h"
5963
#include "boost/smart_ptr/shared_ptr.h"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <Marathon.inl>
4+
#include <Sonicteam/SoX/Engine/Doc.h>
5+
#include <Sonicteam/SoX/Audio/IAudioEngine.h>
6+
#include <Sonicteam/System/CreateStatic.h>
7+
#include <Sonicteam/System/Singleton.h>
8+
9+
namespace Sonicteam
10+
{
11+
class AudioEngineXenon : public SoX::Audio::IAudioEngine, public System::Singleton<AudioEngineXenon, 0x82D37AC8, System::CreateStatic<AudioEngineXenon, 0x824A5C78>>
12+
{
13+
public:
14+
MARATHON_INSERT_PADDING(8);
15+
be<float> m_MusicVolume;
16+
be<float> m_EffectsVolume;
17+
MARATHON_INSERT_PADDING(0x14);
18+
};
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <Marathon.inl>
4+
5+
namespace Sonicteam::SoX::Audio
6+
{
7+
class IAudioEngine
8+
{
9+
public:
10+
xpointer<void> m_pVftable;
11+
};
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
namespace Sonicteam::System
4+
{
5+
template <typename T, uint32_t TCreator>
6+
class CreateStatic
7+
{
8+
public:
9+
static T* Create()
10+
{
11+
return GuestToHostFunction<T*>(TCreator);
12+
}
13+
};
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
namespace Sonicteam::System
4+
{
5+
template <typename T, const uint32_t Ptr, typename TCreator>
6+
class Singleton
7+
{
8+
inline static TCreator ms_Creator{};
9+
10+
public:
11+
static T* GetInstance()
12+
{
13+
auto pInstance = (xpointer<T>*)g_memory.Translate(Ptr);
14+
15+
if (!pInstance->ptr.get())
16+
*pInstance = ms_Creator.Create();
17+
18+
return *pInstance;
19+
}
20+
};
21+
}

MarathonRecomp/app.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ void App::Restart(std::vector<std::string> restartArgs)
1919

2020
void App::Exit()
2121
{
22+
// TODO (Hyper): remove this once the new options menu is implemented.
23+
if (auto pAudioEngine = Sonicteam::AudioEngineXenon::GetInstance())
24+
{
25+
Config::MusicVolume = pAudioEngine->m_MusicVolume;
26+
Config::EffectsVolume = pAudioEngine->m_EffectsVolume;
27+
}
28+
2229
Config::Save();
2330

2431
#ifdef _WIN32
@@ -44,11 +51,15 @@ PPC_FUNC(sub_8262A568)
4451
be<uint32_t> Height;
4552
};
4653

47-
auto cfg = reinterpret_cast<RenderConfig*>(g_memory.Translate(ctx.r4.u32));
48-
cfg->Width = Video::s_viewportWidth;
49-
cfg->Height = Video::s_viewportHeight;
54+
auto pRenderConfig = reinterpret_cast<RenderConfig*>(g_memory.Translate(ctx.r4.u32));
55+
pRenderConfig->Width = Video::s_viewportWidth;
56+
pRenderConfig->Height = Video::s_viewportHeight;
57+
58+
auto pAudioEngine = Sonicteam::AudioEngineXenon::GetInstance();
59+
pAudioEngine->m_MusicVolume = Config::MusicVolume * Config::MasterVolume;
60+
pAudioEngine->m_EffectsVolume = Config::EffectsVolume * Config::MasterVolume;
5061

51-
LOGFN_UTILITY("Changed resolution: {}x{}", cfg->Width.get(), cfg->Height.get());
62+
LOGFN_UTILITY("Changed resolution: {}x{}", pRenderConfig->Width.get(), pRenderConfig->Height.get());
5263

5364
__imp__sub_8262A568(ctx, base);
5465

MarathonRecomp/user/config_def.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ CONFIG_DEFINE_ENUM("Bindings", SDL_Scancode, Key_RightStickLeft, SDL_SCANCODE_UN
3939
CONFIG_DEFINE_ENUM("Bindings", SDL_Scancode, Key_RightStickRight, SDL_SCANCODE_UNKNOWN);
4040

4141
CONFIG_DEFINE_LOCALISED("Audio", float, MasterVolume, 1.0f);
42-
CONFIG_DEFINE_LOCALISED("Audio", float, MusicVolume, 1.0f);
43-
CONFIG_DEFINE_LOCALISED("Audio", float, EffectsVolume, 1.0f);
42+
CONFIG_DEFINE_LOCALISED("Audio", float, MusicVolume, 0.6f);
43+
CONFIG_DEFINE_LOCALISED("Audio", float, EffectsVolume, 0.6f);
4444
CONFIG_DEFINE_ENUM_LOCALISED("Audio", EChannelConfiguration, ChannelConfiguration, EChannelConfiguration::Stereo);
4545
CONFIG_DEFINE_LOCALISED("Audio", bool, MusicAttenuation, false);
4646

0 commit comments

Comments
 (0)