io.agora.rtc

## Class AgoraAudioVadV2

- java.lang.Object
- - io.agora.rtc.AgoraAudioVadV2

-     public class AgoraAudioVadV2
      extends java.lang.Object

  Voice Activity Detection (VAD) V2 implementation for Agora SDK. This is an enhanced version with adaptive RMS threshold support and improved state management.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `private class` | `AgoraAudioVadV2.VadData` Simple container for an audio frame and its activity flag.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private int` | `adaptiveStartRmsThreshold` Adaptive RMS threshold for start speaking (cached for logging)
    `private int` | `adaptiveStopRmsThreshold` Adaptive RMS threshold for stop speaking (cached for logging)
    `private Constants.VadState` | `currentState` Current state of the voice activity detector.
    `private int` | `refAvgRmsInLastSession` range from 0 to 127, respond to db: -127db, to 0db
    `private int` | `silenceCount` Count of recent frames considered as silence.
    `private java.util.Deque<AgoraAudioVadV2.VadData>` | `startQueue` Buffer of frames collected while waiting to start speaking.
    `private int` | `startSize` Required frame count before declaring the speaking state.
    `private java.util.Deque<AgoraAudioVadV2.VadData>` | `stopQueue` Buffer of frames collected while confirming the stop state.
    `private int` | `totalVoiceRms` range from 0 to 127, respond to db: -127db, to 0db
    `private AgoraAudioVadConfigV2` | `vadConfig` Runtime configuration for the VAD algorithm.
    `private int` | `voiceCount` Count of recent frames considered as voice.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AgoraAudioVadV2(AgoraAudioVadConfigV2 config)` Creates a VAD instance with the specified configuration.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `destroy()` Destroys the VAD instance and clears cached data.
    `int` | `getAdaptiveStartRmsThreshold()` Get current adaptive start RMS threshold
    `int` | `getAdaptiveStopRmsThreshold()` Get current adaptive stop RMS threshold
    `private int` | `getRefActiveAvgRms(AgoraAudioVadConfigV2 currentVadConfig)` Get reference average RMS threshold for voice activity detection This implements the adaptive threshold logic from Go SDK
    `int` | `getRefAvgRmsInLastSession()` Get the reference average RMS from last session
    `private int` | `getSilenceCount(java.util.Deque<AgoraAudioVadV2.VadData> queue, int startIndex)` Counts the number of silent frames within a queue.
    `private void` | `initData()` Resets queues and counters according to the current configuration.
    `boolean` | `isActive()` Check if VAD is currently active (speaking detected)
    `private boolean` | `isVadActive(AudioFrame frame, AgoraAudioVadConfigV2 currentVadConfig)` Evaluates a frame to decide whether it is active speech.
    `VadProcessResult` | `processFrame(AudioFrame frame)` Process the audio frame
    `private byte[]` | `processSpeaking(AgoraAudioVadV2.VadData vadData, AgoraAudioVadConfigV2 currentVadConfig)` Handles frames while speaking and tracks potential stop transitions.
    `private byte[]` | `processStart(AgoraAudioVadV2.VadData vadData, AgoraAudioVadConfigV2 currentVadConfig)` Handles the transition from silence to speaking by analysing buffered frames.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### vadConfig

          private volatile AgoraAudioVadConfigV2 vadConfig

      Runtime configuration for the VAD algorithm.

    - #### currentState

          private volatile Constants.VadState currentState

      Current state of the voice activity detector.

    - #### startSize

          private volatile int startSize

      Required frame count before declaring the speaking state.

    - #### startQueue

          private volatile java.util.Deque<AgoraAudioVadV2.VadData> startQueue

      Buffer of frames collected while waiting to start speaking.

    - #### stopQueue

          private volatile java.util.Deque<AgoraAudioVadV2.VadData> stopQueue

      Buffer of frames collected while confirming the stop state.

    - #### voiceCount

          private volatile int voiceCount

      Count of recent frames considered as voice.

    - #### silenceCount

          private volatile int silenceCount

      Count of recent frames considered as silence.

    - #### totalVoiceRms

          private volatile int totalVoiceRms

      range from 0 to 127, respond to db: -127db, to 0db

    - #### refAvgRmsInLastSession

          private volatile int refAvgRmsInLastSession

      range from 0 to 127, respond to db: -127db, to 0db

    - #### adaptiveStartRmsThreshold

          private volatile int adaptiveStartRmsThreshold

      Adaptive RMS threshold for start speaking (cached for logging)

    - #### adaptiveStopRmsThreshold

          private volatile int adaptiveStopRmsThreshold

      Adaptive RMS threshold for stop speaking (cached for logging)

  - ### Constructor Detail

    - #### AgoraAudioVadV2

          public AgoraAudioVadV2(AgoraAudioVadConfigV2 config)

      Creates a VAD instance with the specified configuration.
      Parameters:
        `config` - VAD configuration

  - ### Method Detail

    - #### initData

          private void initData()

      Resets queues and counters according to the current configuration.

    - #### processFrame

          public VadProcessResult processFrame(AudioFrame frame)

      Process the audio frame
      Parameters:
        `frame` - the audio frame
      Returns:
        the result of the VAD process

    - #### isVadActive

          private boolean isVadActive(AudioFrame frame,
                                      AgoraAudioVadConfigV2 currentVadConfig)

      Evaluates a frame to decide whether it is active speech.
      Parameters:
        `frame` - Frame being evaluated
        `currentVadConfig` - Active configuration
      Returns:
        `true` if the frame is considered speech

    - #### getRefActiveAvgRms

          private int getRefActiveAvgRms(AgoraAudioVadConfigV2 currentVadConfig)

      Get reference average RMS threshold for voice activity detection This implements the adaptive threshold logic from Go SDK
      Parameters:
        `currentVadConfig` - the current VAD configuration
      Returns:
        the reference RMS threshold value (range: 0-127)

    - #### processStart

          private byte[] processStart(AgoraAudioVadV2.VadData vadData,
                                      AgoraAudioVadConfigV2 currentVadConfig)

      Handles the transition from silence to speaking by analysing buffered frames.
      Parameters:
        `vadData` - Newly appended data
        `currentVadConfig` - Active configuration
      Returns:
        Concatenated PCM when a start transition is confirmed; otherwise an empty buffer

    - #### processSpeaking

          private byte[] processSpeaking(AgoraAudioVadV2.VadData vadData,
                                         AgoraAudioVadConfigV2 currentVadConfig)

      Handles frames while speaking and tracks potential stop transitions.
      Parameters:
        `vadData` - Newly appended data
        `currentVadConfig` - Active configuration
      Returns:
        Frame data to forward downstream

    - #### getSilenceCount

          private int getSilenceCount(java.util.Deque<AgoraAudioVadV2.VadData> queue,
                                      int startIndex)

      Counts the number of silent frames within a queue.
      Parameters:
        `queue` - Queue containing historical data
        `startIndex` - Index to begin counting
      Returns:
        Number of silent frames

    - #### isActive

          public boolean isActive()

      Check if VAD is currently active (speaking detected)
      Returns:
        true if in speaking state, false otherwise

    - #### getRefAvgRmsInLastSession

          public int getRefAvgRmsInLastSession()

      Get the reference average RMS from last session
      Returns:
        average RMS value from last speaking session (range: 0-127)

    - #### getAdaptiveStartRmsThreshold

          public int getAdaptiveStartRmsThreshold()

      Get current adaptive start RMS threshold
      Returns:
        current adaptive start RMS threshold value (range: 0-127)

    - #### getAdaptiveStopRmsThreshold

          public int getAdaptiveStopRmsThreshold()

      Get current adaptive stop RMS threshold
      Returns:
        current adaptive stop RMS threshold value (range: 0-127)

    - #### destroy

          public void destroy()

      Destroys the VAD instance and clears cached data.