scratch-audio/src/effects/FuzzEffect.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-04-17 11:22:16 -04:00
const Tone = require('tone');
2016-11-29 18:33:09 -05:00
2017-02-01 18:02:04 -05:00
/**
2017-02-02 14:53:17 -05:00
* A fuzz effect (aka 'distortion effect' in audio terms)
2017-02-01 18:02:04 -05:00
* Effect value controls the wet/dry amount:
* 0 passes through none of the effect, 100 passes through all effect
* Clamped 0-100
* @constructor
*/
const FuzzEffect = function () {
2016-11-29 18:33:09 -05:00
Tone.Effect.call(this);
this.value = 0;
this.distortion = new Tone.Distortion(1);
this.effectSend.chain(this.distortion, this.effectReturn);
};
2016-11-29 18:33:09 -05:00
Tone.extend(FuzzEffect, Tone.Effect);
2017-02-01 18:02:04 -05:00
/**
* Set the effect value
* @param {number} val - the new value to set the effect to
*/
2016-11-29 18:33:09 -05:00
FuzzEffect.prototype.set = function (val) {
this.value = val;
this.value = this.clamp(this.value, 0, 100);
this.distortion.wet.value = this.value / 100;
};
2017-02-01 18:02:04 -05:00
/**
* Change the effect value
* @param {number} val - the value to change the effect by
*/
2016-11-29 18:33:09 -05:00
FuzzEffect.prototype.changeBy = function (val) {
this.set(this.value + val);
};
2017-02-01 18:02:04 -05:00
/**
* @param {number} input - the input to clamp
* @param {number} min - the min value to clamp to
* @param {number} max - the max value to clamp to
* @return {number} the clamped value
2017-02-01 18:02:04 -05:00
*/
2016-11-29 18:33:09 -05:00
FuzzEffect.prototype.clamp = function (input, min, max) {
return Math.min(Math.max(input, min), max);
};
module.exports = FuzzEffect;