mirror of
https://github.com/Tonejs/Tone.js
synced 2024-12-28 12:33:12 +00:00
110 lines
No EOL
2.5 KiB
JavaScript
110 lines
No EOL
2.5 KiB
JavaScript
define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/source/Noise", "Tone/component/LowpassCombFilter"], function(Tone){
|
|
|
|
"use strict";
|
|
|
|
/**
|
|
* @class Karplus-String string synthesis. Often out of tune.
|
|
* Will change when the AudioWorkerNode is available across
|
|
* browsers.
|
|
*
|
|
* @constructor
|
|
* @extends {Tone.Instrument}
|
|
* @param {Object} [options] see the defaults
|
|
* @example
|
|
* var plucky = new Tone.PluckSynth();
|
|
*/
|
|
Tone.PluckSynth = function(options){
|
|
|
|
options = this.defaultArg(options, Tone.PluckSynth.defaults);
|
|
Tone.Instrument.call(this);
|
|
|
|
/**
|
|
* @type {Tone.Noise}
|
|
* @private
|
|
*/
|
|
this._noise = new Tone.Noise("pink");
|
|
|
|
/**
|
|
* The amount of noise at the attack.
|
|
* Nominal range of [0.1, 20]
|
|
* @type {number}
|
|
*/
|
|
this.attackNoise = 1;
|
|
|
|
/**
|
|
* the LFCF
|
|
* @type {Tone.LowpassCombFilter}
|
|
* @private
|
|
*/
|
|
this._lfcf = new Tone.LowpassCombFilter({
|
|
"resonance" : options.resonance,
|
|
"dampening" : options.dampening
|
|
});
|
|
|
|
/**
|
|
* the resonance control
|
|
* @type {NormalRange}
|
|
* @signal
|
|
*/
|
|
this.resonance = this._lfcf.resonance;
|
|
|
|
/**
|
|
* the dampening control. i.e. the lowpass filter frequency of the comb filter
|
|
* @type {Frequency}
|
|
* @signal
|
|
*/
|
|
this.dampening = this._lfcf.dampening;
|
|
|
|
//connections
|
|
this._noise.connect(this._lfcf);
|
|
this._lfcf.connect(this.output);
|
|
this._readOnly(["resonance", "dampening"]);
|
|
};
|
|
|
|
Tone.extend(Tone.PluckSynth, Tone.Instrument);
|
|
|
|
/**
|
|
* @static
|
|
* @const
|
|
* @type {Object}
|
|
*/
|
|
Tone.PluckSynth.defaults = {
|
|
"attackNoise" : 1,
|
|
"dampening" : 4000,
|
|
"resonance" : 0.9
|
|
};
|
|
|
|
/**
|
|
* trigger the attack portion
|
|
* @param {string|number} note the note name or frequency
|
|
* @param {Time} [time=now] the time of the note
|
|
* @returns {Tone.PluckSynth} this
|
|
*/
|
|
Tone.PluckSynth.prototype.triggerAttack = function(note, time) {
|
|
note = this.toFrequency(note);
|
|
time = this.toSeconds(time);
|
|
var delayAmount = 1 / note;
|
|
this._lfcf.delayTime.setValueAtTime(delayAmount, time);
|
|
this._noise.start(time);
|
|
this._noise.stop(time + delayAmount * this.attackNoise);
|
|
return this;
|
|
};
|
|
|
|
/**
|
|
* clean up
|
|
* @returns {Tone.PluckSynth} this
|
|
*/
|
|
Tone.PluckSynth.prototype.dispose = function(){
|
|
Tone.Instrument.prototype.dispose.call(this);
|
|
this._noise.dispose();
|
|
this._lfcf.dispose();
|
|
this._noise = null;
|
|
this._lfcf = null;
|
|
this._writable(["resonance", "dampening"]);
|
|
this.dampening = null;
|
|
this.resonance = null;
|
|
return this;
|
|
};
|
|
|
|
return Tone.PluckSynth;
|
|
}); |