85 lines
3.8 KiB
C++
85 lines
3.8 KiB
C++
// 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; }
|
|
}
|