io.agora.rtc.utils

## Class AudioConsumerUtils

- java.lang.Object
- - io.agora.rtc.utils.AudioConsumerUtils

-     public class AudioConsumerUtils
      extends java.lang.Object

  Base class for consuming PCM data and pushing it to RTC channels. In AI scenarios: - When TTS returns data: Call AudioConsumerUtils.pushPcmData method to directly push the returned TTS data to AudioConsumerUtils - In another "timer" trigger function, call AudioConsumerUtils.consume() method to push data to RTC Recommendations: - "Timer" can be implemented using various approaches . Just need to call AudioConsumerUtils.consume() in the timer trigger function - Timer trigger interval can be consistent with existing business timer intervals or adjusted based on business needs, recommended between 40-80ms Usage Pattern: 1. Prerequisites: - Application needs to implement its own timer with interval between [10ms,80ms]. The timer trigger method is represented as app::TimeFunc. - One user can only correspond to one AudioConsumerUtils object, ensuring one producer corresponds to one consumer. 2. Usage Steps: A. Create an AudioConsumerUtils object for each "PCM data producing" userid, ensuring one producer corresponds to one consumer B. When PCM data is generated (e.g., TTS return), call AudioConsumer.AudioConsumerUtils(data) C. When consumption is needed , call AudioConsumerUtils.consume() method, which will automatically complete data consumption by pushing to RTC channel D. For interruption (e.g., stopping current AI dialogue): call AudioConsumerUtils.clear() method, which will automatically clear current buffer data E. On exit, call release() method to free resources

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private java.nio.ByteBuffer` | `buffer` Audio data buffer
    `private static int` | `BYTES_PER_SAMPLE` Bytes per sample for 16-bit audio
    `private AgoraRtcConn` | `conn` RTC connection instance
    `private int` | `consumedFrameCount` Number of consumed frames
    `private static int` | `INTERVAL_ONE_FRAME` Interval for one frame in milliseconds
    `private static int` | `INTERVAL_PCM_INTERRUPT` Interval for PCM interrupt in milliseconds
    `private boolean` | `isDirectSend` Whether to use direct send mode
    `private long` | `lastSendTimestamp` Timestamp of last send operation
    `private int` | `numOfChannels` Number of audio channels
    `private int` | `oneFrameSize` Size of one frame in bytes
    `private int` | `sampleRate` Sample rate in Hz
    `private static int` | `START_BY_MAX_FRAME_SIZE` Maximum frame size for starting cache
    `private static int` | `START_BY_MIN_FRAME_SIZE` Minimum frame size for starting cache
    `private int` | `startCacheDataSize` Initial cache data size
    `private long` | `startedTimestamp` Timestamp when consumption started

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AudioConsumerUtils(AgoraRtcConn conn, int numOfChannels, int sampleRate)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()` Clear the cache.
    `int` | `consume()` Consume data from the cache and return consume frame count.
    `private int` | `directConsume()` Consumes data directly without buffering.
    `private byte[]` | `extractData(int size, long currentTime)` Extract data from the buffer.
    `private int` | `getBufferSize()` Gets the current buffer size.
    `int` | `getOneFrameSize()` Gets the size of one frame in bytes.
    `int` | `getRemainingCacheDurationInMs()` Gets the remaining cache duration in milliseconds.
    `int` | `getSamplesPerChannel(int dataSize)` Calculate samples per channel.
    `void` | `pushPcmData(byte[] data)` Push pcm data into the cache.
    `void` | `release()` Releases all resources and clears the buffer.
    `private int` | `sendPcmData(byte[] data)` Sends PCM data to the RTC connection.
    `private int` | `undirectConsume()` Consumes data from the cache with buffering.

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

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

- - ### Field Detail

    - #### START_BY_MAX_FRAME_SIZE

          private static final int START_BY_MAX_FRAME_SIZE

      Maximum frame size for starting cache
      See Also:
        Constant Field Values

    - #### START_BY_MIN_FRAME_SIZE

          private static final int START_BY_MIN_FRAME_SIZE

      Minimum frame size for starting cache
      See Also:
        Constant Field Values

    - #### INTERVAL_ONE_FRAME

          private static final int INTERVAL_ONE_FRAME

      Interval for one frame in milliseconds
      See Also:
        Constant Field Values

    - #### INTERVAL_PCM_INTERRUPT

          private static final int INTERVAL_PCM_INTERRUPT

      Interval for PCM interrupt in milliseconds
      See Also:
        Constant Field Values

    - #### BYTES_PER_SAMPLE

          private static final int BYTES_PER_SAMPLE

      Bytes per sample for 16-bit audio
      See Also:
        Constant Field Values

    - #### conn

          private AgoraRtcConn conn

      RTC connection instance

    - #### numOfChannels

          private final int numOfChannels

      Number of audio channels

    - #### sampleRate

          private final int sampleRate

      Sample rate in Hz

    - #### oneFrameSize

          private final int oneFrameSize

      Size of one frame in bytes

    - #### startCacheDataSize

          private int startCacheDataSize

      Initial cache data size

    - #### buffer

          private java.nio.ByteBuffer buffer

      Audio data buffer

    - #### startedTimestamp

          private long startedTimestamp

      Timestamp when consumption started

    - #### lastSendTimestamp

          private long lastSendTimestamp

      Timestamp of last send operation

    - #### consumedFrameCount

          private int consumedFrameCount

      Number of consumed frames

    - #### isDirectSend

          private final boolean isDirectSend

      Whether to use direct send mode

  - ### Constructor Detail

    - #### AudioConsumerUtils

          public AudioConsumerUtils(AgoraRtcConn conn,
                                    int numOfChannels,
                                    int sampleRate)

      Constructor.
      Parameters:
        `conn` - The AgoraRtcConn.
        `numOfChannels` - The number of channels.
        `sampleRate` - The sample rate.

  - ### Method Detail

    - #### getOneFrameSize

          public int getOneFrameSize()

      Gets the size of one frame in bytes.
      Returns:
        One frame size in bytes

    - #### getSamplesPerChannel

          public int getSamplesPerChannel(int dataSize)

      Calculate samples per channel.
      Parameters:
        `dataSize` - The data size.
      Returns:
        samples per channel.

    - #### pushPcmData

          public void pushPcmData(byte[] data)

      Push pcm data into the cache.
      Parameters:
        `data` - The pcm data.

    - #### consume

          public int consume()

      Consume data from the cache and return consume frame count.
      Returns:
        frame count with 10ms one frame size

    - #### directConsume

          private int directConsume()

      Consumes data directly without buffering.
      Returns:
        Number of frames consumed, or error code

    - #### undirectConsume

          private int undirectConsume()

      Consumes data from the cache with buffering.
      Returns:
        Number of frames consumed, or error code

    - #### extractData

          private byte[] extractData(int size,
                                     long currentTime)

      Extract data from the buffer.
      Parameters:
        `size` - The size of the data.
        `currentTime` - The current time.
      Returns:
        the extracted data.

    - #### sendPcmData

          private int sendPcmData(byte[] data)

      Sends PCM data to the RTC connection.
      Parameters:
        `data` - PCM data to send
      Returns:
        Number of frames sent, or error code

    - #### getBufferSize

          private int getBufferSize()

      Gets the current buffer size.
      Returns:
        Buffer size in bytes

    - #### getRemainingCacheDurationInMs

          public int getRemainingCacheDurationInMs()

      Gets the remaining cache duration in milliseconds.
      Returns:
        the remaining cache duration in milliseconds.

    - #### clear

          public void clear()

      Clear the cache.

    - #### release

          public void release()

      Releases all resources and clears the buffer.