From 3a93d52ee11609fa61ced84cb8a3a9e7ccc46896 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Sun, 5 Apr 2015 14:00:52 -0400 Subject: [PATCH] making source's signal attributes readOnly this helps avoid a common error which is overwriting a signal property instead of setting the .value attribute --- Tone/core/Buffer.js | 2 +- Tone/core/Tone.js | 34 ++++++++-------------------------- Tone/source/OmniOscillator.js | 5 ++--- Tone/source/Oscillator.js | 4 ++-- Tone/source/PWMOscillator.js | 9 +++------ Tone/source/Player.js | 1 - Tone/source/PulseOscillator.js | 7 +++---- Tone/source/Source.js | 2 +- 8 files changed, 20 insertions(+), 44 deletions(-) diff --git a/Tone/core/Buffer.js b/Tone/core/Buffer.js index fd40ea1b..4df1cf34 100644 --- a/Tone/core/Buffer.js +++ b/Tone/core/Buffer.js @@ -124,7 +124,7 @@ define(["Tone/core/Tone"], function(Tone){ Tone.prototype.dispose.call(this); Tone.Buffer._removeFromQueue(this); this._buffer = null; - this.onload = null; + this.onload = Tone.Buffer.defaults.onload; return this; }; diff --git a/Tone/core/Tone.js b/Tone/core/Tone.js index 72377e3c..0c356946 100644 --- a/Tone/core/Tone.js +++ b/Tone/core/Tone.js @@ -503,33 +503,15 @@ define(function(){ Tone.prototype.isFunction = isFunction; /** - * interpolate the input value (0-1) to be between outputMin and outputMax - * @param {number} input - * @param {number} outputMin - * @param {number} outputMax - * @return {number} + * Make the property not writable. Internal use only. + * @private + * @param {string} property the property to make not writeable */ - Tone.prototype.interpolate = function(input, outputMin, outputMax){ - return input*(outputMax - outputMin) + outputMin; - }; - - /** - * normalize the input to 0-1 from between inputMin to inputMax - * @param {number} input - * @param {number} inputMin - * @param {number} inputMax - * @return {number} - */ - Tone.prototype.normalize = function(input, inputMin, inputMax){ - //make sure that min < max - if (inputMin > inputMax){ - var tmp = inputMax; - inputMax = inputMin; - inputMin = tmp; - } else if (inputMin == inputMax){ - return 0; - } - return (input - inputMin) / (inputMax - inputMin); + Tone.prototype._readOnly = function(property){ + Object.defineProperty(this, property, { + writable: false, + enumerable : true, + }); }; /////////////////////////////////////////////////////////////////////////// diff --git a/Tone/source/OmniOscillator.js b/Tone/source/OmniOscillator.js index a14e6dac..7a4c1a56 100644 --- a/Tone/source/OmniOscillator.js +++ b/Tone/source/OmniOscillator.js @@ -25,12 +25,14 @@ function(Tone){ * @type {Tone.Signal} */ this.frequency = new Tone.Signal(options.frequency, Tone.Signal.Units.Frequency); + this._readOnly("frequency"); /** * the detune control * @type {Tone.Signal} */ this.detune = new Tone.Signal(options.detune); + this._readOnly("detune"); /** * the type of the oscillator source @@ -211,11 +213,8 @@ function(Tone){ Tone.OmniOscillator.prototype.dispose = function(){ Tone.Source.prototype.dispose.call(this); this.detune.dispose(); - this.detune = null; this.frequency.dispose(); - this.frequency = null; this._oscillator.dispose(); - this._oscillator = null; this._sourceType = null; return this; }; diff --git a/Tone/source/Oscillator.js b/Tone/source/Oscillator.js index 4f68d21b..5cc8d10e 100644 --- a/Tone/source/Oscillator.js +++ b/Tone/source/Oscillator.js @@ -30,12 +30,14 @@ function(Tone){ * @type {Tone.Signal} */ this.frequency = new Tone.Signal(options.frequency, Tone.Signal.Units.Frequency); + this._readOnly("frequency"); /** * The detune control signal in cents. * @type {Tone.Signal} */ this.detune = new Tone.Signal(options.detune); + this._readOnly("detune"); /** * the periodic wave @@ -237,9 +239,7 @@ function(Tone){ this._oscillator = null; } this.frequency.dispose(); - this.frequency = null; this.detune.dispose(); - this.detune = null; this._wave = null; return this; }; diff --git a/Tone/source/PWMOscillator.js b/Tone/source/PWMOscillator.js index 50787763..8d515414 100644 --- a/Tone/source/PWMOscillator.js +++ b/Tone/source/PWMOscillator.js @@ -48,18 +48,21 @@ function(Tone){ * @type {Tone.Signal} */ this.frequency = this._modulator.frequency; + this._readOnly("frequency"); /** * the detune control * @type {Tone.Signal} */ this.detune = this._modulator.detune; + this._readOnly("detune"); /** * the modulation rate of the oscillator * @type {Tone.Signal} */ this.modulationFrequency = this._pulse.frequency; + this._readOnly("modulationFrequency"); //connections this._modulator.chain(this._scale, this._pulse.width); @@ -137,14 +140,8 @@ function(Tone){ Tone.PWMOscillator.prototype.dispose = function(){ Tone.Source.prototype.dispose.call(this); this._pulse.dispose(); - this._pulse = null; this._scale.dispose(); - this._scale = null; this._modulator.dispose(); - this._modulator = null; - this.frequency = null; - this.detune = null; - this.modulationFrequency = null; return this; }; diff --git a/Tone/source/Player.js b/Tone/source/Player.js index 3c6ce5f9..6fb23795 100644 --- a/Tone/source/Player.js +++ b/Tone/source/Player.js @@ -304,7 +304,6 @@ define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/source/Source"], function(To this._source = null; } this._buffer.dispose(); - this._buffer = null; return this; }; diff --git a/Tone/source/PulseOscillator.js b/Tone/source/PulseOscillator.js index d7f63a1f..e38ca4b4 100644 --- a/Tone/source/PulseOscillator.js +++ b/Tone/source/PulseOscillator.js @@ -23,6 +23,7 @@ function(Tone){ * @type {Tone.Signal} */ this.width = new Tone.Signal(options.width, Tone.Signal.Units.Normal); + this._readOnly("width"); /** * gate the width amount @@ -48,12 +49,14 @@ function(Tone){ * @type {Tone.Signal} */ this.frequency = this._sawtooth.frequency; + this._readOnly("frequency"); /** * The detune in cents. * @type {Tone.Signal} */ this.detune = this._sawtooth.detune; + this._readOnly("detune"); /** * Threshold the signal to turn it into a square @@ -147,15 +150,11 @@ function(Tone){ Tone.PulseOscillator.prototype.dispose = function(){ Tone.Source.prototype.dispose.call(this); this._sawtooth.dispose(); - this._sawtooth = null; this.width.dispose(); - this.width = null; this._widthGate.disconnect(); this._widthGate = null; this._thresh.disconnect(); this._thresh = null; - this.frequency = null; - this.detune = null; return this; }; diff --git a/Tone/source/Source.js b/Tone/source/Source.js index 54ce102d..02741bc9 100644 --- a/Tone/source/Source.js +++ b/Tone/source/Source.js @@ -47,6 +47,7 @@ define(["Tone/core/Tone", "Tone/core/Transport", "Tone/core/Master"], function(T * source.volume.value = -6; */ this.volume = new Tone.Signal(this.output.gain, Tone.Signal.Units.Decibels); + this._readOnly("volume"); /** * keeps track of the timeout for chaning the state @@ -197,7 +198,6 @@ define(["Tone/core/Tone", "Tone/core/Transport", "Tone/core/Master"], function(T clearTimeout(this._timeout); this.onended = function(){}; this.volume.dispose(); - this.volume = null; }; return Tone.Source;