Package net.dv8tion.jda.api.audio
Class OpusPacket
- java.lang.Object
-
- net.dv8tion.jda.api.audio.OpusPacket
-
- All Implemented Interfaces:
java.lang.Comparable<OpusPacket>
public final class OpusPacket extends java.lang.Object implements java.lang.Comparable<OpusPacket>
A raw OPUS packet received from Discord that can be used for lazy decoding.- Since:
- 4.0.0
- See Also:
AudioReceiveHandler.canReceiveEncoded()
,AudioReceiveHandler.handleEncodedAudio(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()
Whetherdecode()
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 responsibleUser
.int
hashCode()
-
-
-
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
-
-
Method Detail
-
getSequence
public char getSequence()
The sequence number of this packet. This is used as ordering key forcompareTo(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 responsibleUser
.- Returns:
- The user id
-
canDecode
public boolean canDecode()
Whetherdecode()
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-casesgetAudioData(double)
should be used instead.- Returns:
- The decoded audio or
null
if decoding failed for some reason. - Throws:
java.lang.IllegalStateException
- IfcanDecode()
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
- Ifdecode()
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 datavolume
- The volume- Returns:
- The stereo PCM audio data as specified by
AudioReceiveHandler.OUTPUT_FORMAT
. - Throws:
java.lang.IllegalArgumentException
- Ifdecoded
is null
-
compareTo
public int compareTo(@Nonnull OpusPacket o)
- Specified by:
compareTo
in interfacejava.lang.Comparable<OpusPacket>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-