Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/notthetup/smoothfade
smooth fading in webaudio
https://github.com/notthetup/smoothfade
fade gain gainnode web-audio-api webaudio webaudio-api
Last synced: about 1 month ago
JSON representation
smooth fading in webaudio
- Host: GitHub
- URL: https://github.com/notthetup/smoothfade
- Owner: notthetup
- License: apache-2.0
- Created: 2015-07-19T05:22:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-03-09T02:54:24.000Z (almost 2 years ago)
- Last Synced: 2024-10-29T20:12:08.981Z (about 1 month ago)
- Topics: fade, gain, gainnode, web-audio-api, webaudio, webaudio-api
- Language: JavaScript
- Size: 50.8 KB
- Stars: 35
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-javascript-audio - notthetup/smoothfade - smooth fade between AudioNodes (Libraries: Web Audio API / Web MIDI API)
- awesome-webaudio - smoothfade - A library for smoothly fading between two AudioNodes. (Packages / Libraries)
README
# smoothfade
[![npm version](https://badge.fury.io/js/smoothfade.svg)](http://badge.fury.io/js/smoothfade)
Smooth fading of volume (gain) in [WebAudio](http://webaudio.github.io/web-audio-api/) is possible with parameter automation on the [GainNode](http://webaudio.github.io/web-audio-api/#the-gainnode-interface).
However, currently, there is no easy way to stop and change (for eg. reverse) an automation smoothly. Once an [AudioParam](http://webaudio.github.io/web-audio-api/#idl-def-AudioParam) is automated, there is no easy way to know it's value at a given point of time except for calculating it manually using the [automation equations](http://webaudio.github.io/web-audio-api/#widl-AudioParam-exponentialRampToValueAtTime-void-float-value-double-endTime). Hence stopping and reversing the automation is not trivial.
[This JSFiddle](http://jsfiddle.net/notthetup/zrLb0pcy/) shows the various techniques that don't work for smooth fading, and finally the calculation based technique which does.
This library does the calculation and allows fading in/out smoothly.
# Usage
```
npm install smoothfade
``````js
// wrap the smoothfade around a gain nodevar sm = smoothfade(context, gain);
// use fadeIn/fadeOut functions to
// fadeIn/fadeOut the audio.sm.fadeIn();
sm.fadeOut();
```# API
## Constructor
eg: `var sm = smoothfade(context, gainNode, options);`
- `context`: __AudioContext__ - The [AudioContext](http://webaudio.github.io/web-audio-api/#the-audiocontext-interface) within which the [GainNode](http://webaudio.github.io/web-audio-api/#idl-def-AudioNode) has been created.
- `gainNode`: __GainNode__ - The [GainNode](http://webaudio.github.io/web-audio-api/#the-gainnode-interface) which is connected to the audio graph which needs to be faded in/out.
- `options`: __Object__ - Optional object attribute which contains extra configuration options in the following format
- `startValue` : __Number__ - The initial starting value of the gainNode. If not specified, the library attempts to read it from the gainNode itself. This is useful if the gainNode is already being automated before this library is initialized.
- `type` : __String__ - The type of automation to use. Currently support 'linear' and 'exponential'. 'exponential' fade is considered to be more natural to human ears and hence is the default
- `fadeLength` : __Number__ - The time taken (in seconds) for a complete fadeout (from 1 - 0) or fadein (from 0 - 1). This determines how quickly the volume fades for both fadeins and fadeouts. This defaults to 10.## Methods
- `fadeIn` : Fade in the audio. Slowly increase the gain of gainNode.
- eg :
```js
sm.fadeIn(options);
```
- arguments (optional):
- `targetValue` : __Number__ - The targeted value of the gain (between 0 and 1) where the fadeIn will stop. This defaults to 1.
- `startTime` : __Number__ - Timestamp to define when the fading starts, in the same time coordinate system as the AudioContext _currentTime_ attribute. This defaults _currentTime_.- `fadeOut` : Fade out the audio. Slowly decrease the gain of gainNode.
- eg :
```js
sm.fadeOut(options);
```
- arguments (optional):
- `targetValue` : __Number__ - The targeted value of the gain (between 0 and 1) where the fadeOut will stop. This defaults to 1.
- `startTime` : __Number__ - Timestamp to define when the fading starts, in the same time coordinate system as the AudioContext _currentTime_ attribute. This defaults _currentTime_.# License
Apache-2.0
See License file