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

@mohayonao/adsr-envelope

v0.2.0

Published

adsr envelope

Downloads

3

Readme

ADSR ENVELOPE

Build Status NPM Version License

ADSR Envelope

Installation

Node.js

npm install @mohayonao/adsr-envelope

Online Demo

  • http://mohayonao.github.io/adsr-envelope/

ADSR

ADSREnvelope

API

ADSREnvelope

  • constructor(opts: object = {})
    • attackTime: number default: 0.01 (10msec)
    • decayTime: number default: 0.3 (300msec)
    • sustainLevel: number default: 0.5
    • releaseTime: number default: 1 (1sec)
    • gateTime: number default: 1 (1sec)
    • sustainTime: number
    • duration: number
    • peakLevel: number default: 1
    • epsilon: number default: 0.001
    • attackCurve: string default: lin
      • lin or exp (linear or exponential)
    • decayCurve: string default: lin
      • lin or exp (linear or exponential)
    • releaseCurve: string default: lin
      • lin or exp (linear or exponential)

Instance Attributes

  • duration: number
  • attackTime: number
  • decayTime: number
  • sustainTime: number
  • sustainLevel: number
  • releaseTime: number
  • gateTime: number
  • peakLevel: number
  • epsilon: number
  • attackCurve: string
  • decayCurve: string
  • releaseCurve: string

Instance Methods

  • valueAt(time: number = 0): number
  • applyTo(audioParam: AudioParam, playbackTime: number = 0): self
  • getWebAudioAPIMethods(playbackTime: number = 0): Array[]
  • clone(): ADSREnvelope

Example

sequencer model

import ADSREnvelope from "@mohayonao/adsr-envelope";

let audioContext = new AudioContext();
let oscillator = audioContext.createOscillator();
let gain = audioContext.createGain();
let adsr = new ADSREnvelope({
  attackTime: 0.5,
  decayTime: 0.25,
  sustainLevel: 0.8,
  releaseTime: 2.5,
  gateTime: 6,
  releaseCurve: "exp",
});

adsr.applyTo(gain.gain, audioContext.currentTime);

console.log(adsr.getWebAudioAPIMethods());
// [
//   [ "setValueAtTime", 0, 0 ],
//   [ "linearRampToValueAtTime", 1, 0.5 ],
//   [ "linearRampToValueAtTime", 0.8, 0.75],
//   [ "setValueAtTime", 0.8, 6 ],
//   [ "exponentialRampToValueAtTime", 0.001, 8.5 ],
// ]

oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + adsr.duration);

oscillator.connect(gain);
gain.connect(audioContext.destination);

noteOn / noteOff model

import ADSREnvelope from "@mohayonao/adsr-envelope";

let audioContext = new AudioContext();
let adsr = new ADSREnvelope({
  attackTime: 0.5,
  decayTime: 0.25,
  sustainLevel: 0.8,
  releaseTime: 2.5,
  gateTime: 6,
  releaseCurve: "exp",
});
let noteMap = {};

class Note {
  constructor(audioContext, noteNumber, envelope) {
    this.audioContext = audioContext;
    this.noteNumber = noteNumber;
    this.envelope = envelope;
    this.startTime = 0;
    this.oscillator = audioContext.createOscillator();
    this.gain = audioContext.createGain();

    this.oscillator.frequency.value = midicps(noteNumber);
    this.oscillator.onended = () => {
      this.oscillator.disconnect();
      this.gain.disconnect();
    };

    this.oscillator.connect(this.gain);
    this.gain.connect(audioContext.destination);
  }

  noteOn(playbackTime = this.audioContext.currentTime) {
    this.startTime = playbackTime;
    this.envelope.gateTime = Infinity;
    this.envelope.applyTo(this.gain.gain, playbackTime);
    this.oscillator.start(playbackTime);
  }

  noteOff(playbackTime = this.audioContext.currentTime) {
    this.gain.gain.cancelScheduledValues(this.startTime);

    this.envelope.gateTime = playbackTime - this.startTime;
    this.envelope.applyTo(this.gain.gain, this.startTime);

    this.oscillator.stop(this.startTime + this.envelope.duration);
  }
}

midiKeyboard.on("noteOn", ({ noteNumber }) => {
  if (noteMap[noteNumber]) {
    noteMap[noteNumber].noteOff();
  }

  noteMap[noteNumber] = new Note(audioContext, noteNumber, adsr);
  noteMap[noteNumber].noteOn();
});
midiKeyboard.on("noteOff", ({ noteNumber }) => {
  if (noteMap[noteNumber]) {
    noteMap[noteNumber].noteOff();
  }
  noteMap[noteNumber] = null;
});

License

MIT