generated from rbfx/Core.SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamAudio.cpp
More file actions
411 lines (345 loc) · 11.7 KB
/
SteamAudio.cpp
File metadata and controls
411 lines (345 loc) · 11.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
405
406
407
408
409
410
411
//
// Copyright (c) 2008-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 "SteamAudio.h"
#include "SteamSoundListener.h"
#include "SteamSoundMesh.h"
#include "SteamSoundSource.h"
#include <Urho3D/Audio/Sound.h>
#include <Urho3D/Core/Context.h>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/Scene/Node.h>
#include <SDL.h>
namespace Urho3D
{
void SDLSteamAudioCallback(void* userdata, Uint8* stream, int);
SteamAudio::SteamAudio(Context* context) :
Object(context), audioBufferPool_(nullptr)
{
context_->RequireSDL(SDL_INIT_AUDIO);
// Set the master to the default value
masterGain_ = 1.0f;
SubscribeToEvent(E_RENDERUPDATE, URHO3D_HANDLER(SteamAudio, HandleRenderUpdate));
}
SteamAudio::~SteamAudio()
{
Release();
context_->ReleaseSDL();
}
bool SteamAudio::SetMode(int mixRate, SpeakerMode mode)
{
// Clean up first
Release();
// Reset master gain
masterGain_ = 1.0f;
// Convert speaker mode to channel count
switch (mode) {
case SPK_MONO: channelCount_ = 1; break;
case SPK_AUTO:
case SPK_STEREO: channelCount_ = 2; break;
case SPK_QUADROPHONIC: channelCount_ = 4; break;
case SPK_SURROUND_5_1: channelCount_ = 6; break;
}
// Create context
IPLContextSettings contextSettings {};
contextSettings.version = STEAMAUDIO_VERSION;
#ifndef NDEBUG
contextSettings.flags = IPL_CONTEXTFLAGS_VALIDATION;
#endif
iplContextCreate(&contextSettings, &phononContext_);
// Typical audio settings...
audioSettings_ = IPLAudioSettings {};
audioSettings_.samplingRate = mixRate;
audioSettings_.frameSize = 1024;
// Create single frame buffer
// This buffer one individual audio frame per channel at a time.
finalFrameBuffer_.resize(channelCount_ * audioSettings_.frameSize);
// Create the HRTF
// The HRTF basically describes the "set of filters that is applied to audio in order to spatialize it"
IPLHRTFSettings hrtfSettings {};
hrtfSettings.type = IPL_HRTFTYPE_DEFAULT;
hrtfSettings.volume = 1.0f;
iplHRTFCreate(phononContext_, &audioSettings_, &hrtfSettings, &hrtf_);
// Create the scene
IPLSceneSettings sceneSettings {};
sceneSettings.type = IPL_SCENETYPE_DEFAULT;
iplSceneCreate(phononContext_, &sceneSettings, &scene_);
// Create the simulator
IPLSimulationSettings simulationSettings {};
simulationSettings.flags = static_cast<IPLSimulationFlags>(IPL_SIMULATIONFLAGS_DIRECT | IPL_SIMULATIONFLAGS_REFLECTIONS);
simulationSettings.sceneType = IPL_SCENETYPE_DEFAULT;
simulationSettings.reflectionType = IPL_REFLECTIONEFFECTTYPE_CONVOLUTION;
simulationSettings.maxNumOcclusionSamples = 12;
simulationSettings.maxNumRays = 16384;
simulationSettings.numDiffuseSamples = 8; //TODO: No idea about this, find a good default value
simulationSettings.maxDuration = 4.0f;
simulationSettings.maxOrder = 8;
simulationSettings.maxNumSources = 16; //TODO: This should dynamically increase if limit is reached
simulationSettings.numThreads = 3;
simulationSettings.numVisSamples = 8; //TODO: No idea about this, find a good default value
simulationSettings.samplingRate = audioSettings_.samplingRate;
simulationSettings.frameSize = audioSettings_.frameSize;
iplSimulatorCreate(phononContext_, &simulationSettings, &simulator_);
iplSimulatorSetScene(simulator_, scene_);
MarkSimulatorDirty();
// Allocate an output buffer
// That buffer is "deinterleaved", which means that it's actually one buffer for each channel
phononFrameBuffer_ = IPLAudioBuffer {};
iplAudioBufferAllocate(phononContext_, channelCount_, audioSettings_.frameSize, &phononFrameBuffer_);
// Create the buffer pool
audioBufferPool_ = ea::make_unique<SteamAudioBufferPool>(this);
// Set up SDL
SDL_AudioSpec spec {};
spec.freq = audioSettings_.samplingRate;
spec.format = AUDIO_F32;
spec.channels = 2;
spec.samples = static_cast<unsigned short>(audioSettings_.frameSize);
spec.callback = *SDLSteamAudioCallback;
spec.userdata = this;
// Open the audio channel
if (SDL_OpenAudio(&spec, NULL) < 0) {
URHO3D_LOGERROR(ea::string("Failed to open SDL2 audio: ")+SDL_GetError());
return false;
}
// Start playing audio
audioMutex_.Release();
Play();
// Update once
Update(0.0f);
return true;
}
bool SteamAudio::RefreshMode()
{
return SetMode(audioSettings_.samplingRate, GetSpeakerMode());
}
void SteamAudio::Close()
{
Release();
}
void SteamAudio::Update(float timeStep)
{
if (sceneDirty_) {
iplSceneCommit(scene_);
iplSceneSaveOBJ(scene_, "scene-base.obj");
sceneDirty_ = false;
}
if (simulatorDirty_) {
iplSimulatorCommit(simulator_);
simulatorDirty_ = false;
}
// Update listener coordinates in simulator
if (listener_) {
const auto lUp = listener_->GetNode()->GetWorldUp();
const auto lDir = listener_->GetNode()->GetWorldDirection();
const auto lRight = listener_->GetNode()->GetWorldRight();
const auto lPos = listener_->GetNode()->GetWorldPosition();
sharedInputs_.listener.right = {lRight.x_, lRight.y_, lRight.z_};
sharedInputs_.listener.up = {lUp.x_, lUp.y_, lUp.z_};
sharedInputs_.listener.ahead = {lDir.x_, lDir.y_, lDir.z_};
sharedInputs_.listener.origin = {lPos.x_, lPos.y_, lPos.z_};
iplSimulatorSetSharedInputs(simulator_, SimulationFlags(), &sharedInputs_);
// Run simulations
{
MutexLock Lock(simulatorMutex_);
iplSimulatorRunDirect(simulator_);
}
{
MutexLock Lock(simulatorMutex_);
iplSimulatorRunReflections(simulator_);
}
}
}
void SteamAudio::Play()
{
SDL_PauseAudio(0);
}
void SteamAudio::Stop()
{
SDL_PauseAudio(1);
}
void SteamAudio::SetMasterGain(float gain)
{
masterGain_ = gain;
}
void SteamAudio::SetListener(SteamSoundListener *listener)
{
MutexLock Lock(audioMutex_);
listener_ = listener;
}
void SteamAudio::SetReflectionSimulationActive(bool active)
{
simulateReflections_ = active;
}
void SteamAudio::SetImpulseResponseDuration(float duration)
{
sharedInputs_.duration = duration;
}
bool SteamAudio::GetSimulatorOutputs(IPLSource source, IPLSimulationOutputs& outputs) noexcept
{
if (!simulatorMutex_.TryAcquire())
return false;
outputs = IPLSimulationOutputs {};
iplSourceGetOutputs(source, SimulationFlags(), &outputs);
simulatorMutex_.Release();
return true;
}
float SteamAudio::GetMasterGain() const
{
return masterGain_;
}
SpeakerMode SteamAudio::GetSpeakerMode() const
{
switch (channelCount_) {
case 1: return SPK_MONO;
case 2: return SPK_STEREO;
case 4: return SPK_QUADROPHONIC;
case 6: return SPK_SURROUND_5_1;
default: return SPK_AUTO;
}
}
SteamSoundListener *SteamAudio::GetListener() const {
if (!(listener_ && listener_->IsEnabledEffective()))
return nullptr;
return listener_;
}
void SteamAudio::AddSoundSource(SteamSoundSource *soundSource)
{
MutexLock Lock(audioMutex_);
soundSources_.push_back(soundSource);
}
void SteamAudio::RemoveSoundSource(SteamSoundSource *soundSource)
{
MutexLock Lock(audioMutex_);
auto i = soundSources_.find(soundSource);
if (i != soundSources_.end())
{
MutexLock lock(audioMutex_);
soundSources_.erase(i);
}
}
void SteamAudio::MixOutput(float *dest) noexcept
{
// Stop if no listener
if (!GetListener()) {
memset(dest, 0, audioSettings_.frameSize*channelCount_*sizeof(float));
return;
}
// Don't do anything if lock can't be acquired
if (!audioMutex_.TryAcquire())
return;
// Clear frame buffer
for (unsigned channel = 0; channel != phononFrameBuffer_.numChannels; channel++)
for (unsigned sample = 0; sample != phononFrameBuffer_.numSamples; sample++)
phononFrameBuffer_.data[channel][sample] = 0.0f;
// Iterate over all sound sources
for (auto source : soundSources_) {
// Skip disabled ones
if (!source->IsEnabledEffective())
continue;
// Generate audio buffer
auto audioBuffer = source->GenerateAudioBuffer(masterGain_);
// Skip if none generated
if (!audioBuffer)
continue;
// Mix into frame buffer
iplAudioBufferMix(phononContext_, audioBuffer, &phononFrameBuffer_);
}
// Interleave into our buffer
iplAudioBufferInterleave(phononContext_, &phononFrameBuffer_, dest);
audioMutex_.Release();
}
unsigned int SteamAudio::ChannelCount(unsigned int order)
{
switch (order) {
case 1: return 4;
case 2: return 9;
case 3: return 16;
case 4: return 25;
case 5: return 36;
case 6: return 49;
default: return 0;
}
}
IPLSimulationFlags SteamAudio::SimulationFlags() const
{
int fres = IPL_SIMULATIONFLAGS_DIRECT;
if (simulateReflections_)
fres |= IPL_SIMULATIONFLAGS_REFLECTIONS;
return static_cast<IPLSimulationFlags>(fres);
}
void SteamAudio::HandleRenderUpdate(StringHash eventType, VariantMap &eventData)
{
using namespace RenderUpdate;
Update(eventData[P_TIMESTEP].GetFloat());
}
void SteamAudio::Release()
{
iplSimulatorRelease(&simulator_);
iplAudioBufferFree(phononContext_, &phononFrameBuffer_);
iplHRTFRelease(&hrtf_);
iplContextRelease(&phononContext_);
audioBufferPool_.reset();
finalFrameBuffer_.clear();
SDL_CloseAudio();
audioMutex_.Acquire();
}
void SDLSteamAudioCallback(void* userdata, Uint8 *stream, int)
{
auto* audio = static_cast<SteamAudio*>(userdata);
{
audio->MixOutput(reinterpret_cast<float*>(stream));
}
}
SteamAudioBufferPool::SteamAudioBufferPool(SteamAudio *audio) : audio_(audio), bufferIdx_(0)
{
const auto phononContext = audio_->GetPhononContext();
const auto& audioSettings = audio_->GetAudioSettings();
const auto channelCount = audio_->GetChannelCount();
for (auto& buffer : buffers_)
iplAudioBufferAllocate(phononContext, channelCount, audioSettings.frameSize, &buffer);
}
SteamAudioBufferPool::~SteamAudioBufferPool()
{
const auto phononContext = audio_->GetPhononContext();
for (auto& buffer : buffers_)
iplAudioBufferFree(phononContext, &buffer);
}
IPLAudioBuffer *SteamAudioBufferPool::GetCurrentBuffer()
{
return &buffers_[bufferIdx_];
}
IPLAudioBuffer *SteamAudioBufferPool::GetNextBuffer()
{
return &buffers_[GetNextBufferIndex()];
}
void SteamAudioBufferPool::SwitchToNextBuffer()
{
bufferIdx_ = GetNextBufferIndex();
}
unsigned int SteamAudioBufferPool::GetNextBufferIndex() const
{
auto bufferIdx = bufferIdx_;
if (++bufferIdx == buffers_.size())
bufferIdx = 0;
return bufferIdx;
}
}