LabDAW

Envelope

An editor for a breakpoint ramp, such as an ADSR shape or an automation curve.

Edits a ramp made of breakpoints, such as an ADSR shape or an automation curve. The registry component is written on the envelope primitive, which installs with it, and draws the points, the segments between them and a grid.

Overview

  • Value: value is a list of { x, y, curve? } points. Both axes are normalized to 0…1, and the points are sorted by x. Map that range to your own unit, such as time or level.
  • Curves: a point's curve, from -1 to 1, bends the segment that leaves it. 0, or no value, draws a straight line. A positive curve holds the start of the segment and steepens its end; a negative curve is its mirror image, rushing the start and holding the end. The bend is the exponential of an analog envelope stage, (e^(k·t) - 1) / (e^k - 1) with k the curve times 11, so neither end turns vertical. The last point has no segment.
  • Pointer: drag a point to move it. Double-click empty space to add a point, or double-click a point to remove it. A point stays between its neighbors, so points never cross.
  • Point count: points are added and removed between minPoints (default 2, a start and an end) and maxPoints. Set both to the same number to fix the shape.
  • Grid: snap sets the grid that points snap to, for example 0.25 for quarters, or one step per axis, { x: 0.125, y: 0 }, to snap across and leave the level free. Hold Cmd/Ctrl to move off the grid. Without snap, points move freely. The registry component draws the grid.
  • Fine control: hold Shift while dragging to move a point at a quarter of the pointer's speed. The registry component shows the point's value above it while it is dragged or focused.
  • Accessibility: the points share one tab stop. The arrow keys move the focused point, Page Up and Page Down (or Home and End) move between points, and Delete or Backspace removes it. Each point is a role="slider". The registry component names the points "Point n of N". With the primitive, set aria-label and getAriaValueText yourself.

Installation

The primitives are built on the UI SDK kernel, in beta. Nothing of ours comes from npm: the primitive and its adapter are files you copy into your project and own, and the only packages are the kernel's. The commands name its version, @ui-sdk/*@0.1.0-beta.14.

The styled component, with a grid, sizes and themed points, is one command:

The styled component, with size variants and theme styles:

pnpm dlx shadcn@latest add @audio-ui/envelope

Or the primitive alone, to style yourself:

pnpm dlx shadcn@latest add @audio-ui/envelope-react

It writes the primitive, its adapter and primitive.ts into components/audio-ui/ and installs the kernel's packages and what each file imports. The optional styles are one more command:

pnpm dlx shadcn@latest add @audio-ui/primitives-css

Usage

import { Envelope } from "@/components/audio-ui/envelope";

Uncontrolled: set only the initial points.

<Envelope
  defaultValue={[
    { x: 0, y: 0 },
    { x: 0.2, y: 1 },
    { x: 0.6, y: 0.5 },
    { x: 1, y: 0 },
  ]}
/>

Controlled: drive the points from state.

const [points, setPoints] = useState(initialPoints);

<Envelope
  onValueChange={setPoints}
  onValueCommitted={(next, details) => console.log("Committed:", next, details.reason)}
  value={points}
/>

The primitive alone, part by part:

A Root owns the points. A Graph is the surface they are edited on: it takes the double click that adds a point, and the Alt drag that bends a segment. A Line and a Fill draw the curve and the area under it, each as an <svg> stretched over the Graph. A Point is a handle for one point and a Segment a handle on the middle of the stretch between two.

You write a Point for each point and a Segment for each stretch, with their index: the Root's content can read its state, whose value is the points, so the handles follow the points as they are added and removed. That is a function in React, the default slot in Vue and a snippet in Svelte.

Give the Graph its size and a position. The pointer is read against the Graph's box, so a Graph with no height cannot take a click. Make it position: relative or absolute, and the Line and the Fill, which are absolute, take its box.

import { useState } from "react";
import { Envelope } from "@/components/audio-ui/envelope/react";

export function Amp() {
  const [points, setPoints] = useState([
    { x: 0, y: 0 },
    { x: 0.15, y: 1 },
    { x: 0.6, y: 0.5 },
    { x: 1, y: 0 },
  ]);
  return (
    <Envelope.Root className="relative h-40 w-80" onValueChange={setPoints} value={points}>
      {({ value }) => (
        <>
          <Envelope.Graph className="absolute inset-0 rounded-md bg-muted">
            <Envelope.Fill className="text-primary/20" />
            <Envelope.Line className="text-primary" />
          </Envelope.Graph>
          {value.map((_, index) => (
            <Envelope.Point
              aria-label={`Point ${index + 1}`}
              className="size-3 rounded-full bg-primary"
              index={index}
              key={index}
            />
          ))}
          {value.slice(0, -1).map((_, index) => (
            <Envelope.Segment
              aria-label={`Curve ${index + 1}`}
              className="size-2 rounded-full bg-muted-foreground"
              index={index}
              key={index}
            />
          ))}
        </>
      )}
    </Envelope.Root>
  );
}

Interaction

Pointer

Behavior · 10

Drag a point
Moves it; x stays between its neighbors and y within 0…1. Dragging never deletes a point. While dragging, the point follows the pointer even if a controlled value updates late.
Double-click empty space
Adds a point at that location, unless at maxPoints. Added on a bent segment it sits on the curve, and each half is bent to keep its shape.
Double-click a point
Removes it, unless at minPoints.
Hold Shift while dragging a point
Moves it at a quarter of the pointer's speed, for fine adjustments. The point does not jump when Shift is pressed or released mid-drag.
Alt + drag a segment
Bends it, like a DAW's automation lane: the segment's midpoint follows the pointer. A flat segment, or a press away from any segment, does nothing.
Alt + double-click a segment
Straightens it, instead of adding a point.
Alt + click a point
Removes it, unless at minPoints.
Drag a segment's handle
Bends the segment: its midpoint follows the pointer up or down. A segment between two points of the same level has nothing to bend.
Double-click a segment's handle
Straightens the segment.
Commit event
onValueCommitted fires when a drag that moved a point ends, and right away for an add or a remove.

A refused add or remove sets data-blocked for a moment on the graph or the point, which the registry styles turn into a short shake.

Keyboard

The points share one tab stop. On the focused point:

Keys · 6

ArrowUp / ArrowDown
Nudge the focused point's y by step
ArrowLeft / ArrowRight
Nudge the focused point's x by step (clamped between neighbors)
Shift + arrow
Nudge by 10 steps
PageUp / PageDown
Move focus to the next or previous point
Home / End
Move focus to the first or last point
Delete / Backspace
Remove the focused point, unless at minPoints

Each segment has a handle on its curve. The handles share one tab stop:

Keys · 4

ArrowLeft / ArrowRight
Move to the previous or next segment's handle
ArrowUp / ArrowDown
Lift or lower the handle, bending the segment by 5 steps
Shift + arrow
Bend by 50 steps
Delete / Backspace
Straighten the segment

API reference

Registry component

Envelope accepts style props (size, className) plus the props of the Envelope primitive's Root. aria-label / aria-labelledby name the envelope group.

Value and state

Props · 9

Layout and display

Props · 6

The Envelope primitive that installs with it exposes Envelope.Root, Envelope.Graph (the SVG surface; double-click to add), Envelope.Line and Envelope.Fill (the curve and the area under it), Envelope.Point (the draggable handle) and Envelope.Segment (the handle on a segment's midpoint, which bends it). You write one of each per point and per segment, with its index, from the Root's children given as a function of its state, whose value is the points. Every part accepts a render prop and className / style as functions of state; points expose data-dragging, data-removable, data-blocked, data-disabled and data-index, and segment handles data-dragging, data-flat and data-disabled.

Options

Set on the Root, as props (an attribute in kebab case in the web adapter, the points as JSON).

OptionTypeDefault
value, defaultValue{ x, y, curve? }[]an ADSR-like shapeThe controlled points, or the starting ones. Each is normalized to 0 to 1 on both axes, sorted by x, and curve, from -1 to 1, bends the stretch that leaves it.
minPoints, maxPointsnumber2, noneHow few and how many points there may be. Equal, the shape is fixed.
snapnumber or { x, y }0The grid the pointer snaps points to, in normalized units: 0.25 snaps to quarters, 0 is no grid, { x: 0.125, y: 0 } snaps across and leaves the level free.
stepnumber0.01The keyboard step on both axes.
format, localeIntl.NumberFormat options, a localeHow a point's coordinates are written for its aria-valuetext.
disabledbooleanfalseTakes no gesture.
onValueChange(points, details) => voidEvery change, which details.cancel() refuses.
onValueCommitted(points, details) => voidThe end of a gesture.

The Line takes a strokeWidth, in pixels, which stays the same however the Graph is stretched. A Point takes getAriaValueText(point, index) to announce itself in your own words, and a Segment getAriaValueText(curve, index).

What a change reports

details carries what moved the points and how:

  • reason: "drag", "keyboard", "add", "remove" or "curve" (a bend).
  • index: the point or the segment that moved, or the index of the point that was added or removed.
  • source: "mouse", "touch", "pen", "keyboard" or "programmatic", for analytics. It is never a reason to behave differently.
  • event: the DOM event behind it, when there is one.
  • cancel(): refuses the change. A controlled envelope keeps the points you give it.

While a pointer drags, the points follow it even if a controlled value lags behind.

In the web adapter, onValueChange is an input event and onValueCommitted a change event, both with detail: { value, reason, index, source }, and event.preventDefault() refuses a change.

States to style from

  • data-disabled on every part. data-dragging on the Root, the Graph, the Line and the Fill while a point or a segment is dragged, and on a Point or a Segment only while it is the one dragged.
  • data-blocked on the Graph, briefly, when an addition was refused, and on a Point when its removal was refused: a feedback to animate. It is raised a frame after the change, so that an animation can play again.
  • data-removable on a Point while there are more than minPoints, and data-index on a Point and a Segment.
  • data-flat and data-curve on a Segment.

Accessibility

A Point is a role="slider" on its level, from 0 to 1, and its aria-valuetext reads its coordinates, 0.5, 1, with the format and the locale of the Root. A Segment is a slider on its curve, from -1 to 1. Give each an aria-label: the Graph, the Line and the Fill are hidden from assistive technology.

Parts

Parts · 6

Events

Events · 2

Examples

ADSR with its stages read out

Built by Ouest Labs, the source code is available on GitHub.
RSS