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.4.0

Published

A sane way to automate WebAudio parameters

Downloads

112

Readme

param-enveloper


Hey look! A sane way to use parameters in WebAudio.

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, at arbitrary times, without causing discontinuities (i.e. audible clicks or glitches).

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

Live demo

Example

import { Enveloper } from 'param-enveloper'

const env = new Enveloper(myAudioContext)

// initialize a param, with starting value=0 since this is a gain node
const param = masterVolume.gain
env.initParam(param, 0)

// to play a note, first declare the time it starts:
env.startEnvelope(param, startTime)

// then schedule an AHDSR envelope - A/H/D are durations in seconds
env.addRamp(param, A, 1) //           linear attack ramp to gain=1
env.addHold(param, H) //              hold current gain
env.addRamp(param, D, 0.5, true) //   exponential decay ramp to gain=0.5

// to schedule later changes like a release, start a new envelope
env.startEnvelope(param, releaseTime)
env.addSweep(param, -1, 0, 0.2) //    -1 duration means open-ended sweep

// you can also query the scheduled param value at arbitrary times:
const val = env.getValueAtTime(param, ctx.currentTime + 0.5)

The special sauce here is that you can start a new envelope any time (in response to user input, etc), and things will just work without causing discontinuities, even if the new envelope starts during an ongoing ramp/sweep/etc.

Usage

Install via npm/yarn/bun:

pnpm i --save param-enveloper    # or npm/yarn/etc
import { Enveloper } from 'param-enveloper'
var enveloper = new Enveloper(audioCtx)
enveloper.initParam(param, baseValue)
// ...

API

The API is documented in JSDoc comments; consult your local tooltip for details.

import { Enveloper } from 'param-enveloper'

class Enveloper {
  constructor(ctx: AudioContext | OfflineAudioContext)
  initParam(param: AudioParam, initialValue = 0)
  startEnvelope(param: AudioParam, time = 0)
  getValueAtTime(param: AudioParam, time: number)
  addHold(param: AudioParam, duration: number)
  addRamp(param: AudioParam, duration: number, target: number, exponential = false)
  addSweep(param: AudioParam, duration: number, target: number, timeConstant: number)
}

Notes

To hack on this, use the local build / start scripts. Contributions welcome!


By

Made with 🍺 by andy. License is ISC.