Skip to Content
Soundor Components
Components

EnvelopeEditor

An ADSR envelope edited by its handles.

Use EnvelopeEditor for an amplitude, filter or modulation envelope: attack, decay, sustain and release, with a handle for each. It draws on a Plot and only draws. It computes no samples and runs no envelope; your processor does that with the values it edits.

import { EnvelopeEditor } from '@soundor/components';
import { parameters } from 'soundor:parameters';

export default function Demo() {
  return (
    <EnvelopeEditor
      title="Amp envelope"
      parameters={{
        attack: parameters.attack,
        decay: parameters.decay,
        sustain: parameters.sustain,
        release: parameters.release,
      }}
    />
  );
}
envelope-editor.tsx, running in the web runtime.Source

Bind it to parameters

Give any of the stages a parameter. A bound stage takes its range, mapping, formatting and default from the parameter, and reports host gestures and undo history, as a Knob bound to it would.

import { EnvelopeEditor } from '@soundor/components';
import { parameters } from 'soundor:parameters';

<EnvelopeEditor
  title="Amp envelope"
  parameters={{
    attack: parameters.attack,
    decay: parameters.decay,
    sustain: parameters.sustain,
    release: parameters.release,
  }}
/>;

Each handle opens host gestures only for the stages it edits: attack for A, decay and sustain for D, sustain for S, and release for R. An arrow key on D opens only the gesture of the stage it moves, so a host recording touch automation doesn't take the other as touched.

Controlled values

Without parameters, the envelope is in seconds and a level:

FieldUnitDefault range
attackseconds1 ms to 10 s, timeRange
decayseconds1 ms to 10 s, timeRange
sustainlevel, 0 to 10 to 1
releaseseconds1 ms to 10 s, timeRange

Pass value and onChange, or a defaultValue; onChange receives the whole envelope. Stages bound to parameters are in their parameters' own units, and the two can be mixed.

import {
  EnvelopeEditor,
  SegmentedControl,
  Toggle,
  type ADSREnvelope,
} from '@soundor/components';
import { useTheme } from '@soundor/components/theme';
import { Text, View } from '@soundor/react';
import { useState } from 'react';

export default function Demo() {
  const theme = useTheme();
  const [envelope, setEnvelope] = useState<ADSREnvelope>({
    attack: 0.02,
    decay: 0.3,
    sustain: 0.65,
    release: 0.8,
  });
  const [scale, setScale] = useState<'log' | 'linear'>('log');
  const [grid, setGrid] = useState(true);
  return (
    <View style={{ gap: 12 }}>
      <EnvelopeEditor
        title="Filter envelope"
        color="lime"
        value={envelope}
        onChange={setEnvelope}
        timeRange={[0.001, 4]}
        timeScale={scale}
        grid={grid}
        width={420}
        height={180}
      />
      <View style={{ flexDirection: 'row', gap: 12, alignItems: 'center' }}>
        <SegmentedControl
          label="Time scale"
          size="sm"
          value={scale}
          onChange={setScale}
          options={[
            { value: 'log', label: 'Log' },
            { value: 'linear', label: 'Linear' },
          ]}
        />
        <Toggle label="Grid" size="sm" value={grid} onChange={setGrid} />
        <Text
          style={{
            fontFamily: theme.font.mono,
            fontSize: 10,
            color: theme.color.textMuted,
          }}
        >
          {`{ a: ${envelope.attack.toFixed(3)}, d: ${envelope.decay.toFixed(3)}, s: ${envelope.sustain.toFixed(2)}, r: ${envelope.release.toFixed(3)} }`}
        </Text>
      </View>
    </View>
  );
}
envelope-editor-controlled.tsx, running in the web runtime.Source
const [envelope, setEnvelope] = useState({
  attack: 0.02,
  decay: 0.3,
  sustain: 0.65,
  release: 0.8,
});

<EnvelopeEditor value={envelope} onChange={setEnvelope} />;

How it is drawn

Attack, decay and release are lengths, drawn one after another. Each one takes, across, its value's position through its mapping, from nothing to one stage's width at its longest. The sustain is a level held while a note is, with no time of its own, so it is drawn at a fixed width, sustainWidth (half a stage by default). The curve goes from silence up to full level, down to the sustain, holds it, and releases to silence, in straight lines.

  • The plot's width doesn't change with the values, so a handle never moves away from the pointer during a drag. A short envelope sits at the left; a long one fills the plot.
  • No handle can pass another, since each stage is a length that can't go below zero.
  • The time across is not to scale. The vertical lines mark each stage's end, and the values under the plot give the times.

Time scaling

A bound stage uses its parameter's mapping: a log-mapped time parameter shows a 5 ms attack and a 2 s release both large enough to grab. Unbound times use timeScale, 'log' by default, which gives each decade the same room. 'linear' draws times to scale, which suits a short timeRange. A power curve, { type: 'power', exponent: 3 }, is in between, and can reach zero.

Editing

A press goes to the nearest handle within reach, so handles close together, such as a short attack and decay, stay easy to grab. Handles that coincide are all reachable from the keyboard.

InputDoes
DragMove the handle; Shift for fine control
TabNext handle: A, D, S, R
A, R: ← ↓ / → ↑Shorter / longer, by 1% of the travel; Shift ten times finer
S: ↓ / ↑Lower / raise the sustain
D: ← → / ↓ ↑Decay / sustain
Page Up / Page Down, Home / EndA, S and R: 10% at a time, or the minimum and maximum
Wheel over a handleA, S and R: 1% per notch
Double-click or Alt-click a handleReset its stage to its default (on D, the decay only)
Delete or Backspace on a handleReset its stage, as a double-click does
Esc while draggingCancel, and restore the values from before

Each handle is an adjustable element named after its stage, or its parameter's label, with its formatted value, such as "Attack, 20 ms" or "Decay 300 ms, Sustain 65%". It offers increment, decrement, set value and reset actions. The plot itself is an image named by accessibilityLabel, the title, or "Envelope".

Themes and layout

The curve and handles take color, one of the accent colours, and every other colour is a theme token: the display, grid, text and focus ring follow a ThemeProvider. fill, grid, axisLabels and showValues turn parts off for a compact layout. width and height size the display; a styles.plot width such as '100%' lets it follow its container, and the handles follow any resize. Slots: root, plot, handle, values, label, value.

// A synth voice's panel: how the new controls sit in a plugin.
import {
  ContextMenu,
  ControlGroup,
  DropdownMenu,
  EnvelopeEditor,
  Button,
  Knob,
  Menu,
  Panel,
  ParameterMenuItems,
  Select,
  type HostProps,
  type RangeParameter,
} from '@soundor/components';
import { View } from '@soundor/react';
import type { ReactElement } from 'react';
import { parameters } from 'soundor:parameters';

function WithMenu({
  parameter,
  children,
}: {
  parameter: RangeParameter;
  children: ReactElement<HostProps>;
}) {
  return (
    <ContextMenu>
      <ContextMenu.Trigger>{children}</ContextMenu.Trigger>
      <ContextMenu.Content>
        <ParameterMenuItems parameter={parameter} />
      </ContextMenu.Content>
    </ContextMenu>
  );
}

export default function Demo() {
  return (
    <Panel
      title="Voice"
      indicator
      trailing={
        <DropdownMenu>
          <DropdownMenu.Trigger>
            <Button size="sm" variant="ghost">
              Options
            </Button>
          </DropdownMenu.Trigger>
          <DropdownMenu.Content align="end">
            <Menu.Item>Copy Voice</Menu.Item>
            <Menu.Item>Paste Voice</Menu.Item>
            <Menu.Divider />
            <Menu.Item>Initialize Voice</Menu.Item>
          </DropdownMenu.Content>
        </DropdownMenu>
      }
    >
      <View style={{ flexDirection: 'row', gap: 20 }}>
        <View style={{ gap: 14 }}>
          <Select parameter={parameters.filterType} width={130} />
          <ControlGroup title="Filter" direction="row">
            <WithMenu parameter={parameters.feedback}>
              <Knob parameter={parameters.feedback} label="Reso" size="sm" />
            </WithMenu>
            <WithMenu parameter={parameters.mix}>
              <Knob
                parameter={parameters.mix}
                label="Drive"
                size="sm"
                color="lavender"
              />
            </WithMenu>
          </ControlGroup>
        </View>
        <EnvelopeEditor
          width={360}
          height={170}
          title="Amp envelope"
          parameters={{
            attack: parameters.attack,
            decay: parameters.decay,
            sustain: parameters.sustain,
            release: parameters.release,
          }}
        />
      </View>
    </Panel>
  );
}
synth-voice.tsx, running in the web runtime.Source

For a different look, useEnvelopeEditor is the interaction on its own.

Props

Prop

Type

On this page