-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace DSharpPlus.VoiceLink.AudioDecoders | ||
{ | ||
public delegate IAudioDecoder AudioDecoderFactory(IServiceProvider serviceProvider); | ||
public interface IAudioDecoder | ||
{ | ||
public int GetMaxBufferSize(); | ||
public int Decode(bool hasPacketLoss, ReadOnlySpan<byte> input, Span<byte> output); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/DSharpPlus.VoiceLink/AudioDecoders/OpusAudioDecoder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace DSharpPlus.VoiceLink.AudioDecoders | ||
{ | ||
public class OpusAudioDecoder : IAudioDecoder | ||
{ | ||
private const int CHANNELS = 2; | ||
private const int MAX_FRAME_SIZE = 5760; | ||
private const int MAX_BUFFER_SIZE = MAX_FRAME_SIZE * 2 * CHANNELS; | ||
|
||
public int GetMaxBufferSize() => MAX_BUFFER_SIZE; | ||
public int Decode(bool hasPacketLoss, ReadOnlySpan<byte> input, Span<byte> output) | ||
{ | ||
input.CopyTo(output); | ||
return input.Length; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/DSharpPlus.VoiceLink/AudioDecoders/Pcm16BitAudioDecoder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using DSharpPlus.VoiceLink.Opus; | ||
|
||
namespace DSharpPlus.VoiceLink.AudioDecoders | ||
{ | ||
public class Pcm16BitAudioDecoder : IAudioDecoder | ||
{ | ||
// 48 kHz | ||
private const int SAMPLE_RATE = 48000; | ||
|
||
// 20 milliseconds | ||
private const double FRAME_DURATION = 0.020; | ||
|
||
// 960 samples | ||
private const int FRAME_SIZE = (int)(SAMPLE_RATE * FRAME_DURATION); | ||
|
||
// Stereo audio + opus PCM units are 16 bits | ||
private const int BUFFER_SIZE = FRAME_SIZE * 2 * sizeof(short); | ||
|
||
/// <inheritdoc/> | ||
public int GetMaxBufferSize() => BUFFER_SIZE; | ||
|
||
private OpusDecoder _opusDecoder { get; init; } = OpusDecoder.Create(OpusSampleRate.Opus48000Hz, 2); | ||
|
||
/// <inheritdoc/> | ||
public int Decode(bool hasPacketLoss, ReadOnlySpan<byte> input, Span<byte> output) | ||
{ | ||
_opusDecoder.Decode(input, output, FRAME_SIZE, hasPacketLoss); | ||
return BUFFER_SIZE; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters