project64/Source/Project64-core/Plugins/AudioPlugin.cpp

213 lines
5.5 KiB
C++
Raw Normal View History

2016-01-27 09:11:59 +00:00
#include "stdafx.h"
2022-10-10 00:22:17 +00:00
2016-01-27 09:11:59 +00:00
#include <Project64-core/N64System/Mips/MemoryVirtualMem.h>
2021-04-14 05:34:15 +00:00
#include <Project64-core/N64System/Mips/Register.h>
2022-10-10 00:22:17 +00:00
#include <Project64-core/N64System/N64Disk.h>
#include <Project64-core/N64System/N64Rom.h>
2021-04-14 05:34:15 +00:00
#include <Project64-core/N64System/N64System.h>
2022-10-10 00:22:17 +00:00
#include <Project64-core/N64System/SystemGlobals.h>
2016-01-27 09:11:59 +00:00
#include <Project64-core/Plugins/AudioPlugin.h>
#include <Project64-plugin-spec/Audio.h>
2016-01-27 09:11:59 +00:00
#ifdef _WIN32
#include <Windows.h>
#endif
CAudioPlugin::CAudioPlugin() :
2021-04-12 11:35:39 +00:00
AiLenChanged(nullptr),
AiReadLength(nullptr),
ProcessAList(nullptr),
m_hAudioThread(nullptr),
AiUpdate(nullptr),
AiDacrateChanged(nullptr)
2016-01-27 09:11:59 +00:00
{
}
CAudioPlugin::~CAudioPlugin()
{
2021-04-12 11:35:39 +00:00
Close(nullptr);
2016-01-27 09:11:59 +00:00
UnloadPlugin();
}
bool CAudioPlugin::LoadFunctions(void)
{
g_Settings->SaveBool(Setting_SyncViaAudioEnabled, false);
2018-12-20 10:19:14 +00:00
2016-01-27 09:11:59 +00:00
// Find entries for functions in DLL
2022-10-10 00:22:17 +00:00
void(CALL * InitiateAudio)(void);
2016-01-27 09:11:59 +00:00
LoadFunction(InitiateAudio);
LoadFunction(AiDacrateChanged);
LoadFunction(AiLenChanged);
LoadFunction(AiReadLength);
LoadFunction(AiUpdate);
LoadFunction(ProcessAList);
// Make sure DLL has all needed functions
2022-10-10 00:22:17 +00:00
if (AiDacrateChanged == nullptr)
{
UnloadPlugin();
return false;
}
if (AiLenChanged == nullptr)
{
UnloadPlugin();
return false;
}
if (AiReadLength == nullptr)
{
UnloadPlugin();
return false;
}
if (InitiateAudio == nullptr)
{
UnloadPlugin();
return false;
}
if (ProcessAList == nullptr)
{
UnloadPlugin();
return false;
}
2016-01-27 09:11:59 +00:00
if (m_PluginInfo.Version >= 0x0102)
{
2022-10-10 00:22:17 +00:00
if (PluginOpened == nullptr)
{
UnloadPlugin();
return false;
}
2016-01-27 09:11:59 +00:00
}
return true;
}
bool CAudioPlugin::Initiate(CN64System * System, RenderWindow * Window)
{
// Get function from DLL
2022-10-10 00:22:17 +00:00
int32_t(CALL * InitiateAudio)(AUDIO_INFO Audio_Info);
2016-01-27 09:11:59 +00:00
LoadFunction(InitiateAudio);
2022-10-10 00:22:17 +00:00
if (InitiateAudio == nullptr)
{
return false;
}
2016-01-27 09:11:59 +00:00
2022-10-10 00:22:17 +00:00
AUDIO_INFO Info = {0};
2016-01-27 09:11:59 +00:00
#ifdef _WIN32
Info.hWnd = Window ? Window->GetWindowHandle() : nullptr;
2021-04-12 11:35:39 +00:00
Info.hinst = Window ? Window->GetModuleInstance() : nullptr;
#else
Info.hWnd = nullptr;
2021-04-12 11:35:39 +00:00
Info.hinst = nullptr;
#endif
Info.Reserved = true;
2016-01-27 09:11:59 +00:00
Info.CheckInterrupts = DummyCheckInterrupts;
// We are initializing the plugin before any ROM is loaded so we do not have any correct
// parameters here, just needed to we can config the DLL
2021-04-12 11:35:39 +00:00
if (System == nullptr)
2016-01-27 09:11:59 +00:00
{
static uint8_t Buffer[100];
static uint32_t Value = 0;
2016-01-27 09:11:59 +00:00
Info.HEADER = Buffer;
Info.RDRAM = Buffer;
Info.DMEM = Buffer;
Info.IMEM = Buffer;
Info.MI_INTR_REG = &Value;
Info.AI_DRAM_ADDR_REG = &Value;
Info.AI_LEN_REG = &Value;
Info.AI_CONTROL_REG = &Value;
Info.AI_STATUS_REG = &Value;
Info.AI_DACRATE_REG = &Value;
Info.AI_BITRATE_REG = &Value;
2016-01-27 09:11:59 +00:00
}
// Send initialization information to the DLL
else
{
CMipsMemoryVM & MMU = System->m_MMU_VM;
CRegisters & Reg = System->m_Reg;
2021-04-12 11:35:39 +00:00
if (g_Rom->IsLoadedRomDDIPL() && g_Disk != nullptr)
Info.HEADER = g_Disk->GetDiskHeader();
else
Info.HEADER = g_Rom->GetRomAddress();
Info.RDRAM = MMU.Rdram();
Info.DMEM = MMU.Dmem();
Info.IMEM = MMU.Imem();
Info.MI_INTR_REG = &Reg.m_AudioIntrReg;
Info.AI_DRAM_ADDR_REG = &Reg.AI_DRAM_ADDR_REG;
Info.AI_LEN_REG = &Reg.AI_LEN_REG;
Info.AI_CONTROL_REG = &Reg.AI_CONTROL_REG;
Info.AI_STATUS_REG = &Reg.AI_STATUS_REG;
Info.AI_DACRATE_REG = &Reg.AI_DACRATE_REG;
Info.AI_BITRATE_REG = &Reg.AI_BITRATE_REG;
2016-01-27 09:11:59 +00:00
}
m_Initialized = InitiateAudio(Info) != 0;
#ifdef _WIN32
2021-04-12 11:35:39 +00:00
if (System != nullptr)
2016-01-27 09:11:59 +00:00
{
if (AiUpdate)
{
if (m_hAudioThread)
{
WriteTrace(TraceAudioPlugin, TraceDebug, "Terminate Audio Thread");
TerminateThread(m_hAudioThread, 0);
}
DWORD ThreadID;
2021-04-12 11:35:39 +00:00
m_hAudioThread = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)AudioThread, (LPVOID)this, 0, &ThreadID);
2016-01-27 09:11:59 +00:00
}
if (System->m_Reg.AI_DACRATE_REG != 0)
2016-01-27 09:11:59 +00:00
{
DacrateChanged(System->SystemType());
}
}
#endif
return m_Initialized;
}
void CAudioPlugin::UnloadPluginDetails(void)
{
#ifdef _WIN32
if (m_hAudioThread)
{
WriteTrace(TraceAudioPlugin, TraceDebug, "Terminate audio thread");
2016-01-27 09:11:59 +00:00
TerminateThread(m_hAudioThread, 0);
2021-04-12 11:35:39 +00:00
m_hAudioThread = nullptr;
2016-01-27 09:11:59 +00:00
}
#endif
2021-04-12 11:35:39 +00:00
AiDacrateChanged = nullptr;
AiLenChanged = nullptr;
AiReadLength = nullptr;
AiUpdate = nullptr;
ProcessAList = nullptr;
2016-01-27 09:11:59 +00:00
}
void CAudioPlugin::DacrateChanged(SYSTEM_TYPE Type)
{
2022-10-10 00:22:17 +00:00
if (!Initialized())
{
return;
}
2016-01-27 09:11:59 +00:00
WriteTrace(TraceAudioPlugin, TraceDebug, "SystemType: %s", Type == SYSTEM_NTSC ? "SYSTEM_NTSC" : "SYSTEM_PAL");
//uint32_t Frequency = g_Reg->AI_DACRATE_REG * 30;
//uint32_t CountsPerSecond = (g_Reg->VI_V_SYNC_REG != 0 ? (g_Reg->VI_V_SYNC_REG + 1) * g_Settings->LoadDword(Game_ViRefreshRate) : 500000) * 60;
AiDacrateChanged(Type);
}
#ifdef _WIN32
void CAudioPlugin::AudioThread(CAudioPlugin * _this)
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
if (g_Settings->LoadBool(Setting_CN64TimeCritical))
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
}
for (;;)
{
_this->AiUpdate(true);
}
}
#endif