https://github.com/diversen/adsr-gain-node
A simple and small nodejs module for creating an adsr gain node
https://github.com/diversen/adsr-gain-node
adsr audiocontext
Last synced: about 1 year ago
JSON representation
A simple and small nodejs module for creating an adsr gain node
- Host: GitHub
- URL: https://github.com/diversen/adsr-gain-node
- Owner: diversen
- Created: 2017-07-21T14:58:20.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-02-10T18:41:41.000Z (over 7 years ago)
- Last Synced: 2025-03-08T12:02:30.334Z (over 1 year ago)
- Topics: adsr, audiocontext
- Language: JavaScript
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# adsr-gain-node
Simple object for getting an ADSR gain node
## Install:
npm install --save adsr-gain-node
## Example usage
~~~js
var adsrGainNode = require('adsr-gain-node')
var audioCtx = new AudioContext();
var oscillator = audioCtx.createOscillator();
// Helper function to get new gain node
function getADSR () {
let adsr = new adsrGainNode(audioCtx);
adsr.setOptions({
attackAmp: 0.001,
decayAmp: 0.3,
sustainAmp: 0.7,
releaseAmp: 0.001,
attackTime: 1.1,
decayTime: 0.2,
sustainTime: 1.0,
releaseTime: 5.0,
/**
* If you are making e.g. a keyboard, then you may
* not auto-release the note
* If auto release is false then
* you should release the note using.
* `adsr.releaseNow()´
*/
autoRelease: true
});
return adsr
}
// Begin time for gain
var nowTime = audioCtx.currentTime
// Get adsr and the gain node
// Time it to begin in current time + 5 secs
let testTime = 2
var adsr = getADSR()
var gainNode = adsr.getGainNode(nowTime + testTime );
// Connect the oscillator to the gain node
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
// Start
oscillator.start(nowTime + testTime);
// Stop oscillator according to the ADSR
let endTime = adsr.releaseTime() + testTime
oscillator.stop(endTime)
// On a piano you may want to release the note, when
// the key is released.
//
// Then you may do something like this to end the note and the gain node:
// E.g onKeyUp:
// oscillator.stop(this.adsr.releaseTimeNow())
// adsr.releaseNow()
~~~~
There is also a HTML form which can generate a web interface for
the control of gain nodes.
You can see it on [https://diversen.github.io/adsr-gain-node/](https://diversen.github.io/adsr-gain-node/)
The source is [test.js](test.js)
## License
MIT © [Dennis Iversen](https://github.com/diversen)