Class OpusPacket

java.lang.Object
net.dv8tion.jda.api.audio.OpusPacket
All Implemented Interfaces:
Comparable<OpusPacket>

public final class OpusPacket extends Object implements Comparable<OpusPacket>
A raw OPUS packet received from Discord that can be used for lazy decoding.
Since:
4.0.0
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    We want to use stereo.
    static final int
    An opus frame size of 960 at 48000hz represents 20 milliseconds of audio.
    static final int
    This is 20 milliseconds.
    static final int
    (Hz) We want to use the highest of qualities! All the bandwidth!
  • Constructor Summary

    Constructors
    Constructor
    Description
    OpusPacket(net.dv8tion.jda.internal.audio.AudioPacket packet, long userId, net.dv8tion.jda.internal.audio.Decoder decoder)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Whether decode() is possible.
    int
     
    short[]
    Attempts to decode the opus packet.
    boolean
     
    byte[]
    getAudioData(double volume)
    Decodes and adjusts the opus audio for the specified volume.
    static byte[]
    getAudioData(short[] decoded, double volume)
    Decodes and adjusts the opus audio for the specified volume.
    byte[]
    The raw opus audio, copied to a new array.
    char
    The sequence number of this packet.
    int
    The synchronization source identifier (SSRC) for the user that sent this audio packet.
    int
    The timestamp for this packet.
    long
    The ID of the responsible User.
    int
     

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • OPUS_SAMPLE_RATE

      public static final int OPUS_SAMPLE_RATE
      (Hz) We want to use the highest of qualities! All the bandwidth!
      See Also:
    • OPUS_FRAME_SIZE

      public static final int OPUS_FRAME_SIZE
      An opus frame size of 960 at 48000hz represents 20 milliseconds of audio.
      See Also:
    • OPUS_FRAME_TIME_AMOUNT

      public static final int OPUS_FRAME_TIME_AMOUNT
      This is 20 milliseconds. We are only dealing with 20ms opus packets.
      See Also:
    • OPUS_CHANNEL_COUNT

      public static final int OPUS_CHANNEL_COUNT
      We want to use stereo. If the audio given is mono, the encoder promotes it to Left and Right mono (stereo that is the same on both sides)
      See Also:
  • Constructor Details

    • OpusPacket

      public OpusPacket(@Nonnull net.dv8tion.jda.internal.audio.AudioPacket packet, long userId, @Nullable net.dv8tion.jda.internal.audio.Decoder decoder)
  • Method Details

    • getSequence

      public char getSequence()
      The sequence number of this packet. This is used as ordering key for compareTo(OpusPacket).
      A char represents an unsigned short value in this case.

      Note that packet sequence is important for decoding. If a packet is out of sequence the decode step will fail.

      Returns:
      The sequence number of this packet
      See Also:
    • getTimestamp

      public int getTimestamp()
      The timestamp for this packet. As specified by the RTP header.
      Returns:
      The timestamp
      See Also:
    • getSSRC

      public int getSSRC()
      The synchronization source identifier (SSRC) for the user that sent this audio packet.
      Returns:
      The SSRC
      See Also:
    • getUserId

      public long getUserId()
      The ID of the responsible User.
      Returns:
      The user id
    • canDecode

      public boolean canDecode()
      Whether decode() is possible.
      Returns:
      True, if decode is possible.
    • getOpusAudio

      @Nonnull public byte[] getOpusAudio()
      The raw opus audio, copied to a new array.
      Returns:
      The raw opus audio
    • decode

      @Nullable public short[] decode()
      Attempts to decode the opus packet.
      This method is idempotent and will provide the same result on multiple calls without decoding again.

      For most use-cases getAudioData(double) should be used instead.

      Returns:
      The decoded audio or null if decoding failed for some reason.
      Throws:
      IllegalStateException - If canDecode() is false
      See Also:
    • getAudioData

      @Nonnull public byte[] getAudioData(double volume)
      Decodes and adjusts the opus audio for the specified volume.
      The provided volume should be a double precision floating point in the interval from 0 to 1. In this case 0.5 would represent 50% volume for instance.
      Parameters:
      volume - The volume
      Returns:
      The stereo PCM audio data as specified by AudioReceiveHandler.OUTPUT_FORMAT.
      Throws:
      IllegalArgumentException - If decode() returns null
    • getAudioData

      @Nonnull public static byte[] getAudioData(@Nonnull short[] decoded, double volume)
      Decodes and adjusts the opus audio for the specified volume.
      The provided volume should be a double precision floating point in the interval from 0 to 1. In this case 0.5 would represent 50% volume for instance.
      Parameters:
      decoded - The decoded audio data
      volume - The volume
      Returns:
      The stereo PCM audio data as specified by AudioReceiveHandler.OUTPUT_FORMAT.
      Throws:
      IllegalArgumentException - If decoded is null
    • compareTo

      public int compareTo(@Nonnull OpusPacket o)
      Specified by:
      compareTo in interface Comparable<OpusPacket>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object