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 AudioFrame` | `audioFrame`
    `private AgoraAudioPcmDataSender` | `audioFrameSender`
    `private java.nio.ByteBuffer` | `buffer`
    `private static int` | `BYTES_PER_SAMPLE`
    `private int` | `consumedFrameCount`
    `private static int` | `INTERVAL_ONE_FRAME`
    `private static int` | `INTERVAL_PCM_INTERRUPT`
    `private long` | `lastSendTimestamp`
    `private int` | `numOfChannels`
    `private int` | `oneFrameSize`
    `private int` | `sampleRate`
    `private static int` | `START_BY_MAX_FRAME_SIZE`
    `private static int` | `START_BY_MIN_FRAME_SIZE`
    `private int` | `startCacheDataSize`
    `private long` | `startedTimestamp`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AudioConsumerUtils(AgoraAudioPcmDataSender audioFrameSender, int numOfChannels, int sampleRate)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()`
    `int` | `consume()`
    `private byte[]` | `extractData(int size, long currentTime)`
    `private int` | `getBufferSize()`
    `int` | `getOneFrameSize()`
    `int` | `getRemainingCacheDurationInMs()` Gets the remaining cache duration in milliseconds.
    `int` | `getSamplesPerChannel(int dataSize)`
    `void` | `pushPcmData(byte[] data)`
    `void` | `release()`
    `private int` | `sendPcmData(byte[] data)`

    - ### 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

      See Also:
        Constant Field Values

    - #### START_BY_MIN_FRAME_SIZE

          private static final int START_BY_MIN_FRAME_SIZE

      See Also:
        Constant Field Values

    - #### INTERVAL_ONE_FRAME

          private static final int INTERVAL_ONE_FRAME

      See Also:
        Constant Field Values

    - #### INTERVAL_PCM_INTERRUPT

          private static final int INTERVAL_PCM_INTERRUPT

      See Also:
        Constant Field Values

    - #### BYTES_PER_SAMPLE

          private static final int BYTES_PER_SAMPLE

      See Also:
        Constant Field Values

    - #### audioFrameSender

          private AgoraAudioPcmDataSender audioFrameSender

    - #### numOfChannels

          private final int numOfChannels

    - #### sampleRate

          private final int sampleRate

    - #### oneFrameSize

          private final int oneFrameSize

    - #### startCacheDataSize

          private int startCacheDataSize

    - #### buffer

          private java.nio.ByteBuffer buffer

    - #### startedTimestamp

          private long startedTimestamp

    - #### lastSendTimestamp

          private long lastSendTimestamp

    - #### consumedFrameCount

          private int consumedFrameCount

    - #### audioFrame

          private final AudioFrame audioFrame

  - ### Constructor Detail

    - #### AudioConsumerUtils

          public AudioConsumerUtils(AgoraAudioPcmDataSender audioFrameSender,
                                    int numOfChannels,
                                    int sampleRate)

  - ### Method Detail

    - #### getOneFrameSize

          public int getOneFrameSize()

    - #### getSamplesPerChannel

          public int getSamplesPerChannel(int dataSize)

    - #### pushPcmData

          public void pushPcmData(byte[] data)

    - #### consume

          public int consume()

    - #### extractData

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

    - #### sendPcmData

          private int sendPcmData(byte[] data)

    - #### getBufferSize

          private int getBufferSize()

    - #### getRemainingCacheDurationInMs

          public int getRemainingCacheDurationInMs()

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

    - #### clear

          public void clear()

    - #### release

          public void release()