2014-09-20 22:06:52 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/source/Noise", "Tone/component/LowpassCombFilter"], function(Tone){
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2015-06-14 04:32:17 +00:00
|
|
|
* @class Karplus-String string synthesis. Often out of tune.
|
|
|
|
* Will change when the AudioWorkerNode is available across
|
|
|
|
* browsers.
|
2014-09-20 22:06:52 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Instrument}
|
2015-06-14 03:15:57 +00:00
|
|
|
* @param {Object} [options] see the defaults
|
2015-02-28 04:24:51 +00:00
|
|
|
* @example
|
2015-06-16 02:36:20 +00:00
|
|
|
* var plucky = new Tone.PluckSynth().toMaster();
|
2015-06-20 22:03:49 +00:00
|
|
|
* plucky.triggerAttack("C4");
|
2014-09-20 22:06:52 +00:00
|
|
|
*/
|
2014-09-25 03:43:44 +00:00
|
|
|
Tone.PluckSynth = function(options){
|
2014-09-20 22:06:52 +00:00
|
|
|
|
2014-09-25 03:43:44 +00:00
|
|
|
options = this.defaultArg(options, Tone.PluckSynth.defaults);
|
2015-07-04 17:36:22 +00:00
|
|
|
Tone.Instrument.call(this, options);
|
2014-09-20 22:06:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Tone.Noise}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._noise = new Tone.Noise("pink");
|
|
|
|
|
|
|
|
/**
|
2015-02-10 16:40:04 +00:00
|
|
|
* The amount of noise at the attack.
|
|
|
|
* Nominal range of [0.1, 20]
|
2014-09-20 22:06:52 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
2016-10-19 05:01:40 +00:00
|
|
|
this.attackNoise = options.attackNoise;
|
2014-09-20 22:06:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the LFCF
|
|
|
|
* @type {Tone.LowpassCombFilter}
|
|
|
|
* @private
|
|
|
|
*/
|
2015-05-13 16:13:25 +00:00
|
|
|
this._lfcf = new Tone.LowpassCombFilter({
|
|
|
|
"resonance" : options.resonance,
|
|
|
|
"dampening" : options.dampening
|
|
|
|
});
|
2014-09-20 22:06:52 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-20 22:03:49 +00:00
|
|
|
* The resonance control.
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {NormalRange}
|
|
|
|
* @signal
|
2014-09-20 22:06:52 +00:00
|
|
|
*/
|
|
|
|
this.resonance = this._lfcf.resonance;
|
|
|
|
|
|
|
|
/**
|
2015-06-20 22:03:49 +00:00
|
|
|
* The dampening control. i.e. the lowpass filter frequency of the comb filter
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Frequency}
|
|
|
|
* @signal
|
2014-09-20 22:06:52 +00:00
|
|
|
*/
|
|
|
|
this.dampening = this._lfcf.dampening;
|
|
|
|
|
|
|
|
//connections
|
|
|
|
this._noise.connect(this._lfcf);
|
|
|
|
this._lfcf.connect(this.output);
|
2015-04-18 14:54:08 +00:00
|
|
|
this._readOnly(["resonance", "dampening"]);
|
2014-09-20 22:06:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.PluckSynth, Tone.Instrument);
|
|
|
|
|
2014-09-25 03:43:44 +00:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.PluckSynth.defaults = {
|
|
|
|
"attackNoise" : 1,
|
|
|
|
"dampening" : 4000,
|
2015-05-13 16:13:25 +00:00
|
|
|
"resonance" : 0.9
|
2014-09-25 03:43:44 +00:00
|
|
|
};
|
2014-09-20 22:06:52 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-20 22:03:49 +00:00
|
|
|
* Trigger the note.
|
|
|
|
* @param {Frequency} note The note to trigger.
|
|
|
|
* @param {Time} [time=now] When the note should be triggered.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.PluckSynth} this
|
2014-09-20 22:06:52 +00:00
|
|
|
*/
|
|
|
|
Tone.PluckSynth.prototype.triggerAttack = function(note, time) {
|
2015-02-10 16:40:04 +00:00
|
|
|
note = this.toFrequency(note);
|
2014-09-20 22:06:52 +00:00
|
|
|
time = this.toSeconds(time);
|
|
|
|
var delayAmount = 1 / note;
|
2015-04-24 21:45:03 +00:00
|
|
|
this._lfcf.delayTime.setValueAtTime(delayAmount, time);
|
2014-09-20 22:06:52 +00:00
|
|
|
this._noise.start(time);
|
|
|
|
this._noise.stop(time + delayAmount * this.attackNoise);
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-20 22:06:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-20 22:03:49 +00:00
|
|
|
* Clean up.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.PluckSynth} this
|
2014-09-20 22:06:52 +00:00
|
|
|
*/
|
|
|
|
Tone.PluckSynth.prototype.dispose = function(){
|
2014-09-20 23:23:41 +00:00
|
|
|
Tone.Instrument.prototype.dispose.call(this);
|
|
|
|
this._noise.dispose();
|
|
|
|
this._lfcf.dispose();
|
|
|
|
this._noise = null;
|
|
|
|
this._lfcf = null;
|
2015-04-18 14:54:08 +00:00
|
|
|
this._writable(["resonance", "dampening"]);
|
2014-09-20 23:23:41 +00:00
|
|
|
this.dampening = null;
|
|
|
|
this.resonance = null;
|
2015-02-02 18:30:36 +00:00
|
|
|
return this;
|
2014-09-20 22:06:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.PluckSynth;
|
|
|
|
});
|