From 51d06bd9873b2f1936a3169930f9696f1ccfb845 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Mon, 16 Dec 2019 16:42:40 -0500 Subject: [PATCH] feat: Removing Ctrl classes breaking changes: Removing CtrlInterpolate, CtrlRandom, CtrlMarkov. --- Tone/control/CtrlInterpolate.js | 131 ---------------- Tone/control/CtrlMarkov.js | 122 --------------- Tone/control/CtrlPattern.js | 268 -------------------------------- Tone/control/CtrlRandom.js | 76 --------- 4 files changed, 597 deletions(-) delete mode 100644 Tone/control/CtrlInterpolate.js delete mode 100644 Tone/control/CtrlMarkov.js delete mode 100644 Tone/control/CtrlPattern.js delete mode 100644 Tone/control/CtrlRandom.js diff --git a/Tone/control/CtrlInterpolate.js b/Tone/control/CtrlInterpolate.js deleted file mode 100644 index a3e33e8f7..000000000 --- a/Tone/control/CtrlInterpolate.js +++ /dev/null @@ -1,131 +0,0 @@ -import Tone from "../core/Tone"; -import "../type/Type"; - -/** - * @class Tone.CtrlInterpolate will interpolate between given values based - * on the "index" property. Passing in an array or object literal - * will interpolate each of the parameters. Note (i.e. "C3") - * and Time (i.e. "4n + 2") can be interpolated. All other values are - * assumed to be numbers. - * @example - * var interp = new Tone.CtrlInterpolate([0, 2, 9, 4]); - * interp.index = 0.75; - * interp.value; //returns 1.5 - * - * @example - * var interp = new Tone.CtrlInterpolate([ - * [2, 4, 5], - * [9, 3, 2], - * ]); - * @param {Array} values The array of values to interpolate over - * @param {Positive} index The initial interpolation index. - * @extends {Tone} - */ -Tone.CtrlInterpolate = function(){ - - var options = Tone.defaults(arguments, ["values", "index"], Tone.CtrlInterpolate); - Tone.call(this); - - /** - * The values to interpolate between - * @type {Array} - */ - this.values = options.values; - - /** - * The interpolated index between values. For example: a value of 1.5 - * would interpolate equally between the value at index 1 - * and the value at index 2. - * @example - * interp.index = 0; - * interp.value; //returns the value at 0 - * interp.index = 0.5; - * interp.value; //returns the value between indices 0 and 1. - * @type {Positive} - */ - this.index = options.index; -}; - -Tone.extend(Tone.CtrlInterpolate); - -/** - * The defaults - * @const - * @type {Object} - */ -Tone.CtrlInterpolate.defaults = { - "index" : 0, - "values" : [] -}; - -/** - * The current interpolated value based on the index - * @readOnly - * @memberOf Tone.CtrlInterpolate# - * @type {*} - * @name value - */ -Object.defineProperty(Tone.CtrlInterpolate.prototype, "value", { - get : function(){ - var index = this.index; - index = Math.min(index, this.values.length - 1); - var lowerPosition = Math.floor(index); - var lower = this.values[lowerPosition]; - var upper = this.values[Math.ceil(index)]; - return this._interpolate(index - lowerPosition, lower, upper); - } -}); - -/** - * Internal interpolation routine - * @param {NormalRange} index The index between the lower and upper - * @param {*} lower - * @param {*} upper - * @return {*} The interpolated value - * @private - */ -Tone.CtrlInterpolate.prototype._interpolate = function(index, lower, upper){ - if (Tone.isArray(lower)){ - var retArray = []; - for (var i = 0; i < lower.length; i++){ - retArray[i] = this._interpolate(index, lower[i], upper[i]); - } - return retArray; - } else if (Tone.isObject(lower)){ - var retObj = {}; - for (var attr in lower){ - retObj[attr] = this._interpolate(index, lower[attr], upper[attr]); - } - return retObj; - } else { - lower = this._toNumber(lower); - upper = this._toNumber(upper); - return (1 - index) * lower + index * upper; - } -}; - -/** - * Convert from the given type into a number - * @param {Number|String} value - * @return {Number} - * @private - */ -Tone.CtrlInterpolate.prototype._toNumber = function(val){ - if (Tone.isNumber(val)){ - return val; - } else { - //otherwise assume that it's Time... - return this.toSeconds(val); - } -}; - -/** - * Clean up - * @return {Tone.CtrlInterpolate} this - */ -Tone.CtrlInterpolate.prototype.dispose = function(){ - this.values = null; -}; - -export default Tone.CtrlInterpolate; - diff --git a/Tone/control/CtrlMarkov.js b/Tone/control/CtrlMarkov.js deleted file mode 100644 index a14c0c047..000000000 --- a/Tone/control/CtrlMarkov.js +++ /dev/null @@ -1,122 +0,0 @@ -import Tone from "../core/Tone"; - -/** - * @class Tone.CtrlMarkov represents a Markov Chain where each call - * to Tone.CtrlMarkov.next will move to the next state. If the next - * state choice is an array, the next state is chosen randomly with - * even probability for all of the choices. For a weighted probability - * of the next choices, pass in an object with "state" and "probability" attributes. - * The probabilities will be normalized and then chosen. If no next options - * are given for the current state, the state will stay there. - * @extends {Tone} - * @example - * var chain = new Tone.CtrlMarkov({ - * "beginning" : ["end", "middle"], - * "middle" : "end" - * }); - * chain.value = "beginning"; - * chain.next(); //returns "end" or "middle" with 50% probability - * - * @example - * var chain = new Tone.CtrlMarkov({ - * "beginning" : [{"value" : "end", "probability" : 0.8}, - * {"value" : "middle", "probability" : 0.2}], - * "middle" : "end" - * }); - * chain.value = "beginning"; - * chain.next(); //returns "end" with 80% probability or "middle" with 20%. - * @param {Object} values An object with the state names as the keys - * and the next state(s) as the values. - */ -Tone.CtrlMarkov = function(values, initial){ - - Tone.call(this); - - /** - * The Markov values with states as the keys - * and next state(s) as the values. - * @type {Object} - */ - this.values = Tone.defaultArg(values, {}); - - /** - * The current state of the Markov values. The next - * state will be evaluated and returned when Tone.CtrlMarkov.next - * is invoked. - * @type {String} - */ - this.value = Tone.defaultArg(initial, Object.keys(this.values)[0]); -}; - -Tone.extend(Tone.CtrlMarkov); - -/** - * Returns the next state of the Markov values. - * @return {String} - */ -Tone.CtrlMarkov.prototype.next = function(){ - if (this.values.hasOwnProperty(this.value)){ - var next = this.values[this.value]; - if (Tone.isArray(next)){ - var distribution = this._getProbDistribution(next); - var rand = Math.random(); - var total = 0; - for (var i = 0; i < distribution.length; i++){ - var dist = distribution[i]; - if (rand > total && rand < total + dist){ - var chosen = next[i]; - if (Tone.isObject(chosen)){ - this.value = chosen.value; - } else { - this.value = chosen; - } - } - total += dist; - } - } else { - this.value = next; - } - } - return this.value; -}; - -/** - * Choose randomly from an array weighted options in the form - * {"state" : string, "probability" : number} or an array of values - * @param {Array} options - * @return {Array} The randomly selected choice - * @private - */ -Tone.CtrlMarkov.prototype._getProbDistribution = function(options){ - var distribution = []; - var total = 0; - var needsNormalizing = false; - for (var i = 0; i < options.length; i++){ - var option = options[i]; - if (Tone.isObject(option)){ - needsNormalizing = true; - distribution[i] = option.probability; - } else { - distribution[i] = 1 / options.length; - } - total += distribution[i]; - } - if (needsNormalizing){ - //normalize the values - for (var j = 0; j < distribution.length; j++){ - distribution[j] = distribution[j] / total; - } - } - return distribution; -}; - -/** - * Clean up - * @return {Tone.CtrlMarkov} this - */ -Tone.CtrlMarkov.prototype.dispose = function(){ - this.values = null; -}; - -export default Tone.CtrlMarkov; - diff --git a/Tone/control/CtrlPattern.js b/Tone/control/CtrlPattern.js deleted file mode 100644 index 3c2596e8f..000000000 --- a/Tone/control/CtrlPattern.js +++ /dev/null @@ -1,268 +0,0 @@ -import Tone from "../core/Tone"; - -/** - * @class Generate patterns from an array of values. - * Has a number of arpeggiation and randomized - * selection patterns. - * - * @param {Array} values An array of options to choose from. - * @param {Tone.CtrlPattern.Type=} type The name of the pattern. - * @extends {Tone} - */ -Tone.CtrlPattern = function(){ - - var options = Tone.defaults(arguments, ["values", "type"], Tone.CtrlPattern); - Tone.call(this); - - /** - * The array of values to arpeggiate over - * @type {Array} - */ - this.values = options.values; - - /** - * The current position in the values array - * @type {Number} - */ - this.index = 0; - - /** - * The type placeholder - * @type {Tone.CtrlPattern.Type} - * @private - */ - this._type = null; - - /** - * Shuffled values for the RandomOnce type - * @type {Array} - * @private - */ - this._shuffled = null; - - /** - * The direction of the movement - * @type {String} - * @private - */ - this._direction = null; - - this.type = options.type; -}; - -Tone.extend(Tone.CtrlPattern); - -/** - * The Control Patterns - * @type {Object} - * @static - */ -Tone.CtrlPattern.Type = { - Up : "up", - Down : "down", - UpDown : "upDown", - DownUp : "downUp", - AlternateUp : "alternateUp", - AlternateDown : "alternateDown", - Random : "random", - RandomWalk : "randomWalk", - RandomOnce : "randomOnce", -}; - -/** - * The default values. - * @type {Object} - */ -Tone.CtrlPattern.defaults = { - "type" : Tone.CtrlPattern.Type.Up, - "values" : [] -}; - -/** - * The value at the current index of the pattern. - * @readOnly - * @memberOf Tone.CtrlPattern# - * @type {*} - * @name value - */ -Object.defineProperty(Tone.CtrlPattern.prototype, "value", { - get : function(){ - //some safeguards - if (this.values.length === 0){ - return undefined; - } else if (this.values.length === 1){ - return this.values[0]; - } - this.index = Math.min(this.index, this.values.length - 1); - var val = this.values[this.index]; - if (this.type === Tone.CtrlPattern.Type.RandomOnce){ - if (this.values.length !== this._shuffled.length){ - this._shuffleValues(); - } - val = this.values[this._shuffled[this.index]]; - } - return val; - } -}); - -/** - * The pattern used to select the next - * item from the values array - * @memberOf Tone.CtrlPattern# - * @type {Tone.CtrlPattern.Type} - * @name type - */ -Object.defineProperty(Tone.CtrlPattern.prototype, "type", { - get : function(){ - return this._type; - }, - set : function(type){ - this._type = type; - this._shuffled = null; - - //the first index - if (this._type === Tone.CtrlPattern.Type.Up || - this._type === Tone.CtrlPattern.Type.UpDown || - this._type === Tone.CtrlPattern.Type.RandomOnce || - this._type === Tone.CtrlPattern.Type.AlternateUp){ - this.index = 0; - } else if (this._type === Tone.CtrlPattern.Type.Down || - this._type === Tone.CtrlPattern.Type.DownUp || - this._type === Tone.CtrlPattern.Type.AlternateDown){ - this.index = this.values.length - 1; - } - - //the direction - if (this._type === Tone.CtrlPattern.Type.UpDown || - this._type === Tone.CtrlPattern.Type.AlternateUp){ - this._direction = Tone.CtrlPattern.Type.Up; - } else if (this._type === Tone.CtrlPattern.Type.DownUp || - this._type === Tone.CtrlPattern.Type.AlternateDown){ - this._direction = Tone.CtrlPattern.Type.Down; - } - - //randoms - if (this._type === Tone.CtrlPattern.Type.RandomOnce){ - this._shuffleValues(); - } else if (this._type === Tone.CtrlPattern.Type.Random){ - this.index = Math.floor(Math.random() * this.values.length); - } - } -}); - -/** - * Return the next value given the current position - * and pattern. - * @return {*} The next value - */ -Tone.CtrlPattern.prototype.next = function(){ - - var type = this.type; - - //choose the next index - if (type === Tone.CtrlPattern.Type.Up){ - this.index++; - if (this.index >= this.values.length){ - this.index = 0; - } - } else if (type === Tone.CtrlPattern.Type.Down){ - this.index--; - if (this.index < 0){ - this.index = this.values.length - 1; - } - } else if (type === Tone.CtrlPattern.Type.UpDown || - type === Tone.CtrlPattern.Type.DownUp){ - if (this._direction === Tone.CtrlPattern.Type.Up){ - this.index++; - } else { - this.index--; - } - if (this.index < 0){ - this.index = 1; - this._direction = Tone.CtrlPattern.Type.Up; - } else if (this.index >= this.values.length){ - this.index = this.values.length - 2; - this._direction = Tone.CtrlPattern.Type.Down; - } - } else if (type === Tone.CtrlPattern.Type.Random){ - this.index = Math.floor(Math.random() * this.values.length); - } else if (type === Tone.CtrlPattern.Type.RandomWalk){ - if (Math.random() < 0.5){ - this.index--; - this.index = Math.max(this.index, 0); - } else { - this.index++; - this.index = Math.min(this.index, this.values.length - 1); - } - } else if (type === Tone.CtrlPattern.Type.RandomOnce){ - this.index++; - if (this.index >= this.values.length){ - this.index = 0; - //reshuffle the values for next time - this._shuffleValues(); - } - } else if (type === Tone.CtrlPattern.Type.AlternateUp){ - if (this._direction === Tone.CtrlPattern.Type.Up){ - this.index += 2; - this._direction = Tone.CtrlPattern.Type.Down; - } else { - this.index -= 1; - this._direction = Tone.CtrlPattern.Type.Up; - } - if (this.index >= this.values.length){ - this.index = 0; - this._direction = Tone.CtrlPattern.Type.Up; - } - } else if (type === Tone.CtrlPattern.Type.AlternateDown){ - if (this._direction === Tone.CtrlPattern.Type.Up){ - this.index += 1; - this._direction = Tone.CtrlPattern.Type.Down; - } else { - this.index -= 2; - this._direction = Tone.CtrlPattern.Type.Up; - } - if (this.index < 0){ - this.index = this.values.length - 1; - this._direction = Tone.CtrlPattern.Type.Down; - } - } - return this.value; -}; - -/** - * Shuffles the values and places the results into the _shuffled - * @private - */ -Tone.CtrlPattern.prototype._shuffleValues = function(){ - var copy = []; - this._shuffled = []; - for (var i = 0; i < this.values.length; i++){ - copy[i] = i; - } - while (copy.length > 0){ - var randVal = copy.splice(Math.floor(copy.length * Math.random()), 1); - this._shuffled.push(randVal[0]); - } -}; - -/** - * Clean up - * @returns {Tone.CtrlPattern} this - */ -Tone.CtrlPattern.prototype.dispose = function(){ - this._shuffled = null; - this.values = null; -}; - -export default Tone.CtrlPattern; - diff --git a/Tone/control/CtrlRandom.js b/Tone/control/CtrlRandom.js deleted file mode 100644 index 00f676042..000000000 --- a/Tone/control/CtrlRandom.js +++ /dev/null @@ -1,76 +0,0 @@ -import Tone from "../core/Tone"; -import "../type/Type"; - -/** - * @class Choose a random value. - * @extends {Tone} - * @example - * var randomWalk = new Tone.CtrlRandom({ - * "min" : 0, - * "max" : 10, - * "integer" : true - * }); - * randomWalk.eval(); - * - * @param {Number|Time=} min The minimum return value. - * @param {Number|Time=} max The maximum return value. - */ -Tone.CtrlRandom = function(){ - - var options = Tone.defaults(arguments, ["min", "max"], Tone.CtrlRandom); - Tone.call(this); - - /** - * The minimum return value - * @type {Number|Time} - */ - this.min = options.min; - - /** - * The maximum return value - * @type {Number|Time} - */ - this.max = options.max; - - /** - * If the return value should be an integer - * @type {Boolean} - */ - this.integer = options.integer; -}; - -Tone.extend(Tone.CtrlRandom); - -/** - * The defaults - * @const - * @type {Object} - */ -Tone.CtrlRandom.defaults = { - "min" : 0, - "max" : 1, - "integer" : false -}; - -/** - * Return a random value between min and max. - * @readOnly - * @memberOf Tone.CtrlRandom# - * @type {*} - * @name value - */ -Object.defineProperty(Tone.CtrlRandom.prototype, "value", { - get : function(){ - var min = this.toSeconds(this.min); - var max = this.toSeconds(this.max); - var rand = Math.random(); - var val = rand * min + (1 - rand) * max; - if (this.integer){ - val = Math.floor(val); - } - return val; - } -}); - -export default Tone.CtrlRandom; -