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

audio-contour

v0.0.1

Published

A 5 stage audio envelope generator

Downloads

8

Readme

audio-contour npm

js-standard-style license

A 5 stage audio envelope generator. You can see the demo here:

var Contour = require('audio-contour')
var ac = new AudioContext()

var vca = ac.createGain()
var osc = ac.createOscillator()
osc.connect(vca)

var env = Contour(ac, { t1: 0.2, t4: 0.5 })
env.connect(vca.gain)

env.start()
env.onstart = function (when) { osc.start(when) }
env.onended = function () { osc.stop() }
env.stop(ac.currentTime + 3)

This module implements a alpha-juno style envelope generator:

Envelope Diagram from audiorealism.se

If you want to learn more about envelope generators, read this

There are a lot of envelope generator implementations. Here are the standalone ones I know (there are several audio libraries that implements them):

  • https://github.com/mmckegg/adsr
  • https://www.npmjs.com/package/adsr-envelope
  • https://github.com/itsjoesullivan/envelope-generator

Why choose this library over the others:

  • Unlike others, it implements a 5 stage envelope (and can be reduced to a standard ADSR envelope)
  • It supports onstart and onended events
  • Can specify gate duration (for sequencer style)
  • It's small (2.5Kb minified)

Why don't choose this library:

  • It's very young project, still in development and not battle tested.
  • Other libraries are great too!

Installation

Via npm: npm i --save audio-contour

Usage

Create an envelope

To create an envelope use the Contour function:

var ac = new AudioContext()
var Contour = require('audio-contour')
var env = Contour(ac)

You can pass options to that function:

var env = Contour(ac, { t1: 1 })

or change them on the object before start:

var env = Contour(ac)
env.t1 = 1
env.t4 = 0.5

Apply the envelope

To apply the envelope, you have to connect it to something. For example, you can create a vca (voltage controlled amplifier) connecting it to a gain's gain param:

var vca = ac.createGain()
env.connect(vca.gain)

Or create a vcf (voltage controlled filter) ocnnecting it to a filter frequency param:

var vcf = ac.createBiquadFilter()
env.connect(vcf.frequency)

Start and stop the envelope

You can use start and stop function to the envelope:

var now = ac.currentTime
env.start(now)
// suppose your audio source is an oscillator
osc.start(now)
var finish = env.stop(now + 1)

The stop function returns the time when the release phase ended. Can be used to stop the audio sources:

osc.start(finish)

Remeber that if duration is not Infinity, the envelope will stop automatically:

var env = Contour(ac)
env.duration = 1
env.start() // => it will automatically stop after 1 second

Events

Two events are supported: onstart and onended. The onstart event handler will be trigger at same time as the start function of the envelope, so it receives a time parameter. The onended event handler will be called when the envelope effectively stops:

env.duration = 1
env.onstart = function (when) { osc.start(when) }
env.onended = function () { osc.stop(ac.currentTime) }
env.start() // since duration is not Infinity, both envent handlers will be called

Create a standard ADSR

When t3 is 0, the audio-contour behaves like a normal ADSR envelope.

Additionally, you can use the standard attack, decay, sustain and release parameters in the constructor to build the envelope:

var env = Contour(ac, { attack: 0.1, decay: 0.2, sustain: 0.8, release: 0.5 })
env.t1 // => 0.1 (the attack)
env.t2 // => 0.2 (the decay)
env.t3 // => 0
env.t4 // => 0.5 (the release)

Run tests and examples

To run the tests, clone this repo and: npm install && npm test.

To run the example you need watchify installed: npm install -g watchify. Then, move to examples directory and type: npm install && npm start

License

MIT License