Class AudioPacer
java.lang.Object
com.alibaba.dashscope.resources.audio.AudioPacer
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 ClassesModifier and TypeClassDescriptionstatic final classSingle-use, file-backed chunk iterable that owns one handle;AudioPacer.PcmFileChunks.close()releases it and is terminal. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationWall-clock interval between consecutive chunks in a paced upload loop. -
Method Summary
Modifier and TypeMethodDescriptionstatic intbytesPer100ms(int sampleRate) Bytes per 100 ms of pcm16 mono audio atsampleRate(e.g. 3200 for 16000 Hz).static intbytesPer100ms(int sampleRate, int bytesPerSample, int channels) Bytes per 100 ms for arbitrary PCM layouts.static voidpace(long startNanos, int chunkIndex) Drift-free pacing: block untilstartNanos + chunkIndex * CHUNK_INTERVAL(nanoTime clock).static Iterable<byte[]>pcmChunks(byte[] pcm, int bytesPerChunk) Split an in-memory PCM buffer (final short chunk included).static AudioPacer.PcmFileChunksLazily split a PCM file intobytesPerChunkbyte chunks (the final short chunk is included; an empty file yields no chunks).
-
Field Details
-
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 atsampleRate(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
Lazily split a PCM file intobytesPerChunkbyte 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 whenAudioPacer.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. APcmFileChunksis owned by one thread:close()and iteration must not race. -
pcmChunks
Split an in-memory PCM buffer (final short chunk included). -
pace
public static void pace(long startNanos, int chunkIndex) Drift-free pacing: block untilstartNanos + chunkIndex * CHUNK_INTERVAL(nanoTime clock). Call once per sent chunk with a fixedstartNanos; catch-up is immediate when the caller falls behind, and no drift accumulates when it keeps up.
-