POLYTONE — the AI-native programming language

API reference

Every pub item of the standard library — 77 entries across 9 modules, generated from the code that ships (ptc doc at build time). This page cannot drift.

import audio10

Soundrecord
rate: Int
samples: List[Int]

Rendered sound: sample rate plus 16-bit signed samples (mono).

audio.renderfn
fn render(source: Text) -> Result[Sound, Text]

Synthesizes .pta source text into a Sound (formats/pta-audio.md).

audio.silencefn
fn silence(ms: Int) -> Sound

Silence of the given length.

audio.tonefn
fn tone(note: Text, ms: Int, wave: Text, volume: Int) -> Result[Sound, Text]

A single synthesized note, e.g. tone("c4", 500, "sine", 80).

audio.appendfn
fn append(a: Sound, b: Sound) -> Sound

b played after a.

audio.mixfn
fn mix(a: Sound, b: Sound) -> Sound

a and b played together (summed, clamped); length is the longer one.

audio.gainfn
fn gain(s: Sound, percent: Int) -> Sound

The sound scaled to `percent` loudness (0–200), clamped.

audio.repeatfn
fn repeat(s: Sound, times: Int) -> Sound

The sound repeated `times` times.

audio.adsrfn
fn adsr(s: Sound, attack_ms: Int, decay_ms: Int, sustain: Int, release_ms: Int) -> Sound

The classic ADSR envelope applied over the sound's full length: linear attack and decay to `sustain` percent, then a linear release over the final `release_ms`.

audio.to_wav_bytesfn
fn to_wav_bytes(sound: Sound) -> Bytes

The single export bridge for playback: RIFF/WAVE PCM bytes.

import images24

Imagerecord
width: Int
height: Int
pixels: List[Int]

A rendered image: dimensions plus a flat RGB pixel list (3 ints per pixel, row-major, values 0–255).

images.canvasfn
fn canvas(width: Int, height: Int, r: Int, g: Int, b: Int) -> Image

A solid-color canvas.

images.set_pixelfn
fn set_pixel(img: Image, x: Int, y: Int, r: Int, g: Int, b: Int) -> Image

The image with one pixel set (ignores out-of-canvas coordinates).

images.get_pixelfn
fn get_pixel(img: Image, x: Int, y: Int) -> Option[List[Int]]

The red/green/blue triple at (x, y), or None outside the canvas.

images.draw_rectfn
fn draw_rect(img: Image, x: Int, y: Int, w: Int, h: Int, r: Int, g: Int, b: Int) -> Image

The image with a filled rectangle drawn on it (clipped to the canvas).

images.draw_linefn
fn draw_line(img: Image, x1: Int, y1: Int, x2: Int, y2: Int, r: Int, g: Int, b: Int) -> Image

The image with a straight line drawn on it (Bresenham, clipped).

images.draw_circlefn
fn draw_circle(img: Image, cx: Int, cy: Int, radius: Int, r: Int, g: Int, b: Int) -> Image

The image with a filled circle drawn on it (clipped).

images.to_ppm_bytesfn
fn to_ppm_bytes(img: Image) -> Bytes

The single export bridge for viewing: binary PPM (P6) bytes.

images.from_ppm_bytesfn
fn from_ppm_bytes(raw: Bytes) -> Result[Image, Text]

The inverse bridge (since Sprint 27): parses the P6 bytes to_ppm_bytes writes — and any binary PPM with maxval 255 — back into an Image, so existing pictures can enter the toolkit.

images.draw_gradientfn
fn draw_gradient(img: Image, x: Int, y: Int, w: Int, h: Int, r1: Int, g1: Int, b1: Int, r2: Int, g2: Int, b2: Int, direction: Text) -> Image

A linear (`"vertical"`/`"horizontal"`) or `"radial"` gradient fill over the clipped region — any other direction behaves as vertical (v2 toolkit, Sprint 35).

images.draw_polygonfn
fn draw_polygon(img: Image, points: List[Int], r: Int, g: Int, b: Int) -> Image

A filled polygon from flat x/y pairs (even-odd scanline fill, clipped). Fewer than three points paints nothing (v2 toolkit).

images.flood_fillfn
fn flood_fill(img: Image, x: Int, y: Int, r: Int, g: Int, b: Int) -> Image

Four-connected flood fill from (x, y) — the v3 toolkit twin.

images.draw_opsfn
fn draw_ops(img: Image, ops: Text) -> Result[Image, Text]

Applies a draw-section's operations (v3 op set, hex colors) onto an existing image — the primitive that makes layers and imported pictures composable (Sprint 38): each op line is exactly a .pti draw: line without its indentation.

images.draw_textfn
fn draw_text(img: Image, x: Int, y: Int, size: Int, r: Int, g: Int, b: Int, content: Text) -> Image

Draws text in the built-in 5×7 pixel font at (x, y), scaled by whole pixels (v4 toolkit, Sprint 40). Unknown characters draw nothing; letters are case-insensitive; spaces advance the pen.

images.cropfn
fn crop(img: Image, x: Int, y: Int, w: Int, h: Int) -> Image

The w×h region of the image starting at (x, y); out-of-canvas parts read as black.

images.flip_xfn
fn flip_x(img: Image) -> Image

The image mirrored left–right.

images.flip_yfn
fn flip_y(img: Image) -> Image

The image mirrored top–bottom.

images.scalefn
fn scale(img: Image, factor: Int) -> Image

The image scaled up by a whole factor (nearest neighbor).

images.blitfn
fn blit(dst: Image, src: Image, x: Int, y: Int) -> Image

The destination with `src` painted onto it at (x, y), clipped.

images.map_pixelsfn
fn map_pixels(img: Image, f: fn(Int, Int, Int) -> List[Int]) -> Image

A new image with `f` applied to every pixel's r/g/b triple; results are clamped to 0–255.

images.invertfn
fn invert(img: Image) -> Image

Every channel inverted.

images.grayscalefn
fn grayscale(img: Image) -> Image

Luma grayscale (Rec. 601 weights).

images.brightenfn
fn brighten(img: Image, amount: Int) -> Image

Every channel shifted by `amount` (negative darkens), clamped.

images.renderfn
fn render(source: Text) -> Result[Image, Text]

Renders .pti source text into an Image (formats/pti-image.md).

import ints7

ints.minfn
fn min(a: Int, b: Int) -> Int

The smaller of two integers.

ints.maxfn
fn max(a: Int, b: Int) -> Int

The larger of two integers.

ints.clampfn
fn clamp(x: Int, lo: Int, hi: Int) -> Int

Clamps x into the inclusive range [lo, hi].

ints.signfn
fn sign(x: Int) -> Int

-1 for negative numbers, 0 for zero, 1 for positive numbers.

ints.is_evenfn
fn is_even(n: Int) -> Bool

Whether n is divisible by two.

ints.powfn
fn pow(base: Int, exp: Int) -> Int

base raised to a non-negative exponent (asserts exp >= 0).

ints.gcdfn
fn gcd(a: Int, b: Int) -> Int

The greatest common divisor of two integers (always non-negative).

import lists11

lists.sumfn
fn sum(xs: List[Int]) -> Int

The sum of all elements (0 for the empty list).

lists.productfn
fn product(xs: List[Int]) -> Int

The product of all elements (1 for the empty list).

lists.rangefn
fn range(from: Int, to: Int) -> List[Int]

The integers from `from` (inclusive) to `to` (exclusive).

lists.largestfn
fn largest(xs: List[Int]) -> Option[Int]

The largest element, or None for the empty list.

lists.smallestfn
fn smallest(xs: List[Int]) -> Option[Int]

The smallest element, or None for the empty list.

lists.count_wherefn
fn count_where(xs: List[Int], pred: fn(Int) -> Bool) -> Int

How many elements satisfy the predicate.

lists.index_offn
fn index_of[T](xs: List[T], x: T) -> Int

The index of the first element equal to `x`, or -1 (since Sprint 32 — the first generic functions in the stdlib).

lists.reversedfn
fn reversed[T](xs: List[T]) -> List[T]

The list in reverse order.

lists.takefn
fn take[T](xs: List[T], n: Int) -> List[T]

The first `n` elements (fewer when the list is shorter).

lists.dropfn
fn drop[T](xs: List[T], n: Int) -> List[T]

Everything after the first `n` elements.

lists.zipfn
fn zip[A, B](xs: List[A], ys: List[B]) -> List[Tuple[A, B]]

Pairs elements up to the shorter length (since Sprint 33 — the first generic function returning tuples of two parameters).

import mesh7

Meshrecord
vertices: List[Float]
faces: List[Int]
colors: List[Int]

A triangle mesh: flat vertex coordinates (3 floats per vertex), flat face indices (3 zero-based vertex indices per triangle), and flat RGB colors (3 ints 0–255 per triangle, version 2).

mesh.add_boxfn
fn add_box(m: Mesh, w: Float, h: Float, d: Float, x: Float, y: Float, z: Float) -> Mesh

A mesh with a cuboid appended: w×h×d, centered on x/z, base at y.

mesh.add_planefn
fn add_plane(m: Mesh, w: Float, d: Float, x: Float, y: Float, z: Float) -> Mesh

A mesh with a flat ground rectangle appended (w×d, centered).

mesh.add_spherefn
fn add_sphere(m: Mesh, r: Float, segments: Int, x: Float, y: Float, z: Float) -> Mesh

A mesh with a UV sphere appended (radius r, `segments` around the equator, segments / 2 rings).

mesh.renderfn
fn render(source: Text) -> Result[Mesh, Text]

Tessellates .ptm source text into a Mesh (formats/ptm-model.md).

mesh.to_obj_bytesfn
fn to_obj_bytes(m: Mesh) -> Bytes

The single export bridge for viewing: Wavefront OBJ text as bytes.

mesh.render_viewfn
fn render_view(m: Mesh, width: Int, height: Int, yaw: Float, pitch: Float) -> images.Image

A rendered orbit view of the mesh (Sprint 45): the camera is auto-framed on the model's bounding sphere (2.2 radii away), yaw orbits around y and pitch tilts the camera up (both in degrees), triangles are drawn back to front (painter's algorithm) with two-sided shading against a fixed world-space key light.

import pkg6

Deprecord
name: Text
min_version: Text

One dependency: a package name and the minimum version that works.

Packagerecord
name: Text
pkg_version: Text
summary: Text
deps: List[Dep]
modules: List[Text]

A parsed package manifest.

pkg.renderfn
fn render(source: Text) -> Result[Package, Text]

Parses polytone.pkg source text (formats/pkg-manifest.md).

PackageRefrecord
name: Text
pkg_version: Text
summary: Text

One package listed by a registry index.

Registryrecord
title: Text
packages: List[PackageRef]

A parsed registry index (formats/registry-index.md).

pkg.render_indexfn
fn render_index(source: Text) -> Result[Registry, Text]

Parses index.ptr source text (formats/registry-index.md).

import texts4

texts.repeatfn
fn repeat(s: Text, times: Int) -> Text

s repeated `times` times ("" for zero or negative counts).

texts.pad_leftfn
fn pad_left(s: Text, width: Int, fill: Text) -> Text

Pads s on the left with `fill` (one character) until it is `width` long.

texts.pad_rightfn
fn pad_right(s: Text, width: Int, fill: Text) -> Text

Pads s on the right with `fill` (one character) until it is `width` long.

texts.is_blankfn
fn is_blank(s: Text) -> Bool

Whether s is empty or whitespace-only.

import video3

Videorecord
width: Int
height: Int
fps: Int
frames: List[Int]
audio: Text

Rendered video: dimensions, frame rate, flat RGB frame data (width × height × 3 ints per frame, frames in order) — and since v2 the relative address of the declared soundtrack (`""` = none); hosts resolve and render it through the audio codec (spec: the codec stays pure, references resolve outside).

video.renderfn
fn render(source: Text) -> Result[Video, Text]

Renders .ptv source text into a Video (formats/ptv-video.md).

video.to_y4m_bytesfn
fn to_y4m_bytes(v: Video) -> Bytes

The single export bridge for playback: an uncompressed YUV4MPEG2 (C444) stream — `ffplay out.y4m` / `mpv out.y4m` play it directly.

import web5

Blockenum
Block.Heading(level: Int, text: Text)
Block.Paragraph(text: Text)
Block.Code(text: Text)
Block.Bullets(items: List[Text])
Block.Link(url: Text, label: Text)
Block.Image(source: Text, alt: Text)
Block.Film(source: Text, alt: Text)
Block.Sound(source: Text, alt: Text)
Block.Input(name: Text, label: Text)
Block.Button(target: Text, label: Text)
Block.Rule

One typed document block — markup never exists as strings. Image/Film/Sound reference native documents by relative address (since v2, Sprint 34).

Documentrecord
title: Text
blocks: List[Block]

A parsed document: title plus blocks in order.

web.renderfn
fn render(source: Text) -> Result[Document, Text]

Parses .ptw source text into a Document (formats/ptw-web.md).

web.form_valuefn
fn form_value(submitted: List[Text], name: Text) -> Option[Text]

Reads a form field from program arguments (v3, Sprint 41): the viewer submits each input as one `name=value` argument with spaces encoded as '+'. Missing fields are None; empty submissions are Some("").

web.to_html_bytesfn
fn to_html_bytes(doc: Document, assets: Map[Text, images.Image]) -> Bytes

The single export bridge: a standalone HTML page as bytes. `assets` maps image-block sources to rendered images — supplied ones embed as BMP data URIs, so the page stays self-contained; missing ones (and film/sound blocks) render as addressed links (v2, Sprint 34).