-
Notifications
You must be signed in to change notification settings - Fork 6
/
VADAudioClipEchoSample.cs
83 lines (71 loc) · 2.47 KB
/
VADAudioClipEchoSample.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
74
75
76
77
78
79
80
81
82
83
#nullable enable
using System;
using UniRx;
using Unity.Logging;
using UnityEngine;
namespace Mochineko.VoiceActivityDetection.Samples
{
/// <summary>
/// A sample of voice activity detection as a component.
/// Input UnityEngine.Microphone and output AudioClip, then play by AudioSource.
/// </summary>
internal sealed class VADAudioClipEchoSample : MonoBehaviour
{
[SerializeField]
private VADParameters? parameters = null;
[SerializeField]
private AudioSource? audioSource = null;
private UnityMicrophoneProxy? proxy;
private IVoiceActivityDetector? vad;
private void Start()
{
if (parameters == null)
{
throw new NullReferenceException(nameof(parameters));
}
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)(parameters.MaxActiveDurationSeconds * source.SamplingRate),
frequency: source.SamplingRate);
vad = new QueueingVoiceActivityDetector(
source,
buffer,
parameters.MaxQueueingTimeSeconds,
parameters.MinQueueingTimeSeconds,
parameters.ActiveVolumeThreshold,
parameters.ActivationRateThreshold,
parameters.InactivationRateThreshold,
parameters.ActivationIntervalSeconds,
parameters.InactivationIntervalSeconds,
parameters.MaxActiveDurationSeconds);
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();
}
}
}