ixfx
    Preparing search index...

    ADSR (Attack Decay Sustain Release) envelope. An envelope is a value that changes over time, usually in response to an intial trigger.

    See the ixfx Guide on Envelopes.

    import { Envelopes } from 'https://unpkg.com/ixfx/dist/modulation.js'
    const env = new Envelopes.Adsr({
    attackDuration: 1000,
    decayDuration: 200,
    sustainDuration: 100
    });

    Options for envelope are as follows:

    initialLevel?: number
    attackBend: number
    attackDuration: number
    decayBend: number
    decayDuration:number
    sustainLevel: number
    releaseBend: number
    releaseDuration: number
    releaseLevel?: number
    peakLevel: number
    retrigger?: boolean
    shouldLoop: boolean

    If retrigger is false (default), a re-triggered envelope continues at current value rather than resetting to initialLevel.

    If shouldLoop is true, envelope loops until release() is called.

    env.trigger(); // Start envelope
    ...
    // Get current value of envelope
    const [state, scaled, raw] = env.compute();
    • state is a string, one of the following: 'attack', 'decay', 'sustain', 'release', 'complete'
    • scaled is a value scaled according to the stage's levels
    • raw is the progress from 0 to 1 within a stage. ie. 0.5 means we're halfway through a stage.

    Instead of compute(), most usage of the envelope is just fetching the value property, which returns the same scaled value of compute():

    const value = env.value; // Get scaled number
    
    env.trigger(true);   // Pass in true to hold
    ...envelope will stop at sustain stage...
    env.release(); // Release into decay

    Check if it's done:

    env.isDone; // True if envelope is completed
    

    Envelope has events to track activity: 'change' and 'complete':

    env.addEventListener(`change`, ev => {
    console.log(`Old: ${evt.oldState} new: ${ev.newState}`);
    })

    It's also possible to iterate over the values of the envelope:

    const env = new Envelopes.Adsr();
    for await (const v of env) {
    // v is the numeric value
    await Flow.sleep(100); // Want to pause a little to give envelope time to run
    }
    // Envelope has finished

    Hierarchy (View Summary)

    Implements

    • Iterable<number>

    Constructors

    Properties

    attackBend: number
    attackDuration: number
    attackPath: Geometry.Path
    decayBend: number
    decayDuration: number
    decayDurationTotal: number
    decayPath: Geometry.Path
    initialLevel: number
    initialLevelOverride: undefined | number
    peakLevel: number
    releaseBend: number
    releaseDuration: number
    releaseLevel: number
    releasePath: Geometry.Path
    retrigger: boolean
    shouldLoop: boolean

    If true envelope will loop

    sustainLevel: number

    Accessors

    • get hasTriggered(): boolean

      Returns boolean

    • get isDisposed(): boolean

      Returns boolean

    • get isDone(): boolean

      Returns true if envelope has finished

      Returns boolean

    • get value(): number

      Returns the scaled value Same as .compute()[1]

      Returns number

    Methods

    • Returns Iterator<number>

    • Compute value of envelope at this point in time.

      Returns an array of [stage, scaled, raw]. Most likely you want to use value to just get the scaled value.

      Parameters

      • allowStateChange: boolean = true

        If true (default) envelope will be allowed to change state if necessary before returning value

      • allowLooping: boolean = true

      Returns [stage: string, scaled: number, raw: number]

    • Computes a stage's progress from 0-1

      Parameters

      • allowStateChange: boolean = true
      • allowLooping: boolean = true

      Returns [stage: string, amount: number, prevStage: string]

    • Release if 'trigger(true)' was previouslly called. Has no effect if not triggered or held.

      Returns void

    • Changes state based on timer status

      Parameters

      • allowLooping: boolean

      Returns boolean

      True if state was changed

    • Triggers envelope, optionally holding it.

      If hold is false (default), envelope will run through all stages, but sustain stage won't have an affect.

      If hold is true, it will run to, and stay at the sustain stage. Use release to later release the envelope.

      If event is already trigged it will be retriggered. Initial value depends on opts.retrigger

      • false (default): envelope continues at current value.
      • true: envelope value resets to opts.initialValue.

      Parameters

      • hold: boolean = false

        If true envelope will hold at sustain stage

      Returns void