-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNeocortexAudioReceiver.cs
More file actions
151 lines (129 loc) · 4.51 KB
/
NeocortexAudioReceiver.cs
File metadata and controls
151 lines (129 loc) · 4.51 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
using System;
using UnityEngine;
namespace Neocortex
{
public class NeocortexAudioReceiver : AudioReceiver
{
private const int FREQUENCY = 22050;
private const int AUDIO_SAMPLE_WINDOW = 256;
private const int AMPLITUDE_MULTIPLIER = 10;
private const float ZCR_TRESHOLD = 0.2f;
private const float MIN_AMPLITUDE_RISE = 0.04f;
private AudioClip audioClip;
private bool initialized;
public string SelectedMicrophone { get; set; }
public bool IsUserSpeaking { get; private set; }
[SerializeField, Range(0, 1)] private float amplitudeThreshold = 0.05f;
[SerializeField] private float minSpeechDuration = 0.25f;
[SerializeField] private float maxWaitTime = 1f;
private float zcrValue = 0;
private float previousAmplitude = 0;
private float rollingAverage = 0;
private const float smoothingFactor = 0.95f;
private float speechStartTime = -1f;
public override void StartMicrophone()
{
try
{
SelectedMicrophone = NeocortexMicrophone.devices[PlayerPrefs.GetInt(MIC_INDEX_KEY, 0)];
audioClip = NeocortexMicrophone.Start(SelectedMicrophone, true, 999, FREQUENCY);
initialized = true;
}
catch (Exception e)
{
OnRecordingFailed?.Invoke(e.Message);
}
}
public override void StopMicrophone()
{
NeocortexMicrophone.End(SelectedMicrophone);
initialized = false;
IsUserSpeaking = false;
AudioRecorded();
}
private void Update()
{
if (!initialized) return;
UpdateAmplitude();
float ampDelta = Amplitude - previousAmplitude;
if (UsePushToTalk) return;
if (!IsUserSpeaking &&
Amplitude > amplitudeThreshold &&
ampDelta > MIN_AMPLITUDE_RISE &&
zcrValue < ZCR_TRESHOLD)
{
if (speechStartTime < 0) speechStartTime = Time.time;
else if (Time.time - speechStartTime >= minSpeechDuration)
IsUserSpeaking = true;
}
else
{
speechStartTime = -1f;
}
if (IsUserSpeaking)
{
if (Amplitude < amplitudeThreshold)
{
ElapsedWaitTime += Time.deltaTime;
if (ElapsedWaitTime >= maxWaitTime)
{
ElapsedWaitTime = 0;
StopMicrophone();
}
}
else
{
ElapsedWaitTime = 0;
}
}
}
private void AudioRecorded()
{
AudioClip trimmed = audioClip.Trim();
if (!trimmed)
{
StartMicrophone();
}
else
{
OnAudioRecorded?.Invoke(trimmed);
}
}
private void UpdateAmplitude()
{
int clipPosition = NeocortexMicrophone.GetPosition(SelectedMicrophone);
int startPosition = Mathf.Max(0, clipPosition - AUDIO_SAMPLE_WINDOW);
float[] audioSamples = new float[AUDIO_SAMPLE_WINDOW];
audioClip.GetData(audioSamples, startPosition);
float sum = 0;
for (int i = 0; i < AUDIO_SAMPLE_WINDOW; i++)
{
sum += Mathf.Abs(audioSamples[i]);
}
float currentAmplitude = sum / AUDIO_SAMPLE_WINDOW;
rollingAverage = (rollingAverage * smoothingFactor) + (currentAmplitude * (1 - smoothingFactor));
Amplitude = Mathf.Clamp01(rollingAverage * AMPLITUDE_MULTIPLIER);
zcrValue = CalculateZCR(audioSamples);
}
private float CalculateZCR(float[] samples)
{
int zeroCrossings = 0;
for (int i = 1; i < samples.Length; i++)
{
if ((samples[i - 1] > 0 && samples[i] < 0) ||
(samples[i - 1] < 0 && samples[i] > 0))
{
zeroCrossings++;
}
}
return (float)zeroCrossings / samples.Length;
}
private void OnDestroy()
{
if (initialized)
{
NeocortexMicrophone.End(SelectedMicrophone);
}
}
}
}