Skip to content

Commit

Permalink
Initial code to update steam audio
Browse files Browse the repository at this point in the history
  • Loading branch information
LAGonauta committed Nov 24, 2023
1 parent c70a6d2 commit 772b7ee
Show file tree
Hide file tree
Showing 22 changed files with 1,511 additions and 1,020 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ set(SOURCES
src/SoundSources/BaseSoundSource.cpp
src/SoundSources/StaticSoundSource.cpp
src/SoundSources/StreamingSoundSource.cpp
)
src/SteamAudio/Context.cpp
src/SteamAudioLib.cpp
"src/SteamAudio/Simulator.cpp" "src/SteamAudio/Scene.cpp" "src/SteamAudio/StaticMesh.cpp" "src/SteamAudio/Source.cpp")

add_library(MetaAudio SHARED ${SOURCES})

Expand Down
5 changes: 2 additions & 3 deletions include/AudioEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Utilities/AudioCache.hpp"
#include "Loaders/SoundLoader.hpp"
#include "Utilities/ChannelManager.hpp"
#include "SteamAudio/Context.hpp"

namespace MetaAudio
{
Expand Down Expand Up @@ -59,9 +60,7 @@ namespace MetaAudio
void OpenAL_Shutdown();

// SteamAudio
IPLhandle sa_context = nullptr;
IPLSimulationSettings sa_simulationSettings{};
IPLhandle sa_environment = nullptr;
SteamAudio::Context sa_context = nullptr;

void SteamAudio_Init();
void SteamAudio_Shutdown();
Expand Down
128 changes: 51 additions & 77 deletions include/Loaders/SteamAudioMapMeshLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,58 @@
#include <unordered_map>

#include "alure2.h"
#include "dynamic_steamaudio.h"
#include "SteamAudio/Context.hpp"
#include "SteamAudio/Scene.hpp"
#include "SteamAudio/Simulator.hpp"

namespace MetaAudio
{
class SteamAudioMapMeshLoader final
{
private:
class ProcessedMap final
{
private:
std::string mapName;
IPLhandle environment;
IPLhandle scene;
IPLhandle static_mesh;

public:
ProcessedMap(const std::string& mapName, IPLhandle env, IPLhandle scene, IPLhandle mesh)
: environment(env), scene(scene), static_mesh(mesh), mapName(mapName)
{
}

~ProcessedMap()
{
if (environment != nullptr)
{
gSteamAudio.iplDestroyEnvironment(&environment);
}

if (scene != nullptr)
{
gSteamAudio.iplDestroyScene(&scene);
}

if (static_mesh != nullptr)
{
gSteamAudio.iplDestroyStaticMesh(&static_mesh);
}
}

// delete copy
ProcessedMap(const ProcessedMap& other) = delete;
ProcessedMap& ProcessedMap::operator=(const ProcessedMap& other) = delete;

// allow move
ProcessedMap(ProcessedMap&& other) = default;
ProcessedMap& operator=(ProcessedMap&& other) = default;

const std::string& Name()
{
return mapName;
}

IPLhandle Env()
{
return environment;
}
};

IPLSimulationSettings sa_simul_settings;
IPLhandle sa_context;

std::unique_ptr<ProcessedMap> current_map;

alure::Vector3 Normalize(const alure::Vector3& vector);
float DotProduct(const alure::Vector3& left, const alure::Vector3& right);

// Transmission details:
// SteamAudio returns the transmission property of the material that was hit, not how much was transmitted
// We should calculate ourselves how much is actually transmitted. The unit used in MetaAudio is actually
// the attenuation `dB/m`, not how much is transmitted per meter.
std::array<IPLMaterial, 1> materials{ {0.10f, 0.20f, 0.30f, 0.05f, 2.0f, 4.0f, (1.0f / 0.15f)} };
public:
SteamAudioMapMeshLoader(IPLhandle sa_context, IPLSimulationSettings simulSettings);

// Checks if map is current , if not update it
void update();

// get current scene data as an IPLhandle
IPLhandle CurrentEnvironment();
};
class SteamAudioMapMeshLoader final
{
private:
class ProcessedMap final
{
private:
SteamAudio::Scene scene = nullptr;
SteamAudio::StaticMesh static_mesh = nullptr;
SteamAudio::Simulator simulator = nullptr;
std::string mapName;

public:
ProcessedMap(const std::string& mapName, SteamAudio::Scene scene, SteamAudio::StaticMesh mesh, SteamAudio::Simulator simulator)
: scene(scene), static_mesh(mesh), simulator(simulator), mapName(mapName)
{
}

const std::string& Name()
{
return mapName;
}

SteamAudio::Simulator Simulator() {
return simulator;
}
};

IPLSimulationSettings sa_simul_settings;
SteamAudio::Context sa_context;

std::unique_ptr<ProcessedMap> current_map = nullptr;

alure::Vector3 Normalize(const alure::Vector3& vector);
float DotProduct(const alure::Vector3& left, const alure::Vector3& right);

// Transmission details:
// SteamAudio returns the transmission property of the material that was hit, not how much was transmitted
// We should calculate ourselves how much is actually transmitted. The unit used in MetaAudio is actually
// the attenuation `dB/m`, not how much is transmitted per meter.
std::array<IPLMaterial, 1> materials{ {0.10f,0.20f,0.30f,0.05f,0.100f,0.050f,0.030f} };
public:
SteamAudioMapMeshLoader(SteamAudio::Context sa_context, IPLSimulationSettings simulSettings);

// Checks if map is current, if not update it
void update();

SteamAudio::Simulator CurrentSimulator();
};
}
27 changes: 27 additions & 0 deletions include/SteamAudio/Context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <variant>

#include "SteamAudioLib.h"
#include "ObjectPtr.hpp"
#include "Simulator.hpp"
#include "Scene.hpp"

namespace MetaAudio {
namespace SteamAudio {
class Context final : public ObjectPtr<IPLContext> {
public:
static std::variant<Context, IPLerror> Create(IPLContextSettings& settings);
constexpr Context(nullptr_t) noexcept : ObjectPtr(nullptr) {}
Context(IPLContext ptr) : ObjectPtr(ptr) {};

std::variant<Simulator, IPLerror> CreateSimulator(IPLSimulationSettings& simulationSettings);
std::variant<Scene, IPLerror> CreateScene(IPLSceneSettings& sceneSettings);

protected:
// Inherited via ObjectPtr
void retain(IPLContext handle) override;
void release(IPLContext handle) override;
};
}
}
63 changes: 63 additions & 0 deletions include/SteamAudio/ObjectPtr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <stdexcept>

#include "SteamAudioLib.h"

namespace MetaAudio {
namespace SteamAudio {
template<class HANDLE>
class ObjectPtr {
public:
constexpr ObjectPtr(nullptr_t) noexcept {}
virtual ~ObjectPtr() {
if (m_handle != nullptr) {
release(m_handle);
}
}

ObjectPtr(const ObjectPtr& other) : ObjectPtr(other.m_handle) {
if (m_handle != nullptr) {
retain(m_handle);
}
}
ObjectPtr& operator=(const ObjectPtr& other) {
if (this == &other) {
return *this;
}

if (this->m_handle == other.m_handle) {
return *this;
}
retain(other.m_handle);
release(this->m_handle);
this->m_handle = other.m_handle;
return *this;
}

ObjectPtr(ObjectPtr&& other) : m_handle(std::exchange(other.m_handle, nullptr)) noexcept {}
ObjectPtr& operator=(ObjectPtr&& other) noexcept {
std::swap(m_handle, other.m_handle);
return *this;
}

bool operator==(ObjectPtr const& other) {
return this->m_handle == other.m_handle;
}

bool operator!=(ObjectPtr const& other) {
return this->m_handle != other.m_handle;
}

protected:
virtual void retain(HANDLE handle) {
throw std::logic_error("retain not implemented");
}
virtual void release(HANDLE handle) {
throw std::logic_error("release not implemented");
}
HANDLE m_handle = nullptr;
ObjectPtr(HANDLE handle) : m_handle(handle) {}
};
}
}
30 changes: 30 additions & 0 deletions include/SteamAudio/Scene.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <variant>

#include "SteamAudioLib.h"
#include "ObjectPtr.hpp"
#include "StaticMesh.hpp"

namespace MetaAudio {
namespace SteamAudio {
class Simulator;

class Scene final : public ObjectPtr<IPLScene> {
friend class Simulator;
public:
//constexpr Scene() : ObjectPtr() {};
constexpr Scene(nullptr_t) noexcept : ObjectPtr(nullptr) {}
Scene(IPLScene ptr) : ObjectPtr(ptr) {};
std::variant<StaticMesh, IPLerror> StaticMeshCreate(IPLStaticMeshSettings& staticMeshSettings);
void StaticMeshAdd(const StaticMesh& staticMesh);

void Commit();

protected:
// Inherited via ObjectPtr
void retain(IPLScene handle) override;
void release(IPLScene handle) override;
};
}
}
32 changes: 32 additions & 0 deletions include/SteamAudio/Simulator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <variant>

#include "SteamAudioLib.h"
#include "ObjectPtr.hpp"
#include "Scene.hpp"
#include "Source.hpp"

namespace MetaAudio {
namespace SteamAudio {
class Simulator final : public ObjectPtr<IPLSimulator> {
public:
//constexpr Simulator() : ObjectPtr() {};
constexpr Simulator(nullptr_t) noexcept : ObjectPtr(nullptr) {}
Simulator(IPLSimulator ptr) : ObjectPtr(ptr) {};
std::variant<Source, IPLerror> SourceCreate(IPLSourceSettings& sourceSettings);
void SourceRemove(const Source& source);
void SetScene(const Scene& scene);
void SourceAdd(const Source& source);
void Commit();

void SetSharedInputs(IPLSimulationFlags flags, IPLSimulationSharedInputs& inputs);
void RunDirect();

protected:
// Inherited via ObjectPtr
void retain(IPLSimulator handle) override;
void release(IPLSimulator handle) override;
};
}
}
26 changes: 26 additions & 0 deletions include/SteamAudio/Source.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <variant>

#include "SteamAudioLib.h"
#include "ObjectPtr.hpp"

namespace MetaAudio {
namespace SteamAudio {
class Simulator;

class Source final : public ObjectPtr<IPLSource> {
friend class Simulator;
public:
constexpr Source(nullptr_t) noexcept : ObjectPtr(nullptr) {}
Source(IPLSource ptr) : ObjectPtr(ptr) {};
void SetInputs(IPLSimulationFlags flags, IPLSimulationInputs& inputs);
IPLSimulationOutputs GetOutputs(IPLSimulationFlags flags);

protected:
// Inherited via ObjectPtr
void retain(IPLSource handle) override;
void release(IPLSource handle) override;
};
}
}
25 changes: 25 additions & 0 deletions include/SteamAudio/StaticMesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <variant>

#include "SteamAudioLib.h"
#include "ObjectPtr.hpp"

namespace MetaAudio {
namespace SteamAudio {
class Scene;

class StaticMesh final : public ObjectPtr<IPLStaticMesh> {
friend class Scene;
public:
//constexpr StaticMesh() : ObjectPtr() {};
constexpr StaticMesh(nullptr_t) noexcept : ObjectPtr(nullptr) {}
StaticMesh(IPLStaticMesh ptr) : ObjectPtr(ptr) {};

protected:
// Inherited via ObjectPtr
void retain(IPLStaticMesh handle) override;
void release(IPLStaticMesh handle) override;
};
}
}
Loading

0 comments on commit 772b7ee

Please sign in to comment.