Add server-side spatial audio with HRTF, occlusion and adaptive quality
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(sverse_hrtf LANGUAGES CXX)
|
||||
set(STEAMAUDIO_ROOT "" CACHE PATH "Steam Audio 4.8.1 SDK root")
|
||||
if(NOT EXISTS "${STEAMAUDIO_ROOT}/include/phonon.h")
|
||||
message(FATAL_ERROR "Set STEAMAUDIO_ROOT to the extracted steamaudio directory")
|
||||
endif()
|
||||
add_library(sverse_hrtf SHARED bridge.cpp)
|
||||
target_compile_features(sverse_hrtf PRIVATE cxx_std_17)
|
||||
target_include_directories(sverse_hrtf PRIVATE "${STEAMAUDIO_ROOT}/include")
|
||||
if(WIN32)
|
||||
target_link_libraries(sverse_hrtf PRIVATE "${STEAMAUDIO_ROOT}/lib/windows-x64/phonon.lib")
|
||||
add_custom_command(TARGET sverse_hrtf POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${STEAMAUDIO_ROOT}/lib/windows-x64/phonon.dll" "$<TARGET_FILE_DIR:sverse_hrtf>")
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
|
||||
target_link_libraries(sverse_hrtf PRIVATE "${STEAMAUDIO_ROOT}/lib/linux-x64/libphonon.so")
|
||||
set_target_properties(sverse_hrtf PROPERTIES BUILD_RPATH "$ORIGIN" INSTALL_RPATH "$ORIGIN")
|
||||
add_custom_command(TARGET sverse_hrtf POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${STEAMAUDIO_ROOT}/lib/linux-x64/libphonon.so" "$<TARGET_FILE_DIR:sverse_hrtf>")
|
||||
else()
|
||||
message(FATAL_ERROR "This bridge currently supports Windows x64 and Linux x64")
|
||||
endif()
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0.
|
||||
#include <phonon.h>
|
||||
#include <phonon_version.h>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define EXPORT extern "C" __declspec(dllexport)
|
||||
#else
|
||||
#define EXPORT extern "C" __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
struct Renderer {
|
||||
IPLContext context = nullptr;
|
||||
IPLHRTF hrtf = nullptr;
|
||||
IPLAudioSettings audio{};
|
||||
~Renderer() { iplHRTFRelease(&hrtf); iplContextRelease(&context); }
|
||||
};
|
||||
struct Pair {
|
||||
Renderer* renderer;
|
||||
IPLBinauralEffect effect = nullptr;
|
||||
std::vector<float> left, right;
|
||||
explicit Pair(Renderer* r) : renderer(r), left(r->audio.frameSize), right(r->audio.frameSize) {}
|
||||
~Pair() { iplBinauralEffectRelease(&effect); }
|
||||
};
|
||||
|
||||
EXPORT int sverse_hrtf_create(int sampleRate, int frameSize, void** output) {
|
||||
if (!output || sampleRate != 48000 || frameSize != 480) return -1;
|
||||
*output = nullptr;
|
||||
try {
|
||||
auto renderer = std::make_unique<Renderer>();
|
||||
IPLContextSettings context{};
|
||||
context.version = STEAMAUDIO_VERSION;
|
||||
context.simdLevel = IPL_SIMDLEVEL_SSE2;
|
||||
auto error = iplContextCreate(&context, &renderer->context);
|
||||
if (error != IPL_STATUS_SUCCESS) return static_cast<int>(error);
|
||||
renderer->audio = {sampleRate, frameSize};
|
||||
IPLHRTFSettings settings{};
|
||||
settings.type = IPL_HRTFTYPE_DEFAULT;
|
||||
settings.volume = 1.0f;
|
||||
settings.normType = IPL_HRTFNORMTYPE_RMS;
|
||||
error = iplHRTFCreate(renderer->context, &renderer->audio, &settings, &renderer->hrtf);
|
||||
if (error != IPL_STATUS_SUCCESS) return static_cast<int>(error);
|
||||
*output = renderer.release();
|
||||
return 0;
|
||||
} catch (...) { return -2; }
|
||||
}
|
||||
EXPORT void sverse_hrtf_destroy(void* renderer) { delete static_cast<Renderer*>(renderer); }
|
||||
EXPORT int sverse_hrtf_pair_create(void* renderer, void** output) {
|
||||
if (!renderer || !output) return -1;
|
||||
*output = nullptr;
|
||||
try {
|
||||
auto pair = std::make_unique<Pair>(static_cast<Renderer*>(renderer));
|
||||
IPLBinauralEffectSettings settings{pair->renderer->hrtf};
|
||||
auto error = iplBinauralEffectCreate(pair->renderer->context, &pair->renderer->audio, &settings, &pair->effect);
|
||||
if (error != IPL_STATUS_SUCCESS) return static_cast<int>(error);
|
||||
*output = pair.release();
|
||||
return 0;
|
||||
} catch (...) { return -2; }
|
||||
}
|
||||
EXPORT void sverse_hrtf_pair_destroy(void* pair) { delete static_cast<Pair*>(pair); }
|
||||
|
||||
// Borrowed Go buffers are only accessed during this call, never retained.
|
||||
// The bridge keeps all C API structures and channel-pointer arrays in native code.
|
||||
EXPORT int sverse_hrtf_apply(void* handle, const float* mono, float* stereo, const float* direction, int interpolation) {
|
||||
if (!handle || !mono || !stereo || !direction) return -1;
|
||||
try {
|
||||
auto* pair = static_cast<Pair*>(handle);
|
||||
IPLBinauralEffectParams params{};
|
||||
const float length = std::sqrt(direction[0]*direction[0] + direction[1]*direction[1] + direction[2]*direction[2]);
|
||||
params.direction = length > 1e-6f ? IPLVector3{direction[0]/length, direction[1]/length, direction[2]/length} : IPLVector3{0,0,-1};
|
||||
params.interpolation = interpolation ? IPL_HRTFINTERPOLATION_BILINEAR : IPL_HRTFINTERPOLATION_NEAREST;
|
||||
params.spatialBlend = 1.0f;
|
||||
params.hrtf = pair->renderer->hrtf;
|
||||
float* inChannels[] = {const_cast<float*>(mono)};
|
||||
float* outChannels[] = {pair->left.data(), pair->right.data()};
|
||||
const int samples = pair->renderer->audio.frameSize;
|
||||
IPLAudioBuffer input{1,samples,inChannels}, output{2,samples,outChannels};
|
||||
iplBinauralEffectApply(pair->effect,¶ms,&input,&output);
|
||||
for (int n = 0; n < samples; ++n) { stereo[2*n] = pair->left[n]; stereo[2*n+1] = pair->right[n]; }
|
||||
return 0;
|
||||
} catch (...) { return -2; }
|
||||
}
|
||||
Reference in New Issue
Block a user