Skip to content

Commit

Permalink
FF8: Fix volume inheritance for SFX effects
Browse files Browse the repository at this point in the history
  • Loading branch information
julianxhokaxhiu committed Dec 18, 2024
1 parent c3e3f11 commit c7775f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Music: Add the ability to play a custom `main_menu` music file on the new game screen.

## FF8

- SFX: Fix incorrect volume assignment when playing sfx effects using the external layer

# 1.21.3

- Full commit list since last stable release: https://github.com/julianxhokaxhiu/FFNx/compare/1.21.2...1.21.3
Expand Down
9 changes: 7 additions & 2 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void NxAudioEngine::unloadSFXChannel(int channel)
}
}

bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning, bool loop)
bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning, bool loop, float volume)
{
NxAudioEngineSFX *options = &_sfxChannels[channel - 1];
int _curId = id;
Expand Down Expand Up @@ -325,6 +325,9 @@ bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning
unloadSFXChannel(channel);
}

// Reset state
options->volume = volume;

auto node = nxAudioEngineConfig[NxAudioEngineLayer::NXAUDIOENGINE_SFX][name];
if (node)
{
Expand Down Expand Up @@ -382,7 +385,7 @@ bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning
if (trace_all || trace_sfx) ffnx_trace("NxAudioEngine::%s: panning overridden because of external_sfx_always_centered\n", __func__);
}

if (trace_all || trace_sfx) ffnx_trace("NxAudioEngine::%s: name=%s,id=%d,channel=%d,panning=%f\n", __func__, name, options->id, channel, panning);
if (trace_all || trace_sfx) ffnx_trace("NxAudioEngine::%s: name=%s,id=%d,channel=%d,panning=%f,volume=%f\n", __func__, name, options->id, channel, panning, options->volume);

if (options->stream != nullptr)
{
Expand Down Expand Up @@ -462,6 +465,8 @@ void NxAudioEngine::setSFXVolume(int channel, float volume, double time)

options->volume = volume;

ffnx_trace("NxAudioEngine::%s: channel=%d,volume=%f\n", __func__, channel, volume);

if (time > 0.0) {
time /= gamehacks.getCurrentSpeedhack();
_engine.fadeVolume(options->handle, volume * getSFXMasterVolume(), time);
Expand Down
2 changes: 1 addition & 1 deletion src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class NxAudioEngine
// SFX
int getSFXIdFromChannel(int channel);
void unloadSFX(int id);
bool playSFX(const char* name, int id, int channel, float panning, bool loop = false);
bool playSFX(const char* name, int id, int channel, float panning, bool loop = false, float volume = 1.0f);
void stopSFX(int channel, double time = 0);
void pauseSFX(int channel);
void resumeSFX(int channel);
Expand Down
9 changes: 4 additions & 5 deletions src/sfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ bool ff8_sfx_play_layered(int channel, int id, int volume, float panning)
bool playing = false;
char track_name[64];
float panningf = panning == 64 ? 0.0f : panning * 2 / 127.0f - 1.0f;
float volumef = volume / 127.0;
float volumef = volume / 127.0f;
bool loop = false;

// Get loop info from audio.fmt
Expand All @@ -254,13 +254,12 @@ bool ff8_sfx_play_layered(int channel, int id, int volume, float panning)
}

// TODO: inverted panning option ((Reg.SoundOptions >> 20) & 1)
nxAudioEngine.setSFXVolume(channel, volumef);

switch(mode->driver_mode)
{
case MODE_FIELD:
sprintf(track_name, "%s_%d_%d", get_current_field_name(), *common_externals.current_triangle_id, id);
playing = nxAudioEngine.playSFX(track_name, id, channel, panningf, loop);
playing = nxAudioEngine.playSFX(track_name, id, channel, panningf, loop, volumef);
if (!playing) sprintf(track_name, "%s_%d", get_current_field_name(), id);
break;
case MODE_MENU:
Expand All @@ -278,10 +277,10 @@ bool ff8_sfx_play_layered(int channel, int id, int volume, float panning)
}

// If any overridden layer could not be played, fallback to default
if (!(playing = nxAudioEngine.playSFX(track_name, id, channel, panningf, loop)))
if (!(playing = nxAudioEngine.playSFX(track_name, id, channel, panningf, loop, volumef)))
{
sprintf(track_name, "%d", id);
playing = nxAudioEngine.playSFX(track_name, id, channel, panningf, loop);
playing = nxAudioEngine.playSFX(track_name, id, channel, panningf, loop, volumef);
}

return playing;
Expand Down

0 comments on commit c7775f6

Please sign in to comment.