generated from rbfx/Core.SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamSoundSource.cpp
More file actions
404 lines (339 loc) · 15.7 KB
/
SteamSoundSource.cpp
File metadata and controls
404 lines (339 loc) · 15.7 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// Copyright (c) 2024-2024 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "SteamSoundSource.h"
#include "SteamAudio.h"
#include "SteamSoundListener.h"
#include <Urho3D/Audio/Sound.h>
#include <Urho3D/Core/Context.h>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Scene/Node.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <phonon.h>
namespace Urho3D
{
SteamSoundSource::SteamSoundSource(Context* context) :
Component(context), sound_(nullptr), binauralEffect_(nullptr), directEffect_(nullptr), source_(nullptr), simulatorOutputs_({}), gain_(1.0f), paused_(false), loop_(false), binaural_(false), distanceAttenuation_(false), airAbsorption_(false), occlusion_(false), transmission_(false), reflection_(false), reflectionAmbisonicsOrder_(1), binauralSpatialBlend_(1.0f), binauralBilinearInterpolation_(false), effectsLoaded_(false), effectsDirty_(false)
{
audio_ = GetSubsystem<SteamAudio>();
if (audio_) {
// Add this sound source
audio_->AddSoundSource(this);
// Subscribe to render updates
SubscribeToEvent(E_RENDERUPDATE, URHO3D_HANDLER(SteamSoundSource, HandleRenderUpdate));
}
}
SteamSoundSource::~SteamSoundSource()
{
if (audio_)
// Remove this sound source
audio_->RemoveSoundSource(this);
}
void SteamSoundSource::RegisterObject(Context* context)
{
context->AddFactoryReflection<SteamSoundSource>(Category_Audio);
URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
URHO3D_ACCESSOR_ATTRIBUTE("Is Playing", IsPlaying, SetPlayingAttr, bool, true, AM_DEFAULT);
URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Sound", GetSoundAttr, SetSoundAttr, ResourceRef, ResourceRef(Sound::GetTypeStatic()), AM_DEFAULT);
URHO3D_ATTRIBUTE("Gain", float, gain_, 1.0f, AM_DEFAULT);
URHO3D_ATTRIBUTE("Loop", bool, loop_, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Binaural", bool, binaural_, MarkEffectsDirty, false, AM_DEFAULT);
URHO3D_ATTRIBUTE("Binaural Spacial Blend", float, binauralSpatialBlend_, 1.0f, AM_DEFAULT);
URHO3D_ATTRIBUTE("Binaural Bilinear Interpolation", bool, binauralBilinearInterpolation_, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Distance Attenuation", bool, distanceAttenuation_, MarkEffectsDirty, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Air absorption", bool, airAbsorption_, MarkEffectsDirty, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Occlusion", bool, occlusion_, MarkEffectsDirty, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Transmission", bool, transmission_, MarkEffectsDirty, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Reflection", bool, reflection_, MarkEffectsDirty, false, AM_DEFAULT);
URHO3D_ATTRIBUTE_EX("Reflection Ambisonics Order", unsigned, reflectionAmbisonicsOrder_, MarkEffectsDirty, 1, AM_DEFAULT);
}
void SteamSoundSource::Play(Sound *sound)
{
// Reset current frame (playback position)
frame_ = 0;
// Set sound
sound_ = sound;
// Update effects
MarkEffectsDirty();
}
bool SteamSoundSource::IsPlaying() const
{
const auto& audioSettings = audio_->GetAudioSettings();
return sound_ && !paused_;
}
void SteamSoundSource::SetPlayingAttr(bool playing)
{
paused_ = !playing;
}
void SteamSoundSource::SetSoundAttr(const ResourceRef &value)
{
auto* cache = GetSubsystem<ResourceCache>();
Play(cache->GetResource<Sound>(value.name_));
}
ResourceRef SteamSoundSource::GetSoundAttr() const
{
return GetResourceRef(sound_, Sound::GetTypeStatic());
}
IPLAudioBuffer *SteamSoundSource::GenerateAudioBuffer(float gain)
{
// Stop if effects are dirty
if (effectsDirty_)
return nullptr;
// Return nothing if not playing
if (!IsPlaying())
return nullptr;
// Get phonon context and audio settings
const auto phononContext = audio_->GetPhononContext();
const auto hrtf = audio_->GetHRTF();
const auto& audioSettings = audio_->GetAudioSettings();
// Get audio pool
MutexLock Lock(effectsMutex_);
auto& pool = audio_->GetAudioBufferPool();
// Calculate size of one full interleaved frame
const auto fullFrameSize = audioSettings.frameSize*(sound_->IsStereo()?2:1);
// Stop or reset if file ends before end of frame
if ((frame_ + 1)*fullFrameSize*(sound_->IsSixteenBit()?2:1) > sound_->GetDataSize()) {
if (loop_)
frame_ = 0;
else
return nullptr;
}
// Convert sound data to interleaved float buffer
ea::vector<float> rawInputBuffer(fullFrameSize);
for (unsigned sample = 0; sample != fullFrameSize; sample++) {
// Convert to float32
if (sound_->IsSixteenBit()) {
int16_t integerData = reinterpret_cast<const int16_t*>(sound_->GetData().get())[frame_*fullFrameSize + sample];
rawInputBuffer[sample] = float(integerData)/32767.0f;
} else {
int8_t integerData = sound_->GetData()[frame_*fullFrameSize + sample];
rawInputBuffer[sample] = float(integerData)/128.f;
}
// Apply gain
rawInputBuffer[sample] *= gain_*gain;
}
// Get listener node
auto listener = audio_->GetListener()->GetNode();
// Get positions
const auto lPos = listener->GetWorldPosition();
const auto lDir = listener->GetWorldDirection();
const auto lUp = listener->GetWorldUp();
const auto sPos = GetNode()->GetWorldPosition();
const IPLVector3 psPos {sPos.x_, sPos.y_, sPos.z_};
const IPLVector3 plPos {lPos.x_, lPos.y_, lPos.z_};
// Deinterleave sound data into buffer
iplAudioBufferDeinterleave(phononContext, rawInputBuffer.data(), pool.GetCurrentBuffer());
// Get simulator outputs
audio_->GetSimulatorOutputs(source_, simulatorOutputs_);
// Apply reflection effect
if (reflection_) {
const unsigned ambisonicsChannels = audio_->ChannelCount(reflectionAmbisonicsOrder_);
// Make sure input is mono
IPLAudioBuffer monoBuffer;
const bool originalIsMono = pool.GetCurrentBuffer()->numChannels <= 1;
if (!originalIsMono) {
iplAudioBufferAllocate(phononContext, 1, audioSettings.frameSize, &monoBuffer);
iplAudioBufferDownmix(phononContext, pool.GetCurrentBuffer(), &monoBuffer);
} else {
monoBuffer = *pool.GetCurrentBuffer();
}
// Make sure output is ambisonics
IPLAudioBuffer ambiBuffer;
iplAudioBufferAllocate(phononContext, ambisonicsChannels, audioSettings.frameSize, &ambiBuffer);
// Actually apply effect
IPLReflectionEffectParams reflectionEffectParams = simulatorOutputs_.reflections;
reflectionEffectParams.irSize = audio_->ImpulseResponseDuration() * audioSettings.samplingRate;
reflectionEffectParams.numChannels = ambisonicsChannels;
iplReflectionEffectApply(reflectionEffect_, &reflectionEffectParams, &monoBuffer, &ambiBuffer, nullptr);
if (!originalIsMono)
iplAudioBufferFree(phononContext, &monoBuffer);
// Convert ambisonics back to target channel count
IPLAmbisonicsBinauralEffectParams ambisonicsBinauralEffectParams{};
ambisonicsBinauralEffectParams.hrtf = audio_->GetHRTF();
ambisonicsBinauralEffectParams.order = static_cast<IPLint32>(reflectionAmbisonicsOrder_);
iplAmbisonicsBinauralEffectApply(ambisonicsBinauralEffect_, &ambisonicsBinauralEffectParams, &ambiBuffer, pool.GetNextBuffer());
iplAudioBufferFree(phononContext, &ambiBuffer);
pool.SwitchToNextBuffer();
}
// Apply binaural effect
if (binaural_) {
IPLBinauralEffectParams binauralEffectParams {};
binauralEffectParams.interpolation = binauralBilinearInterpolation_?IPL_HRTFINTERPOLATION_BILINEAR:IPL_HRTFINTERPOLATION_NEAREST;
binauralEffectParams.spatialBlend = binauralSpatialBlend_;
binauralEffectParams.hrtf = hrtf;
binauralEffectParams.direction = iplCalculateRelativeDirection(phononContext, psPos, plPos, {lDir.x_, lDir.y_, lDir.z_}, {lUp.x_, lUp.y_, lUp.z_});
binauralEffectParams.direction.x = -binauralEffectParams.direction.x; // Why is this required?
iplBinauralEffectApply(binauralEffect_, &binauralEffectParams, pool.GetCurrentBuffer(), pool.GetNextBuffer());
pool.SwitchToNextBuffer();
}
// Apply all direct effects
if (UsingDirectEffect()) {
// Create direct effect properties
IPLDirectEffectParams directEffectParams {};
// Get direct effect flags
IPLDirectEffectFlags directEffectFlags {};
if (distanceAttenuation_)
directEffectFlags = static_cast<IPLDirectEffectFlags>(directEffectFlags | IPL_DIRECTEFFECTFLAGS_APPLYDISTANCEATTENUATION);
if (airAbsorption_)
directEffectFlags = static_cast<IPLDirectEffectFlags>(directEffectFlags | IPL_DIRECTEFFECTFLAGS_APPLYAIRABSORPTION);
if (occlusion_)
directEffectFlags = static_cast<IPLDirectEffectFlags>(directEffectFlags | IPL_DIRECTEFFECTFLAGS_APPLYOCCLUSION);
if (transmission_)
directEffectFlags = static_cast<IPLDirectEffectFlags>(directEffectFlags | IPL_DIRECTEFFECTFLAGS_APPLYTRANSMISSION);
// Apply direct effect
if (source_ && directEffectFlags) {
// Get parameters
directEffectParams = simulatorOutputs_.direct;
directEffectParams.flags = directEffectFlags;
if (transmission_)
directEffectParams.transmissionType = IPL_TRANSMISSIONTYPE_FREQDEPENDENT;
// Apply effect using them
iplDirectEffectApply(directEffect_, &directEffectParams, pool.GetCurrentBuffer(), pool.GetNextBuffer());
pool.SwitchToNextBuffer();
}
}
// Increment to next frame
frame_++;
// Don't process any further for now, just return that buffer as is...
return pool.GetCurrentBuffer();
}
IPLSimulationFlags SteamSoundSource::SimulationFlags() const
{
int fres = IPL_SIMULATIONFLAGS_DIRECT;
if (reflection_)
fres |= IPL_SIMULATIONFLAGS_REFLECTIONS;
return static_cast<IPLSimulationFlags>(fres);
}
void SteamSoundSource::HandleRenderUpdate(StringHash eventType, VariantMap &eventData)
{
if (effectsDirty_) {
UpdateEffects();
effectsDirty_ = false;
}
}
void SteamSoundSource::OnMarkedDirty(Node *)
{
UpdateSimulationInputs();
}
bool SteamSoundSource::UsingDirectEffect() const
{
return distanceAttenuation_ || airAbsorption_ || occlusion_ || transmission_;
}
void SteamSoundSource::UpdateEffects()
{
// Fix settings
if (reflection_) {
distanceAttenuation_ = false;
occlusion_ = false;
}
// Make sure reflection simulation is enabled
if (reflection_)
audio_->SetReflectionSimulationActive(true); //TODO: Use some kind of user counter instead
const auto phononContext = audio_->GetPhononContext();
const auto& audioSettings = audio_->GetAudioSettings();
// Destroy previous effects
MutexLock Lock(effectsMutex_);
UnlockedDestroyEffects();
if (!sound_)
return;
// Create new effects
if (binaural_) {
// Create binaural effect
IPLBinauralEffectSettings binauralEffectSettings {};
binauralEffectSettings.hrtf = audio_->GetHRTF();
iplBinauralEffectCreate(phononContext, const_cast<IPLAudioSettings*>(&audioSettings), &binauralEffectSettings, &binauralEffect_);
}
if (UsingDirectEffect()) {
// Create source
IPLSourceSettings sourceSettings{};
sourceSettings.flags = SimulationFlags();
iplSourceCreate(audio_->GetSimulator(), &sourceSettings, &source_);
iplSourceAdd(source_, audio_->GetSimulator());
UpdateSimulationInputs();
audio_->MarkSimulatorDirty();
// Create direct effect
IPLDirectEffectSettings directEffectSettings {};
directEffectSettings.numChannels = sound_->IsStereo()?2:1;
iplDirectEffectCreate(phononContext, const_cast<IPLAudioSettings*>(&audioSettings), &directEffectSettings, &directEffect_);
}
if (reflection_) {
IPLReflectionEffectSettings reflectionEffectSettings {};
reflectionEffectSettings.type = IPL_REFLECTIONEFFECTTYPE_CONVOLUTION;
reflectionEffectSettings.irSize = audio_->ImpulseResponseDuration() * audioSettings.samplingRate; // (IR duration) * (sampling rate)
reflectionEffectSettings.numChannels = audio_->ChannelCount(reflectionAmbisonicsOrder_);
iplReflectionEffectCreate(phononContext, const_cast<IPLAudioSettings*>(&audioSettings), &reflectionEffectSettings, &reflectionEffect_);
IPLAmbisonicsBinauralEffectSettings ambisonicsBinauralEffectSettings{};
ambisonicsBinauralEffectSettings.hrtf = audio_->GetHRTF();
ambisonicsBinauralEffectSettings.maxOrder = static_cast<IPLint32>(reflectionAmbisonicsOrder_);
iplAmbisonicsBinauralEffectCreate(phononContext, const_cast<IPLAudioSettings*>(&audioSettings), &ambisonicsBinauralEffectSettings, &ambisonicsBinauralEffect_);
}
// Start listening
GetNode()->AddListener(this);
}
void SteamSoundSource::UnlockedDestroyEffects()
{
if (!effectsLoaded_)
return;
if (binauralEffect_)
iplBinauralEffectRelease(&binauralEffect_);
if (directEffect_)
iplDirectEffectRelease(&directEffect_);
if (source_) {
// Delete source
iplSourceRemove(source_, audio_->GetSimulator());
audio_->MarkSimulatorDirty();
iplSourceRelease(&source_);
// Stop listening
GetNode()->RemoveListener(this);
}
}
void SteamSoundSource::UpdateSimulationInputs()
{
const auto lUp = GetNode()->GetWorldUp();
const auto lDir = GetNode()->GetWorldDirection();
const auto lRight = GetNode()->GetWorldRight();
const auto lPos = GetNode()->GetWorldPosition();
IPLSimulationInputs inputs {};
inputs.flags = SimulationFlags();
inputs.directFlags = static_cast<IPLDirectSimulationFlags>(
(distanceAttenuation_?IPL_DIRECTSIMULATIONFLAGS_DISTANCEATTENUATION:0) |
(airAbsorption_?IPL_DIRECTSIMULATIONFLAGS_AIRABSORPTION:0) |
(occlusion_?IPL_DIRECTSIMULATIONFLAGS_OCCLUSION:0) |
(transmission_?IPL_DIRECTSIMULATIONFLAGS_TRANSMISSION:0));
inputs.source.right = {lRight.x_, lRight.y_, lRight.z_};
inputs.source.up = {lUp.x_, lUp.y_, lUp.z_};
inputs.source.ahead = {lDir.x_, lDir.y_, lDir.z_};
inputs.source.origin = {lPos.x_, lPos.y_, lPos.z_};
inputs.directivity.dipoleWeight = 1.0f;
inputs.occlusionType = IPL_OCCLUSIONTYPE_RAYCAST;
inputs.occlusionRadius = 0.25f;
inputs.numOcclusionSamples = 8;
inputs.reverbScale[0] = 1.0f;
inputs.reverbScale[1] = 1.0f;
inputs.reverbScale[2] = 1.0f;
inputs.hybridReverbTransitionTime = 1.0f;
inputs.hybridReverbOverlapPercent = 0.25f;
inputs.numTransmissionRays = 16;
iplSourceSetInputs(source_, SimulationFlags(), &inputs);
}
}