Skip to content

Commit 0892526

Browse files
committed
Add set stream delay method
1 parent aad73bc commit 0892526

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Runtime/Scripts/AudioProcessingModule.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,35 @@ public void ProcessReverseStream(AudioFrame data)
9696
throw new Exception(res.ApmProcessReverseStream.Error);
9797
}
9898
}
99+
100+
/// <summary>
101+
/// This must be called if and only if echo processing is enabled.
102+
/// </summary>
103+
/// <remarks>
104+
/// Sets the `delay` in milliseconds between receiving a far-end frame in <see cref="ProcessReverseStream"/>
105+
/// and receiving the corresponding echo in a near-end frame in <see cref="ProcessStream"/>.
106+
///
107+
/// The delay can be calculated as: delay = (t_render - t_analyze) + (t_process - t_capture)
108+
///
109+
/// Where:
110+
/// - t_analyze: Time when frame is passed to <see cref="ProcessReverseStream"/>
111+
/// - t_render: Time when first sample of frame is rendered by audio hardware
112+
/// - t_capture: Time when first sample of frame is captured by audio hardware
113+
/// - t_process: Time when frame is passed to <see cref="ProcessStream"/>
114+
/// </remarks>
115+
public void SetStreamDelayMs(int delayMs)
116+
{
117+
using var request = FFIBridge.Instance.NewRequest<ApmSetStreamDelayRequest>();
118+
var setStreamDelay = request.request;
119+
setStreamDelay.ApmHandle = (ulong)Handle.DangerousGetHandle();
120+
setStreamDelay.DelayMs = delayMs;
121+
122+
using var response = request.Send();
123+
FfiResponse res = response;
124+
if (res.ApmSetStreamDelay.HasError)
125+
{
126+
throw new Exception(res.ApmSetStreamDelay.Error);
127+
}
128+
}
99129
}
100130
}

Runtime/Scripts/Internal/FFIClients/FfiRequestExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ public static void Inject<T>(this FfiRequest ffiRequest, T request)
8787
case E2eeRequest e2EeRequest:
8888
ffiRequest.E2Ee = e2EeRequest;
8989
break;
90+
// Apm
91+
case NewApmRequest newApmRequest:
92+
ffiRequest.NewApm = newApmRequest;
93+
break;
94+
case ApmProcessStreamRequest apmProcessStreamRequest:
95+
ffiRequest.ApmProcessStream = apmProcessStreamRequest;
96+
break;
97+
case ApmProcessReverseStreamRequest apmProcessReverseStreamRequest:
98+
ffiRequest.ApmProcessReverseStream = apmProcessReverseStreamRequest;
99+
break;
100+
case ApmSetStreamDelayRequest apmSetStreamDelayRequest:
101+
ffiRequest.ApmSetStreamDelay = apmSetStreamDelayRequest;
102+
break;
90103
// Rpc
91104
case RegisterRpcMethodRequest registerRpcMethodRequest:
92105
ffiRequest.RegisterRpcMethod = registerRpcMethodRequest;

Tests/AudioProcessingModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class AudioProcessingModuleTest
1010
public void TestAudioProcessing()
1111
{
1212
var apm = new AudioProcessingModule(true, true, true, true);
13-
13+
apm.SetStreamDelayMs(100);
1414
apm.ProcessStream(CreateTestFrame());
1515
apm.ProcessReverseStream(CreateTestFrame());
1616

0 commit comments

Comments
 (0)