Vixen
02b45cc4
csharp
public sealed class OpusPacketEncoder

Turns captured audio into Opus packets, one packet at a time.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

A packet, not a stream. OpusStreamDecoder reads Opus out of an Ogg, because a track on disk is a container full of packets. Voice is the other shape: one datagram is one packet, it is self-contained, and the next one may never arrive. Framing it in an Ogg would be adding a container so it can immediately be taken off again.

Twenty milliseconds, because a conversation notices latency before it notices bandwidth. Opus offers 2.5 to 60 ms. Ten halves the delay and spends about a third more on per-packet overhead; sixty saves bandwidth and adds delay two people talking over each other can feel. Twenty is what every telephony system converged on, and it is the default here for the same reason.

VOIP and not AUDIO. The application hint changes what the encoder protects when it runs out of bits: speech intelligibility, rather than the top octave of a cymbal. For a game's voice channel that is the right trade every time, and it is why this is a separate type from the music path rather than a flag on it.

Forward error correction is off until somebody says what the link is like. Opus can carry a coarse copy of the previous frame inside the current one, so a single lost packet is recoverable from its successor — but only if ExpectedPacketLoss is set, and it costs bitrate whether or not anything is lost. An encoder that guessed would be spending a player's bandwidth on a problem they may not have.

Fields and properties (7)

  • public const int Rate

    The rate Opus encodes at. Anything else would be resampled twice on the way through.

  • public const int MaxPacketBytes

    The largest a single-frame Opus packet can be, from the specification.

  • public int FrameSize

    How many frames go into one packet.

  • public AudioFormat Format

    What it expects to be handed.

  • public int Bitrate

    Bits a second. Changing it takes effect on the next packet.

  • public int ExpectedPacketLoss

    What percentage of packets the link is losing, and with it whether to carry error correction at all.

  • public bool UseDiscontinuous

    Whether the encoder may emit a two-byte packet instead of a real one when nothing is happening.

Methods (4)

  • public OpusPacketEncoder(int channels = 1, int frameMilliseconds = 20, int bitrate = 24000)

    An encoder for one voice.

  • public int Encode(ReadOnlySpan<float> pcm, Span<byte> packet)

    Encodes exactly one frame.

  • public void Reset()

    Forgets everything it knew about the signal, for a new talker in the same slot.

  • public void Dispose()

    Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Used by (8)

  • GeneratedVideoVideoPlayback
  • PeerVoiceChat
  • VoiceLinkVoiceChat
  • OpusAudioTrackTestsVixen.Video.Codecs.Tests
  • OpusPacketDecoderVixen.Audio.Codecs
  • VoiceReceiverVixen.Audio.Codecs
  • VoiceSenderVixen.Audio.Codecs
  • VoiceTestsVixen.Audio.Codecs.Tests