Skip to content

Commit

Permalink
[Refactor]: Clean the sound system and fix game launching when bgm is…
Browse files Browse the repository at this point in the history
… activated.
  • Loading branch information
Wolf3s committed Jan 1, 2025
1 parent 9d4e286 commit b64530a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 25 deletions.
5 changes: 2 additions & 3 deletions include/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ void sfxPlay(int id);

void bgmStart(void);
void bgmStop(void);
void bgmEnd();
int isBgmPlaying(void);
void bgmMute(void);
void bgmUnMute(void);

int bgmIsMuted(int muted);
#endif
3 changes: 3 additions & 0 deletions src/appsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "include/system.h"
#include "include/ioman.h"
#include "include/util.h"
#include "include/sound.h"

#include <elf-loader.h>

Expand Down Expand Up @@ -356,6 +357,8 @@ static void appLaunchItem(item_list_t *itemList, int id, config_set_t *configSet
int fd;
char filename[256];
const char *argv1;

bgmEnd();

// Retrieve configuration set by appGetConfig()
configGetStrCopy(configSet, CONFIG_ITEM_STARTUP, filename, sizeof(filename));
Expand Down
2 changes: 2 additions & 0 deletions src/bdmsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ void bdmLaunchGame(item_list_t *itemList, int id, config_set_t *configSet)

bdm_device_data_t *pDeviceData = NULL;

bgmEnd();

if (gAutoLaunchBDMGame == NULL) {
pDeviceData = (bdm_device_data_t *)itemList->priv;
game = &pDeviceData->bdmGames[id];
Expand Down
4 changes: 2 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ int configWrite(config_set_t *configSet)
if (fileBuffer) {
char line[512];

bgmMute();
bgmIsMuted(1);
struct config_value_t *cur = configSet->head;
while (cur) {
if ((cur->key[0] != '\0') && (cur->key[0] != '#')) {
Expand All @@ -558,7 +558,7 @@ int configWrite(config_set_t *configSet)

closeFileBuffer(fileBuffer);
configSet->modified = 0;
bgmUnMute();
bgmIsMuted(0);
return 1;
}
return 0;
Expand Down
3 changes: 3 additions & 0 deletions src/ethsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "include/cheatman.h"
#include "modules/iopcore/common/cdvd_config.h"
#include "include/ethsupport.h"
#include "include/sound.h"

#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioDevctl(ethBase, SMB_***)
Expand Down Expand Up @@ -584,6 +585,8 @@ static void ethLaunchGame(item_list_t *itemList, int id, config_set_t *configSet
u32 layer1_start, layer1_offset;
unsigned short int layer1_part;

bgmEnd();

if (!gPCShareName[0]) {
memcpy(gPCShareName, game->name, sizeof(gPCShareName));
ethULSizePrev = -2;
Expand Down
3 changes: 3 additions & 0 deletions src/hddsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "include/imports.h"
#include "include/cheatman.h"
#include "modules/iopcore/common/cdvd_config.h"
#include "include/sound.h"

#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioFormat, fileXioMount, fileXioUmount, fileXioDevctl
Expand Down Expand Up @@ -426,6 +427,8 @@ void hddLaunchGame(item_list_t *itemList, int id, config_set_t *configSet)
hdl_game_info_t *game;
struct cdvdman_settings_hdd *settings;

bgmEnd();

if (gAutoLaunchGame == NULL)
game = &hddGames.games[id];
else
Expand Down
6 changes: 3 additions & 3 deletions src/lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ int lngSetGuiValue(int langID)
{
if (langID != -1) {
if (guiLangID != langID) {
bgmMute();
bgmIsMuted(1);
if (langID != 0) {
language_t *currLang = &languages[langID - 1];
if (lngLoadFromFile(currLang->filePath, currLang->name)) {
guiLangID = langID;
thmSetGuiValue(thmGetGuiValue(), 1);
bgmUnMute();
bgmIsMuted(0);
return 1;
}
}
Expand All @@ -208,7 +208,7 @@ int lngSetGuiValue(int langID)
// lang switched back to internalEnglish, reload default font
fntLoadDefault(NULL);
thmSetGuiValue(thmGetGuiValue(), 1);
bgmUnMute();
bgmIsMuted(0);
}
}
return 0;
Expand Down
8 changes: 5 additions & 3 deletions src/opl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ void applyConfig(int themeID, int langID, int skipDeviceRefresh)

int changed = rmSetMode(0);
if (changed) {
bgmMute();
bgmIsMuted(1);
// reinit the graphics...
thmReloadScreenExtents();
guiReloadScreenExtents();
Expand Down Expand Up @@ -1186,7 +1186,7 @@ void applyConfig(int themeID, int langID, int skipDeviceRefresh)
}
}

bgmUnMute();
bgmIsMuted(0);

#ifdef __DEBUG
debugApplyConfig();
Expand Down Expand Up @@ -1493,8 +1493,9 @@ static int loadLwnbdSvr(void)
};
struct lwnbd_config config;

// deint audio lib while nbd server is running
// deinit audio lib and background music while nbd server is running
audioEnd();
bgmEnd();

// block all io ops, wait for the ones still running to finish
ioBlockOps(1);
Expand Down Expand Up @@ -1627,6 +1628,7 @@ void deinit(int exception, int modeSelected)
deinitAllSupport(exception, modeSelected);

audioEnd();
bgmEnd();
ioEnd();
guiEnd();
menuEnd();
Expand Down
27 changes: 13 additions & 14 deletions src/sound.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022, Thanks to SP193
Copyright 2022-2025, Thanks to SP193 and KrahJohilto
Licenced under Academic Free License version 3.0
Review OpenPS2Loader README & LICENSE files for further details.
*/
Expand Down Expand Up @@ -223,7 +223,8 @@ extern void *_gp;

static int bgmThreadID, bgmIoThreadID;
static int outSema, inSema;
static unsigned char terminateFlag, bgmIsPlaying;
static unsigned char terminateFlag;
static int bgmIsPlaying;
static unsigned char rdPtr, wrPtr;
static char bgmBuffer[BGM_RING_BUFFER_COUNT][BGM_RING_BUFFER_SIZE];
static volatile unsigned char bgmThreadRunning, bgmIoThreadRunning;
Expand Down Expand Up @@ -477,22 +478,23 @@ void bgmStop(void)

int isBgmPlaying(void)
{
int ret = (int)bgmIsPlaying;

return ret;
return bgmIsPlaying ? 1 : 0;
}

// HACK: BGM stutters while perfroming certain tasks, mute during these operations and unmute once completed.
void bgmMute(void)
int bgmIsMuted(int muted)
{
if (audio_initialized)
audsrv_set_volume(0);
return audio_initialized ? audsrv_set_volume(muted ? 0 : gBGMVolume) : -1;
}

void bgmUnMute(void)
void bgmEnd(void)
{
if (audio_initialized)
audsrv_set_volume(gBGMVolume);
if (audio_initialized) {
if (gEnableBGM && isBgmPlaying()) {
gEnableBGM = 0;
bgmStop();
}
}
}

/*-- General Audio ------------------------------------------------------------------------------------------------------
Expand All @@ -517,9 +519,6 @@ void audioEnd(void)
return;
}

if (gEnableBGM && isBgmPlaying())
bgmStop();

audsrv_quit();
audio_initialized = 0;
}
Expand Down

0 comments on commit b64530a

Please sign in to comment.