Skip to content

Commit 59094d2

Browse files
F0903quinchs
andauthored
Added a method for sending silent audio frames on OpusEncodeStream (#2668)
* Implemented ClientDisconnect event for audio client. * Added a method for sending silent frames. * moved comment * Added method summary. + removed changes to project file. * Removed residual stuff remaining from previous edits. * bunch of things * Revert "bunch of things" This reverts commit 292f23f. * Update src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs * Update src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs * Update src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs --------- Co-authored-by: Quin Lynch <[email protected]>
1 parent 2b8584d commit 59094d2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using System.Threading;
34
using System.Threading.Tasks;
45

@@ -23,6 +24,30 @@ public OpusEncodeStream(AudioStream next, int bitrate, AudioApplication applicat
2324
_buffer = new byte[OpusConverter.FrameBytes];
2425
}
2526

27+
/// <summary>
28+
/// Sends silent frames to avoid interpolation errors after breaks in data transmission.
29+
/// </summary>
30+
/// <returns>A task representing the asynchronous operation of sending a silent frame.</returns>
31+
public async Task WriteSilentFramesAsync()
32+
{
33+
// https://discord.com/developers/docs/topics/voice-connections#voice-data-interpolation
34+
35+
byte[] frameBytes = new byte[OpusConverter.FrameBytes];
36+
37+
// Magic silence numbers.
38+
frameBytes[0] = 0xF8;
39+
frameBytes[1] = 0xFF;
40+
frameBytes[2] = 0xFE;
41+
42+
// The rest of the array is already zeroes, so no need to fill the rest.
43+
44+
const int frameCount = 5;
45+
for (int i = 0; i < frameCount; i += 1)
46+
{
47+
await WriteAsync(frameBytes, 0, frameBytes.Length).ConfigureAwait(false);
48+
}
49+
}
50+
2651
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken)
2752
{
2853
//Assume thread-safe

0 commit comments

Comments
 (0)