-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathApmReverseStream.cs
More file actions
53 lines (47 loc) · 1.59 KB
/
ApmReverseStream.cs
File metadata and controls
53 lines (47 loc) · 1.59 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
using LiveKit.Internal;
using UnityEngine;
namespace LiveKit
{
/// <summary>
/// Captures and processes the reverse audio stream using an <see cref="AudioProcessingModule"/>.
/// </summary>
/// <remarks>
/// The reverse stream is captured from the scene's audio listener.
/// </remarks>
internal class ApmReverseStream
{
private readonly AudioBuffer _captureBuffer = new AudioBuffer();
private readonly AudioProcessingModule _apm; // APM is thread safe
private AudioFilter _audioFilter;
internal ApmReverseStream(AudioProcessingModule apm)
{
_apm = apm;
}
internal void Start()
{
var audioListener = GameObject.FindObjectOfType<AudioListener>();
if (audioListener == null)
{
Utils.Error("AudioListener not found in scene");
return;
}
_audioFilter = audioListener.gameObject.AddComponent<AudioFilter>();
_audioFilter.AudioRead += OnAudioRead;
}
internal void Stop()
{
if (_audioFilter != null)
Object.Destroy(_audioFilter);
}
private void OnAudioRead(float[] data, int channels, int sampleRate)
{
_captureBuffer.Write(data, (uint)channels, (uint)sampleRate);
while (true)
{
using var frame = _captureBuffer.ReadDuration(AudioProcessingModule.FRAME_DURATION_MS);
if (frame == null) break;
_apm.ProcessReverseStream(frame);
}
}
}
}