-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoaLibraryUnity.cpp
140 lines (120 loc) · 3.98 KB
/
HoaLibraryUnity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//==============================================================================
// HoaLibrary for Unity - version 1.0.0
// https://github.com/CICM/HoaLibrary-Unity
// Copyright (c) 2019, Eliott Paris, CICM, ArTeC.
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
// Thirdparty :
// - HoaLibrary-Light: https://github.com/CICM/HoaLibrary-Light
// - Unity nativeaudioplugins SDK: https://bitbucket.org/Unity-Technologies/nativeaudioplugins.
//==============================================================================
#include "HoaLibraryApi.h"
#include <memory> // unique_ptr...
#include <algorithm> // std::fill...
namespace HoaLibraryUnity {
namespace
{
// Stores the necessary components for the HoaLibrary system. Methods called
// from the native implementation below must check the validity of this
// instance.
struct HoaLibrarySystem
{
HoaLibrarySystem(size_t sampleframes)
: api(CreateHoaLibraryApi(sampleframes))
{}
// HoaLibrary API instance to communicate with the internal system.
std::unique_ptr<HoaLibraryApi> api = nullptr;
};
// Singleton instance to communicate with the internal API.
static std::shared_ptr<HoaLibrarySystem> hoalib = nullptr;
} // namespace
void Initialize(size_t vectorsize)
{
assert(vectorsize != 0);
hoalib = std::make_shared<HoaLibrarySystem>(vectorsize);
}
void Shutdown()
{
hoalib.reset();
}
void ProcessListener(size_t frames, float_t* output)
{
assert(output != nullptr);
const size_t channels = 2;
auto hoalib_copy = hoalib;
if (hoalib_copy == nullptr
|| !hoalib_copy->api->fillInterleavedOutputBuffer(frames, output))
{
// No valid output was rendered, fill the output buffer with zeros.
const size_t buffer_size_samples = channels * frames;
std::fill(output, output + buffer_size_samples, 0.0f);
}
}
void SetMasterGain(float_t gain)
{
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->setMasterGain(gain);
}
}
HoaLibraryApi::source_id_t CreateSource()
{
auto id = HoaLibraryApi::invalid_source_id;
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
id = hoalib_copy->api->createSource();
}
return id;
}
void DestroySource(HoaLibraryApi::source_id_t id)
{
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->destroySource(id);
}
}
void ProcessSource(HoaLibraryApi::source_id_t id, size_t num_frames, float_t* inputs)
{
assert(inputs != nullptr);
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->setInterleavedSourceBuffer(id, inputs, num_frames);
}
}
void SetSourcePan(HoaLibraryApi::source_id_t id, float_t pan)
{
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->setSourcePan(id, pan);
}
}
void SetSourceGain(HoaLibraryApi::source_id_t id, float_t gain)
{
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->setSourceGain(id, gain);
}
}
void SetSourcePosition(HoaLibraryApi::source_id_t id, float_t px, float_t py, float_t pz)
{
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->setSourcePosition(id, px, py, pz);
}
}
void SetSourceOptim(HoaLibraryApi::source_id_t id, int optim)
{
auto hoalib_copy = hoalib;
if (hoalib_copy != nullptr)
{
hoalib_copy->api->setSourceOptim(id, optim);
}
}
}