# envelope-ramp

> Envelope curves to gain automation: slicing an envelope at its sustain and scheduling its ramps on a GainNode's AudioParam, for synth voices

```bash
npx shadcn@latest add @audio-ui/envelope-ramp
```

```ts
import { SUSTAIN_SPLIT_X, sliceEnvelope, scheduleEnvelopeRamp } from "@/lib/envelope-ramp";
```

- Files: `lib/envelope-ramp.ts`
- Depends on: nothing; npm: nothing
- Used by: `spectrum-analyzer-demo`, `synth-voice`, `use-piano-roll-playback`, `use-synth-voices`

| Export | Kind | What it is |
| --- | --- | --- |
| `EnvelopePoint` | interface |  |
| `SUSTAIN_SPLIT_X` | const | Where an envelope's attack/decay phase ends and its release phase begins, as a fraction of the 0-1 curve. |
| `sliceEnvelope` | function | Extracts the [fromX, toX] slice of an envelope curve, interpolating exact boundary values, and remaps it to its own local 0-1 x range. |
| `scheduleEnvelopeRamp` | function | Schedules a gain ramp through `points` (local 0-1 x) starting at `startTime`, stretched over `durationSeconds`, scaled by `peakGain`. |

## Usage

The ramps behind the voices, for your own sounds: `scheduleEnvelopeRamp(param, points, startTime, seconds, peak, from)` runs a gain through an envelope's points, and `sliceEnvelope(points, fromX, toX)` takes a part of one, such as the release after `SUSTAIN_SPLIT_X`. A ramp cancels what was scheduled on the param first, so starting a note again never clicks.
