-
Notifications
You must be signed in to change notification settings - Fork 6
/
AudioClipBuffer.cs
85 lines (68 loc) · 2.72 KB
/
AudioClipBuffer.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
84
85
#nullable enable
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using UniRx;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Mochineko.VoiceActivityDetection
{
/// <summary>
/// A voice buffer to write to AudioClip.
/// </summary>
public sealed class AudioClipBuffer : IVoiceBuffer
{
private readonly AudioClip audioClip;
private readonly float[] resetBuffer;
private int position;
private readonly Subject<AudioClip> onVoiceInactive = new();
public IObservable<AudioClip> OnVoiceInactive => onVoiceInactive;
/// <summary>
/// Creates a new instance of <see cref="AudioClipBuffer"/>.
/// </summary>
/// <param name="maxSampleLength">Max sample length to record.</param>
/// <param name="frequency">Frequency (= sampling rate) of voice data.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public AudioClipBuffer(int maxSampleLength, int frequency)
{
if (maxSampleLength <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxSampleLength), maxSampleLength, "maxSampleLength must be positive value.");
}
if (frequency <= 0)
{
throw new ArgumentOutOfRangeException(nameof(frequency), frequency, "frequency must be positive value.");
}
this.audioClip = AudioClip.Create(
name: "VAD_AudioClipBuffer",
lengthSamples: maxSampleLength,
channels: 1,
frequency: frequency,
stream: false);
this.resetBuffer = new float[maxSampleLength];
}
async UniTask IVoiceBuffer.BufferAsync(VoiceSegment segment, CancellationToken cancellationToken)
{
await UniTask.SwitchToMainThread(cancellationToken);
// NOTE: Copy to new buffer, namely allocated new array.
var writeBuffer = segment.Buffer.AsSpan(0..segment.Length).ToArray();
this.audioClip.SetData(writeBuffer, offsetSamples: position);
position += segment.Length;
}
async UniTask IVoiceBuffer.OnVoiceActiveAsync(CancellationToken cancellationToken)
{
await UniTask.SwitchToMainThread(cancellationToken);
this.audioClip.SetData(this.resetBuffer, offsetSamples: 0);
position = 0;
}
UniTask IVoiceBuffer.OnVoiceInactiveAsync(CancellationToken cancellationToken)
{
onVoiceInactive.OnNext(this.audioClip);
return UniTask.CompletedTask;
}
void IDisposable.Dispose()
{
Object.Destroy(this.audioClip);
}
}
}