Skip to content

Commit

Permalink
Common: Introduce a new Audio Volume management via config
Browse files Browse the repository at this point in the history
  • Loading branch information
julianxhokaxhiu committed Jul 23, 2023
1 parent e521213 commit 6acd031
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 35 deletions.
50 changes: 50 additions & 0 deletions misc/FFNx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ external_audio_sample_rate = 44100
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
use_external_sfx = false

#[EXTERNAL SFX VOLUME]
# This setting will allow you to configure the volume of the SFX layer.
# Possible values: 0..100
# If -1, the engine will try to detect the volume by other meanings:
# - FF7 1998: It will attempt to use the original game registry paths
# - FF7 Steam: It will attempt to read it from the ff7sound.cfg file
# - FF8 2000: It will be set to 100% by default
# - FF8 Steam: It will be set to 100% by default
external_sfx_volume = -1

#[EXTERNAL SFX PATH]
# Path of the external sound files
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -182,6 +192,16 @@ external_sfx_always_centered = false
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
use_external_music = false

#[EXTERNAL MUSIC VOLUME]
# This setting will allow you to configure the volume of the Music layer.
# Possible values: 0..100
# If -1, the engine will try to detect the volume by other meanings:
# - FF7 1998: It will attempt to use the original game registry paths
# - FF7 Steam: It will attempt to read it from the ff7sound.cfg file
# - FF8 2000: It will be set to 100% by default
# - FF8 Steam: It will be set to 100% by default
external_music_volume = -1

#[EXTERNAL MUSIC RESUME]
# This flag will enable the support for music resume on known areas of the game ( for eg. World-Map ).
# If you enable this flag, instead of starting the OST for a known area from the beginning everytime, it will resume where you left it off the last time you visited it.
Expand Down Expand Up @@ -234,6 +254,16 @@ external_voice_path = "voice"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
external_voice_ext = "ogg"

#[EXTERNAL VOICE VOLUME]
# This setting will allow you to configure the volume of the Voice layer.
# Possible values: 0..100
# If -1, the engine will try to detect the volume by other meanings:
# - FF7 1998: It will attempt to use the FFNx registry paths ( also used by 7th Heaven )
# - FF7 Steam: It will be set to 100% by default
# - FF8 2000: It will be set to 100% by default
# - FF8 Steam: It will be set to 100% by default
external_voice_volume = -1

#[ENABLE VOICE MUSIC FADE]
# This flag will enable the music volume fade out and fade in, when a voice acting is being played back.
# Please note this flag will take effect ONLY when "use_external_music" is enabled.
Expand Down Expand Up @@ -264,6 +294,16 @@ external_ambient_path = "ambient"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
external_ambient_ext = "ogg"

#[EXTERNAL AMBIENT VOLUME]
# This setting will allow you to configure the volume of the Ambient layer.
# Possible values: 0..100
# If -1, the engine will try to detect the volume by other meanings:
# - FF7 1998: It will attempt to use the FFNx registry paths ( also used by 7th Heaven )
# - FF7 Steam: It will be set to 100% by default
# - FF8 2000: It will be set to 100% by default
# - FF8 Steam: It will be set to 100% by default
external_ambient_volume = -1

###########################
# Video Player Options
###########################
Expand All @@ -278,6 +318,16 @@ external_ambient_ext = "ogg"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
enable_ffmpeg_videos = -1

#[FFMPEG VIDEO VOLUME]
# This setting will allow you to configure the volume of the FFMpeg Video layer.
# Possible values: 0..100
# If -1, the engine will try to detect the volume by other meanings:
# - FF7 1998: It will attempt to use the FFNx registry paths ( also used by 7th Heaven )
# - FF7 Steam: It will be set to 100% by default
# - FF8 2000: NOT SUPPORTED!
# - FF8 Steam: It will be set to 100% by default
ffmpeg_video_volume = -1

#[FFMPEG VIDEO FILE EXTENSION]
# The type of file that the ffmpeg layer will search for. Default is avi.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
17 changes: 17 additions & 0 deletions src/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ bool ff8_fix_uv_coords_precision;
std::string app_path;
std::string data_drive;
bool enable_ntscj_gamut_mode;
long external_music_volume;
long external_sfx_volume;
long external_voice_volume;
long external_ambient_volume;
long ffmpeg_video_volume;

std::vector<std::string> get_string_or_array_of_strings(const toml::node_view<toml::node> &node)
{
Expand Down Expand Up @@ -280,6 +285,11 @@ void read_cfg()
app_path = config["app_path"].value_or("");
data_drive = config["data_drive"].value_or("");
enable_ntscj_gamut_mode = config["enable_ntscj_gamut_mode"].value_or(false);
external_music_volume = config["external_music_volume"].value_or(-1);
external_sfx_volume = config["external_sfx_volume"].value_or(-1);
external_voice_volume = config["external_voice_volume"].value_or(-1);
external_ambient_volume = config["external_ambient_volume"].value_or(-1);
ffmpeg_video_volume = config["ffmpeg_video_volume"].value_or(-1);

// Windows x or y size can't be less then 0
if (window_size_x < 0) window_size_x = 0;
Expand Down Expand Up @@ -462,4 +472,11 @@ void read_cfg()

// WIDESCREEN
if (ff8 && aspect_ratio > AR_STRETCH) aspect_ratio = AR_ORIGINAL;

// VOLUME
if (external_music_volume > 100) external_music_volume = 100;
if (external_sfx_volume > 100) external_sfx_volume = 100;
if (external_voice_volume > 100) external_voice_volume = 100;
if (external_ambient_volume > 100) external_ambient_volume = 100;
if (ffmpeg_video_volume > 100) ffmpeg_video_volume = 100;
}
5 changes: 5 additions & 0 deletions src/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,10 @@ extern bool ff8_fix_uv_coords_precision;
extern std::string app_path;
extern std::string data_drive;
extern bool enable_ntscj_gamut_mode;
extern long external_music_volume;
extern long external_sfx_volume;
extern long external_voice_volume;
extern long external_ambient_volume;
extern long ffmpeg_video_volume;

void read_cfg();
55 changes: 27 additions & 28 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,6 @@ uint32_t estore_edition = false;
// global FF7 flag, check if is japanese edition ( detected as US )
uint32_t ff7_japanese_edition = false;

// global FF7 flag, they usually contain the values normally being written in registry
DWORD ff7_sfx_volume = 0x64;
DWORD ff7_music_volume = 0x64;
// SPECIAL: Added by FFNx, do not exist on vanilla
DWORD ff7_ambient_volume = 0x64;
DWORD ff7_movie_volume = 0x64;
DWORD ff7_voice_volume = 0x64;

// window dimensions requested by the game, normally 640x480
uint32_t game_width;
uint32_t game_height;
Expand Down Expand Up @@ -928,13 +920,10 @@ uint32_t common_init(struct game_obj *game_object)
common_externals.make_pixelformat(32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000, texture_format);
common_externals.add_texture_format(texture_format, game_object);

if (!ff8)
{
nxAudioEngine.setMusicMasterVolume(ff7_music_volume / 100.0f);
nxAudioEngine.setSFXMasterVolume(ff7_sfx_volume / 100.0f);
nxAudioEngine.setAmbientMasterVolume(ff7_ambient_volume / 100.0f);
nxAudioEngine.setVoiceMasterVolume(ff7_voice_volume / 100.0f);
}
nxAudioEngine.setMusicMasterVolume(external_music_volume / 100.0f);
nxAudioEngine.setSFXMasterVolume(external_sfx_volume / 100.0f);
nxAudioEngine.setAmbientMasterVolume(external_ambient_volume / 100.0f);
nxAudioEngine.setVoiceMasterVolume(external_voice_volume / 100.0f);

proxyWndProc = true;

Expand All @@ -961,8 +950,8 @@ void common_cleanup(struct game_obj *game_object)

if (ff7sound)
{
fwrite(&ff7_sfx_volume, sizeof(DWORD), 1, ff7sound);
fwrite(&ff7_music_volume, sizeof(DWORD), 1, ff7sound);
fwrite(&external_sfx_volume, sizeof(DWORD), 1, ff7sound);
fwrite(&external_music_volume, sizeof(DWORD), 1, ff7sound);
fclose(ff7sound);
}
}
Expand Down Expand Up @@ -2872,8 +2861,8 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)

if (ff7sound)
{
fread(&ff7_sfx_volume, sizeof(DWORD), 1, ff7sound);
fread(&ff7_music_volume, sizeof(DWORD), 1, ff7sound);
if (external_sfx_volume > -1) fread(&external_sfx_volume, sizeof(DWORD), 1, ff7sound);
if (external_sfx_volume > -1) fread(&external_music_volume, sizeof(DWORD), 1, ff7sound);
fclose(ff7sound);
}
}
Expand All @@ -2884,6 +2873,10 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
estore_edition = true;
}

if (external_voice_volume < 0) external_voice_volume = 100;
if (external_ambient_volume < 0) external_ambient_volume = 100;
if (ffmpeg_video_volume < 0) ffmpeg_video_volume = 100;

use_external_music = true;
if (external_music_path.empty()) external_music_path = "data/music_ogg";

Expand All @@ -2894,16 +2887,16 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
DWORD regsize = sizeof(DWORD);

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, R"(Software\Square Soft, Inc.\Final Fantasy VII\1.00\MIDI)", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &ff7_regkey) == ERROR_SUCCESS)
RegQueryValueEx(ff7_regkey, "MusicVolume", NULL, NULL, (LPBYTE)&ff7_music_volume, &regsize);
if (external_music_volume < 0) RegQueryValueEx(ff7_regkey, "MusicVolume", NULL, NULL, (LPBYTE)&external_music_volume, &regsize);

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, R"(Software\Square Soft, Inc.\Final Fantasy VII\1.00\Sound)", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &ff7_regkey) == ERROR_SUCCESS)
RegQueryValueEx(ff7_regkey, "SFXVolume", NULL, NULL, (LPBYTE)&ff7_sfx_volume, &regsize);
if (external_sfx_volume < 0) RegQueryValueEx(ff7_regkey, "SFXVolume", NULL, NULL, (LPBYTE)&external_sfx_volume, &regsize);

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, R"(Software\Square Soft, Inc.\Final Fantasy VII\1.00\FFNx)", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &ff7_regkey) == ERROR_SUCCESS)
{
RegQueryValueEx(ff7_regkey, "AmbientVolume", NULL, NULL, (LPBYTE)&ff7_ambient_volume, &regsize);
RegQueryValueEx(ff7_regkey, "MovieVolume", NULL, NULL, (LPBYTE)&ff7_movie_volume, &regsize);
RegQueryValueEx(ff7_regkey, "VoiceVolume", NULL, NULL, (LPBYTE)&ff7_voice_volume, &regsize);
if (external_ambient_volume < 0) RegQueryValueEx(ff7_regkey, "AmbientVolume", NULL, NULL, (LPBYTE)&external_ambient_volume, &regsize);
if (ffmpeg_video_volume < 0) RegQueryValueEx(ff7_regkey, "MovieVolume", NULL, NULL, (LPBYTE)&ffmpeg_video_volume, &regsize);
if (external_voice_volume < 0) RegQueryValueEx(ff7_regkey, "VoiceVolume", NULL, NULL, (LPBYTE)&external_voice_volume, &regsize);
}

if (external_music_path.empty()) external_music_path = "music/vgmstream";
Expand Down Expand Up @@ -3001,6 +2994,12 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)

patch_code_dword(mciSendCommandA, (DWORD)dotemuMciSendCommandA);
}

if (external_music_volume < 0) external_music_volume = 100;
if (external_sfx_volume < 0) external_sfx_volume = 100;
if (external_voice_volume < 0) external_voice_volume = 100;
if (external_ambient_volume < 0) external_ambient_volume = 100;
if (ffmpeg_video_volume < 0) ffmpeg_video_volume = 100;
}

// Apply hext patching
Expand Down Expand Up @@ -3052,14 +3051,14 @@ __declspec(dllexport) LSTATUS __stdcall dotemuRegSetValueExA(HKEY hKey, LPCSTR l
if (lpData[0] > 0x64)
lpData[0] = 0x64;

ff7_sfx_volume = lpData[0];
external_sfx_volume = lpData[0];
}
else if (strcmp(lpValueName, "MusicVolume") == 0)
{
if (lpData[0] > 0x64)
lpData[0] = 0x64;

ff7_music_volume = lpData[0];
external_music_volume = lpData[0];
}
}

Expand Down Expand Up @@ -3118,11 +3117,11 @@ __declspec(dllexport) LSTATUS __stdcall dotemuRegQueryValueExA(HKEY hKey, LPCSTR
}
else if (strcmp(lpValueName, "SFXVolume") == 0)
{
lpData[0] = ff7_sfx_volume;
lpData[0] = external_sfx_volume;
}
else if (strcmp(lpValueName, "MusicVolume") == 0)
{
lpData[0] = ff7_music_volume;
lpData[0] = external_music_volume;
}
// Graphics
else if (strcmp(lpValueName, "Driver") == 0)
Expand Down
5 changes: 0 additions & 5 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ extern uint32_t version;
extern uint32_t steam_edition;
extern uint32_t estore_edition;
extern uint32_t ff7_japanese_edition;
extern DWORD ff7_sfx_volume;
extern DWORD ff7_music_volume;
extern DWORD ff7_ambient_volume;
extern DWORD ff7_movie_volume;
extern DWORD ff7_voice_volume;
extern uint32_t ff7_do_reset;

#define BASEDIR_LENGTH 512
Expand Down
4 changes: 2 additions & 2 deletions src/movies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ uint32_t ff7_prepare_movie(char *name, uint32_t loop, struct dddevice **dddevice

bool has_ext_audio_file = nxAudioEngine.canPlayMovieAudio(movie_music_path);
ffmpeg_prepare_movie(newFmvName, !has_ext_audio_file);
if (!has_ext_audio_file) nxAudioEngine.setStreamMasterVolume(ff7_movie_volume / 100.0f);
if (!has_ext_audio_file) nxAudioEngine.setStreamMasterVolume(ffmpeg_video_volume / 100.0f);

ff7_externals.movie_object->global_movie_flag = 1;

Expand Down Expand Up @@ -138,7 +138,7 @@ uint32_t ff7_start_movie()
ff7_externals.movie_object->is_playing = 1;

nxAudioEngine.pauseAmbient();
nxAudioEngine.setMovieMasterVolume(ff7_movie_volume / 100.0f);
nxAudioEngine.setMovieMasterVolume(ffmpeg_video_volume / 100.0f);
nxAudioEngine.playMovieAudio(movie_music_path, MovieAudioLayers::MUSIC);
nxAudioEngine.playMovieAudio(movie_voice_path, MovieAudioLayers::VOICE, 3.0f);

Expand Down

0 comments on commit 6acd031

Please sign in to comment.