2021-03-02 02:13:17 +00:00
|
|
|
// Project64 - A Nintendo 64 emulator
|
2021-05-18 11:51:36 +00:00
|
|
|
// https://www.pj64-emu.com/
|
2021-03-02 02:13:17 +00:00
|
|
|
// Copyright(C) 2001-2021 Project64.
|
|
|
|
// Copyright(C) 2015 Gilles Siberlin
|
|
|
|
// Copyright(C) 2007-2009 Richard Goedeken
|
|
|
|
// Copyright(C) 2007-2008 Ebenblues
|
|
|
|
// Copyright(C) 2003 JttL
|
|
|
|
// Copyright(C) 2002 Hacktarux
|
|
|
|
// GNU/GPLv2 licensed: https://gnu.org/licenses/gpl-2.0.html
|
2021-03-27 08:48:59 +00:00
|
|
|
|
2017-09-20 07:24:54 +00:00
|
|
|
#include <Common/Util.h>
|
2017-09-21 13:20:36 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Project64-audio/Driver/DirectSound.h>
|
|
|
|
#else
|
2017-09-12 07:58:35 +00:00
|
|
|
#include <Project64-audio/Driver/OpenSLES.h>
|
2017-09-21 13:20:36 +00:00
|
|
|
#endif
|
2023-09-21 04:43:08 +00:00
|
|
|
#include <Project64-plugin-spec/Audio.h>
|
2016-06-05 01:33:35 +00:00
|
|
|
#include "Version.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "AudioSettings.h"
|
|
|
|
#include "trace.h"
|
2017-09-11 12:20:34 +00:00
|
|
|
#include "AudioMain.h"
|
2017-09-25 12:00:04 +00:00
|
|
|
#include "ConfigUI.h"
|
2017-09-11 22:37:32 +00:00
|
|
|
#include "SettingsID.h"
|
2016-06-05 01:33:35 +00:00
|
|
|
|
2018-12-02 04:36:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
void SetTimerResolution ( void );
|
|
|
|
#endif
|
|
|
|
|
2019-01-21 05:45:52 +00:00
|
|
|
#define ENDIAN_SWAP_BYTE (~0 & 0x7 & 3)
|
|
|
|
#define BES(address) ((address) ^ ENDIAN_SWAP_BYTE)
|
|
|
|
|
2021-03-27 08:48:59 +00:00
|
|
|
// Read header for type definition
|
2016-06-05 01:33:35 +00:00
|
|
|
AUDIO_INFO g_AudioInfo;
|
|
|
|
|
|
|
|
bool g_PluginInit = false;
|
2017-09-26 13:57:33 +00:00
|
|
|
bool g_romopen = false;
|
2019-01-21 05:45:52 +00:00
|
|
|
uint32_t g_Dacrate = 0, hack = 0;
|
2016-06-05 01:33:35 +00:00
|
|
|
|
2017-09-21 13:20:36 +00:00
|
|
|
#ifdef _WIN32
|
2021-04-12 11:35:39 +00:00
|
|
|
DirectSoundDriver * g_SoundDriver = nullptr;
|
2017-09-21 13:20:36 +00:00
|
|
|
#else
|
2021-04-12 11:35:39 +00:00
|
|
|
OpenSLESDriver * g_SoundDriver = nullptr;
|
2017-09-21 13:20:36 +00:00
|
|
|
#endif
|
2017-09-12 07:58:35 +00:00
|
|
|
|
2022-06-27 10:02:38 +00:00
|
|
|
enum SYSTEM_TYPE
|
|
|
|
{
|
|
|
|
SYSTEM_NTSC = 0,
|
|
|
|
SYSTEM_PAL = 1,
|
|
|
|
SYSTEM_MPAL = 2,
|
|
|
|
};
|
|
|
|
|
2016-08-11 10:46:03 +00:00
|
|
|
void PluginInit(void)
|
2016-06-05 01:33:35 +00:00
|
|
|
{
|
|
|
|
if (g_PluginInit)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetupTrace();
|
|
|
|
SetupAudioSettings();
|
2017-09-11 22:16:45 +00:00
|
|
|
StartTrace();
|
2019-11-29 00:17:19 +00:00
|
|
|
//SetTimerResolution();
|
2016-06-05 01:33:35 +00:00
|
|
|
g_PluginInit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL PluginLoaded(void)
|
|
|
|
{
|
|
|
|
PluginInit();
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Called");
|
2021-04-12 11:35:39 +00:00
|
|
|
if (g_settings != nullptr)
|
2019-01-21 05:45:52 +00:00
|
|
|
{
|
|
|
|
g_settings->SetSyncViaAudioEnabled(true);
|
|
|
|
}
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL AiDacrateChanged(int SystemType)
|
|
|
|
{
|
2016-08-11 10:46:03 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start (SystemType: %d)", SystemType);
|
2016-06-05 01:33:35 +00:00
|
|
|
if (!g_PluginInit)
|
|
|
|
{
|
2021-03-27 08:48:59 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceNotice, "Plugin has not been initialized");
|
2016-06-05 01:33:35 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-13 07:41:52 +00:00
|
|
|
if (g_SoundDriver && g_Dacrate != *g_AudioInfo.AI_DACRATE_REG)
|
2016-06-05 01:33:35 +00:00
|
|
|
{
|
2017-09-13 07:41:52 +00:00
|
|
|
g_Dacrate = *g_AudioInfo.AI_DACRATE_REG & 0x00003FFF;
|
|
|
|
if (g_Dacrate != *g_AudioInfo.AI_DACRATE_REG)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceNotice, "Unknown/reserved bits in AI_DACRATE_REG set. 0x%08X", *g_AudioInfo.AI_DACRATE_REG);
|
|
|
|
}
|
2016-08-11 10:46:03 +00:00
|
|
|
|
2019-01-21 05:45:52 +00:00
|
|
|
uint32_t video_clock = 0, BufferSize = 0, Frequency = 0, divider = 0;
|
2018-12-04 03:54:44 +00:00
|
|
|
|
2017-09-13 07:41:52 +00:00
|
|
|
switch (SystemType)
|
|
|
|
{
|
|
|
|
case SYSTEM_NTSC: video_clock = 48681812; break;
|
2019-01-21 05:45:52 +00:00
|
|
|
case SYSTEM_PAL: video_clock = 49656530; break;
|
2017-09-13 07:41:52 +00:00
|
|
|
case SYSTEM_MPAL: video_clock = 48628316; break;
|
|
|
|
}
|
2017-09-28 19:32:43 +00:00
|
|
|
|
2019-01-21 05:45:52 +00:00
|
|
|
Frequency = (video_clock / (g_Dacrate + 1));
|
|
|
|
|
|
|
|
if (Frequency < 8000)
|
2017-10-15 16:43:32 +00:00
|
|
|
{
|
2021-03-27 08:48:59 +00:00
|
|
|
WriteTrace(TraceAudioDriver, TraceDebug, "Not audio data!");
|
2017-10-15 16:43:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-21 05:45:52 +00:00
|
|
|
|
|
|
|
switch (g_settings->GetBuffer())
|
|
|
|
{
|
|
|
|
case 1: divider = 50; break;
|
|
|
|
case 2: divider = 45; break;
|
|
|
|
case 3: divider = 40; break;
|
|
|
|
case 4: divider = 30; break;
|
|
|
|
case 5: divider = 25; break;
|
|
|
|
case 6: divider = 20; break;
|
|
|
|
case 7: divider = 15; break;
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:41:50 +00:00
|
|
|
BufferSize = (Frequency / divider) + 3 & ~0x3;
|
2019-01-21 05:45:52 +00:00
|
|
|
|
2022-09-26 03:23:14 +00:00
|
|
|
if (hack == 0x4248 /*BH*/ && SystemType != SYSTEM_PAL) BufferSize -= 16;
|
2019-01-21 05:45:52 +00:00
|
|
|
|
|
|
|
g_SoundDriver->AI_SetFrequency(Frequency, BufferSize);
|
2017-09-13 07:41:52 +00:00
|
|
|
}
|
2016-06-05 01:33:35 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL AiLenChanged(void)
|
|
|
|
{
|
2017-09-13 07:41:52 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start (DRAM_ADDR = 0x%X Len = 0x%X)", *g_AudioInfo.AI_DRAM_ADDR_REG, *g_AudioInfo.AI_LEN_REG);
|
2017-09-25 12:00:04 +00:00
|
|
|
if (g_SoundDriver && g_settings->AudioEnabled())
|
2016-08-11 10:46:03 +00:00
|
|
|
{
|
2017-09-13 07:41:52 +00:00
|
|
|
uint32_t Len = *g_AudioInfo.AI_LEN_REG & 0x3FFF8;
|
|
|
|
uint8_t * Buffer = (g_AudioInfo.RDRAM + (*g_AudioInfo.AI_DRAM_ADDR_REG & 0x00FFFFF8));
|
2016-06-05 01:33:35 +00:00
|
|
|
|
2017-09-13 07:41:52 +00:00
|
|
|
g_SoundDriver->AI_LenChanged(Buffer, Len);
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT uint32_t CALL AiReadLength(void)
|
|
|
|
{
|
2017-09-22 21:45:18 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start");
|
|
|
|
uint32_t len = 0;
|
2021-04-12 11:35:39 +00:00
|
|
|
if (g_SoundDriver != nullptr)
|
2017-09-22 21:45:18 +00:00
|
|
|
{
|
|
|
|
*g_AudioInfo.AI_LEN_REG = g_SoundDriver->AI_ReadLength();
|
|
|
|
len = *g_AudioInfo.AI_LEN_REG;
|
|
|
|
}
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done (len: 0x%X)", len);
|
|
|
|
return len;
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 07:24:54 +00:00
|
|
|
EXPORT void CALL AiUpdate(int32_t Wait)
|
2016-06-05 01:33:35 +00:00
|
|
|
{
|
2017-09-20 07:24:54 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start (Wait: %s)", Wait ? "true" : "false");
|
|
|
|
if (g_SoundDriver)
|
|
|
|
{
|
|
|
|
g_SoundDriver->AI_Update(Wait != 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-27 08:48:59 +00:00
|
|
|
pjutil::Sleep(1); // TODO: Fix this: Ai update appears to be problematic
|
2017-09-20 07:24:54 +00:00
|
|
|
}
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL CloseDLL(void)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Called");
|
2021-04-12 11:35:39 +00:00
|
|
|
if (g_SoundDriver != nullptr)
|
2019-01-21 05:45:52 +00:00
|
|
|
{
|
|
|
|
g_SoundDriver->AI_Shutdown();
|
|
|
|
delete g_SoundDriver;
|
2021-04-12 11:35:39 +00:00
|
|
|
g_SoundDriver = nullptr;
|
2019-01-21 05:45:52 +00:00
|
|
|
}
|
2017-09-14 08:07:12 +00:00
|
|
|
CleanupAudioSettings();
|
2018-11-24 02:41:50 +00:00
|
|
|
StopTrace();
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL DllAbout(void * /*hParent*/)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Called");
|
|
|
|
}
|
|
|
|
|
2017-09-25 12:00:04 +00:00
|
|
|
EXPORT void CALL DllConfig(void * hParent)
|
2016-06-05 01:33:35 +00:00
|
|
|
{
|
2017-09-25 12:00:04 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
ConfigAudio(hParent);
|
|
|
|
if (g_SoundDriver)
|
|
|
|
{
|
|
|
|
g_SoundDriver->SetVolume(g_settings->GetVolume());
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL DllTest(void * /*hParent*/)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Called");
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL GetDllInfo(PLUGIN_INFO * PluginInfo)
|
|
|
|
{
|
|
|
|
PluginInfo->Version = 0x0101;
|
|
|
|
PluginInfo->Type = PLUGIN_TYPE_AUDIO;
|
|
|
|
#ifdef _DEBUG
|
2021-03-27 08:48:59 +00:00
|
|
|
sprintf(PluginInfo->Name, "Project64 audio plugin (Debug): %s", VER_FILE_VERSION_STR);
|
2016-06-05 01:33:35 +00:00
|
|
|
#else
|
2021-03-27 08:48:59 +00:00
|
|
|
sprintf(PluginInfo->Name, "Project64 audio plugin: %s", VER_FILE_VERSION_STR);
|
2016-06-05 01:33:35 +00:00
|
|
|
#endif
|
2022-06-27 10:02:38 +00:00
|
|
|
PluginInfo->Reserved1 = false;
|
|
|
|
PluginInfo->Reserved2 = true;
|
2016-06-05 01:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT int32_t CALL InitiateAudio(AUDIO_INFO Audio_Info)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start");
|
2021-04-12 11:35:39 +00:00
|
|
|
if (g_SoundDriver != nullptr)
|
2017-09-12 07:58:35 +00:00
|
|
|
{
|
2017-09-20 07:24:54 +00:00
|
|
|
g_SoundDriver->AI_Shutdown();
|
2017-09-12 07:58:35 +00:00
|
|
|
delete g_SoundDriver;
|
|
|
|
}
|
2019-11-29 00:17:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
SetTimerResolution();
|
|
|
|
#endif
|
2016-06-05 01:33:35 +00:00
|
|
|
g_AudioInfo = Audio_Info;
|
2017-09-21 13:20:36 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
g_SoundDriver = new DirectSoundDriver;
|
|
|
|
#else
|
2017-09-12 07:58:35 +00:00
|
|
|
g_SoundDriver = new OpenSLESDriver;
|
|
|
|
#endif
|
2019-01-21 05:45:52 +00:00
|
|
|
const uint16_t cart_ID = 0x0000
|
|
|
|
| (g_AudioInfo.HEADER[BES(0x3C)] << 8)
|
|
|
|
| (g_AudioInfo.HEADER[BES(0x3D)] << 0)
|
|
|
|
;
|
|
|
|
hack = cart_ID;
|
2016-06-05 01:33:35 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done (res: true)");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL RomOpen()
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start");
|
2017-09-26 13:57:33 +00:00
|
|
|
g_romopen = true;
|
|
|
|
g_settings->ReadSettings();
|
2017-09-13 07:41:52 +00:00
|
|
|
if (g_SoundDriver)
|
|
|
|
{
|
2017-09-14 08:07:12 +00:00
|
|
|
g_SoundDriver->AI_Startup();
|
2017-09-13 07:41:52 +00:00
|
|
|
}
|
2016-06-05 01:33:35 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL RomClosed(void)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Start");
|
2017-09-13 07:41:52 +00:00
|
|
|
g_Dacrate = 0;
|
|
|
|
if (g_SoundDriver)
|
|
|
|
{
|
|
|
|
g_SoundDriver->AI_Shutdown();
|
|
|
|
}
|
2017-09-26 13:57:33 +00:00
|
|
|
g_romopen = false;
|
2016-06-05 01:33:35 +00:00
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void CALL ProcessAList(void)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Called");
|
|
|
|
}
|
|
|
|
|
2018-11-18 00:42:49 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2016-06-05 01:33:35 +00:00
|
|
|
extern "C" void UseUnregisteredSetting(int /*SettingID*/)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceAudioInterface, TraceDebug, "Called");
|
|
|
|
#ifdef _WIN32
|
|
|
|
DebugBreak();
|
|
|
|
#endif
|
2017-10-13 00:29:45 +00:00
|
|
|
}
|
2018-12-02 04:36:37 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
void SetTimerResolution(void)
|
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
HMODULE hMod = GetModuleHandle(L"ntdll.dll");
|
2021-04-12 11:35:39 +00:00
|
|
|
if (hMod != nullptr)
|
2019-01-21 05:45:52 +00:00
|
|
|
{
|
|
|
|
typedef LONG(NTAPI* tNtSetTimerResolution)(IN ULONG DesiredResolution, IN BOOLEAN SetResolution, OUT PULONG CurrentResolution);
|
|
|
|
tNtSetTimerResolution NtSetTimerResolution = (tNtSetTimerResolution)GetProcAddress(hMod, "NtSetTimerResolution");
|
|
|
|
ULONG CurrentResolution = 0;
|
|
|
|
NtSetTimerResolution(10000, TRUE, &CurrentResolution);
|
|
|
|
}
|
2018-12-02 04:36:37 +00:00
|
|
|
}
|
|
|
|
#endif
|