Skip to content

Commit

Permalink
Added min/max sound distance
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestionableM committed Jul 17, 2023
1 parent a9c2c1e commit 895bdac
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 61 deletions.
75 changes: 31 additions & 44 deletions Code/Hooks/fmod_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ void FakeEventDescription::updateReverbData()
this->channel->setReverbProperties(a, (this->reverb_idx == a) ? 1.0f : 0.0f);
}

void FakeEventDescription::playSound()
{
SM::AudioManager* v_audio_mgr = SM::AudioManager::GetInstance();
if (!v_audio_mgr || this->isPlaying()) return;

if (v_audio_mgr->fmod_system->playSound(this->sound, nullptr, true, &this->channel) != FMOD_OK)
return;

this->channel->setVolume(SM::GameSettings::GetEffectsVolume());
this->channel->set3DMinMaxDistance(this->min_distance, this->max_distance);
this->channel->set3DDistanceFilter(false, 1.0f, 10000.0f);

if (this->is_3d)
this->channel->setMode(FMOD_3D);

this->updateReverbData();
}

FMOD::Sound* SoundStorage::CreateSound(const std::string& path)
{
const std::size_t hash = std::hash<std::string>{}(path);
Expand Down Expand Up @@ -91,23 +109,7 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_start(FMOD::Studio::EventInst
FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_instance);
if (v_fake_event->isValidHook())
{
bool is_playing = false;
v_fake_event->channel->isPlaying(&is_playing);

if (!is_playing)
{
FMOD_RESULT v_result = SM::AudioManager::GetInstance()->fmod_system->playSound(
v_fake_event->sound, nullptr, false, &v_fake_event->channel);

if (v_result == FMOD_OK)
{
v_fake_event->channel->setMode(FMOD_3D);
v_fake_event->channel->set3DConeSettings(75.0f, 360.0f, 0.1f);
}

return v_result;
}

v_fake_event->channel->setPaused(false);
return FMOD_OK;
}

Expand Down Expand Up @@ -265,20 +267,22 @@ using v_fmod_set_parameter_function = FMOD_RESULT(*)(FakeEventDescription* fake_

static FMOD_RESULT fake_event_desc_setPitch(FakeEventDescription* fake_event, float value)
{
return fake_event->channel->setPitch(value);
fake_event->channel->setPitch(value);
return FMOD_OK;
}

static FMOD_RESULT fake_event_desc_setVolume(FakeEventDescription* fake_event, float volume)
{
return fake_event->setVolume(volume);
fake_event->setVolume(volume);
return FMOD_OK;
}

static FMOD_RESULT fake_event_desc_setReverb(FakeEventDescription* fake_event, float reverb)
{
if (fake_event->reverb_idx == -1)
return FMOD_OK;
if (fake_event->reverb_idx != -1)
fake_event->channel->setReverbProperties(fake_event->reverb_idx, reverb);

return fake_event->channel->setReverbProperties(fake_event->reverb_idx, reverb);
return FMOD_OK;
}

static FMOD_RESULT fake_event_desc_setReverbIndex(FakeEventDescription* fake_event, float reverb_idx)
Expand Down Expand Up @@ -328,29 +332,12 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventDescription_createInstance(FMOD::Studi
SoundData* v_sound_data = SoundStorage::GetSoundData(reinterpret_cast<std::size_t>(event_desc));
if (v_sound_data)
{
SM::AudioManager* v_audio_mgr = SM::AudioManager::GetInstance();
if (v_audio_mgr)
{
FMOD::Channel* v_channel;
if (v_audio_mgr->fmod_system->playSound(v_sound_data->sound, nullptr, false, &v_channel) == FMOD_OK)
{
v_channel->setVolume(SM::GameSettings::GetEffectsVolume());

v_channel->set3DMinMaxDistance(0.0f, 5000.0f);

//TODO: Add more config options later
if (v_sound_data->effect_data.is_3d)
v_channel->setMode(FMOD_3D);

FakeEventDescription* v_new_fake_event = new FakeEventDescription(v_sound_data->sound, v_channel);
v_new_fake_event->reverb_idx = v_sound_data->effect_data.reverb_idx;
v_new_fake_event->updateReverbData();
FakeEventDescription* v_new_fake_event = new FakeEventDescription(v_sound_data, nullptr);
v_new_fake_event->playSound();

*instance = reinterpret_cast<FMOD::Studio::EventInstance*>(v_new_fake_event);

return FMOD_OK;
}
}
*instance = reinterpret_cast<FMOD::Studio::EventInstance*>(v_new_fake_event);

return FMOD_OK;
}

return FMODHooks::o_FMOD_Studio_EventDescription_createInstance(event_desc, instance);
Expand Down
57 changes: 40 additions & 17 deletions Code/Hooks/fmod_hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,63 @@ namespace FStudioSystem
using GetEventById = FMOD_RESULT(__fastcall*)(FMOD::Studio::System*, const FMOD_GUID*, FMOD::Studio::EventDescription**);
}

struct SoundEffectData
{
bool is_3d;
int reverb_idx;
float min_distance;
float max_distance;
};

struct SoundData
{
FMOD::Sound* sound;
SoundEffectData effect_data;
};

#define FAKE_EVENT_DESC_MAGIC 13372281488

struct FakeEventDescription
{
std::size_t v_secret_number = FAKE_EVENT_DESC_MAGIC;

FMOD::Sound* sound = nullptr;
FMOD::Channel* channel = nullptr;
FMOD::Sound* sound;
FMOD::Channel* channel;

float custom_volume = 1.0f;
int reverb_idx = -1;
int reverb_idx;

FakeEventDescription(FMOD::Sound* v_sound, FMOD::Channel* v_channel)
float min_distance;
float max_distance;

bool is_3d;

FakeEventDescription(const SoundData* snd_data, FMOD::Channel* v_channel)
{
this->sound = v_sound;
this->sound = snd_data->sound;
this->channel = v_channel;

this->reverb_idx = snd_data->effect_data.reverb_idx;
this->min_distance = snd_data->effect_data.min_distance;
this->max_distance = snd_data->effect_data.max_distance;
this->is_3d = snd_data->effect_data.is_3d;
}

FMOD_RESULT setVolume(float new_volume);
FMOD_RESULT updateVolume();

void updateReverbData();
void playSound();

bool isPlaying() const
{
if (!this->channel) return false;

bool is_playing = false;
this->channel->isPlaying(&is_playing);

return is_playing;
}

bool isValidHook() const
{
Expand All @@ -77,18 +112,6 @@ struct FakeEventDescription
}
};

struct SoundEffectData
{
bool is_3d;
int reverb_idx;
};

struct SoundData
{
FMOD::Sound* sound;
SoundEffectData effect_data;
};

class SoundStorage
{
public:
Expand Down
14 changes: 14 additions & 0 deletions Code/Hooks/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,32 @@ int get_reverb_setting(const simdjson::dom::document_stream::iterator::value_typ

auto v_iter = g_reverbStringToIdx.find(v_reverb_str);
if (v_iter == g_reverbStringToIdx.end())
{
DebugErrorL("Invalid reverb preset name: ", v_reverb_str);
return -1;
}

return v_iter->second;
}

void load_min_max_distance(const simdjson::dom::element& cur_sound, SoundEffectData& effect_data)
{
const auto v_min_distance = cur_sound["min_distance"];
const auto v_max_distance = cur_sound["max_distance"];

effect_data.min_distance = v_min_distance.is_number() ? JsonReader::GetNumber<float>(v_min_distance) : 0.0f;
effect_data.max_distance = v_max_distance.is_number() ? JsonReader::GetNumber<float>(v_max_distance) : 10000.0f;
}

void load_effect_data(const simdjson::dom::element& cur_sound, SoundEffectData& effect_data)
{
const auto v_sound_is_3d_node = cur_sound["is3D"];
const auto v_reverb_node = cur_sound["reverb"];

effect_data.is_3d = v_sound_is_3d_node.is_bool() ? v_sound_is_3d_node.get_bool().value() : false;
effect_data.reverb_idx = get_reverb_setting(v_reverb_node);

load_min_max_distance(cur_sound, effect_data);
}

void load_sound_config(const std::string& key_repl)
Expand Down

0 comments on commit 895bdac

Please sign in to comment.