Interface AudioReceiveHandler


  • public interface AudioReceiveHandler
    Interface used to receive audio from Discord through JDA.
    • Field Detail

      • OUTPUT_FORMAT

        static final javax.sound.sampled.AudioFormat OUTPUT_FORMAT
        Audio Output Format used by JDA. 48KHz 16bit stereo signed BigEndian PCM.
    • Method Detail

      • canReceiveCombined

        default boolean canReceiveCombined()
        If this method returns true, then JDA will generate combined audio data and provide it to the handler.
        Only enable if you specifically want combined audio because combining audio is costly if unused.
        Returns:
        If true, JDA enables subsystems to combine all user audio into a single provided data packet.
      • canReceiveUser

        default boolean canReceiveUser()
        If this method returns true, then JDA will provide audio data to the handleUserAudio(UserAudio) method.
        Returns:
        If true, JDA enables subsystems to provide user specific audio data.
      • canReceiveEncoded

        default boolean canReceiveEncoded()
        If this method returns true, then JDA will provide raw OPUS encoded packets to handleEncodedAudio(OpusPacket).
        This can be used in combination with the other receive methods but will not be combined audio of multiple users.

        Each user sends their own stream of OPUS encoded audio and each packet is assigned with a user id and SSRC. The decoder will be provided by JDA but need not be used.

        Returns:
        True, if handleEncodedAudio(OpusPacket) should receive opus packets.
        Since:
        4.0.0
      • handleCombinedAudio

        default void handleCombinedAudio​(@Nonnull
                                         CombinedAudio combinedAudio)
        If canReceiveCombined() returns true, JDA will provide a CombinedAudio object to this method every 20 milliseconds. The data provided by CombinedAudio is all audio that occurred during the 20 millisecond period mixed together into a single 20 millisecond packet. If no users spoke, this method will still be provided with a CombinedAudio object containing 20 milliseconds of silence and CombinedAudio.getUsers()'s list will be empty.

        The main use of this method is if you are wanting to record audio. Because it automatically combines audio and maintains timeline (no gaps in audio due to silence) it is an incredible resource for audio recording.

        If you are wanting to do audio processing (voice recognition) or you only want to deal with a single user's audio, please consider handleUserAudio(UserAudio).

        Output audio format: 48KHz 16bit stereo signed BigEndian PCM
        and is defined by: AudioRecieveHandler.OUTPUT_FORMAT

        Parameters:
        combinedAudio - The combined audio data.
      • handleUserAudio

        default void handleUserAudio​(@Nonnull
                                     UserAudio userAudio)
        If canReceiveUser() returns true, JDA will provide a UserAudio object to this method every time the user speaks. Continuing with the last statement: This method is only fired when discord provides us audio data which is very different from the scheduled firing time of handleCombinedAudio(CombinedAudio).

        The UserAudio object provided to this method will contain the User that spoke along with only the audio data sent by the specific user.

        The main use of this method is for listening to specific users. Whether that is for audio recording, custom mixing (possibly for user muting), or even voice recognition, this is the method you will want.

        If you are wanting to do audio recording, please consider handleCombinedAudio(CombinedAudio) as it was created just for that reason.

        Output audio format: 48KHz 16bit stereo signed BigEndian PCM
        and is defined by: AudioRecieveHandler.OUTPUT_FORMAT

        Parameters:
        userAudio - The user audio data
      • includeUserInCombinedAudio

        default boolean includeUserInCombinedAudio​(@Nonnull
                                                   User user)
        This method is a filter predicate used by JDA to determine whether or not to include a User's audio when creating a CombinedAudio packet.

        This method is especially useful in creating whitelist / blacklist functionality for receiving audio.

        A few possible examples:

        • Have this method always return false for Users that are bots.
        • Have this method return false for users who have been placed on a blacklist for abusing the bot's functionality.
        • Have this method only return true if the user is in a special whitelist of power users.
        Parameters:
        user - The user whose audio was received
        Returns:
        If true, JDA will include the user's audio when merging audio sources when created packets for handleCombinedAudio(CombinedAudio)