-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathRtcAudioSource.cs
More file actions
202 lines (173 loc) · 7.07 KB
/
Copy pathRtcAudioSource.cs
File metadata and controls
202 lines (173 loc) · 7.07 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections;
using LiveKit.Proto;
using LiveKit.Internal;
using LiveKit.Internal.FFIClients.Requests;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using System.Diagnostics;
namespace LiveKit
{
/// <summary>
/// Defines the type of audio source, influencing processing behavior.
/// </summary>
public enum RtcAudioSourceType
{
AudioSourceCustom = 0,
AudioSourceMicrophone = 1
}
/// <summary>
/// Capture source for a local audio track.
/// </summary>
public abstract class RtcAudioSource : IRtcSource, IDisposable
{
/// <summary>
/// Event triggered when audio samples are captured from the underlying source.
/// Provides the audio data, channel count, and sample rate.
/// </summary>
/// <remarks>
/// This event is not guaranteed to be called on the main thread.
/// </remarks>
public abstract event Action<float[], int, int> AudioRead;
#if UNITY_IOS && !UNITY_EDITOR
// iOS microphone sample rate is 24k
public static uint DefaultMicrophoneSampleRate = 24000;
public static uint DefaultSampleRate = 48000;
#else
public static uint DefaultSampleRate = 48000;
public static uint DefaultMicrophoneSampleRate = DefaultSampleRate;
#endif
public static uint DefaultChannels = 2;
private readonly RtcAudioSourceType _sourceType;
public RtcAudioSourceType SourceType => _sourceType;
internal readonly FfiHandle Handle;
protected AudioSourceInfo _info;
/// <summary>
/// Temporary frame buffer for invoking the FFI capture method.
/// </summary>
private NativeArray<short> _frameData;
private bool _muted = false;
public override bool Muted => _muted;
private bool _started = false;
private bool _disposed = false;
protected RtcAudioSource(int channels = 2, RtcAudioSourceType audioSourceType = RtcAudioSourceType.AudioSourceCustom, uint? sampleRate = null)
{
_sourceType = audioSourceType;
// Use provided sample rate, or fall back to defaults
uint actualSampleRate = sampleRate ??
(_sourceType == RtcAudioSourceType.AudioSourceMicrophone ? DefaultMicrophoneSampleRate : DefaultSampleRate);
using var request = FFIBridge.Instance.NewRequest<NewAudioSourceRequest>();
var newAudioSource = request.request;
newAudioSource.Type = AudioSourceType.AudioSourceNative;
newAudioSource.NumChannels = (uint)channels;
newAudioSource.SampleRate = actualSampleRate;
UnityEngine.Debug.Log($"NewAudioSource: {newAudioSource.NumChannels} {newAudioSource.SampleRate}");
newAudioSource.Options = request.TempResource<AudioSourceOptions>();
newAudioSource.Options.EchoCancellation = true;
newAudioSource.Options.AutoGainControl = true;
newAudioSource.Options.NoiseSuppression = true;
using var response = request.Send();
FfiResponse res = response;
_info = res.NewAudioSource.Source.Info;
Handle = FfiHandle.FromOwnedHandle(res.NewAudioSource.Source.Handle);
}
/// <summary>
/// Begin capturing audio samples from the underlying source.
/// </summary>
public virtual void Start()
{
if (_started) return;
AudioRead += OnAudioRead;
_started = true;
}
/// <summary>
/// Stop capturing audio samples from the underlying source.
/// </summary>
public virtual void Stop()
{
if (!_started) return;
AudioRead -= OnAudioRead;
_started = false;
}
private void OnAudioRead(float[] data, int channels, int sampleRate)
{
if (_muted) return;
// The length of the data buffer corresponds to the DSP buffer size.
if (_frameData.Length != data.Length)
{
if (_frameData.IsCreated) _frameData.Dispose();
_frameData = new NativeArray<short>(data.Length, Allocator.Persistent);
}
// Copy from the audio read buffer into the frame buffer, converting
// each sample to a 16-bit signed integer.
static short FloatToS16(float v)
{
v *= 32768f;
v = Math.Min(v, 32767f);
v = Math.Max(v, -32768f);
return (short)(v + Math.Sign(v) * 0.5f);
}
for (int i = 0; i < data.Length; i++)
_frameData[i] = FloatToS16(data[i]);
// Capture the frame.
using var request = FFIBridge.Instance.NewRequest<CaptureAudioFrameRequest>();
using var audioFrameBufferInfo = request.TempResource<AudioFrameBufferInfo>();
var pushFrame = request.request;
pushFrame.SourceHandle = (ulong)Handle.DangerousGetHandle();
pushFrame.Buffer = audioFrameBufferInfo;
unsafe
{
pushFrame.Buffer.DataPtr = (ulong)NativeArrayUnsafeUtility
.GetUnsafePtr(_frameData);
}
pushFrame.Buffer.NumChannels = (uint)channels;
pushFrame.Buffer.SampleRate = (uint)sampleRate;
pushFrame.Buffer.SamplesPerChannel = (uint)data.Length / (uint)channels;
using var response = request.Send();
FfiResponse res = response;
// Wait for async callback, log an error if the capture fails.
var asyncId = res.CaptureAudioFrame.AsyncId;
void Callback(CaptureAudioFrameCallback callback)
{
if (callback.AsyncId != asyncId) return;
if (callback.HasError)
Utils.Error($"Audio capture failed: {callback.Error}");
FfiClient.Instance.CaptureAudioFrameReceived -= Callback;
}
FfiClient.Instance.CaptureAudioFrameReceived += Callback;
}
/// <summary>
/// Mutes or unmutes the audio source.
/// </summary>
public override void SetMute(bool muted)
{
_muted = muted;
}
/// <summary>
/// Disposes of the audio source, stopping it first if necessary.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed && disposing) Stop();
if (_frameData.IsCreated) _frameData.Dispose();
_disposed = true;
}
~RtcAudioSource()
{
Dispose(false);
}
[Obsolete("No longer used, audio sources should perform any preparation in Start() asynchronously")]
public virtual IEnumerator Prepare(float timeout = 0) { yield break; }
[Obsolete("Use Start() instead")]
public IEnumerator PrepareAndStart()
{
Start();
yield break;
}
}
}