-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvoicecontrol_extension.cpp
More file actions
1091 lines (957 loc) · 31.7 KB
/
Copy pathvoicecontrol_extension.cpp
File metadata and controls
1091 lines (957 loc) · 31.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
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "voicecontrol_extension.h"
#include "inetmessage.h"
#include <algorithm>
#include <cmath>
#include <edict.h>
#include <engine/ICollideable.h>
#include <game/server/iplayerinfo.h>
#include <inetchannel.h>
#include <networkstringtabledefs.h>
ConVar vc_enabled("vc_enabled", "1", FCVAR_NONE, "Enables voice control processing");
ConVar vc_agc_enabled("vc_agc_enabled", "1", FCVAR_NONE, "Enables automatic voice level control");
ConVar vc_agc_target_rms("vc_agc_target_rms", "0.12", FCVAR_NONE, "Target normalized RMS for AGC");
ConVar vc_agc_noise_floor_rms("vc_agc_noise_floor_rms", "0.0015", FCVAR_NONE, "RMS below which AGC will not boost voice");
ConVar vc_agc_max_boost_db("vc_agc_max_boost_db", "18", FCVAR_NONE, "Maximum AGC boost in dB");
ConVar vc_agc_max_cut_db("vc_agc_max_cut_db", "-12", FCVAR_NONE, "Maximum AGC cut in dB");
ConVar vc_limiter_ceiling("vc_limiter_ceiling", "0.95", FCVAR_NONE, "Normalized limiter ceiling");
ConVar vc_dsp_enabled("vc_dsp_enabled", "1", FCVAR_NONE, "Enables lightweight voice cleanup DSP");
ConVar vc_highpass_enabled("vc_highpass_enabled", "1", FCVAR_NONE, "Enables low-rumble high-pass filtering");
ConVar vc_highpass_cutoff_hz("vc_highpass_cutoff_hz", "100", FCVAR_NONE, "High-pass cutoff frequency in Hz");
ConVar vc_noise_gate_enabled("vc_noise_gate_enabled", "1", FCVAR_NONE, "Enables soft noise gate/expander");
ConVar vc_noise_gate_threshold_rms("vc_noise_gate_threshold_rms", "0.002", FCVAR_NONE, "RMS threshold below which background noise is attenuated");
ConVar vc_noise_gate_hysteresis_rms("vc_noise_gate_hysteresis_rms", "0.001", FCVAR_NONE, "RMS hysteresis used to avoid noise gate chatter");
ConVar vc_noise_gate_atten_db("vc_noise_gate_atten_db", "-8", FCVAR_NONE, "Noise gate attenuation in dB when closed");
ConVar vc_noise_gate_attack_ms("vc_noise_gate_attack_ms", "5", FCVAR_NONE, "Noise gate opening time in milliseconds");
ConVar vc_noise_gate_release_ms("vc_noise_gate_release_ms", "120", FCVAR_NONE, "Noise gate closing time in milliseconds");
ConVar vc_softclip_enabled("vc_softclip_enabled", "1", FCVAR_NONE, "Enables soft clipping before the hard limiter ceiling");
ConVar vc_softclip_threshold("vc_softclip_threshold", "0.85", FCVAR_NONE, "Normalized soft clipping threshold");
ConVar vc_debug("vc_debug", "0", FCVAR_NONE, "Logs per-packet voice processing details to the server console");
ConVar vc_debug_recipients("vc_debug_recipients", "0", FCVAR_NONE, "Logs voice recipient scan details to the server console");
ConVar vc_respect_hearing("vc_respect_hearing", "1", FCVAR_NONE, "Uses engine IsHearingClient filtering when sending processed voice");
ConVar vc_include_sourcetv("vc_include_sourcetv", "0", FCVAR_NONE, "Includes SourceTV and Replay clients when sending processed voice");
ConVar vc_proximity_enabled("vc_proximity_enabled", "0", FCVAR_NONE, "Enables server-side proximity voice cutoff for live players");
ConVar vc_proximity_max_distance("vc_proximity_max_distance", "1200", FCVAR_NONE, "Maximum distance in Hammer units for proximity voice");
ConVar vc_proximity_falloff_enabled("vc_proximity_falloff_enabled", "1", FCVAR_NONE, "Enables server-side proximity voice volume falloff");
ConVar vc_proximity_full_volume_distance("vc_proximity_full_volume_distance", "300", FCVAR_NONE, "Distance in Hammer units where proximity voice stays at full volume");
ConVar vc_proximity_min_gain_db("vc_proximity_min_gain_db", "-24", FCVAR_NONE, "Minimum proximity voice gain at max distance");
ConVar vc_send_via_netchannel("vc_send_via_netchannel", "0", FCVAR_NONE, "Sends processed proximity voice through INetChannel with bVoice=true");
DECL_DETOUR(VC_SV_BroadcastVoiceData);
VoiceControlExt g_VoiceControl;
IGameConfig* g_pVoiceControlGameConf = nullptr;
ISDKTools* g_pVoiceControlSDKTools = nullptr;
IServer* g_pVoiceControlServer = nullptr;
INetworkStringTableContainer* g_pVoiceControlStringTables = nullptr;
static void Command_DumpStringTables(const CCommand& args);
static ConCommand vc_dump_stringtables_cmd("vc_dump_stringtables", Command_DumpStringTables, "Dumps network string table usage for VoiceControl diagnostics", FCVAR_NONE);
struct RuntimePlayerSettings
{
float gainDb = 0.0f;
bool enabled = false;
float duckGainDb = 0.0f;
bool duckEnabled = false;
int preset = 0;
};
static std::array<VoiceControlProcessor, MAXPLAYERS + 1> g_processors;
static constexpr int kMaxProximityBuckets = 32;
static constexpr float kProximityBucketStepDb = 3.0f;
static std::array<RuntimePlayerSettings, MAXPLAYERS + 1> g_playerSettings;
SMEXT_LINK(&g_VoiceControl);
static void ResetClientProcessors(int client)
{
if (client < 1 || client > MAXPLAYERS)
{
return;
}
g_processors[client].Reset();
}
static bool IsValidClientIndex(int client)
{
return client >= 1 && client <= playerhelpers->GetMaxClients();
}
static bool HasMeaningfulGain(float gainDb)
{
return std::fabs(gainDb) > 0.001f;
}
static float ClampFloat(float value, float minValue, float maxValue)
{
return std::max(minValue, std::min(maxValue, value));
}
static bool IsProximityEnabled()
{
return vc_proximity_enabled.GetBool() && vc_proximity_max_distance.GetFloat() > 0.0f;
}
static bool IsProximityFalloffEnabled()
{
return IsProximityEnabled() && vc_proximity_falloff_enabled.GetBool();
}
static bool IsDspProcessingRequired()
{
return vc_dsp_enabled.GetBool() && (vc_highpass_enabled.GetBool() || vc_noise_gate_enabled.GetBool());
}
static void ApplyPresetToSettings(int preset, VoiceControlSettings& settings)
{
switch (preset)
{
case 1: // Quiet mic: allow more boost while keeping the gate gentle.
settings.agcNoiseFloorRms = std::min(settings.agcNoiseFloorRms, 0.0010f);
settings.agcMaxBoostDb = std::max(settings.agcMaxBoostDb, 20.0f);
settings.noiseGateThresholdRms = std::min(settings.noiseGateThresholdRms, 0.0015f);
settings.noiseGateAttenDb = std::max(settings.noiseGateAttenDb, -6.0f);
break;
case 2: // Noisy mic: avoid boosting background noise and gate harder.
settings.dspEnabled = true;
settings.highpassEnabled = true;
settings.highpassCutoffHz = std::max(settings.highpassCutoffHz, 120.0f);
settings.noiseGateEnabled = true;
settings.noiseGateThresholdRms = std::max(settings.noiseGateThresholdRms, 0.0040f);
settings.noiseGateAttenDb = std::min(settings.noiseGateAttenDb, -14.0f);
settings.noiseGateReleaseMs = std::max(settings.noiseGateReleaseMs, 160.0f);
settings.agcNoiseFloorRms = std::max(settings.agcNoiseFloorRms, 0.0035f);
settings.agcMaxBoostDb = std::min(settings.agcMaxBoostDb, 12.0f);
break;
case 3: // Loud/clipping mic: reduce level and clamp peaks more gently.
settings.manualGainDb -= 3.0f;
settings.agcMaxBoostDb = std::min(settings.agcMaxBoostDb, 6.0f);
settings.agcMaxCutDb = std::min(settings.agcMaxCutDb, -18.0f);
settings.limiterCeiling = std::min(settings.limiterCeiling, 0.85f);
settings.softClipEnabled = true;
settings.softClipThreshold = std::min(settings.softClipThreshold, 0.75f);
break;
default:
break;
}
settings.preset = preset;
}
static bool IsAudioProcessingRequired(int client)
{
if (!IsValidClientIndex(client))
{
return false;
}
const RuntimePlayerSettings& settings = g_playerSettings[client];
return vc_agc_enabled.GetBool() ||
IsDspProcessingRequired() ||
(settings.enabled && HasMeaningfulGain(settings.gainDb)) ||
(settings.duckEnabled && HasMeaningfulGain(settings.duckGainDb)) ||
settings.preset != 0;
}
static bool ShouldInterceptVoice(int client)
{
if (!vc_enabled.GetBool() || !IsValidClientIndex(client))
{
return false;
}
return IsAudioProcessingRequired(client) || IsProximityEnabled();
}
static VoiceControlSettings BuildSettingsForClient(int client)
{
VoiceControlSettings settings;
settings.manualGainDb = g_playerSettings[client].enabled ? g_playerSettings[client].gainDb : 0.0f;
settings.duckGainDb = g_playerSettings[client].duckEnabled ? g_playerSettings[client].duckGainDb : 0.0f;
settings.agcEnabled = vc_agc_enabled.GetBool();
settings.agcTargetRms = vc_agc_target_rms.GetFloat();
settings.agcNoiseFloorRms = vc_agc_noise_floor_rms.GetFloat();
settings.agcMaxBoostDb = vc_agc_max_boost_db.GetFloat();
settings.agcMaxCutDb = vc_agc_max_cut_db.GetFloat();
settings.limiterCeiling = vc_limiter_ceiling.GetFloat();
settings.dspEnabled = vc_dsp_enabled.GetBool();
settings.highpassEnabled = vc_highpass_enabled.GetBool();
settings.highpassCutoffHz = vc_highpass_cutoff_hz.GetFloat();
settings.noiseGateEnabled = vc_noise_gate_enabled.GetBool();
settings.noiseGateThresholdRms = vc_noise_gate_threshold_rms.GetFloat();
settings.noiseGateHysteresisRms = vc_noise_gate_hysteresis_rms.GetFloat();
settings.noiseGateAttenDb = vc_noise_gate_atten_db.GetFloat();
settings.noiseGateAttackMs = vc_noise_gate_attack_ms.GetFloat();
settings.noiseGateReleaseMs = vc_noise_gate_release_ms.GetFloat();
settings.softClipEnabled = vc_softclip_enabled.GetBool();
settings.softClipThreshold = vc_softclip_threshold.GetFloat();
ApplyPresetToSettings(g_playerSettings[client].preset, settings);
return settings;
}
struct VoiceRecipientScan
{
int candidates = 0;
int connected = 0;
int fake = 0;
int sourcetv = 0;
int replay = 0;
int sourcetvSent = 0;
int replaySent = 0;
int nullClient = 0;
int notHearing = 0;
int tooFar = 0;
int originFail = 0;
int bypassDead = 0;
int bypassObserver = 0;
int liveDistanceChecked = 0;
int recipients = -1;
int bucketCount = 0;
int sendOk = 0;
int sendFail = 0;
int netchanNull = 0;
bool msgProximity = false;
bool falloffEnabled = false;
float minDistance = -1.0f;
float maxDistance = 0.0f;
};
static void LogVoiceDebug(int client, int fromClientSlot, const VoiceControlDebugInfo& debugInfo, const VoiceRecipientScan& scan, const char* sendPath)
{
const char* name = "unknown";
IGamePlayer* player = playerhelpers->GetGamePlayer(client);
if (player && player->IsConnected())
{
name = player->GetName();
}
META_CONPRINTF("[VoiceControl] audio speaker=%d slot=%d name=\"%s\" in_rms=%.4f rms_hp=%.4f rms_gate=%.4f out_rms=%.4f peak_in=%.4f peak_out=%.4f clips=%d dsp=%d hp=%d gate_open=%d gate_db=%.1fdb softclip=%d preset=%d manual=%.1fdb agc=%.1fdb duck=%.1fdb final=%.1fdb sr=%d chunks=%d samples=%d bytes=%d->%d\n",
client,
fromClientSlot,
name,
debugInfo.inputRms,
debugInfo.rmsAfterHighpass,
debugInfo.rmsAfterGate,
debugInfo.outputRms,
debugInfo.inputPeak,
debugInfo.outputPeak,
debugInfo.clipCount,
debugInfo.dspEnabled ? 1 : 0,
debugInfo.highpassEnabled ? 1 : 0,
debugInfo.gateOpen ? 1 : 0,
debugInfo.gateGainDb,
debugInfo.softClipEnabled ? 1 : 0,
debugInfo.preset,
debugInfo.manualGainDb,
debugInfo.agcGainDb,
debugInfo.duckGainDb,
debugInfo.finalGainDb,
debugInfo.sampleRate,
debugInfo.chunkCount,
debugInfo.sampleCount,
debugInfo.inputBytes,
debugInfo.outputBytes);
META_CONPRINTF("[VoiceControl] recipients speaker=%d slot=%d recipients=%d candidates=%d connected=%d fake=%d sourcetv=%d replay=%d sourcetv_sent=%d replay_sent=%d null=%d not_hearing=%d too_far=%d origin_fail=%d bypass_dead=%d bypass_observer=%d live_distance_checked=%d msg_proximity=%d falloff=%d bucket_count=%d min_dist=%.1f max_dist=%.1f send_ok=%d send_fail=%d netchan_null=%d send=%s\n",
client,
fromClientSlot,
scan.recipients,
scan.candidates,
scan.connected,
scan.fake,
scan.sourcetv,
scan.replay,
scan.sourcetvSent,
scan.replaySent,
scan.nullClient,
scan.notHearing,
scan.tooFar,
scan.originFail,
scan.bypassDead,
scan.bypassObserver,
scan.liveDistanceChecked,
scan.msgProximity ? 1 : 0,
scan.falloffEnabled ? 1 : 0,
scan.bucketCount,
scan.minDistance,
scan.maxDistance,
scan.sendOk,
scan.sendFail,
scan.netchanNull,
sendPath);
}
static bool SendVoiceControlDataMsg(int fromClientSlot, IClient* pToClient, uint8_t* data, int nBytes, int64 xuid, bool proximity, VoiceRecipientScan& scan)
{
if (pToClient == nullptr)
{
scan.sendFail++;
return false;
}
SVC_VoiceData msg;
msg.m_bProximity = proximity;
scan.msgProximity = proximity;
msg.m_nLength = nBytes * 8;
msg.m_xuid = xuid;
msg.m_nFromClient = fromClientSlot;
msg.m_DataOut = data;
INetChannel* netChannel = pToClient->GetNetChannel();
msg.SetNetChannel(netChannel);
if (vc_send_via_netchannel.GetBool())
{
if (netChannel != nullptr)
{
bool sent = netChannel->SendNetMsg(msg, false, true);
if (sent)
{
scan.sendOk++;
return true;
}
}
else
{
scan.netchanNull++;
}
}
bool sent = pToClient->SendNetMsg(msg, false);
sent ? scan.sendOk++ : scan.sendFail++;
return sent;
}
static bool VoiceControlIsHearingClient(IClient* pToClient, int fromClientSlot)
{
return pToClient != nullptr && pToClient->IsHearingClient(fromClientSlot);
}
struct StringTableUsage
{
const char* name = "";
int entries = 0;
int maxEntries = 0;
int entryBits = 0;
long long userDataBytes = 0;
};
static void Command_DumpStringTables(const CCommand& args)
{
if (g_pVoiceControlStringTables == nullptr)
{
META_CONPRINTF("[VoiceControl] Network string table container is not available.\n");
return;
}
std::vector<StringTableUsage> usages;
int tableCount = g_pVoiceControlStringTables->GetNumTables();
META_CONPRINTF("[VoiceControl] Dumping %d network string tables:\n", tableCount);
for (int tableIndex = 0; tableIndex < tableCount; tableIndex++)
{
INetworkStringTable* table = g_pVoiceControlStringTables->GetTable(tableIndex);
if (table == nullptr)
{
continue;
}
StringTableUsage usage;
usage.name = table->GetTableName();
usage.entries = table->GetNumStrings();
usage.maxEntries = table->GetMaxStrings();
usage.entryBits = table->GetEntryBits();
for (int entry = 0; entry < usage.entries; entry++)
{
int userDataLength = 0;
const void* userData = table->GetStringUserData(entry, &userDataLength);
if (userData != nullptr && userDataLength > 0)
{
usage.userDataBytes += userDataLength;
}
}
usages.push_back(usage);
META_CONPRINTF("[VoiceControl] stringtable name=\"%s\" entries=%d max=%d entry_bits=%d userdata_bytes=%lld\n",
usage.name,
usage.entries,
usage.maxEntries,
usage.entryBits,
usage.userDataBytes);
}
std::sort(usages.begin(), usages.end(), [](const StringTableUsage& left, const StringTableUsage& right) {
return left.userDataBytes > right.userDataBytes;
});
int heavyCount = std::min(5, static_cast<int>(usages.size()));
META_CONPRINTF("[VoiceControl] Heaviest string tables by userdata bytes:\n");
for (int i = 0; i < heavyCount; i++)
{
const StringTableUsage& usage = usages[i];
META_CONPRINTF("[VoiceControl] heavy #%d name=\"%s\" entries=%d max=%d entry_bits=%d userdata_bytes=%lld\n",
i + 1,
usage.name,
usage.entries,
usage.maxEntries,
usage.entryBits,
usage.userDataBytes);
}
}
static bool IsFiniteVector(const Vector& origin)
{
return std::isfinite(origin.x) && std::isfinite(origin.y) && std::isfinite(origin.z);
}
static IPlayerInfo* GetClientPlayerInfo(int client)
{
IGamePlayer* pGamePlayer = playerhelpers->GetGamePlayer(client);
if (pGamePlayer == nullptr || !pGamePlayer->IsConnected())
{
return nullptr;
}
return pGamePlayer->GetPlayerInfo();
}
static bool IsDeadOrObserverForProximity(IPlayerInfo* playerInfo, VoiceRecipientScan& scan)
{
if (playerInfo == nullptr)
{
return false;
}
bool bypass = false;
if (playerInfo->IsDead())
{
scan.bypassDead++;
bypass = true;
}
if (playerInfo->IsObserver())
{
scan.bypassObserver++;
bypass = true;
}
return bypass;
}
static bool GetLiveClientOrigin(int client, Vector& origin, VoiceRecipientScan& scan)
{
IPlayerInfo* playerInfo = GetClientPlayerInfo(client);
if (playerInfo == nullptr)
{
scan.originFail++;
return false;
}
if (IsDeadOrObserverForProximity(playerInfo, scan))
{
return false;
}
origin = playerInfo->GetAbsOrigin();
if (!IsFiniteVector(origin))
{
scan.originFail++;
return false;
}
return true;
}
static float GetDistanceSqr(const Vector& a, const Vector& b)
{
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return dx * dx + dy * dy + dz * dz;
}
static float CalculateProximityGainDb(float distance)
{
float maxDistance = std::max(1.0f, vc_proximity_max_distance.GetFloat());
float fullDistance = std::max(0.0f, vc_proximity_full_volume_distance.GetFloat());
float minGainDb = std::max(-60.0f, std::min(0.0f, vc_proximity_min_gain_db.GetFloat()));
if (!IsProximityFalloffEnabled() || distance <= fullDistance || maxDistance <= fullDistance)
{
return 0.0f;
}
float t = (distance - fullDistance) / (maxDistance - fullDistance);
t = std::max(0.0f, std::min(1.0f, t));
return minGainDb * t;
}
static float QuantizeProximityGainDb(float gainDb)
{
float minGainDb = std::max(-60.0f, std::min(0.0f, vc_proximity_min_gain_db.GetFloat()));
float quantized = std::round(gainDb / kProximityBucketStepDb) * kProximityBucketStepDb;
return std::max(minGainDb, std::min(0.0f, quantized));
}
static int GetProximityBucket(float gainDb)
{
float positiveCutDb = std::max(0.0f, -gainDb);
int bucket = static_cast<int>(std::round(positiveCutDb / kProximityBucketStepDb));
return std::max(0, std::min(kMaxProximityBuckets - 1, bucket));
}
struct VoiceRecipient
{
IClient* client = nullptr;
bool sourcetv = false;
bool replay = false;
float distance = 0.0f;
float gainDb = 0.0f;
int bucket = 0;
};
static void UpdateDistanceStats(VoiceRecipientScan& scan, float distance)
{
if (scan.minDistance < 0.0f || distance < scan.minDistance)
{
scan.minDistance = distance;
}
if (distance > scan.maxDistance)
{
scan.maxDistance = distance;
}
}
static std::vector<VoiceRecipient> GetVoiceRecipients(int fromClientIndex, bool applyProximity, bool allowTvReplay, VoiceRecipientScan& scan)
{
std::vector<VoiceRecipient> recipients;
int fromClientSlot = fromClientIndex - 1;
Vector speakerOrigin;
bool hasSpeakerOrigin = !applyProximity || GetLiveClientOrigin(fromClientIndex, speakerOrigin, scan);
float maxDistance = vc_proximity_max_distance.GetFloat();
float maxDistanceSqr = maxDistance * maxDistance;
for (int client = 1; client <= playerhelpers->GetMaxClients(); client++)
{
scan.candidates++;
IGamePlayer* pGamePlayer = playerhelpers->GetGamePlayer(client);
if (pGamePlayer == nullptr || !pGamePlayer->IsConnected())
{
continue;
}
scan.connected++;
if (client == fromClientIndex)
{
continue;
}
if (pGamePlayer->IsFakeClient())
{
if (pGamePlayer->IsSourceTV())
{
scan.sourcetv++;
if (!allowTvReplay || !vc_include_sourcetv.GetBool())
{
scan.fake++;
continue;
}
}
else if (pGamePlayer->IsReplay())
{
scan.replay++;
if (!allowTvReplay || !vc_include_sourcetv.GetBool())
{
scan.fake++;
continue;
}
}
else
{
scan.fake++;
continue;
}
}
IClient* pToClient = g_pVoiceControlServer->GetClient(client - 1);
if (pToClient == nullptr)
{
scan.nullClient++;
continue;
}
bool isTvOrReplay = pGamePlayer->IsSourceTV() || pGamePlayer->IsReplay();
if (!isTvOrReplay && vc_respect_hearing.GetBool() && !VoiceControlIsHearingClient(pToClient, fromClientSlot))
{
scan.notHearing++;
continue;
}
VoiceRecipient recipient;
recipient.client = pToClient;
recipient.sourcetv = pGamePlayer->IsSourceTV();
recipient.replay = pGamePlayer->IsReplay();
if (recipient.sourcetv)
{
scan.sourcetvSent++;
recipients.push_back(recipient);
continue;
}
if (recipient.replay)
{
scan.replaySent++;
recipients.push_back(recipient);
continue;
}
if (applyProximity)
{
IPlayerInfo* listenerInfo = GetClientPlayerInfo(client);
if (listenerInfo == nullptr)
{
scan.originFail++;
recipients.push_back(recipient);
continue;
}
if (IsDeadOrObserverForProximity(listenerInfo, scan))
{
recipients.push_back(recipient);
continue;
}
Vector listenerOrigin = listenerInfo->GetAbsOrigin();
if (!hasSpeakerOrigin || !IsFiniteVector(listenerOrigin))
{
scan.originFail++;
recipients.push_back(recipient);
continue;
}
float distanceSqr = GetDistanceSqr(speakerOrigin, listenerOrigin);
scan.liveDistanceChecked++;
if (distanceSqr > maxDistanceSqr)
{
scan.tooFar++;
continue;
}
recipient.distance = std::sqrt(distanceSqr);
UpdateDistanceStats(scan, recipient.distance);
recipient.gainDb = QuantizeProximityGainDb(CalculateProximityGainDb(recipient.distance));
recipient.bucket = GetProximityBucket(recipient.gainDb);
}
recipients.push_back(recipient);
}
scan.recipients = static_cast<int>(recipients.size());
return recipients;
}
DETOUR_DECL_STATIC4(VC_SV_BroadcastVoiceData, void, IClient*, pClient, int, nBytes, uint8_t*, data, int64, xuid)
{
int fromClientSlot = pClient->GetPlayerSlot();
int fromClientIndex = fromClientSlot + 1;
if (!ShouldInterceptVoice(fromClientIndex))
{
DETOUR_STATIC_CALL(VC_SV_BroadcastVoiceData)(pClient, nBytes, data, xuid);
return;
}
std::vector<uint8_t> processedData;
VoiceControlDebugInfo debugInfo;
VoiceControlDebugInfo* debugInfoPtr = (vc_debug.GetBool() || vc_debug_recipients.GetBool()) ? &debugInfo : nullptr;
VoiceRecipientScan proximityBypassScan;
bool hasProximityBypassScan = false;
bool proximityEnabled = IsProximityEnabled();
if (proximityEnabled)
{
Vector speakerOrigin;
if (!GetLiveClientOrigin(fromClientIndex, speakerOrigin, proximityBypassScan))
{
proximityEnabled = false;
hasProximityBypassScan = true;
}
}
if (proximityEnabled)
{
VoiceRecipientScan scan;
scan.msgProximity = false;
scan.falloffEnabled = IsProximityFalloffEnabled();
std::vector<VoiceRecipient> recipients = GetVoiceRecipients(fromClientIndex, true, true, scan);
const char* sendPath = vc_send_via_netchannel.GetBool() ? "netchan" : "client";
if (recipients.empty())
{
if (debugInfoPtr)
{
debugInfo.inputBytes = nBytes;
debugInfo.outputBytes = nBytes;
LogVoiceDebug(fromClientIndex, fromClientSlot, debugInfo, scan, sendPath);
}
return;
}
if (scan.falloffEnabled)
{
std::map<int, std::vector<const VoiceRecipient*>> buckets;
for (const VoiceRecipient& recipient : recipients)
{
buckets[recipient.bucket].push_back(&recipient);
}
scan.bucketCount = static_cast<int>(buckets.size());
bool audioProcessingRequired = IsAudioProcessingRequired(fromClientIndex);
bool hasAttenuatedBuckets = false;
for (const auto& bucket : buckets)
{
if (bucket.first != 0)
{
hasAttenuatedBuckets = true;
break;
}
}
VoiceControlFrame baseFrame;
bool hasBaseFrame = false;
std::vector<uint8_t> basePacket;
bool hasBasePacket = false;
if (audioProcessingRequired)
{
VoiceControlSettings settings = BuildSettingsForClient(fromClientIndex);
if (g_processors[fromClientIndex].ProcessToFrame(data, nBytes, settings, baseFrame, debugInfoPtr) &&
g_processors[fromClientIndex].EncodeFrame(data, nBytes, baseFrame, basePacket))
{
hasBaseFrame = true;
hasBasePacket = true;
if (debugInfoPtr)
{
debugInfo.outputBytes = static_cast<int>(basePacket.size());
}
}
else
{
basePacket.assign(data, data + nBytes);
hasBasePacket = true;
if (debugInfoPtr)
{
debugInfo = VoiceControlDebugInfo();
debugInfo.inputBytes = nBytes;
debugInfo.outputBytes = nBytes;
}
}
}
else
{
basePacket.assign(data, data + nBytes);
hasBasePacket = true;
if (hasAttenuatedBuckets)
{
VoiceControlProcessor neutralProcessor;
VoiceControlSettings neutralSettings;
neutralSettings.agcEnabled = false;
neutralSettings.dspEnabled = false;
neutralSettings.highpassEnabled = false;
neutralSettings.noiseGateEnabled = false;
neutralSettings.softClipEnabled = false;
hasBaseFrame = neutralProcessor.ProcessToFrame(data, nBytes, neutralSettings, baseFrame, nullptr);
}
if (debugInfoPtr)
{
debugInfo.inputBytes = nBytes;
debugInfo.outputBytes = nBytes;
}
}
for (const auto& bucket : buckets)
{
int bucketIndex = bucket.first;
float bucketGainDb = bucket.second.empty() ? 0.0f : bucket.second.front()->gainDb;
std::vector<uint8_t> bucketData;
if (bucketIndex == 0)
{
bucketData = hasBasePacket ? basePacket : std::vector<uint8_t>(data, data + nBytes);
}
else
{
if (hasBaseFrame)
{
VoiceControlFrame bucketFrame = baseFrame;
VoiceControlProcessor::ApplyGainToFrame(bucketFrame, bucketGainDb);
if (!VoiceControlProcessor::EncodeFrameStateless(data, nBytes, bucketFrame, bucketData))
{
bucketData = hasBasePacket ? basePacket : std::vector<uint8_t>(data, data + nBytes);
}
}
else
{
bucketData = hasBasePacket ? basePacket : std::vector<uint8_t>(data, data + nBytes);
}
}
for (const VoiceRecipient* recipient : bucket.second)
{
SendVoiceControlDataMsg(fromClientSlot, recipient->client, bucketData.data(), static_cast<int>(bucketData.size()), xuid, false, scan);
}
}
if (debugInfoPtr)
{
LogVoiceDebug(fromClientIndex, fromClientSlot, debugInfo, scan, sendPath);
}
return;
}
if (IsAudioProcessingRequired(fromClientIndex))
{
VoiceControlSettings settings = BuildSettingsForClient(fromClientIndex);
if (!g_processors[fromClientIndex].Process(data, nBytes, settings, processedData, debugInfoPtr))
{
processedData.assign(data, data + nBytes);
if (debugInfoPtr)
{
debugInfo = VoiceControlDebugInfo();
debugInfo.inputBytes = nBytes;
debugInfo.outputBytes = nBytes;
}
}
}
else
{
processedData.assign(data, data + nBytes);
if (debugInfoPtr)
{
debugInfo.inputBytes = nBytes;
debugInfo.outputBytes = nBytes;
}
}
scan.bucketCount = recipients.empty() ? 0 : 1;
for (const VoiceRecipient& recipient : recipients)
{
SendVoiceControlDataMsg(fromClientSlot, recipient.client, processedData.data(), static_cast<int>(processedData.size()), xuid, false, scan);
}
if (debugInfoPtr)
{
LogVoiceDebug(fromClientIndex, fromClientSlot, debugInfo, scan, sendPath);
}
return;
}
if (IsAudioProcessingRequired(fromClientIndex))
{
VoiceControlSettings settings = BuildSettingsForClient(fromClientIndex);
if (!g_processors[fromClientIndex].Process(data, nBytes, settings, processedData, debugInfoPtr))
{
DETOUR_STATIC_CALL(VC_SV_BroadcastVoiceData)(pClient, nBytes, data, xuid);
return;
}
}
else
{
processedData.assign(data, data + nBytes);
if (debugInfoPtr)
{
debugInfo.inputBytes = nBytes;
debugInfo.outputBytes = nBytes;
}
}
if (vc_debug_recipients.GetBool())
{
VoiceRecipientScan scan = hasProximityBypassScan ? proximityBypassScan : VoiceRecipientScan();
GetVoiceRecipients(fromClientIndex, false, true, scan);
if (debugInfoPtr)
{
LogVoiceDebug(fromClientIndex, fromClientSlot, debugInfo, scan, "engine");
}
}
else if (debugInfoPtr)
{
VoiceRecipientScan scan = hasProximityBypassScan ? proximityBypassScan : VoiceRecipientScan();
LogVoiceDebug(fromClientIndex, fromClientSlot, debugInfo, scan, "engine");
}
DETOUR_STATIC_CALL(VC_SV_BroadcastVoiceData)(pClient, static_cast<int>(processedData.size()), processedData.data(), xuid);
}
static cell_t VC_SetPlayerGain(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return false;
}
float gainDb = sp_ctof(params[2]);
gainDb = std::max(-24.0f, std::min(24.0f, gainDb));
g_playerSettings[target].gainDb = gainDb;
g_playerSettings[target].enabled = HasMeaningfulGain(gainDb);
return true;
}
static cell_t VC_ClearPlayerGain(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return false;
}
g_playerSettings[target].gainDb = 0.0f;
g_playerSettings[target].enabled = false;
return true;
}
static cell_t VC_GetPlayerGain(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return sp_ftoc(0.0f);
}
return sp_ftoc(g_playerSettings[target].enabled ? g_playerSettings[target].gainDb : 0.0f);
}
static cell_t VC_SetPlayerDuckGain(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return false;
}
float gainDb = sp_ctof(params[2]);
gainDb = std::max(-24.0f, std::min(0.0f, gainDb));
g_playerSettings[target].duckGainDb = gainDb;
g_playerSettings[target].duckEnabled = HasMeaningfulGain(gainDb);
return true;
}
static cell_t VC_ClearPlayerDuckGain(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return false;
}
g_playerSettings[target].duckGainDb = 0.0f;
g_playerSettings[target].duckEnabled = false;
return true;
}
static cell_t VC_GetPlayerDuckGain(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return sp_ftoc(0.0f);
}
return sp_ftoc(g_playerSettings[target].duckEnabled ? g_playerSettings[target].duckGainDb : 0.0f);
}
static cell_t VC_SetPlayerPreset(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return false;
}
int preset = std::max(0, std::min(3, params[2]));
g_playerSettings[target].preset = preset;
ResetClientProcessors(target);
return true;
}
static cell_t VC_ClearPlayerPreset(IPluginContext* pContext, const cell_t* params)
{
int target = params[1];
if (!IsValidClientIndex(target))
{
return false;
}
g_playerSettings[target].preset = 0;
ResetClientProcessors(target);
return true;
}
static cell_t VC_GetPlayerPreset(IPluginContext* pContext, const cell_t* params)