Fixed audio destruction and construction.

This commit is contained in:
Karutoh 2025-05-18 23:40:09 -07:00
parent 61fb586f86
commit f52afe9c92
7 changed files with 67 additions and 22 deletions

View File

@ -25,6 +25,7 @@ namespace ehs
pw_core *core;
pw_stream *input;
pw_stream *output;
spa_hook paramsHook;
static void RegistryEventGlobal(void *data, UInt_32 id, UInt_32 permissions, const char *type, UInt_32 version, const spa_dict *props);

View File

@ -6,7 +6,7 @@
#define EHS_64_BIT
#elif defined(_M_ARM64) || defined(__aarch64__)
#define EHS_LITTLE_ENDIAN
#define EHS_ARCH_ARM64
#define EHS_ARCH_AARCH64
#define EHS_64_BIT
#else
#error Unsupported architecture.

View File

@ -281,6 +281,8 @@ namespace ehs
//static Str_8 ToStr();
static UInt_64 RetrieveFreq_AARCH64();
static UInt_64 RetrieveTSC_Freq();
static UInt_64 CalculateTSC_Freq();

View File

@ -51,7 +51,7 @@ namespace ehs
}
return result;
#elif defined(EHS_ARCH_ARM64)
#elif defined(EHS_ARCH_AARCH64)
return Sqrt_VFP4(from);
#endif
}
@ -74,7 +74,7 @@ namespace ehs
}
return result;
#elif defined(EHS_ARCH_ARM64)
#elif defined(EHS_ARCH_AARCH64)
return Sqrt_VFP4(from);
#endif
}

View File

@ -93,13 +93,13 @@ namespace ehs
}
AudioDevice::AudioDevice()
: id(0), loop(nullptr), context(nullptr), core(nullptr), input(nullptr), output(nullptr)
: id(0), loop(nullptr), context(nullptr), core(nullptr), input(nullptr), output(nullptr), paramsHook{}
{
}
AudioDevice::AudioDevice(AudioDevice&& device) noexcept
: BaseAudioDevice(device), id(device.id), name((Str_8 &&)device.name), loop(device.loop),
context(device.context), core(device.core), input(device.input), output(device.output)
context(device.context), core(device.core), input(device.input), output(device.output), paramsHook(device.paramsHook)
{
device.id = 0;
device.loop = nullptr;
@ -107,11 +107,12 @@ namespace ehs
device.core = nullptr;
device.input = nullptr;
device.output = nullptr;
device.paramsHook = {};
}
AudioDevice::AudioDevice(const AudioDevice& device)
: BaseAudioDevice(device), id(device.id), name(device.name), loop(nullptr), context(nullptr), core(nullptr),
input(nullptr), output(nullptr)
input(nullptr), output(nullptr), paramsHook{}
{
}
@ -129,6 +130,7 @@ namespace ehs
core = device.core;
input = device.input;
output = device.output;
paramsHook = device.paramsHook;
device.id = 0;
device.loop = nullptr;
@ -136,6 +138,7 @@ namespace ehs
device.core = nullptr;
device.input = nullptr;
device.output = nullptr;
device.paramsHook = {};
return *this;
}
@ -154,6 +157,7 @@ namespace ehs
core = nullptr;
input = nullptr;
output = nullptr;
paramsHook = {};
return *this;
}
@ -206,14 +210,18 @@ namespace ehs
return;
}
pw_stream_connect(
output,
SInt_32 err = pw_stream_connect(
input,
PW_DIRECTION_INPUT,
id,
(pw_stream_flags)(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS),
pod,
1
);
if (err)
{
EHS_LOG_INT(LogType::ERR, 2, "Failed to connect input stream for audio device.");
}
}
if (GetType() == AudioDeviceType::OUTPUT || GetType() == AudioDeviceType::ALL)
@ -225,7 +233,7 @@ namespace ehs
return;
}
pw_stream_connect(
SInt_32 err = pw_stream_connect(
output,
PW_DIRECTION_OUTPUT,
id,
@ -233,6 +241,10 @@ namespace ehs
pod,
1
);
if (err)
{
EHS_LOG_INT(LogType::ERR, 2, "Failed to connect output stream for audio device.");
}
}
static constexpr pw_stream_events streamEvents = {
@ -240,8 +252,17 @@ namespace ehs
.param_changed = OnParamChanged
};
spa_hook streamListener = {};
pw_stream_add_listener(output, &streamListener, &streamEvents, this);
pw_stream_add_listener(output, &paramsHook, &streamEvents, this);
while (pw_stream_get_state(output, nullptr) == PW_STREAM_STATE_CONNECTING)
{
SInt_32 err = pw_loop_iterate(loop, 0);
if (err < 0)
{
EHS_LOG_INT(LogType::ERR, 3, "Failed to update audio with error #" + Str_8::FromNum(err) + ".");
return;
}
}
EHS_LOG_SUCCESS();
}
@ -283,7 +304,15 @@ namespace ehs
return 0;
}
pw_loop_iterate(loop, 0);
if (!data || !size)
return 0;
SInt_32 err = pw_loop_iterate(loop, 0);
if (err < 0)
{
EHS_LOG_INT(LogType::ERR, 1, "Failed to update audio with error #" + Str_8::FromNum(err) + ".");
return 0;
}
if (pw_stream_get_state(output, nullptr) != PW_STREAM_STATE_STREAMING)
return 0;

View File

@ -78,7 +78,7 @@ namespace ehs
{
#if defined(EHS_ARCH_X64)
return Architecture::X64;
#elif defined(EHS_ARCH_ARM64)
#elif defined(EHS_ARCH_AARCH64)
return Architecture::ARM64;
#else
return Architecture::UNKNOWN;
@ -113,13 +113,19 @@ namespace ehs
return frequency.QuadPart;
#elif defined(EHS_OS_LINUX)
if (!TSC_Freq)
TSC_Freq = RetrieveTSC_Freq();
#if defined(EHS_ARCH_X64)
if (!TSC_Freq)
TSC_Freq = RetrieveTSC_Freq();
return TSC_Freq;
return TSC_Freq;
#elif defined(EHS_ARCH_AARCH64)
return RetrieveFreq_AARCH64();
#else
return 0;
#endif
#else
return 0;
#endif
return 0;
}
UInt_64 CPU::GetTSC()
@ -133,7 +139,7 @@ namespace ehs
TSC tsc;
RDTSCP(&tsc);
#if defined(EHS_ARCH_X64)
#if defined(EHS_ARCH_X64) || defined(EHS_ARCH_AARCH64)
UInt_64 result = 0;
#if defined(EHS_LITTLE_ENDIAN)

View File

@ -1,7 +1,14 @@
.global _ZN3ehs3CPU6RDTSCPEPNS_3TSCE
.global _ZN3ehs3CPU20RetrieveFreq_AARCH64Ev
.section .text
_ZN3ehs3CPU6RDTSCPEPNS_3TSCE:
MRS X1, CNTVCT_EL0
STR X1, [X0, #4]
RET
MRS X2, CNTVCT_EL0
EOR W1, W1, W1
STR W1, [X0]
STR X2, [X0, #4]
RET
_ZN3ehs3CPU20RetrieveFreq_AARCH64Ev:
MRS X0, CNTFRQ_EL0
RET