Tone.js/Tone/instrument/DuoSynth.js

211 lines
5.3 KiB
JavaScript
Raw Normal View History

define(["../core/Tone", "../instrument/MonoSynth", "../component/LFO", "../signal/Signal",
"../signal/Multiply", "../instrument/Monophonic", "../core/Param"], function(Tone){
"use strict";
2014-08-26 01:34:09 +00:00
/**
2017-10-21 23:02:46 +00:00
* @class Tone.DuoSynth is a monophonic synth composed of two
* MonoSynths run in parallel with control over the
2014-08-26 01:34:09 +00:00
* frequency ratio between the two voices and vibrato effect.
2015-06-22 05:20:20 +00:00
* <img src="https://docs.google.com/drawings/d/1bL4GXvfRMMlqS7XyBm9CjL9KJPSUKbcdBNpqOlkFLxk/pub?w=1012&h=448">
2014-08-26 01:34:09 +00:00
*
* @constructor
* @extends {Tone.Monophonic}
2017-10-21 23:02:46 +00:00
* @param {Object} [options] the options available for the synth
2014-08-26 01:34:09 +00:00
* see defaults below
2015-02-28 04:24:51 +00:00
* @example
2015-06-16 02:36:20 +00:00
* var duoSynth = new Tone.DuoSynth().toMaster();
* duoSynth.triggerAttackRelease("C4", "2n");
2014-08-26 01:34:09 +00:00
*/
Tone.DuoSynth = function(options){
options = Tone.defaultArg(options, Tone.DuoSynth.defaults);
Tone.Monophonic.call(this, options);
2014-08-26 01:34:09 +00:00
/**
* the first voice
* @type {Tone.MonoSynth}
*/
this.voice0 = new Tone.MonoSynth(options.voice0);
2015-02-10 16:40:04 +00:00
this.voice0.volume.value = -10;
2014-08-26 01:34:09 +00:00
/**
* the second voice
* @type {Tone.MonoSynth}
*/
this.voice1 = new Tone.MonoSynth(options.voice1);
2015-02-10 16:40:04 +00:00
this.voice1.volume.value = -10;
2014-08-26 01:34:09 +00:00
/**
2017-10-21 23:02:46 +00:00
* The vibrato LFO.
2014-08-26 01:34:09 +00:00
* @type {Tone.LFO}
2014-09-05 15:32:33 +00:00
* @private
2014-08-26 01:34:09 +00:00
*/
this._vibrato = new Tone.LFO(options.vibratoRate, -50, 50);
this._vibrato.start();
2015-02-10 16:40:04 +00:00
/**
* the vibrato frequency
2015-06-14 00:20:36 +00:00
* @type {Frequency}
* @signal
2015-02-10 16:40:04 +00:00
*/
this.vibratoRate = this._vibrato.frequency;
2014-08-26 01:34:09 +00:00
/**
* the vibrato gain
* @type {Tone.Gain}
2014-08-26 01:34:09 +00:00
* @private
*/
this._vibratoGain = new Tone.Gain(options.vibratoAmount, Tone.Type.Positive);
2014-08-26 01:34:09 +00:00
/**
2015-02-10 16:40:04 +00:00
* The amount of vibrato
2015-11-18 03:52:22 +00:00
* @type {Positive}
2015-06-14 00:20:36 +00:00
* @signal
2014-08-26 01:34:09 +00:00
*/
this.vibratoAmount = this._vibratoGain.gain;
2014-08-26 01:34:09 +00:00
/**
* the frequency control
2015-06-14 00:20:36 +00:00
* @type {Frequency}
* @signal
2014-08-26 01:34:09 +00:00
*/
this.frequency = new Tone.Signal(440, Tone.Type.Frequency);
2014-08-26 01:34:09 +00:00
/**
2015-06-20 22:03:49 +00:00
* Harmonicity is the ratio between the two voices. A harmonicity of
2017-10-21 23:02:46 +00:00
* 1 is no change. Harmonicity = 2 means a change of an octave.
2015-06-14 02:30:53 +00:00
* @type {Positive}
* @signal
2015-06-20 22:03:49 +00:00
* @example
* //pitch voice1 an octave below voice0
* duoSynth.harmonicity.value = 0.5;
2014-08-26 01:34:09 +00:00
*/
2015-06-14 02:30:53 +00:00
this.harmonicity = new Tone.Multiply(options.harmonicity);
this.harmonicity.units = Tone.Type.Positive;
2014-08-26 01:34:09 +00:00
//control the two voices frequency
2014-09-03 18:53:05 +00:00
this.frequency.connect(this.voice0.frequency);
2015-06-14 02:30:53 +00:00
this.frequency.chain(this.harmonicity, this.voice1.frequency);
2014-08-26 01:34:09 +00:00
this._vibrato.connect(this._vibratoGain);
2014-12-01 02:20:12 +00:00
this._vibratoGain.fan(this.voice0.detune, this.voice1.detune);
2014-08-26 01:34:09 +00:00
this.voice0.connect(this.output);
this.voice1.connect(this.output);
2015-04-18 14:54:08 +00:00
this._readOnly(["voice0", "voice1", "frequency", "vibratoAmount", "vibratoRate"]);
2014-08-26 01:34:09 +00:00
};
Tone.extend(Tone.DuoSynth, Tone.Monophonic);
2014-08-26 01:34:09 +00:00
/**
* @static
* @type {Object}
*/
Tone.DuoSynth.defaults = {
"vibratoAmount" : 0.5,
"vibratoRate" : 5,
2014-09-03 18:53:05 +00:00
"harmonicity" : 1.5,
2014-08-26 01:34:09 +00:00
"voice0" : {
2015-01-06 04:33:05 +00:00
"volume" : -10,
2014-08-26 01:34:09 +00:00
"portamento" : 0,
2014-10-01 02:49:17 +00:00
"oscillator" : {
"type" : "sine"
},
2014-08-26 01:47:45 +00:00
"filterEnvelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
2014-09-02 20:57:46 +00:00
},
"envelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
2014-08-26 01:47:45 +00:00
}
2014-08-26 01:34:09 +00:00
},
"voice1" : {
2015-01-06 04:33:05 +00:00
"volume" : -10,
2014-08-26 01:34:09 +00:00
"portamento" : 0,
2014-10-01 02:49:17 +00:00
"oscillator" : {
"type" : "sine"
},
2014-08-26 01:47:45 +00:00
"filterEnvelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
2014-09-02 20:57:46 +00:00
},
"envelope" : {
"attack" : 0.01,
"decay" : 0.0,
"sustain" : 1,
"release" : 0.5
2014-08-26 01:47:45 +00:00
}
2014-08-26 01:34:09 +00:00
}
};
/**
* start the attack portion of the envelopes
2017-10-21 23:02:46 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the attack should start
* @param {NormalRange} [velocity=1] the velocity of the note (0-1)
* @returns {Tone.DuoSynth} this
* @private
2014-08-26 01:34:09 +00:00
*/
Tone.DuoSynth.prototype._triggerEnvelopeAttack = function(time, velocity){
time = this.toSeconds(time);
this.voice0._triggerEnvelopeAttack(time, velocity);
this.voice1._triggerEnvelopeAttack(time, velocity);
2015-02-02 18:30:36 +00:00
return this;
2014-08-26 01:34:09 +00:00
};
/**
* start the release portion of the envelopes
2017-10-21 23:02:46 +00:00
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time the release should start
* @returns {Tone.DuoSynth} this
* @private
2014-08-26 01:34:09 +00:00
*/
Tone.DuoSynth.prototype._triggerEnvelopeRelease = function(time){
this.voice0._triggerEnvelopeRelease(time);
this.voice1._triggerEnvelopeRelease(time);
2015-02-02 18:30:36 +00:00
return this;
2014-08-26 01:34:09 +00:00
};
/**
* Get the level of the output at the given time. Measures
* the envelope(s) value at the time.
* @param {Time} time The time to query the envelope value
* @return {NormalRange} The output level between 0-1
*/
Tone.DuoSynth.prototype.getLevelAtTime = function(time){
return (this.voice0.getLevelAtTime(time) + this.voice1.getLevelAtTime(time))/2;
};
2014-08-26 01:34:09 +00:00
/**
* clean up
* @returns {Tone.DuoSynth} this
2014-08-26 01:34:09 +00:00
*/
Tone.DuoSynth.prototype.dispose = function(){
Tone.Monophonic.prototype.dispose.call(this);
2015-04-18 14:54:08 +00:00
this._writable(["voice0", "voice1", "frequency", "vibratoAmount", "vibratoRate"]);
2014-08-26 01:34:09 +00:00
this.voice0.dispose();
this.voice0 = null;
2015-02-10 16:40:04 +00:00
this.voice1.dispose();
2014-08-26 01:34:09 +00:00
this.voice1 = null;
2015-02-10 16:40:04 +00:00
this.frequency.dispose();
2014-08-26 01:34:09 +00:00
this.frequency = null;
this._vibratoGain.dispose();
2014-08-26 01:34:09 +00:00
this._vibratoGain = null;
this._vibrato = null;
2015-06-14 02:30:53 +00:00
this.harmonicity.dispose();
this.harmonicity = null;
2015-02-10 16:40:04 +00:00
this.vibratoAmount.dispose();
this.vibratoAmount = null;
this.vibratoRate = null;
2015-02-02 18:30:36 +00:00
return this;
2014-08-26 01:34:09 +00:00
};
return Tone.DuoSynth;
2017-10-21 23:02:46 +00:00
});