Class OpusPacket

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int OPUS_CHANNEL_COUNT
      We want to use stereo.
      static int OPUS_FRAME_SIZE
      An opus frame size of 960 at 48000hz represents 20 milliseconds of audio.
      static int OPUS_FRAME_TIME_AMOUNT
      This is 20 milliseconds.
      static int OPUS_SAMPLE_RATE
      (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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canDecode()
      Whether decode() is possible.
      int compareTo​(OpusPacket o)  
      short[] decode()
      Attempts to decode the opus packet.
      boolean equals​(java.lang.Object obj)  
      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[] getOpusAudio()
      The raw opus audio, copied to a new array.
      char getSequence()
      The sequence number of this packet.
      int getSSRC()
      The synchronization source identifier (SSRC) for the user that sent this audio packet.
      int getTimestamp()
      The timestamp for this packet.
      long getUserId()
      The ID of the responsible User.
      int hashCode()  
      • Methods inherited from class java.lang.Object

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

      • OPUS_SAMPLE_RATE

        public static final int OPUS_SAMPLE_RATE
        (Hz) We want to use the highest of qualities! All the bandwidth!
        See Also:
        Constant Field Values
      • 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:
        Constant Field Values
      • 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:
        Constant Field Values
      • 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:
        Constant Field Values
    • Constructor Detail

      • OpusPacket

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

      • 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:
        RTP Header
      • getTimestamp

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

        public int getSSRC()
        The synchronization source identifier (SSRC) for the user that sent this audio packet.
        Returns:
        The SSRC
        See Also:
        RTP Header
      • 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:
        java.lang.IllegalStateException - If canDecode() is false
        See Also:
        canDecode(), getAudioData(double)
      • 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:
        java.lang.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:
        java.lang.IllegalArgumentException - If decoded is null
      • compareTo

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

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object