AudioFlow

class AudioFlow(val format: AudioFormat, data: Flow<ByteArray>, val knownByteCount: Long? = null) : Flow<ByteArray>

A typed stream of raw PCM audio: an AudioFormat paired with a Flow of ByteArray chunks.

Every emitted chunk is PCM in format: same sample rate, channel count, and sample encoding. Chunk boundaries are arbitrary; consumers must treat the stream as one continuous byte sequence.

The wrapped Flow may be cold or hot. A cold flow re-emits its full sequence on each collector (typical for replayable recordings). A hot flow shares live emissions among concurrent collectors (typical while recording). format always describes the PCM layout of every chunk, regardless of cold or hot behavior.

Finite vs unbounded

When knownByteCount is non-null, the stream is a known-finite recording of exactly that many PCM bytes and is safe to materialize for buffer-backed seek. When null (live monitors, infinite/long streams, unknown size), playback must stream chunks without draining the whole flow; seek should return SeekResult.Unsupported.

Constructors

Link copied to clipboard
constructor(format: AudioFormat, data: Flow<ByteArray>, knownByteCount: Long? = null)

Properties

Link copied to clipboard

PCM layout shared by every chunk in the wrapped flow.

Link copied to clipboard

true when knownByteCount is set and buffer-backed seek is allowed.

Link copied to clipboard

Total PCM byte length when this flow is a known-finite recording. null means unbounded, live, or unknown; never fully collect for seek.

Functions

Link copied to clipboard
open suspend override fun collect(collector: FlowCollector<ByteArray>)
Link copied to clipboard
suspend fun AudioFlow.collectAsBuffer(format: AudioFileFormat): Buffer

Collects this flow and returns an in-memory buffer containing the encoded file bytes for format (WAV, AIFF, or AU).

Link copied to clipboard

Collects every chunk from this flow into one contiguous PCM buffer wrapped as an AudioSource with this flow's AudioFlow.format.

Link copied to clipboard

Collects every chunk into a single ByteArray.

Link copied to clipboard

Convert an AudioFlow to a different AudioFormat. Pipeline: bytes -> normalized samples -> resample -> channel convert -> bytes

Link copied to clipboard
suspend fun AudioFlow.writeToFile(format: AudioFileFormat, path: Path)

Collects this flow and writes the PCM data to a file at path.

Link copied to clipboard
suspend fun AudioFlow.writeToSink(format: AudioFileFormat, sink: Sink)

Collects this flow and writes the PCM data to sink in the chosen file format (WAV, AIFF, or AU).