-
Notifications
You must be signed in to change notification settings - Fork 0
/
OdinFmodAdapter.cpp
219 lines (160 loc) · 6.85 KB
/
OdinFmodAdapter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Fill out your copyright notice in the Description page of Project Settings.
#include "OdinFmodAdapter.h"
#include "fmod_studio.hpp"
#include "FMODStudioModule.h"
#include "odin.h"
#include "OdinFunctionLibrary.h"
#include "OdinMediaSoundGenerator.h"
#include "OdinPlaybackMedia.h"
#include <Kismet/KismetMathLibrary.h>
void UOdinFmodAdapter::AssignOdinMedia(UOdinPlaybackMedia*& Media)
{
if (nullptr == Media)
return;
this->SoundGenerator = MakeShared<OdinMediaSoundGenerator, ESPMode::ThreadSafe>();
this->PlaybackMedia = Media;
SoundGenerator->SetOdinStream(Media->GetMediaHandle());
}
void UOdinFmodAdapter::SetAttenuation(EFmodDspPan3dRolloffType InRolloffType, float InMinimumDistance, float InMaximumDistance, EFmodDspPan3dExtentMode InExtentMode, float InSoundSize, float InMinimumExtent, float InOutputGain)
{
this->RolloffType = InRolloffType;
this->MinimumDistance = InMinimumDistance;
this->MaximumDistance = InMaximumDistance;
this->ExtentMode = InExtentMode;
this->SoundSize = InSoundSize;
this->MinimumExtent = InMinimumExtent;
this->OutputGain = InOutputGain;
UpdateAttenSettings();
Update3DPosition();
}
FMOD_RESULT UOdinFmodAdapter::OdinDSPReadCallback(FMOD_DSP_STATE* dsp_state, float* inbuffer, float* outbuffer, unsigned int length, int inchannels, int* outchannels)
{
void* userdata;
dsp_state->functions->getuserdata(dsp_state, &userdata);
*outchannels = 2;
UOdinFmodAdapter* instance = reinterpret_cast<UOdinFmodAdapter*>(userdata);
return instance->dspreadcallback(dsp_state, outbuffer, length, inchannels);
}
void UOdinFmodAdapter::Update3DPosition()
{
FMOD_DSP_PARAMETER_3DATTRIBUTES_MULTI attrs = { 0 };
FMOD_3D_ATTRIBUTES relattr = { 0 };
FMOD_3D_ATTRIBUTES absattr = { 0 };
if (GEngine == nullptr) return;
UWorld* world = this->GetWorld();
if (world == nullptr) return;
ULocalPlayer* player = GEngine->GetGamePlayer(world, 0);
if (player == nullptr) return;
TObjectPtr<APlayerController> controller = player->PlayerController;
if (controller == nullptr) return;
AActor* listener = controller->GetPawn();
if (listener == nullptr) return;
relattr.position = ConvertUnrealToFmodVector(this->GetComponentLocation() - listener->GetActorLocation(), 0.01f);
relattr.velocity = ConvertUnrealToFmodVector(this->GetComponentVelocity() - listener->GetVelocity(), 0.01f);
relattr.forward = ConvertUnrealToFmodVector(UKismetMathLibrary::GetForwardVector(this->GetComponentRotation()));
relattr.up = ConvertUnrealToFmodVector(UKismetMathLibrary::GetUpVector(this->GetComponentRotation()));
absattr.position = ConvertUnrealToFmodVector(this->GetComponentLocation(), 0.01f);
absattr.velocity = ConvertUnrealToFmodVector(this->GetComponentVelocity(), 0.01f);
absattr.forward = ConvertUnrealToFmodVector(UKismetMathLibrary::GetForwardVector(this->GetComponentRotation()));
absattr.up = ConvertUnrealToFmodVector(UKismetMathLibrary::GetUpVector(this->GetComponentRotation()));
attrs.relative[0] = relattr;
attrs.numlisteners = 1;
attrs.absolute = absattr;
dsp_pan->setParameterData(FMOD_DSP_PAN_3D_POSITION, &attrs, sizeof(FMOD_DSP_PARAMETER_3DATTRIBUTES_MULTI));
group->set3DAttributes(&absattr.position, &absattr.velocity);
FMOD::Studio::System* System = IFMODStudioModule::Get().GetStudioSystem(EFMODSystemContext::Runtime);
FMOD::System* CoreSystem = nullptr;
System->getCoreSystem(&CoreSystem);
FMOD_VECTOR pos = ConvertUnrealToFmodVector(listener->GetActorLocation(), 0.01f);
FMOD_VECTOR vel = ConvertUnrealToFmodVector(listener->GetVelocity(), 0.01f);
FMOD_VECTOR forward = ConvertUnrealToFmodVector(listener->GetActorForwardVector());
FMOD_VECTOR up = ConvertUnrealToFmodVector(listener->GetActorUpVector());
CoreSystem->set3DListenerAttributes(0, &pos, &vel, &forward, &up);
CoreSystem->update();
}
void UOdinFmodAdapter::UpdateAttenSettings()
{
dsp_pan->setParameterInt(FMOD_DSP_PAN_3D_ROLLOFF, (int)RolloffType);
dsp_pan->setParameterFloat(FMOD_DSP_PAN_3D_MIN_DISTANCE, MinimumDistance);
dsp_pan->setParameterFloat(FMOD_DSP_PAN_3D_MAX_DISTANCE, MaximumDistance);
dsp_pan->setParameterInt(FMOD_DSP_PAN_3D_EXTENT_MODE, (int)ExtentMode);
dsp_pan->setParameterFloat(FMOD_DSP_PAN_3D_SOUND_SIZE, SoundSize);
dsp_pan->setParameterFloat(FMOD_DSP_PAN_3D_MIN_EXTENT, MinimumExtent);
}
FMOD_VECTOR UOdinFmodAdapter::ConvertUnrealToFmodVector(FVector in, float scale)
{
auto out = FMOD_VECTOR();
out.x = in.Y * scale;
out.y = in.Z * scale;
out.z = in.X * scale;
return out;
}
void UOdinFmodAdapter::BeginPlay()
{
FMOD::Studio::System* System = IFMODStudioModule::Get().GetStudioSystem(EFMODSystemContext::Runtime);
FMOD::System* CoreSystem = nullptr;
System->getCoreSystem(&CoreSystem);
FMOD_DSP_READ_CALLBACK mReadCallback = OdinDSPReadCallback;
FMOD_DSP_DESCRIPTION desc = { 0 };
desc.read = mReadCallback;
desc.userdata = this;
desc.numoutputbuffers = 1;
FMOD_RESULT res = CoreSystem->createDSP(&desc, &mOdinDSP);
if (res == FMOD_RESULT::FMOD_OK)
{
CoreSystem->getMasterChannelGroup(&group);
if (group->addDSP(0, mOdinDSP) == FMOD_RESULT::FMOD_OK)
{
UE_LOG(LogTemp, Warning, TEXT("Added Odin DSP to channel group"));
}
}
FMOD_RESULT result = CoreSystem->createDSPByType(FMOD_DSP_TYPE_PAN, &dsp_pan);
if (result == FMOD_RESULT::FMOD_OK)
{
CoreSystem->getMasterChannelGroup(&group);
if (group->addDSP(FMOD_CHANNELCONTROL_DSP_HEAD, dsp_pan) == FMOD_RESULT::FMOD_OK)
{
UE_LOG(LogTemp, Warning, TEXT("Added Object Spatializer DSP to channel group"));
group->setMode(FMOD_3D | FMOD_3D_WORLDRELATIVE | FMOD_3D_LINEARROLLOFF);
}
}
// Set Pan Output to Stereo
dsp_pan->setParameterInt(FMOD_DSP_PAN_MODE, (int)FMOD_DSP_PAN_MODE_SURROUND);
// Set Pan Mode to full 3D Positional
dsp_pan->setParameterFloat(FMOD_DSP_PAN_3D_PAN_BLEND, 1.0f);
UpdateAttenSettings();
Update3DPosition();
}
void UOdinFmodAdapter::DestroyComponent(bool bPromoteChildren)
{
auto result2 = group->removeDSP(dsp_pan);
auto result4 = group->removeDSP(mOdinDSP);
dsp_pan->release();
mOdinDSP->release();
Super::DestroyComponent(bPromoteChildren);
}
UOdinFmodAdapter::UOdinFmodAdapter()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UOdinFmodAdapter::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
Update3DPosition();
}
FMOD_RESULT UOdinFmodAdapter::dspreadcallback(FMOD_DSP_STATE* dsp_state, float* data, unsigned int datalen, int inchannels)
{
if (!data)
return FMOD_ERR_INVALID_PARAM;
if (!SoundGenerator || !PlaybackMedia)
return FMOD_OK;
unsigned int requestedDataArrayLength = datalen * 2;
const uint32 Result = SoundGenerator->OnGenerateAudio(data, (int32)requestedDataArrayLength);
if (odin_is_error(Result))
{
FString ErrorString = UOdinFunctionLibrary::FormatError(Result, true);
UE_LOG(LogTemp, Error, TEXT("UOdinFmodAdapter: Error during FillSamplesBuffer: %s"), *ErrorString);
return FMOD_OK;
}
return FMOD_OK;
}