Skip to content

Commit

Permalink
move audio playback code to a more reasonable place
Browse files Browse the repository at this point in the history
  • Loading branch information
goeiecool9999 committed Dec 17, 2023
1 parent cab8a1f commit 5033318
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 64 deletions.
6 changes: 1 addition & 5 deletions src/Cafe/HW/Latte/Core/Latte.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,4 @@ void LatteRenderTarget_updateViewport();
void Latte_Start();
void Latte_Stop();
bool Latte_GetStopSignal(); // returns true if stop was requested or if in stopped state
void LatteThread_Exit();

void LatteThread_InitBootSound();
void LatteThread_StreamBootSound();
void LatteThread_ShutdownBootSound();
void LatteThread_Exit();
6 changes: 5 additions & 1 deletion src/Cafe/HW/Latte/Core/LatteShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ void LatteShaderCache_writeSeparableGeometryShader(uint64 shaderBaseHash, uint64
void LatteShaderCache_writeSeparablePixelShader(uint64 shaderBaseHash, uint64 shaderAuxHash, uint8* pixelShader, uint32 pixelShaderSize, uint32* contextRegisters, bool usesGeometryShader);

// todo - refactor this
sint32 LatteDecompiler_getTextureSamplerBaseIndex(LatteConst::ShaderType shaderType);
sint32 LatteDecompiler_getTextureSamplerBaseIndex(LatteConst::ShaderType shaderType);

void LatteShaderCache_InitBootSound();
void LatteShaderCache_StreamBootSound();
void LatteShaderCache_ShutdownBootSound();
54 changes: 53 additions & 1 deletion src/Cafe/HW/Latte/Core/LatteShaderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ void LatteShaderCache_Load()
loadBackgroundTexture(true, g_shaderCacheLoaderState.textureTVId);
loadBackgroundTexture(false, g_shaderCacheLoaderState.textureDRCId);

// initialise resources for playing bootup sound
if(GetConfig().play_boot_sound)
LatteShaderCache_InitBootSound();

sint32 numLoadedShaders = 0;
uint32 loadIndex = 0;

Expand Down Expand Up @@ -367,6 +371,9 @@ void LatteShaderCache_Load()
g_renderer->DeleteTexture(g_shaderCacheLoaderState.textureTVId);
if (g_shaderCacheLoaderState.textureDRCId)
g_renderer->DeleteTexture(g_shaderCacheLoaderState.textureDRCId);

// free resources for playing boot sound
LatteShaderCache_ShutdownBootSound();
}

void LatteShaderCache_ShowProgress(const std::function <bool(void)>& loadUpdateFunc, bool isPipelines)
Expand Down Expand Up @@ -497,7 +504,7 @@ void LatteShaderCache_ShowProgress(const std::function <bool(void)>& loadUpdateF

// finish frame
g_renderer->SwapBuffers(true, true);
LatteThread_StreamBootSound();
LatteShaderCache_StreamBootSound();
}
}

Expand Down Expand Up @@ -809,3 +816,48 @@ void LatteShaderCache_handleDeprecatedCacheFiles(fs::path pathGeneric, fs::path
}
}
}

AudioAPIPtr g_BootSndAudioDev = nullptr;
std::unique_ptr<BootSoundReader> g_BootSndFileReader;
FSCVirtualFile* g_bootSndFileHandle = 0;

void LatteShaderCache_InitBootSound()
{
const sint32 samplesPerBlock = 4800;
const sint32 audioBlockSize = samplesPerBlock * 2 * 2;
try
{
g_BootSndAudioDev = IAudioAPI::CreateDeviceFromConfig(true, 48000, 2, samplesPerBlock, 16);
}
catch (const std::runtime_error& ex)
{
cemuLog_log(LogType::Force, "Failed to initialise audio device for bootup sound");
return;
}
g_BootSndAudioDev->Play();

std::string sndPath = fmt::format("{}/meta/{}", CafeSystem::GetMlcStoragePath(CafeSystem::GetForegroundTitleId()), "bootSound.btsnd");
sint32 fscStatus = FSC_STATUS_UNDEFINED;
g_bootSndFileHandle = fsc_open(sndPath.c_str(), FSC_ACCESS_FLAG::OPEN_FILE | FSC_ACCESS_FLAG::READ_PERMISSION, &fscStatus);
if(!g_bootSndFileHandle)
return;

g_BootSndFileReader = std::make_unique<BootSoundReader>(g_bootSndFileHandle, audioBlockSize);
}

void LatteShaderCache_StreamBootSound()
{
if(g_BootSndAudioDev && g_bootSndFileHandle && g_BootSndFileReader)
{
if (g_BootSndAudioDev->NeedAdditionalBlocks())
g_BootSndAudioDev->FeedBlock(g_BootSndFileReader->getSamples());
}
}

void LatteShaderCache_ShutdownBootSound()
{
g_BootSndFileReader.reset();
if(g_bootSndFileHandle)
fsc_close(g_bootSndFileHandle);
g_BootSndAudioDev.reset();
}
57 changes: 0 additions & 57 deletions src/Cafe/HW/Latte/Core/LatteThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
#include "util/helpers/helpers.h"

#include <imgui.h>
#include <audio/IAudioAPI.h>
#include <Filesystem/fsc.h>
#include <util/bootSound/BootSoundReader.h>
#include "config/ActiveSettings.h"

#include "Cafe/CafeSystem.h"
Expand Down Expand Up @@ -184,17 +181,8 @@ int Latte_ThreadEntry()

// before doing anything with game specific shaders, we need to wait for graphic packs to finish loading
GraphicPack2::WaitUntilReady();

// initialise resources for playing bootup sound
if(GetConfig().play_boot_sound)
LatteThread_InitBootSound();

// load disk shader cache
LatteShaderCache_Load();

// free resources for playing boot sound
LatteThread_ShutdownBootSound();

// init registers
Latte_LoadInitialRegisters();
// let CPU thread know the GPU is done initializing
Expand Down Expand Up @@ -274,48 +262,3 @@ void LatteThread_Exit()
#endif
cemu_assert_unimplemented();
}

AudioAPIPtr g_BootSndAudioDev = nullptr;
std::unique_ptr<BootSoundReader> g_BootSndFileReader;
FSCVirtualFile* g_bootSndFileHandle = 0;

void LatteThread_InitBootSound()
{
const sint32 samplesPerBlock = 4800;
const sint32 audioBlockSize = samplesPerBlock * 2 * 2;
try
{
g_BootSndAudioDev = IAudioAPI::CreateDeviceFromConfig(true, 48000, 2, samplesPerBlock, 16);
}
catch (const std::runtime_error& ex)
{
cemuLog_log(LogType::Force, "Failed to initialise audio device for bootup sound");
return;
}
g_BootSndAudioDev->Play();

std::string sndPath = fmt::format("{}/meta/{}", CafeSystem::GetMlcStoragePath(CafeSystem::GetForegroundTitleId()), "bootSound.btsnd");
sint32 fscStatus = FSC_STATUS_UNDEFINED;
g_bootSndFileHandle = fsc_open(sndPath.c_str(), FSC_ACCESS_FLAG::OPEN_FILE | FSC_ACCESS_FLAG::READ_PERMISSION, &fscStatus);
if(!g_bootSndFileHandle)
return;

g_BootSndFileReader = std::make_unique<BootSoundReader>(g_bootSndFileHandle, audioBlockSize);
}

void LatteThread_StreamBootSound()
{
if(g_BootSndAudioDev && g_bootSndFileHandle && g_BootSndFileReader)
{
if (g_BootSndAudioDev->NeedAdditionalBlocks())
g_BootSndAudioDev->FeedBlock(g_BootSndFileReader->getSamples());
}
}

void LatteThread_ShutdownBootSound()
{
g_BootSndFileReader.reset();
if(g_bootSndFileHandle)
fsc_close(g_bootSndFileHandle);
g_BootSndAudioDev.reset();
}

0 comments on commit 5033318

Please sign in to comment.