-
Notifications
You must be signed in to change notification settings - Fork 6
/
VADCamulationSample.cs
73 lines (62 loc) · 1.99 KB
/
VADCamulationSample.cs
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
#nullable enable
using System;
using UniRx;
using Unity.Logging;
using UnityEngine;
namespace Mochineko.VoiceActivityDetection.Samples
{
/// <summary>
/// A sample of voice activity detection by CumulativeVoiceActivityDetector.
/// </summary>
internal sealed class VADCamulationSample : MonoBehaviour
{
[SerializeField]
private AudioSource? audioSource = null;
private UnityMicrophoneProxy? proxy;
private IVoiceActivityDetector? vad;
private void Start()
{
if (audioSource == null)
{
throw new NullReferenceException(nameof(audioSource));
}
Application.targetFrameRate = 60;
proxy = new UnityMicrophoneProxy();
IVoiceSource source = new UnityMicrophoneSource(proxy);
var buffer = new AudioClipBuffer(
maxSampleLength: (int)(15f * source.SamplingRate),
frequency: source.SamplingRate);
vad = new CumulativeVoiceActivityDetector(
source,
buffer,
activeVolumeThreshold: 0.007f,
activeChargeTimeRate: 2f,
maxChargeTimeSeconds: 2f,
minCumulatedTimeSeconds: 0.3f,
maxCumulatedTimeSeconds: 10f
);
vad
.VoiceIsActive
.Subscribe(isActive => Log.Debug("[VAD.Sample] IsActive: {0}", isActive))
.AddTo(this);
buffer
.OnVoiceInactive
.Subscribe(clip =>
{
Log.Info("[VAD.Sample] OnInactive and receive AudioClip and play.");
audioSource.clip = clip;
audioSource.Play();
})
.AddTo(this);
}
private void OnDestroy()
{
vad?.Dispose();
proxy?.Dispose();
}
private void Update()
{
vad?.Update();
}
}
}