Repository navigation
Expand file tree
/
Copy pathPlugin_PyBinSimSpatializerBatched_1_0.cpp
More file actions
600 lines (446 loc) · 23.9 KB
/
Copy pathPlugin_PyBinSimSpatializerBatched_1_0.cpp
File metadata and controls
600 lines (446 loc) · 23.9 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Please note that this will only work on Unity 5.2 or higher.
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include "AudioPluginUtil.h"
#include "LogUtils.h"
#include <zmq/zmq.hpp>
#include <Eigen/Dense>
#define MAX_NUM_CHANNELS 32
#define ENABLE_INSTANTIATION_LOGGING true
#define ENABLE_LOGGING false
typedef Eigen::Matrix<float, 4, 4, Eigen::RowMajor> Mat4f_RM;
typedef Eigen::Matrix<float, 4, 4, Eigen::ColMajor> Mat4f_CM;
constexpr float MAX_FIXED_DELAY_SEC = 3.f;
namespace PyBinSimSpatializerBatched
{
enum
{
P_AUDIOSRCATTN,
P_FIXEDVOLUME,
P_CUSTOMFALLOFF,
P_UTIL,
P_DELAY_SECONDS,
P_NUM
};
const float GAINCORRECTION = 2.0f;
const uint32_t poseDataElements = 3;
const float kRad2Deg = 180.0f / kPI;
// Ear and mouth offsets from head transform
const Mat4f_CM mouth_offset = Eigen::Affine3f(Eigen::Translation3f(0.f, -0.11f, -0.09f)).matrix();
const Mat4f_CM ear_offset_L = Eigen::Affine3f(Eigen::Translation3f(0.11f, -0.028f, 0.02f)).matrix();
const Mat4f_CM ear_offset_R = Eigen::Affine3f(Eigen::Translation3f(-0.11f, -0.028f, 0.02f)).matrix();
// The allocation of this must happen in the corresponding CreateCallback which is called for each instance of the plugin
struct EffectData
{
float p[P_NUM];
uint32_t instance_id;
std::unique_ptr<std::vector<float>> recv_buffer;
std::unique_ptr<zmq::mutable_buffer> recv_buffer_wrapper;
static uint32_t num_instances;
static uint32_t packets_sent_to_pybinsim_this_frame;
static uint64_t last_dsp_tick;
static std::unique_ptr<zmq::context_t> context;
static std::unique_ptr<zmq::socket_t> audio_socket;
static std::unique_ptr<std::vector<bool>> channel_occupied;
static std::size_t bufferLength;
static std::size_t messageSize;
// variables for fixed delay buffer
uint32_t buffers_chunks_available_for_delay = 0;
uint32_t buffer_slots_written = 0;
uint32_t write_buffer_slot = 0;
std::vector<float> fixed_delay_buffer;
};
struct SpatialData
{
float lst_to_src_azimuth = 0.f;
float lst_to_src_elevation = 0.f;
float src_to_lst_azimuth = 0.f;
float src_to_lst_elevation = 0.f;
float lst_to_src_dist = 0.f;
Mat4f_CM lst_transform;
Mat4f_CM src_transform;
float ear_L_to_mouth_azimuth = 0.f;
float ear_L_to_mouth_elevation = 0.f;
float ear_R_to_mouth_azimuth = 0.f;
float ear_R_to_mouth_elevation = 0.f;
float mouth_to_ear_L_azimuth = 0.f;
float mouth_to_ear_L_elevation = 0.f;
float mouth_to_ear_R_azimuth = 0.f;
float mouth_to_ear_R_elevation = 0.f;
float ear_L_to_mouth_dist = 0.f;
float ear_R_to_mouth_dist = 0.f;
};
uint32_t EffectData::num_instances = 0;
uint32_t EffectData::packets_sent_to_pybinsim_this_frame = 0;
uint64_t EffectData::last_dsp_tick = 0;
std::unique_ptr<zmq::context_t> EffectData::context = nullptr;
std::unique_ptr<zmq::socket_t> EffectData::audio_socket = nullptr;
std::unique_ptr<std::vector<bool>> EffectData::channel_occupied = std::make_unique<std::vector<bool>>(MAX_NUM_CHANNELS, false);
std::size_t EffectData::bufferLength = 512;
std::size_t EffectData::messageSize = bufferLength * 2 * sizeof(float);
uint32_t getFirstFreeChannel() {
for (size_t i = 0; i < MAX_NUM_CHANNELS; i++)
{
if (!(*EffectData::channel_occupied)[i]) {
(*EffectData::channel_occupied)[i] = true;
return i;
}
}
writeToLogFile("Max num channels reached");
throw std::runtime_error("Max num channels reached");
}
void InitZmq() {
#if ENABLE_INSTANTIATION_LOGGING
writeToLogFile("Init ZMQ");
#endif
EffectData::context = std::make_unique<zmq::context_t>(1);
// will send audio data and expect a reply (processed audio data)
EffectData::audio_socket = std::make_unique<zmq::socket_t>(*(EffectData::context), ZMQ_REQ);
EffectData::audio_socket->connect("tcp://127.0.0.1:12346");
EffectData::audio_socket->set(zmq::sockopt::rcvtimeo, 2000); // milliseconds
// just sends filter information for each sound source, no reply expected
//EffectData::filter_socket = std::make_unique<zmq::socket_t>(*(EffectData::context), ZMQ_PUB);
//EffectData::filter_socket->bind("tcp://127.0.0.1:12345");
#if ENABLE_INSTANTIATION_LOGGING
writeToLogFile("Init ZMQ Done");
#endif
}
void InitZmqBuffer(EffectData* effectdata) {
effectdata->recv_buffer = std::make_unique<std::vector<float>>(effectdata->bufferLength * 2);
effectdata->recv_buffer_wrapper = std::make_unique<zmq::mutable_buffer>(effectdata->recv_buffer->data(), effectdata->messageSize);
#if ENABLE_INSTANTIATION_LOGGING
writeToLogFile("Init ZMQ Buffer");
#endif
}
void CloseZmq() {
// tell pose parser to quit
uint32_t message_data[poseDataElements] = { 999, 0, 0 };
zmq::message_t msg(message_data, poseDataElements * sizeof(uint32_t));
//EffectData::filter_socket->send(msg, zmq::send_flags::none);
EffectData::audio_socket->close();
//EffectData::filter_socket->close();
EffectData::context->close();
#if ENABLE_INSTANTIATION_LOGGING
writeToLogFile("Closed ZMQ");
#endif
}
inline bool IsHostCompatible(UnityAudioEffectState* state)
{
// Somewhat convoluted error checking here because hostapiversion is only supported from SDK version 1.03 (i.e. Unity 5.2) and onwards.
// Since we are only checking for version 0x010300 here, we can't use newer fields in the UnityAudioSpatializerData struct, such as minDistance and maxDistance.
return
state->structsize >= sizeof(UnityAudioEffectState) &&
state->hostapiversion >= 0x010300;
}
int InternalRegisterEffectDefinition(UnityAudioEffectDefinition& definition)
{
int numparams = P_NUM;
definition.paramdefs = new UnityAudioParameterDefinition[numparams];
RegisterParameter(definition, "AudioSrc Attn", "", 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, P_AUDIOSRCATTN, "AudioSource distance attenuation");
RegisterParameter(definition, "Fixed Volume", "", 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, P_FIXEDVOLUME, "Fixed volume amount");
RegisterParameter(definition, "Custom Falloff", "", 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, P_CUSTOMFALLOFF, "Custom volume falloff amount (logarithmic)");
RegisterParameter(definition, "Utility", "", 0.f, 2000.f, 0.f, 1.f, 1.f, P_UTIL, "Utility float");
RegisterParameter(definition, "Fixed Delay", "seconds", 0.f, MAX_FIXED_DELAY_SEC, 0.f, 1.f, 1.f, P_DELAY_SECONDS, "Fixed delay applied to source in seconds");
definition.flags |= UnityAudioEffectDefinitionFlags_IsSpatializer;
definition.flags |= UnityAudioEffectDefinitionFlags_AppliesDistanceAttenuation;
return numparams;
}
static UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK DistanceAttenuationCallback(UnityAudioEffectState* state, float distanceIn, float attenuationIn, float* attenuationOut)
{
*attenuationOut = 1.0f;
return UNITY_AUDIODSP_OK;
}
// The UnityAudioEffectState contains various data from the host such as the sampling rate, the total number of samples processed (for timing),
// or whether the plugin is bypassed, and is passed to all callback functions.
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK CreateCallback(UnityAudioEffectState* state)
{
EffectData* effectdata = new EffectData;
memset(effectdata, 0, sizeof(EffectData));
const uint32_t new_instance_id = getFirstFreeChannel();
effectdata->instance_id = new_instance_id;
++EffectData::num_instances;
#if ENABLE_INSTANTIATION_LOGGING
writeToLogFile("CreateCallback");
writeValueToLogFile("new instance ID: ", effectdata->instance_id);
writeValueToLogFile("num_instances: ", EffectData::num_instances);
#endif
if (EffectData::num_instances == 1) {
//if (effectdata->instance_id == 0) {
EffectData::bufferLength = state->dspbuffersize;
EffectData::messageSize = EffectData::bufferLength * 2 * sizeof(float);
EffectData::packets_sent_to_pybinsim_this_frame = 0;
InitZmq();
}
InitZmqBuffer(effectdata);
// setup buffer for fixed delay
uint32_t sampleRate = state->samplerate;
uint32_t samplesForLongestDelayBuffer = uint32_t(sampleRate * MAX_FIXED_DELAY_SEC);
effectdata->buffers_chunks_available_for_delay = int(float(samplesForLongestDelayBuffer) / float(EffectData::bufferLength));
uint32_t delayBufferElements = effectdata->buffers_chunks_available_for_delay * EffectData::bufferLength * 2;
effectdata->fixed_delay_buffer = std::vector<float>(delayBufferElements, 0.f);
state->effectdata = effectdata;
if (IsHostCompatible(state))
state->spatializerdata->distanceattenuationcallback = DistanceAttenuationCallback;
InitParametersFromDefinitions(InternalRegisterEffectDefinition, effectdata->p);
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ReleaseCallback(UnityAudioEffectState* state)
{
EffectData* data = state->GetEffectData<EffectData>();
const uint32_t channel_to_free = data->instance_id;
(*EffectData::channel_occupied)[channel_to_free] = false;
#if ENABLE_INSTANTIATION_LOGGING
writeToLogFile("ReleaseCallback");
writeValueToLogFile("before release callback: num_instances = ", EffectData::num_instances);
#endif
--EffectData::num_instances;
if (EffectData::num_instances == 0) {
CloseZmq();
}
#if ENABLE_INSTANTIATION_LOGGING
writeValueToLogFile("after release callback: num_instances = ", EffectData::num_instances);
#endif
delete data;
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK SetFloatParameterCallback(UnityAudioEffectState* state, int index, float value)
{
EffectData* data = state->GetEffectData<EffectData>();
if (index >= P_NUM)
return UNITY_AUDIODSP_ERR_UNSUPPORTED;
data->p[index] = value;
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK GetFloatParameterCallback(UnityAudioEffectState* state, int index, float* value, char* valuestr)
{
EffectData* data = state->GetEffectData<EffectData>();
if (index >= P_NUM)
return UNITY_AUDIODSP_ERR_UNSUPPORTED;
if (value != NULL)
*value = data->p[index];
if (valuestr != NULL)
valuestr[0] = 0;
return UNITY_AUDIODSP_OK;
}
int UNITY_AUDIODSP_CALLBACK GetFloatBufferCallback(UnityAudioEffectState* state, const char* name, float* buffer, int numsamples)
{
return UNITY_AUDIODSP_OK;
}
void ZmqSendPoseData(zmq::socket_t& socket, uint32_t channel, uint32_t azimuth, uint32_t elevation) {
elevation = 0;
azimuth = uint32_t(round(float(azimuth) / 5.f) * 5);
uint32_t message_data[poseDataElements] = { channel, azimuth, elevation };
zmq::message_t msg(message_data, poseDataElements * sizeof(uint32_t));
socket.send(msg, zmq::send_flags::none);
}
void ZmqSendAudioForProcessing(EffectData* effectdata, zmq::socket_t& socket, float* dryBuffer, float* processedBuffer, const SpatialData& spatialData, const bool shouldApplySpatialization, const float spatialBlend) {
#if 1
// add channel as first sample of second audio channel, as only mono is needed anyway
dryBuffer[1] = float(effectdata->instance_id);
// add direction info also in second audio channel
dryBuffer[3] = spatialData.lst_to_src_azimuth;
dryBuffer[5] = spatialData.lst_to_src_elevation;
dryBuffer[7] = spatialData.src_to_lst_azimuth;
dryBuffer[9] = spatialData.src_to_lst_elevation;
dryBuffer[11] = spatialData.lst_to_src_dist;
// add matrices
// source starts element 13
// listener starts element 45
for (uint32_t i = 0; i < 16; i++)
{
// access elements in row-major order because numpy wants row major matrices
dryBuffer[13 + i * 2] = spatialData.src_transform.coeff(i / 4, i % 4);
dryBuffer[45 + i * 2] = spatialData.lst_transform.coeff(i / 4, i % 4);
}
// encode total number of active sources
dryBuffer[77] = float(EffectData::num_instances);
// mouth-ear info starts element 79
dryBuffer[79] = spatialData.ear_L_to_mouth_azimuth;
dryBuffer[81] = spatialData.ear_L_to_mouth_elevation;
dryBuffer[83] = spatialData.ear_R_to_mouth_azimuth;
dryBuffer[85] = spatialData.ear_R_to_mouth_elevation;
dryBuffer[87] = spatialData.mouth_to_ear_L_azimuth;
dryBuffer[89] = spatialData.mouth_to_ear_L_elevation;
dryBuffer[91] = spatialData.mouth_to_ear_R_azimuth;
dryBuffer[93] = spatialData.mouth_to_ear_R_elevation;
dryBuffer[95] = spatialData.ear_L_to_mouth_dist;
dryBuffer[97] = spatialData.ear_R_to_mouth_dist;
// tell pyBinSim whether to apply spatialization
//dryBuffer[99] = (shouldApplySpatialization ? 1.f : 0.f);
dryBuffer[99] = spatialBlend;
dryBuffer[101] = float(EffectData::packets_sent_to_pybinsim_this_frame);
// wrap dry buffer as const zmq buffer
zmq::const_buffer dryBufferWrapper((void*)dryBuffer, effectdata->messageSize);
socket.send(dryBufferWrapper, zmq::send_flags::none);
#else
// include azimuth & elevation in audio message
zmq::message_t msg(effectdata->messageSize + 2 * sizeof(float));
memcpy(msg.data(), &azimuth, sizeof(float));
memcpy((float*)(msg.data()) + 1, &elevation, sizeof(float));
memcpy((float*)(msg.data()) + 2, dryBuffer, effectdata->messageSize);
socket.send(msg, zmq::send_flags::none);
#endif
// wrap existing outbuffer as a mutable zmq buffer, use to receive message bytes
zmq::mutable_buffer processedBufferWrapper((void*)processedBuffer, effectdata->messageSize);
auto res = socket.recv(processedBufferWrapper, zmq::recv_flags::none);
// unfortunately this error is not handled gracefully and somehow results in a crash. Not sure what the cause is - have tried catching exceptions, to no avail.
//if (!res) {
//writeToLogFile("Error: No reply received");
//}
}
void ClearBuffer(float* buffer, unsigned int length) {
memset(buffer, 0, length * 2 * sizeof(float));
}
void getAzimuthAndElevationFromVector(const Eigen::Vector4f& vec, float& azimuth, float& elevation) {
// azimuth is rotation around Y
azimuth = (fabsf(vec[2]) < 0.001f) ? 0.0f : atan2f(vec[0], vec[2]);
if (azimuth < 0.0f)
azimuth += 2.0f * kPI;
azimuth = FastClip(azimuth * kRad2Deg, 0.0f, 360.0f);
elevation = atan2f(vec[1], sqrtf(vec[0] * vec[0] + vec[2] * vec[2]) + 0.001f) * kRad2Deg;
}
void getAzimuthAndElevationFromVector(float dir_x, float dir_y, float dir_z, float& azimuth, float& elevation) {
Eigen::Vector4f vec{ dir_x, dir_y, dir_z, 1.f };
getAzimuthAndElevationFromVector(vec, azimuth, elevation);
}
// check if matrix encodes a transformation with non-zero scale
bool matrixValid(const Mat4f_CM& mat) {
// return true if all diagonal elements are non-zero
return (mat(0, 0) != 0.f && mat(1, 1) != 0.f && mat(2, 2) != 0.f);
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ProcessCallback(UnityAudioEffectState* state, float* inbuffer, float* outbuffer, unsigned int length, int inchannels, int outchannels)
{
// Check that I/O formats are right and that the host API supports this feature
if (inchannels != 2 || outchannels != 2 ||
!IsHostCompatible(state) || state->spatializerdata == NULL)
{
memcpy(outbuffer, inbuffer, std::size_t(length) * std::size_t(outchannels) * sizeof(float));
return UNITY_AUDIODSP_OK;
}
EffectData* data = state->GetEffectData<EffectData>();
// SUPPORT FIXED DELAY ----------------------------------------------------
// note that delayed buffer chunk is written into 'inbuffer'
// write incoming data to buffer slot
uint32_t write_start_idx = data->write_buffer_slot * data->bufferLength * 2;
memcpy(&(data->fixed_delay_buffer[write_start_idx]), inbuffer, data->bufferLength * 2 * sizeof(float));
// calculate required amount of data to start playing out what has already been received
uint32_t requiredDelayInBufferSlots = uint32_t((state->samplerate * data->p[P_DELAY_SECONDS]) / data->bufferLength);
// if we don't have enough yet, keep counting
if (data->buffer_slots_written < requiredDelayInBufferSlots) {
++data->buffer_slots_written;
ClearBuffer(inbuffer, data->bufferLength);
}
// if we have enough data, copy it to output
else {
int32_t read_buffer_slot = data->write_buffer_slot - requiredDelayInBufferSlots;
if (read_buffer_slot < 0) {
read_buffer_slot += data->buffers_chunks_available_for_delay;
}
uint32_t read_start_idx = read_buffer_slot * data->bufferLength * 2;
memcpy(inbuffer, &(data->fixed_delay_buffer[read_start_idx]), data->bufferLength * 2 * sizeof(float));
}
++data->write_buffer_slot;
if (data->write_buffer_slot >= data->buffers_chunks_available_for_delay) {
data->write_buffer_slot = 0;
}
// END FIXED DELAY CODE ----------------------------------------------------
// reset the packet number when a new part of the DSP buffer is processed, i.e. when the DSP tick changes
uint64_t curr_dsp_tick = state->currdsptick;
if (curr_dsp_tick != EffectData::last_dsp_tick) {
EffectData::last_dsp_tick = curr_dsp_tick;
EffectData::packets_sent_to_pybinsim_this_frame = 0;
}
const bool shouldApplySpatialization = (state->spatializerdata->spatialblend > 0.1f);
const float spatialBlend = state->spatializerdata->spatialblend;
//float spatialBlend = state->spatializerdata->spatialblend;
//if (spatialBlend == 0.f) {
// CopyInputToOutput(inbuffer, outbuffer, length);
//}
//else {
// results of spatial calculations
SpatialData spatialData;
// unity provides inverse of audio listener's transformation matrix
Mat4f_CM lst_inv(state->spatializerdata->listenermatrix);
Mat4f_CM lst = Mat4f_CM(lst_inv).inverse();
Mat4f_CM src(state->spatializerdata->sourcematrix);
Mat4f_CM src_inv = Mat4f_CM(src).inverse();
// guard against null matrices caused by setting scale to 0
if (!matrixValid(lst_inv) || !matrixValid(src)) {
const uint32_t instance_id = data->instance_id;
writeToLogFile("Error: invalid matrix detected, not spatializing source (instance " + std::to_string(instance_id) + ")");
memcpy(outbuffer, inbuffer, std::size_t(length) * std::size_t(outchannels) * sizeof(float));
return UNITY_AUDIODSP_OK;
}
spatialData.lst_transform = Mat4f_CM(lst);
spatialData.src_transform = Mat4f_CM(src);
// get respective world positions of source and listener
Eigen::Vector4f src_wp = src.col(3);
Eigen::Vector4f lst_wp = lst.col(3);
spatialData.lst_to_src_dist = (src_wp - lst_wp).norm();
Eigen::Vector4f src_in_lst_coords = lst_inv * src_wp;
Eigen::Vector4f lst_in_src_coords = src_inv * lst_wp;
// get ear and mouth positions
const Mat4f_CM ear_transform_L = lst * ear_offset_L;
const Mat4f_CM ear_transform_R = lst * ear_offset_R;
const Mat4f_CM mouth_transform = src * mouth_offset;
const Eigen::Vector4f ear_L_wp = ear_transform_L.col(3);
const Eigen::Vector4f ear_R_wp = ear_transform_R.col(3);
const Eigen::Vector4f mouth_wp = mouth_transform.col(3);
const Eigen::Vector4f mouth_in_ear_L_coords = ear_transform_L.inverse() * mouth_wp;
const Eigen::Vector4f mouth_in_ear_R_coords = ear_transform_R.inverse() * mouth_wp;
const Eigen::Vector4f ear_L_in_mouth_coords = mouth_transform.inverse() * ear_L_wp;
const Eigen::Vector4f ear_R_in_mouth_coords = mouth_transform.inverse() * ear_R_wp;
/*writeVectorToLogFile("listener in world space", lst_wp.data(), 4);
writeVectorToLogFile("ear L in world space", ear_L_wp.data(), 4);
writeVectorToLogFile("ear R in world space", ear_R_wp.data(), 4);*/
// calculate angle and elevation
getAzimuthAndElevationFromVector(src_in_lst_coords, spatialData.lst_to_src_azimuth, spatialData.lst_to_src_elevation);
getAzimuthAndElevationFromVector(lst_in_src_coords, spatialData.src_to_lst_azimuth, spatialData.src_to_lst_elevation);
// calculate mouth/ear output angles and distances:
getAzimuthAndElevationFromVector(mouth_in_ear_L_coords, spatialData.ear_L_to_mouth_azimuth, spatialData.ear_L_to_mouth_elevation);
getAzimuthAndElevationFromVector(mouth_in_ear_R_coords, spatialData.ear_R_to_mouth_azimuth, spatialData.ear_R_to_mouth_elevation);
getAzimuthAndElevationFromVector(ear_L_in_mouth_coords, spatialData.mouth_to_ear_L_azimuth, spatialData.mouth_to_ear_L_elevation);
getAzimuthAndElevationFromVector(ear_R_in_mouth_coords, spatialData.mouth_to_ear_R_azimuth, spatialData.mouth_to_ear_R_elevation);
spatialData.ear_L_to_mouth_dist = (mouth_wp - ear_L_wp).norm();
spatialData.ear_R_to_mouth_dist = (mouth_wp - ear_R_wp).norm();
#if ENABLE_LOGGING
writeMatrixToLogFile("listener", lst.data(), 4, 4);
writeMatrixToLogFile("source", src.data(), 4, 4);
writeVectorToLogFile("source in world space", src_wp.data(), 4);
writeVectorToLogFile("listener in world space", lst_wp.data(), 4);
writeVectorToLogFile("source in listener space", src_in_lst_coords.data(), 3);
writeVectorToLogFile("listener in source space", lst_in_src_coords.data(), 3);
writeValueToLogFile("lst_to_src_azimuth", spatialData.lst_to_src_azimuth);
writeValueToLogFile("lst_to_src_elevation", spatialData.lst_to_src_elevation);
writeValueToLogFile("src_to_lst_azimuth", spatialData.src_to_lst_azimuth);
writeValueToLogFile("src_to_lst_elevation", spatialData.src_to_lst_elevation);
writeValueToLogFile("lst_to_src distance", spatialData.lst_to_src_dist);
auto start = std::chrono::steady_clock::now();
#endif
ZmqSendAudioForProcessing(data, *(EffectData::audio_socket), inbuffer, outbuffer, spatialData, shouldApplySpatialization, spatialBlend);
++EffectData::packets_sent_to_pybinsim_this_frame;
// if all instances have sent their audio data, receive the processed audio data
// otherwise clear the output buffer as it will not be used
if (EffectData::packets_sent_to_pybinsim_this_frame < EffectData::num_instances) {
//EffectData::packets_sent_to_pybinsim_this_frame = 0;
//ZmqReceiveAudioAfterProcessing(data, *(EffectData::audio_socket), outbuffer);
//}
//else {
ClearBuffer(outbuffer, length);
}
// TODO handle blending case
/*if (spatialBlend < 1.f) {
}*/
#if ENABLE_LOGGING
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
float const elapsed_micro_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
writeToLogFile("Elapsed: " + std::to_string(elapsed_micro_seconds) + " us");
writeValueToLogFile("Length", length);
#endif
//}
return UNITY_AUDIODSP_OK;
}
}