-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPushToTalkVcInputFilter.cs.txt
More file actions
98 lines (81 loc) · 2.42 KB
/
Copy pathPushToTalkVcInputFilter.cs.txt
File metadata and controls
98 lines (81 loc) · 2.42 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
using System;
using Assets.Metater;
using MetaVoiceChat.Input;
using UnityEngine;
public class PushToTalkVcInputFilter : VcInputFilter
{
private const int FrameDivisor = 16;
public float debounceSeconds = 0.2f;
public bool isPressed = false;
public float? CurrentDecibelLevel { get; private set; } = null;
public float Gain { get; set; } = 1f;
private MetaInstant lastActivityInstant;
protected override void Filter(int index, ref float[] samples)
{
float maxRms = GetMaxRms(samples);
// range: -80 to 0
float decibelLevel = Mathf.Log10(Mathf.Max(0.0001f, maxRms)) * 20;
bool isPttEnabled = MetaCache.Object<SettingsUi>().voiceChatIsPushToTalkEnabled.Value;
if (isPttEnabled)
{
if (isPressed)
{
lastActivityInstant = MetaInstant.Time;
}
}
else
{
if (InputSensitivityUi.HasInstance)
{
InputSensitivityUi.Instance.activity.Value = decibelLevel;
}
float inputSensitivity = SettingsUi.Instance.voiceChatInputSensitivity.Value;
if (decibelLevel >= inputSensitivity)
{
lastActivityInstant = MetaInstant.Time;
}
}
if (lastActivityInstant.IsInsideCooldown(debounceSeconds))
{
for (int i = 0; i < samples.Length; i++)
{
samples[i] *= Gain;
}
CurrentDecibelLevel = decibelLevel;
}
else
{
samples = null;
CurrentDecibelLevel = null;
}
}
private float GetRms(ReadOnlySpan<float> samples)
{
if (samples == null || samples.Length == 0)
{
return 0f;
}
float sum = 0f;
foreach (var sample in samples)
{
sum += sample * sample;
}
float rms = Mathf.Sqrt(sum / samples.Length);
return rms;
}
private float GetMaxRms(float[] samples)
{
if (samples == null || samples.Length == 0)
{
return 0f;
}
// this assumes samples.Length % FrameDivisor == 0
float maxRms = 0f;
for (int i = 0; i < samples.Length; i += FrameDivisor)
{
float rms = GetRms(samples.AsSpan(i, FrameDivisor));
maxRms = Mathf.Max(maxRms, rms);
}
return maxRms;
}
}