Skip to content

Commit

Permalink
Added a method for sending silent audio frames on OpusEncodeStream (#…
Browse files Browse the repository at this point in the history
…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 <49576606+quinchs@users.noreply.github.com>
  • Loading branch information
F0903 and quinchs committed Aug 10, 2023
1 parent 2b8584d commit 59094d2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Discord.Net.WebSocket/Audio/Streams/OpusEncodeStream.cs
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

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

/// <summary>
/// Sends silent frames to avoid interpolation errors after breaks in data transmission.
/// </summary>
/// <returns>A task representing the asynchronous operation of sending a silent frame.</returns>
public async Task WriteSilentFramesAsync()
{
// https://discord.com/developers/docs/topics/voice-connections#voice-data-interpolation

byte[] frameBytes = new byte[OpusConverter.FrameBytes];

// Magic silence numbers.
frameBytes[0] = 0xF8;
frameBytes[1] = 0xFF;
frameBytes[2] = 0xFE;

// The rest of the array is already zeroes, so no need to fill the rest.

const int frameCount = 5;
for (int i = 0; i < frameCount; i += 1)
{
await WriteAsync(frameBytes, 0, frameBytes.Length).ConfigureAwait(false);
}
}

public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken)
{
//Assume thread-safe
Expand Down

0 comments on commit 59094d2

Please sign in to comment.