-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVBAP3DTest.cpp
More file actions
940 lines (773 loc) · 28.8 KB
/
Copy pathVBAP3DTest.cpp
File metadata and controls
940 lines (773 loc) · 28.8 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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//
// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
// ██║██████╔╝ ██║ ██║██████╔╝███████╗
// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
//
// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "JPLSpatial/Algo/Algorithm.h"
#include "JPLSpatial/Math/Math.h"
#include "JPLSpatial/Math/MinimalVec3.h"
#include "JPLSpatial/Math/MinimalQuat.h"
#include "JPLSpatial/Memory/Memory.h"
#include "JPLSpatial/Panning/PannerBase.h"
#include "JPLSpatial/Panning/VBAPanning2D.h"
#include "JPLSpatial/Panning/VBAPanning3D.h"
#include "JPLSpatial/Panning/VBAPEx.h"
#include "JPLSpatial/ChannelMap.h"
#include "JPLSpatial/Math/Vec3Math.h"
#include "JPLSpatial/Math/MinimalBasis.h"
#include "JPLSpatial/Math/MinimalQuat.h"
#include "../Utility/TestUtils.h"
#include <gtest/gtest.h>
#include <numbers>
#include <algorithm>
#include <format>
#include <sstream>
#include <set>
#include <concepts>
#include <memory>
#include <ranges>
#include "../Utility/Vec3Types.h"
// The test takes quite a while,
// this flag is to skp it temporarily
#define JPL_ENABLE_LUT_VALIDATION_TEST 0
namespace JPL
{
static constexpr auto toRad(std::floating_point auto degree) { return static_cast<decltype(degree)>(degree * (std::numbers::pi / 180.0)); };
static constexpr auto toDegrees(std::floating_point auto rads) { return static_cast<decltype(rads)>(rads * (180.0 / std::numbers::pi)); };
class VBAP3DTest : public testing::Test
{
protected:
using Vec3i = SpeakerTriangulation::Vec3i;
using Vec3 = MinimalVec3;
static constexpr float epsilon = 1e-6f;
protected:
VBAP3DTest() = default;
void SetUp() override
{
}
void TearDown() override
{
}
protected:
const std::vector<NamedChannelMask> mAllChannelMapLayouts
{
{ ChannelMask::Invalid },
//========================================
{ ChannelMask::Mono },
{ ChannelMask::Stereo },
{ ChannelMask::LCR },
{ ChannelMask::LRS },
{ ChannelMask::LCRS },
{ ChannelMask::Quad },
{ ChannelMask::Octagonal },
//========================================
{ ChannelMask::Surround_4_1 },
{ ChannelMask::Surround_5_0 },
{ ChannelMask::Surround_5_1 },
{ ChannelMask::Surround_6_0 },
{ ChannelMask::Surround_6_1 },
//========================================
// DTS surround setups
{ ChannelMask::Surround_7_0 },
{ ChannelMask::Surround_7_1 },
//========================================
{ ChannelMask::Surround_5_0_2 },
{ ChannelMask::Surround_5_1_2 },
{ ChannelMask::Surround_5_0_4 },
{ ChannelMask::Surround_5_1_4 },
//========================================
// Dolby Atmos surround setups
{ ChannelMask::Surround_7_0_2 },
{ ChannelMask::Surround_7_1_2 },
{ ChannelMask::Surround_7_0_4 },
{ ChannelMask::Surround_7_1_4 },
{ ChannelMask::Surround_7_0_6 },
{ ChannelMask::Surround_7_1_6 },
//========================================
// Atmos surround setups
{ ChannelMask::Surround_9_0_4 },
{ ChannelMask::Surround_9_1_4 },
{ ChannelMask::Surround_9_0_6 },
{ ChannelMask::Surround_9_1_6 }
};
const std::vector<NamedChannelMask> mValid3DLayouts
{
//========================================
{ ChannelMask::Surround_5_0_2 },
{ ChannelMask::Surround_5_1_2 },
{ ChannelMask::Surround_5_0_4 },
{ ChannelMask::Surround_5_1_4 },
//========================================
// Dolby Atmos surround setups
{ ChannelMask::Surround_7_0_2 },
{ ChannelMask::Surround_7_1_2 },
{ ChannelMask::Surround_7_0_4 },
{ ChannelMask::Surround_7_1_4 },
{ ChannelMask::Surround_7_0_6 },
{ ChannelMask::Surround_7_1_6 },
//========================================
// Atmos surround setups
{ ChannelMask::Surround_9_0_4 },
{ ChannelMask::Surround_9_1_4 },
{ ChannelMask::Surround_9_0_6 },
{ ChannelMask::Surround_9_1_6 }
};
bool IsValid3DLayout(ChannelMap channelLayout) const
{
return std::ranges::find_if(mValid3DLayouts, [channelLayout](const NamedChannelMask& chl)
{
return channelLayout == chl.Layout;
}) != std::ranges::end(mValid3DLayouts);
}
const std::vector<NamedChannelMask> mValidSourceLayouts
{
//========================================
{ ChannelMask::Mono },
{ ChannelMask::Stereo },
{ ChannelMask::LCR },
{ ChannelMask::LRS },
{ ChannelMask::LCRS },
{ ChannelMask::Quad },
{ ChannelMask::Octagonal },
//========================================
{ ChannelMask::Surround_4_1 },
{ ChannelMask::Surround_5_0 },
{ ChannelMask::Surround_5_1 },
{ ChannelMask::Surround_6_0 },
{ ChannelMask::Surround_6_1 },
//========================================
// DTS surround setups
{ ChannelMask::Surround_7_0 },
{ ChannelMask::Surround_7_1 },
};
};
TEST_F(VBAP3DTest, VBAPData_AllSupportedLayoutsInitialize)
{
for (const auto& [targetLayoutName, targetLayoutMap] : mValid3DLayouts)
{
VBAPanner3D<> panner;
ASSERT_TRUE(panner.InitializeLUT(targetLayoutMap)) << targetLayoutName;
for (const auto& [sourceLayoutName, sourceLayoutMap] : mValidSourceLayouts)
{
typename VBAPanner3D<>::SourceLayoutType sourceLayout;
EXPECT_TRUE(panner.InitializeSourceLayout(sourceLayoutMap, sourceLayout)) << sourceLayoutName;
}
}
}
#if JPL_USE_SIMPLE_SPEAKER_MESHING
TEST_F(VBAP3DTest, TriangleCrossing)
{
const std::vector<Vec3> channelVectors
{
{ 0.0f, 0.0f, 1.0f }, // forward
{ -1.0f, 0.0f, 0.0f }, // left
{ 1.0f, 0.0f, 0.0f }, // right
{ 0.0f, 0.7071f, -0.7071f }, // forward up
{ 0.0f, -0.7071f, 0.7071f }, // backward down
};
// forward-left-right
// forward-forward_up-backward_down
const Vec3i triA{ 0, 1, 2 };
const Vec3i triB{ 0, 3, 4 };
//? Not sure why these are considered "non crossing", but in the paper they are, need to clarify this
const int crossing = SpeakerTriangulation::TestCrossing(triA, triB, channelVectors);
EXPECT_EQ(crossing, 0);
}
//==========================================================================
TEST_F(VBAP3DTest, TriangleCrossing_ExtraCases)
{
constexpr float s = 0.70710678f; // √½
const std::vector<Vec3> channelVectors = {
/* 0 */ { 1.0f, 0.0f, 0.0f }, // Right
/* 1 */ { -1.0f, 0.0f, 0.0f }, // Left
/* 2 */ { 0.0f, 1.0f, 0.0f }, // Up
/* 3 */ { 0.0f, -1.0f, 0.0f }, // Down
/* 4 */ { 0.0f, 0.0f, -1.0f }, // Forward
/* 5 */ { 0.0f, 0.0f, 1.0f }, // Backward
/* 6 */ { s, 0.0f, -s }, // Front-Right
/* 7 */ { -s, 0.0f, -s }, // Front-Left
/* 8 */ { s, 0.0f, s }, // Back-Right
/* 9 */ { -s, 0.0f, s }, // Back-Left
/*10 */ { s, s, 0.0f } // Right-Up
};
// 1. Baseline – no crossing (single shared vertex)
{
Vec3i triA{ 0, 1, 4 };
Vec3i triB{ 0, 2, 5 };
EXPECT_EQ(SpeakerTriangulation::TestCrossing(triA, triB, channelVectors), 0);
}
// 2. Genuine crossing
{
Vec3i triA{ 0, 2, 4 };
Vec3i triB{ 1, 3, 6 };
EXPECT_NE(SpeakerTriangulation::TestCrossing(triA, triB, channelVectors), 0);
}
// 3. False-positive trap: great-circle intersection is outside both minor arcs
{
Vec3i triA{ 1, 7, 4 }; // Left, Front-Left, Forward
Vec3i triB{ 6,10, 3 }; // Front-Right, Right-Up, Down
EXPECT_EQ(SpeakerTriangulation::TestCrossing(triA, triB, channelVectors), 0);
}
// 4. Shared edge – adjacent faces
{
Vec3i triA{ 0, 4, 2 };
Vec3i triB{ 4, 0, 3 };
EXPECT_EQ(SpeakerTriangulation::TestCrossing(triA, triB, channelVectors), 0);
}
}
#endif // JPL_USE_SIMPLE_SPEAKER_MESHING
TEST_F(VBAP3DTest, TriangulateSpeakerLayout)
{
// Utility to generate constexpr speaker mesh data for the common speaker setups.
// Only for development purposes.
static auto printEmbedding = [](std::span<const Vec3> verts, std::span<const Vec3i> faces, std::string_view title)
{
// Printer can be swapped, e.g. in case we want to print for file
Printer<PrintToCout> printer;
auto printFloat = [](float value) -> std::string
{
return value == 0.0f ? "0.0000f" : std::format("{:.4f}f", value);
};
printer.PrintLine(std::format("static constexpr Data {} = {{", title));;
printer.PrintLine("\tstd::to_array({");
for (auto i = 0; i < verts.size(); ++i)
{
const auto& vert = verts[i];
printer.PrintLine(std::format("\t\tVec3{{ {}, {}, {} {}"
, printFloat(vert.X)
, printFloat(vert.Y)
, printFloat(vert.Z)
, (i == verts.size() - 1 ? "}" : "},")));
}
printer.PrintLine("\t}),");
printer.PrintLine("\tstd::to_array({");
for (auto i = 0; i < faces.size(); ++i)
{
const auto& face = faces[i];
printer.PrintLine(std::format("\t\tVec3i{{ {}, {}, {} {}"
, face[0]
, face[1]
, face[2]
, (i == faces.size() - 1 ? " }" : " },")));
}
printer.PrintLine("\t})");
printer.PrintLine("};");
printer.PrintLine("");
};
auto runSpekerMeshTest = [this](const NamedChannelMask& test)
{
SCOPED_TRACE(test.Name);
std::pmr::vector<Vec3> vertices(GetDefaultMemoryResource());
std::pmr::vector<Vec3i> triangles(GetDefaultMemoryResource());
const bool bSuccess = SpeakerTriangulation::TriangulateSpeakerLayout<&VBAPStandartTraits::GetChannelVector, Vec3>(test.Layout, vertices, triangles);
if (IsValid3DLayout(test.Layout))
{
EXPECT_TRUE(bSuccess);
}
else
{
EXPECT_FALSE(bSuccess);
return;
}
#if 0 // Debugging
if (bSuccess)
{
SaveOBJ(vertices, triangles, std::format("{}.obj", test.Name));
printEmbedding(vertices, triangles, test.Name);
}
#endif
};
for (const NamedChannelMask& test : mAllChannelMapLayouts)
runSpekerMeshTest(test);
}
#if JPL_ENABLE_LUT_VALIDATION_TEST
TEST_F(VBAP3DTest, VBAP3D_BuildsValidLUT)
{
auto testLUTFor = [this](ChannelMap targetLayout)
{
using PannerType = VBAPanner3D<>;
using CodecType = typename PannerType::PanType::LUTCodec;
PannerType panner;
if (!IsValid3DLayout(targetLayout))
{
EXPECT_FALSE(panner.InitializeLUT(targetLayout));
return;
}
ASSERT_TRUE(panner.InitializeLUT(targetLayout));
const auto* LUT = panner.GetLUT();
ASSERT_TRUE(LUT != nullptr);
size_t invalidLUTCells = 0;
auto isValidIndex = [](uint32 i)
{
CodecType::EncodedType dx = i & CodecType::cAxisMask;
CodecType::EncodedType dy = (i >> CodecType::cBitsPerAxis) & CodecType::cAxisMask;
return dx != CodecType::cAxisMask && dy != CodecType::cAxisMask;
};
for (uint32 i = 0; i < LUT->Speakers.size(); ++i)
{
if (!isValidIndex(i))
continue; // skip padded cells that has no direction
const auto& speakers = LUT->Speakers[i];
const bool bDuplicateSpeakers =
speakers[0] == speakers[1] ||
speakers[1] == speakers[2] ||
speakers[2] == speakers[0];
const Vec3 direction = CodecType::Decode<Vec3>(i);
EXPECT_FALSE(bDuplicateSpeakers)
<< std::format("Found duplicate speakers {{{}, {}, {}}} in triplet for LUT index [{}], direction: {}, {}, {}",
speakers[0], speakers[1], speakers[2],
i,
direction.X, direction.Y, direction.Z);
invalidLUTCells += bDuplicateSpeakers;
}
EXPECT_TRUE(invalidLUTCells == 0) << std::format("LUT contains {}/{} invalid speaker triplets", invalidLUTCells, LUT->Speakers.size());
invalidLUTCells = 0;
for (uint32 i = 0; i < LUT->Gains.size(); ++i)
{
if (!isValidIndex(i))
continue; // skip padded cells that has no direction
const auto& gains = LUT->Gains[i];
const Vec3 direction = CodecType::Decode<Vec3>(i);
const bool bGainNearlyZero = Math::IsNearlyZero(gains[0] + gains[1] + gains[2]);
const bool bHasNegativeGains = gains[0] < 0.0f || gains[1] < 0.0f || gains[2] < 0.0f;
EXPECT_FALSE(bGainNearlyZero || bHasNegativeGains)
<< std::format("Invalid gains {{{}, {}, {}}} for speaker triplet for LUT index [{}], direction: {}, {}, {}",
gains[0], gains[1], gains[2],
i,
direction.X, direction.Y, direction.Z);
invalidLUTCells += (bGainNearlyZero || bHasNegativeGains);
}
EXPECT_TRUE(invalidLUTCells == 0) << std::format("LUT contains {}/{} invalid speaker gains", invalidLUTCells, LUT->Gains.size());
};
for (const NamedChannelMask& test : mAllChannelMapLayouts)
{
SCOPED_TRACE(test.Name);
testLUTFor(test.Layout);
}
}
#endif
TEST_F(VBAP3DTest, PanningNoRoll)
{
// Testing channel rotation quat/basis math
// canonical centre of channel ring
static constexpr Vec3 FWD = { 0, 0, -1 }; // engine forwardm change to +Z if needed
static constexpr Vec3 UP = { 0, 1, 0 };
static constexpr float yawRad = std::numbers::pi_v<float> *0.5f;
const std::vector<Vec3> testPanDiections
{
Vec3(1.0f, 0.0f, 0.0f),
Vec3(-1.0f, 0.0f, 0.0f),
Vec3(0.0f, 1.0f, 0.0f),
Vec3(0.0f, -1.0f, 0.0f),
Vec3(0.0f, 0.0f, 1.0f),
Vec3(0.0f, 0.0f, -1.0f),
Vec3(0.0f, 1.0f, 1.0f),
Vec3(0.0f, 1.0f, -1.0f),
Vec3(0.0f, -1.0f, 1.0f),
Vec3(0.0f, -1.0f, -1.0f),
Vec3(1.0f, 1.0f, 0.0f),
Vec3(1.0f, -1.0f, 0.0f),
Vec3(-1.0f, 1.0f, 0.0f),
Vec3(-1.0f, -1.0f, 0.0f),
Vec3(1.0f, 0.0f, 1.0f),
Vec3(1.0f, 0.0f, -1.0f),
Vec3(-1.0f, 0.0f, 1.0f),
Vec3(-1.0f, 0.0f, -1.0f),
Vec3(1.0f, 1.0f, 1.0f),
Vec3(1.0f, 1.0f, -1.0f),
Vec3(1.0f, -1.0f, 1.0f),
Vec3(1.0f, -1.0f, -1.0f),
Vec3(-1.0f, 1.0f, 1.0f),
Vec3(-1.0f, 1.0f, -1.0f),
Vec3(-1.0f, -1.0f, 1.0f),
Vec3(-1.0f, -1.0f, -1.0f),
};
// Steereo source channels spread left and rigth
auto qYawL = Quat<Vec3>::Rotation(UP, -yawRad); // left (world-Y)
auto qYawR = Quat<Vec3>::Rotation(UP, +yawRad); // right
for (const Vec3& panDir : testPanDiections)
{
// Normalizing here instead of on construction to be able
// to print more readable `panDir` in case of test fail
const auto qPan = Math::QuatLookAt(Normalized(panDir), UP);
const Quat<Vec3> qL = (qPan * qYawL);
const Quat<Vec3> qR = (qPan * qYawR);
const Vec3 L = qL.Rotate(FWD);
const Vec3 R = qR.Rotate(FWD);
// L.Y and R.Y must be the roughly equal if things working correctly
EXPECT_NEAR(L.Y, R.Y, 1e-6f)
<< std::format("Pan direction: {}, {}, {}", panDir.X, panDir.Y, panDir.Z);
}
}
TEST_F(VBAP3DTest, VBAPPannedGainsL2Normalized)
{
VBAPanner3D<> panner;
using ParametersType = typename VBAPanner3D<>::PanUpdateData;
const std::vector<ChannelMap> testSourceChannelMaps
{
ChannelMap::FromChannelMask(ChannelMask::Mono),
ChannelMap::FromChannelMask(ChannelMask::Stereo),
ChannelMap::FromChannelMask(ChannelMask::LCR),
ChannelMap::FromChannelMask(ChannelMask::Quad),
ChannelMap::FromChannelMask(ChannelMask::Surround_5_0),
ChannelMap::FromChannelMask(ChannelMask::Surround_6_0),
ChannelMap::FromChannelMask(ChannelMask::Surround_7_0),
ChannelMap::FromChannelMask(ChannelMask::Octagonal),
// layouts with LFE
ChannelMap::FromChannelMask(ChannelMask::Surround_4_1),
ChannelMap::FromChannelMask(ChannelMask::Surround_5_1),
ChannelMap::FromChannelMask(ChannelMask::Surround_6_1),
ChannelMap::FromChannelMask(ChannelMask::Surround_7_1),
};
const std::vector<Vec3> testDirection
{
Vec3(1, 0, 0),
Vec3(-1, 0, 0),
Vec3(0, 1, 0),
Vec3(0, -1, 0),
Vec3(0, 0, 1),
Vec3(0, 0, -1),
};
const std::vector<float> testFocus
{
0.0f,
0.5f,
1.0f
};
const std::vector<float> testSpread
{
0.0f,
0.25f,
0.5f,
0.75f,
1.0f
};
auto forEachTestParam = [&](auto testBody)
{
for (const ChannelMap& channelMap : testSourceChannelMaps)
{
for (const Vec3& direction : testDirection)
{
for (float focus : testFocus)
{
for (float spread : testSpread)
{
testBody(channelMap,
ParametersType{
.SourceDirection = direction,
.Focus = focus,
.Spread = spread
});
}
}
}
}
};
static auto paramsToString = [](const ParametersType& params)
{
return std::format("SourceDireciton = {}, Focus = {}, Spread = {}",
(std::stringstream() << params.SourceDirection).str(),
params.Focus,
params.Spread);
};
for (const NamedChannelMask& test : mValid3DLayouts)
{
SCOPED_TRACE(std::format("Target Speaker Layout: {}", test.Name));
if (!test.Layout.IsValid() || test.Layout.GetNumChannels() < 2)
continue;
// TODO: for now we skip large target channel count layouts,
// because they have tiny min aperture, which requires > max samples of virtual sources
// to cover the space without holes.
//! this may be fixed when we skip center channel. Maybe "wides" as well?
// We could also ditrch the RountUpBy4 for number of rings per source channel,
// and keep at least 2
if (test.Layout.GetNumChannels() >= 13)
continue;
ASSERT_TRUE(panner.InitializeLUT(test.Layout));
auto testResultingGainSum = [&panner](
const ChannelMap& sourceChannels,
const ParametersType& params)
{
typename VBAPanner3D<>::SourceLayoutType data;
ASSERT_TRUE(panner.InitializeSourceLayout(sourceChannels, data));
//const uint32 numTargetChannels = panner.GetNumChannels();
const uint32 numChannels = sourceChannels.GetNumChannels() - sourceChannels.HasLFE();
std::array<float, VBAPStandartTraits::MAX_CHANNEL_MIX_MAP_SIZE> gainsData;
std::span<float> gains(gainsData.data(), numChannels * panner.GetNumChannels());
std::ranges::fill(gains, 0.0f);
panner.ProcessVBAPData(data, params, gains);
SCOPED_TRACE(std::format("Source number of channels: {} | Parameters: {}",
numChannels, paramsToString(params)));
typename VBAPStandartTraits::ChannelGains accumulatedGains;
accumulatedGains.fill(0.0f);
// Accumulate per output channel gains
for (uint32 i = 0; i < numChannels * panner.GetNumChannels(); i += panner.GetNumChannels())
{
std::span<const float> channelGains = gains.subspan(i, panner.GetNumChannels());
for (uint32 outChannelI = 0; outChannelI < channelGains.size(); ++outChannelI)
accumulatedGains[outChannelI] += channelGains[outChannelI];
}
// Overall loudness of the output channels has to be near 1
const float gainSumNorm = Accumulate(accumulatedGains, 0.0f, Algo::AccPow2<float>{});
EXPECT_NEAR(gainSumNorm, 1.0f, 2e-6f);
/*if (Math::IsNearlyEqual(params.SourceDirection, Vec3(0, 0, 1)))
{
speakerVis.SetLayout(panner.GetChannelMap());
speakerVis.AddChannelGains(sourceChannelGains);
SaveToJSON(sourceVis.Points, speakerVis.Points, "visdata.json");
}*/
};
forEachTestParam(testResultingGainSum);
}
}
TEST_F(VBAP3DTest, ProcessVirtualSources)
{
static constexpr auto targetChannelMap = ChannelMap::FromChannelMask(ChannelMask::Surround_5_0_2);
static constexpr auto targetChannelCount = targetChannelMap.GetNumChannels();
struct VirtualSourcesTestCase
{
std::string Description;
std::vector<float> VirtualSourceAnglesDegrees;
std::array<float, targetChannelCount> ExpectedGains;
};
// Testing for Quadraphonic output channel map
const std::vector<VirtualSourcesTestCase> testCases =
{
{
"Single virtual source at 0 degrees",
{ 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
},
{
"Multiple virtual sources at 0 degrees",
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
},
{
"Virtual sources at speaker angles",
{ -45.0f, 45.0f, -135.0f, 135.0f },
{ 0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.0f }
}
};
VBAPanner3D<> panner;
static constexpr auto tolerance = static_cast<float>(VBAPanner3D<>::PanType::LUTCodec::cMaxComponentError);
for (const auto& testCase : testCases)
{
SCOPED_TRACE(testCase.Description);
ASSERT_TRUE(panner.InitializeLUT(targetChannelMap));
// Prepare virtual sources
std::vector<typename VBAPanner3D<>::VirtualSource> virtualSources;
const float weight = 1.0f / testCase.VirtualSourceAnglesDegrees.size();
for (float angleDeg : testCase.VirtualSourceAnglesDegrees)
{
const float rad = toRad(angleDeg);
virtualSources.push_back({ Vec3{ std::sin(rad), 0.0f, -std::cos(rad)}, weight });
}
std::vector<float> outGains(panner.GetNumChannels(), 0.0f);
panner.ProcessVirtualSources(virtualSources, outGains);
ASSERT_EQ(outGains.size(), testCase.ExpectedGains.size());
for (size_t i = 0; i < outGains.size(); ++i)
{
EXPECT_NEAR(outGains[i], testCase.ExpectedGains[i], tolerance) << "at index " << i;
}
}
}
TEST_F(VBAP3DTest, ProcessVBAPData)
{
static constexpr auto targetChannelMap = ChannelMap::FromChannelMask(ChannelMask::Surround_5_0_2);
static constexpr auto targetChannelCount = targetChannelMap.GetNumChannels();
// TODO: At Focus 1, spread 1 (or 0.5?), channel mapping should be direct from source to output, or do we still want to spread evenly?
using PannerType = VBAPanner3D<>;
PannerType panner;
// Quadraphonic channel layout
ASSERT_TRUE(panner.InitializeLUT(targetChannelMap));
struct ProcessVBAPDataTestCase
{
std::string Description;
float PanAngleDegrees;
float Spread;
float Focus;
std::array<float, targetChannelCount> ExpectedGains; // Expected gains for the ChannelGroup's gains
};
const std::vector<ProcessVBAPDataTestCase> testCases = {
{
.Description = "PanAngle 0 degrees",
.PanAngleDegrees = 0.0f,
.Spread = 1.0f,
.Focus = 1.0f,
.ExpectedGains = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
},
{
.Description = "PanAngle 90 degrees",
.PanAngleDegrees = 90.0f,
.Spread = 1.0f,
.Focus = 1.0f,
.ExpectedGains = { 0.0f, 0.7071f, 0.0f, 0.0f, 0.7071f, 0.0f, 0.0f }
},
{
.Description = "PanAngle -90 degrees",
.PanAngleDegrees = -90.0f,
.Spread = 1.0f,
.Focus = 1.0f,
.ExpectedGains = { 0.7071f, 0.0f, 0.0f, 0.7071f, 0.0f, 0.0f, 0.0f }
},
{
.Description = "PanAngle 180 degrees",
.PanAngleDegrees = 180.0f,
.Spread = 1.0f,
.Focus = 1.0f,
.ExpectedGains = { 0.0f, 0.0f, 0.0f, 0.7071f, 0.7071f, 0.0f, 0.0f }
},
{
.Description = "PanAngle 45 degrees",
.PanAngleDegrees = 45.0f,
.Spread = 1.0f,
.Focus = 1.0f,
.ExpectedGains = { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }
}
// Add more tests if needed...
};
static constexpr auto tolerance = static_cast<float>(VBAPanner3D<>::PanType::LUTCodec::cMaxComponentError);
for (const auto& testCase : testCases)
{
SCOPED_TRACE(testCase.Description);
typename PannerType::SourceLayoutType data;
// 1 channel group with 2 virtual sources at positions { -90, 90 } in radians
ASSERT_TRUE(panner.InitializeSourceLayout(ChannelMap::FromNumChannels(1), data));
const float panRad = toRad(testCase.PanAngleDegrees);
typename PannerType::PanUpdateData positionData
{
.SourceDirection = Vec3{ std::sin(panRad), 0.0f, -std::cos(panRad) } ,
.Focus = testCase.Focus,
.Spread = testCase.Spread
};
typename VBAPStandartTraits::ChannelGains gains;
gains.fill(0.0f);
panner.ProcessVBAPData(data, positionData, std::span(gains.data(), panner.GetNumChannels()));
ASSERT_TRUE(testCase.ExpectedGains.size() <= gains.size());
for (size_t i = 0; i < testCase.ExpectedGains.size(); ++i)
{
EXPECT_NEAR(gains[i], testCase.ExpectedGains[i], tolerance) << "at index " << i;
}
}
{
PannerType pannerB;
pannerB.InitializeLUT(ChannelMap::FromChannelMask(ChannelMask::Surround_9_0_4));
SCOPED_TRACE("Mono source in front, 9.0.4 target, equal gains LR");
static constexpr uint32 numSourceChannels = 1;
typename PannerType::SourceLayoutType data;
// 1 channel group with 2 virtual sources at positions { -90, 90 } in radians
ASSERT_TRUE(pannerB.InitializeSourceLayout(ChannelMap::FromNumChannels(numSourceChannels), data));
typename PannerType::PanUpdateData positionData
{
.SourceDirection = Vec3{ 0.0, 0.0, -1.0 },
.Focus = 0.0f,
.Spread = 0.5f
};
StaticArray<float, VBAPStandartTraits::MAX_CHANNEL_MIX_MAP_SIZE> gains(numSourceChannels * pannerB.GetNumChannels(), 0.0f);
const uint32 numOutputChannels = pannerB.GetNumChannels();
pannerB.ProcessVBAPData(data, positionData, gains);
static constexpr float gainToleranse = 1e-4f;
// Expect left and right channel to have equal gains
EXPECT_NEAR(gains[0], gains[1], gainToleranse);
// Expect side left and side right to have equal gains
EXPECT_NEAR(gains[3], gains[4], gainToleranse);
// Expect rear left and rear right to have equal gains
EXPECT_NEAR(gains[5], gains[6], gainToleranse);
}
}
TEST_F(VBAP3DTest, WorksWithDifferentVec3Types)
{
//static constexpr auto sourceChannelMap = ChannelMap::FromChannelMask(ChannelMask::Quad);
static constexpr auto targetChannelMap = ChannelMap::FromChannelMask(ChannelMask::Surround_5_0_2);
static constexpr auto targetChannelCount = targetChannelMap.GetNumChannels();
struct VirtualSourcesTestCase
{
std::string Description;
std::vector<float> VirtualSourceAnglesDegrees;
std::array<float, targetChannelCount> ExpectedGains;
};
// Testing for Quadraphonic output channel map
const std::vector<VirtualSourcesTestCase> testCases =
{
{
"Single virtual source at 0 degrees",
{ 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
},
{
"Multiple virtual sources at 0 degrees",
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
},
{
"Virtual sources at speaker angles",
{ -45.0f, 45.0f, -135.0f, 135.0f },
{ 0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.0f }
}
};
auto testVec3Type = [&]<class Vec3Type>()
{
using PannerType = VBAPanner3D<VBAPBaseTraits<Vec3Type>>;
PannerType panner;
static constexpr auto tolerance = static_cast<float>(VBAPanner3D<>::PanType::LUTCodec::cMaxComponentError);
for (const auto& testCase : testCases)
{
SCOPED_TRACE(testCase.Description);
ASSERT_TRUE(panner.InitializeLUT(targetChannelMap));
// Prepare virtual sources
std::vector<typename PannerType::VirtualSource> virtualSources;
const float weight = 1.0f / testCase.VirtualSourceAnglesDegrees.size();
for (float angleDeg : testCase.VirtualSourceAnglesDegrees)
{
const float rad = toRad(angleDeg);
virtualSources.push_back({ Vec3Type{ std::sin(rad), 0.0f, -std::cos(rad)}, weight });
}
std::vector<float> outGains(panner.GetNumChannels(), 0.0f);
panner.ProcessVirtualSources(virtualSources, outGains);
ASSERT_EQ(outGains.size(), testCase.ExpectedGains.size());
for (size_t i = 0; i < outGains.size(); ++i)
{
EXPECT_NEAR(outGains[i], testCase.ExpectedGains[i], tolerance) << "at index " << i;
}
}
};
#if defined(JPL_TEST_WITH_JOLT)
testVec3Type.operator()<JPH::Vec3>();
#endif
testVec3Type.operator()<glm::vec3>();
testVec3Type.operator()<MinimalVec3>();
}
TEST_F(VBAP3DTest, SourceLayoutsLeaveNoGapsInTargetSpeakerLayouts)
{
static constexpr float tolerance = JPL_FLOAT_EPS;
for (const auto& [targetLayoutName, targetLayoutMap] : mValid3DLayouts)
{
VBAPanner3D<> panner;
ASSERT_TRUE(panner.InitializeLUT(targetLayoutMap)) << targetLayoutName;
for (const auto& [sourceLayoutName, sourceLayoutMap] : mValidSourceLayouts)
{
typename VBAPanner3D<>::SourceLayoutType sourceLayout;
ASSERT_TRUE(panner.InitializeSourceLayout(sourceLayoutMap, sourceLayout)) << sourceLayoutName;
const float minDistanceBetweenSamples = sourceLayout.GetMinDistanceBetweenSamples();
const float shortestSpeakerAperture = std::acos(panner.GetShortestSpeakerAperture());
EXPECT_GT(shortestSpeakerAperture - minDistanceBetweenSamples, tolerance)
<< "Source: " << sourceLayoutName << " " << Math::ToDegrees(minDistanceBetweenSamples) << " Rings: " << sourceLayout.GetDimensions().NumRings
<< " -> Target: " << targetLayoutName << " " << Math::ToDegrees(shortestSpeakerAperture);
}
}
}
} // namespace JPL