npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

param-enveloper

v0.3.0

Published

A sane way to automate parameters in WebAudio

Downloads

29

Readme

param-enveloper


Hey look, finally a sane way to automate parameters in Web Audio.

This is a small library for automating changes to an AudioParam (like a signal's gain or frequency). It lets you schedule any series of ramps, sweeps, and delays, and then later cancel at any arbitrary time and schedule new changes, without causing discontinuities (i.e. audible clicks).

(That may not sound like much, but suffice to say that the WebAudio API makes it very difficult, and this library makes it very easy.)

Live demo

Example

var A=0.1, H=0.01, S=0.5, D=0.2, R=0.5  // or whatever
var param = masterVolumeNode.gain

// initialize, starting from 0 since this is a gain node
enveloper.initParam(param, 0)

// play a note by making an ADHSR envelope...
enveloper.startEnvelope(param, startTime)
enveloper.addRamp(param, A, 1)
enveloper.addHold(param, H)
enveloper.addSweep(param, -1, S, D)

// later, when the note release is triggered..
enveloper.startEnvelope(param, releaseTime)
enveloper.addRamp(param, R, 0, true)

The second call to startEnvelope calculates what value the param is scheduled to have at releaseTime and edits the current envelope to end at that value. This way any subsequent automation happens without discontinuities, even if a release happens during the attack ramp or whatever.

Usage

Install via npm:

npm i --save param-enveloper
import Enveloper from 'param-enveloper'
var enveloper = new Enveloper(audioCtx)
enveloper.initParam(param, baseValue)
// ...

API

  • var env = new Enveloper(ctx)
    Constructor takes an AudioContext reference.

  • env.initParam(param, baseValue)
    Any param to be automated should be initted once. baseValue is the value the param will initially transition from.

  • env.startEnvelope(param, time)
    Start a new envelope from time, canceling any subsequent changes. If an envelope was already scheduled, it will be edited to end at whatever value it would have had at the specified time.

  • env.addHold(param, duration)
    Hold the current value for the given duration.

  • env.addRamp(param, duration, target, isExponential)
    Ramp from the current value to a new target. By default ramps are linear; set the final argument to true for an exponential ramp.

  • env.addSweep(param, duration, target, timeConst)
    Adds a sweep towards the given target value. Remember that sweeps approach the specified target value, but never completely reach it.
    If duration is a positive number, the sweep will last for the specified time and then end. Otherwise, the sweep will continue forever or until a new envelope is started (with startEnvelope).

  • env.getValueAtTime(param, time)
    Queries what value the param is scheduled to have at a given time. Useful for e.g. making an attack ramp whose duration depends on the value started from.

Note: When a sweep is added with non-positive duration, it is treated as an open-ended sweep that lasts forever. Scheduling a ramp or another sweep after this will throw an error; to add subsequent events either start a new envelope, or schedule a hold (whose duration will become the sweep's duration).

Notes

To hack on this, use the local npm build / start scripts. If you don't have webpack installed globally, you'll need to do npm i -D webpack webpack-cli webpack-dev-server or similar.

Currently this library doesn't do much heavy error checking - doing unexpected things (e.g. scheduling events in the past) might throw errors or have undefined behavior.


By

Made with 🍺 by Andy Hall. License is ISC.