Package net.dv8tion.jda.api.audio
Class OpusPacket
java.lang.Object
net.dv8tion.jda.api.audio.OpusPacket
- All Implemented Interfaces:
Comparable<OpusPacket>
A raw OPUS packet received from Discord that can be used for lazy decoding.
- Since:
- 4.0.0
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic 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
ConstructorDescriptionOpusPacket
(net.dv8tion.jda.internal.audio.AudioPacket packet, long userId, net.dv8tion.jda.internal.audio.Decoder decoder) -
Method Summary
Modifier and TypeMethodDescriptionboolean
Whetherdecode()
is possible.int
short[]
decode()
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
getSSRC()
The synchronization source identifier (SSRC) for the user that sent this audio packet.int
The timestamp for this packet.long
The ID of the responsibleUser
.int
hashCode()
-
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_SIZEAn 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_AMOUNTThis is 20 milliseconds. We are only dealing with 20ms opus packets.- See Also:
-
OPUS_CHANNEL_COUNT
public static final int OPUS_CHANNEL_COUNTWe 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
-
-
Method Details
-
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:
-
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 responsibleUser
.- Returns:
- The user id
-
canDecode
public boolean canDecode()Whetherdecode()
is possible.- Returns:
- True, if decode is possible.
-
getOpusAudio
The raw opus audio, copied to a new array.- Returns:
- The raw opus audio
-
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
- IfcanDecode()
is false- See Also:
-
getAudioData
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
- Ifdecode()
returns null
-
getAudioData
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:
IllegalArgumentException
- Ifdecoded
is null
-
compareTo
- Specified by:
compareTo
in interfaceComparable<OpusPacket>
-
hashCode
public int hashCode() -
equals
-