//clika-runtime/io.clika.runtime/Io
Io
[common]
object Io
Decoding and encoding of images, audio and video, and the NumPy .npy entry and exit forms of a tensor.
Images decode from JPEG, PNG, GIF, BMP, TGA, PSD, HDR, PNM and PIC to a host [H, W, C] UInt8 tensor, the image as displayed; audio decodes from WAV, FLAC, MP3 and OGG (Vorbis) to a host Float32 waveform; a video decodes through the ffmpeg command the host provides (nothing in the runtime links a decoder), so videoCapability says whether video decode works on this device. Every failure is a ClikaRtException with its status and code name: a missing file, a format outside the set (UNSUPPORTED), damaged data or a file of another kind (INVALID_ARGUMENT).
val image = Io.loadImage("/path/photo.jpg") // [H, W, 3] UInt8
val png = Io.encodeImage(image) // a PNG payload
val clip = Io.loadAudio("/path/speech.wav", targetSampleRate = 16000, targetChannels = 1)
Functions
| Name | Summary |
|---|---|
| encodeAudio | [common] fun encodeAudio(samples: Tensor, sampleRate: Int): ByteArray Encode a waveform as a 16-bit PCM WAV payload. samples is [frames] (mono) or [frames, channels] interleaved: a float tensor holds values in -1, 1 (a value outside clips), an Int16 tensor is written as is. sampleRate is in Hz. loadAudio reads the payload back with the same layout. |
| encodeImage | [common] fun encodeImage(image: Tensor): ByteArray Encode a host image tensor as a PNG payload: [H, W] or [H, W, C] channels-last, UInt8 with 1, 3 or 4 channels, or a UInt16 single channel (16-bit gray). The PNG carries no color profile, so a decoder never changes the pixel values; a class-index mask or a depth map round-trips exactly. |
| loadAudio | [common] fun loadAudio(bytes: ByteArray, targetSampleRate: Int = 0, targetChannels: Int = 0): AudioData Decode an encoded audio payload in memory; the same targets as the file form. [common] fun loadAudio(path: String, targetSampleRate: Int = 0, targetChannels: Int = 0): AudioData Decode an audio file to a host Float32 waveform plus its layout. targetSampleRate and targetChannels of 0 keep the file's own values; a non-zero channel count mixes to it (mono is the equal-weight average of the source channels) and a non-zero rate resamples, so peekAudio with the same targets reports the frame count this returns. |
| loadImage | [common] fun loadImage(bytes: ByteArray, channels: Int = 0): Tensor Decode an encoded image payload in memory; the same contract as the file form. [common] fun loadImage(path: String, channels: Int = 0): Tensor Decode an image file to a host [H, W, C] UInt8 tensor. channels of 0 keeps the file's own channel count; 1, 3 or 4 forces gray, RGB or RGBA. An EXIF orientation tag is applied, so the tensor is the image as displayed. Move it to a device with Tensor.to. |
| loadNpy | [common] fun loadNpy(path: String, device: Device? = null): Tensor Load a NumPy .npy array as a tensor on device (the CPU when null). |
| loadVideo | [common] fun loadVideo(bytes: ByteArray): VideoData Decode a whole video from encoded bytes in memory. [common] fun loadVideo(path: String): VideoData Decode a whole video file: every frame as [T, H, W, 3] UInt8 on the host plus the stream facts. A large clip reads through VideoReader instead, which decodes the frames it is asked for. |
| peekAudio | [common] fun peekAudio(bytes: ByteArray, targetSampleRate: Int = 0, targetChannels: Int = 0): AudioInfo Read an audio header from an encoded payload in memory; the same targets as the file form. [common] fun peekAudio(path: String, targetSampleRate: Int = 0, targetChannels: Int = 0): AudioInfo Read an audio header from a file. targetSampleRate and targetChannels of 0 report the file's own values; a non-zero value reports the effective figures after the resample or downmix a decode applies. |
| peekImage | [common] fun peekImage(bytes: ByteArray): ImageInfo Read an image header from an encoded payload in memory. [common] fun peekImage(path: String): ImageInfo Read an image header from a file: its displayed height, width and channel count. |
| peekVideo | [common] fun peekVideo(bytes: ByteArray): VideoInfo Read a video header from encoded bytes in memory. [common] fun peekVideo(path: String): VideoInfo Read a video file's header without decoding a frame. |
| saveAudio | [common] fun saveAudio(samples: Tensor, sampleRate: Int, path: String) Write a waveform as a 16-bit PCM WAV file at path; the bytes are exactly encodeAudio's. A name asking for another container ( .mp3, .ogg, .flac, ...) is refused (UNSUPPORTED) with nothing written. |
| saveImage | [common] fun saveImage(image: Tensor, path: String) Write an image tensor to path in the format its extension asks for: .png (or none), .jpg / .jpeg, .bmp or .tga; a 16-bit image writes as PNG only. Another extension is refused (UNSUPPORTED) with nothing written. |
| saveNpy | [common] fun saveNpy(tensor: Tensor, path: String) Write a tensor as a .npy file at path (materialized contiguous on the host). A quantized tensor is refused with nothing written: dequantize it first. |
| temporaryDirectoryPath | [common] fun temporaryDirectoryPath(): String The system's temporary directory, as the runtime resolves it. |
| videoCapability | [common] fun videoCapability(): Boolean Whether video decode works on this device: the runtime drives the ffmpeg command the host provides and links no decoder, so this is false where none is found. The video entries then fail with a readable ClikaRtException. |