Class AudioPacer

java.lang.Object
com.alibaba.dashscope.resources.audio.AudioPacer

public final class AudioPacer extends Object
PCM chunking + pacing helpers (audio design §4/§12): chunk size is derived from the sample rate — one chunk carries exactly 100L ms of audio, so the classic "sleep 100 ms per chunk" upload loop stays real-time at any format. This fixes the 2.x pattern of a fixed 16 KB chunk + sleep(100) that drifted off real time for non-16kHz/mono/16-bit sources.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Single-use, file-backed chunk iterable that owns one handle; AudioPacer.PcmFileChunks.close() releases it and is terminal.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Duration
    Wall-clock interval between consecutive chunks in a paced upload loop.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    bytesPer100ms(int sampleRate)
    Bytes per 100 ms of pcm16 mono audio at sampleRate (e.g. 3200 for 16000 Hz).
    static int
    bytesPer100ms(int sampleRate, int bytesPerSample, int channels)
    Bytes per 100 ms for arbitrary PCM layouts.
    static void
    pace(long startNanos, int chunkIndex)
    Drift-free pacing: block until startNanos + chunkIndex * CHUNK_INTERVAL (nanoTime clock).
    static Iterable<byte[]>
    pcmChunks(byte[] pcm, int bytesPerChunk)
    Split an in-memory PCM buffer (final short chunk included).
    pcmChunks(Path path, int bytesPerChunk)
    Lazily split a PCM file into bytesPerChunk byte chunks (the final short chunk is included; an empty file yields no chunks).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CHUNK_INTERVAL

      public static final Duration CHUNK_INTERVAL
      Wall-clock interval between consecutive chunks in a paced upload loop.
  • Method Details

    • bytesPer100ms

      public static int bytesPer100ms(int sampleRate)
      Bytes per 100 ms of pcm16 mono audio at sampleRate (e.g. 3200 for 16000 Hz).
    • bytesPer100ms

      public static int bytesPer100ms(int sampleRate, int bytesPerSample, int channels)
      Bytes per 100 ms for arbitrary PCM layouts.
    • pcmChunks

      public static AudioPacer.PcmFileChunks pcmChunks(Path path, int bytesPerChunk)
      Lazily split a PCM file into bytesPerChunk byte chunks (the final short chunk is included; an empty file yields no chunks). The returned iterable is single-use and owns one file handle, opened when AudioPacer.PcmFileChunks.iterator() is first called and closed when iteration reaches EOF. Callers that abandon iteration early (a failed send, break) must close it, preferably with try-with-resources, otherwise the handle leaks. A PcmFileChunks is owned by one thread: close() and iteration must not race.
    • pcmChunks

      public static Iterable<byte[]> pcmChunks(byte[] pcm, int bytesPerChunk)
      Split an in-memory PCM buffer (final short chunk included).
    • pace

      public static void pace(long startNanos, int chunkIndex)
      Drift-free pacing: block until startNanos + chunkIndex * CHUNK_INTERVAL (nanoTime clock). Call once per sent chunk with a fixed startNanos; catch-up is immediate when the caller falls behind, and no drift accumulates when it keeps up.