UNPKG

adsr-gain-node

Version:

A simple and small nodejs module for creating an adsr gain node

102 lines (85 loc) 3.93 kB
/* Package: adsr-gain-node. Version: 2.0.0. License: MIT. Author: dennis iversen. Homepage: https://github.com/diversen/adsr-gain-node#readme */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.adsrGainNode = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ function AdsrGainNode(ctx) { this.ctx = ctx; this.options = { attackAmp: 0.1, decayAmp: 0.3, sustainAmp: 0.7, releaseAmp: 0.01, attackTime: 0.1, decayTime: 0.2, sustainTime: 1.0, releaseTime: 3.4, autoRelease: true }; /** * Set options or use defaults * @param {object} options */ this.setOptions = function (options) { this.options = Object.assign(this.options, options); }; this.gainNode this.audioTime /** * Get a gain node from defined options * @param {float} audioTime an audio context time stamp */ this.getGainNode = (audioTime) => { this.gainNode = this.ctx.createGain(); this.audioTime = audioTime this.gainNode.gain.setValueAtTime(0, audioTime) // Attack this.gainNode.gain.exponentialRampToValueAtTime( this.options.attackAmp, audioTime + this.options.attackTime) // Decay this.gainNode.gain.exponentialRampToValueAtTime( this.options.decayAmp, audioTime + this.options.attackTime + this.options.decayTime) // Sustain this.gainNode.gain.exponentialRampToValueAtTime( this.options.sustainAmp, audioTime + this.options.attackTime + this.options.sustainTime) // Check if auto-release // Then calculate when note should stop if (this.options.autoRelease) { this.gainNode.gain.exponentialRampToValueAtTime( this.options.releaseAmp, this.audioTime + this.options.attackTime + this.options.decayTime + this.options.sustainTime + this.options.releaseTime) // Disconnect the gain node this.disconnect(this.options.attackTime + this.options.decayTime + this.options.sustainTime + this.options.releaseTime) } return this.gainNode; }; /** * Release the note dynamicaly * E.g. if your are making a keyboard, and you want the note * to be released according to current audio time added the ADSR release time */ this.releaseNow = () => { this.gainNode.gain.exponentialRampToValueAtTime( this.options.releaseAmp, this.releaseTime()) this.disconnect(this.options.releaseTime) } /** * Get release time according to audio ctx time and the adsr release time */ this.releaseTime = function() { return this.ctx.currentTime + this.options.releaseTime } /** * * @param {float} disconnectTime the time when gainNode should disconnect */ this.disconnect = (disconnectTime) => { setTimeout( () => { this.gainNode.disconnect(); }, disconnectTime * 1000); }; } module.exports = AdsrGainNode; },{}]},{},[1])(1) });