Skip to content

Commit

Permalink
eval -> valueOf
Browse files Browse the repository at this point in the history
thanks for the suggestion @JackCA
fixes #205
  • Loading branch information
tambien committed Mar 13, 2017
1 parent ea1b92e commit d037c28
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 194 deletions.
4 changes: 2 additions & 2 deletions Tone/core/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function(Tone){
//add some swing
var progress = (ticks % (this._swingTicks * 2)) / (this._swingTicks * 2);
var amount = Math.sin((progress) * Math.PI) * this._swingAmount;
tickTime += Tone.Time(this._swingTicks * 2/3, "i").eval() * amount;
tickTime += Tone.Time(this._swingTicks * 2/3, "i") * amount;
}
//do the loop test
if (this.loop){
Expand Down Expand Up @@ -707,7 +707,7 @@ function(Tone){
} else {
return 0;
}
var transportPos = Tone.Time(this.ticks, "i").eval();
var transportPos = Tone.Time(this.ticks, "i");
var remainingTime = subdivision - (transportPos % subdivision);
if (remainingTime === 0){
remainingTime = subdivision;
Expand Down
16 changes: 8 additions & 8 deletions Tone/type/Frequency.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* @param {String|Number} val The time value.
* @param {String=} units The units of the value.
* @example
* Tone.Frequency("C3").eval() // 261
* Tone.Frequency(38, "midi").eval() //
* Tone.Frequency("C3").transpose(4).eval();
* Tone.Frequency("C3") // 261
* Tone.Frequency(38, "midi") //
* Tone.Frequency("C3").transpose(4);
*/
Tone.Frequency = function(val, units){
if (this instanceof Tone.Frequency){
Expand Down Expand Up @@ -131,7 +131,7 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* Tone.Frequency("C4").toMidi(); //60
*/
Tone.Frequency.prototype.toMidi = function(){
return this.frequencyToMidi(this.eval());
return this.frequencyToMidi(this.valueOf());
};

/**
Expand All @@ -141,7 +141,7 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* Tone.Frequency(69, "midi").toNote(); //"A4"
*/
Tone.Frequency.prototype.toNote = function(){
var freq = this.eval();
var freq = this.valueOf();
var log = Math.log(freq / Tone.Frequency.A4) / Math.LN2;
var noteNumber = Math.round(12 * log) + 57;
var octave = Math.floor(noteNumber/12);
Expand All @@ -157,15 +157,15 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* @return {Seconds}
*/
Tone.Frequency.prototype.toSeconds = function(){
return 1 / this.eval();
return 1 / this.valueOf();
};

/**
* Return the value in Hertz
* @return {Frequency}
*/
Tone.Frequency.prototype.toFrequency = function(){
return this.eval();
return this.valueOf();
};

/**
Expand All @@ -174,7 +174,7 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
*/
Tone.Frequency.prototype.toTicks = function(){
var quarterTime = this._beatsToUnits(1);
var quarters = this.eval() / quarterTime;
var quarters = this.valueOf() / quarterTime;
return Math.floor(quarters * Tone.Transport.PPQ);
};

Expand Down
10 changes: 5 additions & 5 deletions Tone/type/Time.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* a percentage.
* @return {Tone.Time} this
* @example
* Tone.Time(21).quantize(2).eval() //returns 22
* Tone.Time(0.6).quantize("4n", 0.5).eval() //returns 0.55
* Tone.Time(21).quantize(2) //returns 22
* Tone.Time(0.6).quantize("4n", 0.5) //returns 0.55
*/
Tone.Time.prototype.quantize = function(subdiv, percent){
percent = this.defaultArg(percent, 1);
Expand Down Expand Up @@ -230,7 +230,7 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
*/
Tone.Time.prototype.toTicks = function(){
var quarterTime = this._beatsToUnits(1);
var quarters = this.eval() / quarterTime;
var quarters = this.valueOf() / quarterTime;
return Math.floor(quarters * Tone.Transport.PPQ);
};

Expand All @@ -257,7 +257,7 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* @return {Seconds}
*/
Tone.Time.prototype.toSeconds = function(){
return this.eval();
return this.valueOf();
};

/**
Expand All @@ -272,7 +272,7 @@ define(["Tone/core/Tone", "Tone/type/TimeBase"], function (Tone) {
* Return the time in seconds.
* @return {Seconds}
*/
Tone.Time.prototype.eval = function(){
Tone.Time.prototype.valueOf = function(){
var val = this._expr();
return val + (this._plusNow?this.now():0);
};
Expand Down
2 changes: 1 addition & 1 deletion Tone/type/TimeBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ define(["Tone/core/Tone"], function (Tone) {
* in seconds.
* @return {Seconds}
*/
Tone.TimeBase.prototype.eval = function(){
Tone.TimeBase.prototype.valueOf = function(){
return this._expr();
};

Expand Down
4 changes: 2 additions & 2 deletions Tone/type/TransportTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ define(["Tone/core/Tone", "Tone/type/Time"], function (Tone) {
* Evaluate the time expression. Returns values in ticks
* @return {Ticks}
*/
Tone.TransportTime.prototype.eval = function(){
Tone.TransportTime.prototype.valueOf = function(){
var val = this._secondsToTicks(this._expr());
return val + (this._plusNow ? Tone.Transport.ticks : 0);
};
Expand All @@ -67,7 +67,7 @@ define(["Tone/core/Tone", "Tone/type/Time"], function (Tone) {
* @return {Ticks}
*/
Tone.TransportTime.prototype.toTicks = function(){
return this.eval();
return this.valueOf();
};

/**
Expand Down
2 changes: 1 addition & 1 deletion Tone/type/Type.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function (Tone) {
if (this.isNumber(freq)){
return freq;
} else if (this.isString(freq) || this.isUndef(freq)){
return (new Tone.Frequency(freq)).eval();
return (new Tone.Frequency(freq)).valueOf();
} else if (freq instanceof Tone.TimeBase){
return freq.toFrequency();
}
Expand Down
103 changes: 55 additions & 48 deletions test/type/Frequency.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,95 +29,102 @@ define(["helper/Basic", "Test", "Tone/type/Frequency", "Tone/core/Tone", "deps/t
});

it("can pass in a value and a type", function(){
expect(Frequency(4, "n").eval()).to.equal(2);
expect(Frequency(4, "n").valueOf()).to.equal(2);
});

it("with no arguments evaluates to 0", function(){
expect(Frequency().eval()).to.equal(0);
expect(Frequency().valueOf()).to.equal(0);
});

it("is evaluated in equations and comparisons using valueOf", function(){
expect(Frequency(1) + 1).to.equal(2);
expect(Frequency(1) + Frequency(1)).to.equal(2);
expect(Frequency(1) > Frequency(0)).to.be.true;
expect(+Frequency(1)).to.equal(1);
});
});

context("Eval Types", function(){

it("evaluates numbers as frequency", function(){
expect(Frequency("1").eval()).to.equal(1);
expect(Frequency("123").eval()).to.equal(123);
expect(Frequency(3.2).eval()).to.equal(3.2);
expect(Frequency("1").valueOf()).to.equal(1);
expect(Frequency("123").valueOf()).to.equal(123);
expect(Frequency(3.2).valueOf()).to.equal(3.2);
});

it("evaluates notation", function(){
return Offline(function(Transport){
Transport.bpm.value = 120;
Transport.timeSignature = 4;
expect(Frequency("4n").eval()).to.equal(2);
expect(Frequency("8n").eval()).to.equal(4);
expect(Frequency(16, "n").eval()).to.equal(8);
expect(Frequency("4n").valueOf()).to.equal(2);
expect(Frequency("8n").valueOf()).to.equal(4);
expect(Frequency(16, "n").valueOf()).to.equal(8);
Transport.bpm.value = 60;
Transport.timeSignature = [5,4];
expect(Frequency("1m").eval()).to.equal(1/5);
expect(Frequency("1m").valueOf()).to.equal(1/5);
Transport.bpm.value = 120;
Transport.timeSignature = 4;
});
});

it("evalutes hertz", function(){
expect(Frequency("1hz").eval()).to.equal(1);
expect(Frequency("2hz").eval()).to.equal(2);
expect(Frequency(4, "hz").eval()).to.equal(4);
expect(Frequency("0.25hz").eval()).to.equal(0.25);
expect(Frequency("1hz").valueOf()).to.equal(1);
expect(Frequency("2hz").valueOf()).to.equal(2);
expect(Frequency(4, "hz").valueOf()).to.equal(4);
expect(Frequency("0.25hz").valueOf()).to.equal(0.25);
});

it("evalutes ticks", function(){
return Offline(function(Transport){
expect(Frequency(Transport.PPQ, "i").eval()).to.equal(2);
expect(Frequency(1, "i").eval()).to.equal(Transport.PPQ * 2);
expect(Frequency(Transport.PPQ, "i").valueOf()).to.equal(2);
expect(Frequency(1, "i").valueOf()).to.equal(Transport.PPQ * 2);
});
});

it("evalutes transport time", function(){
expect(Frequency("1:0").eval()).to.equal(0.5);
expect(Frequency("1:4:0").eval()).to.equal(0.25);
// expect(Frequency("2:1:0").eval()).to.equal(0.25);
expect(Frequency("1:0").valueOf()).to.equal(0.5);
expect(Frequency("1:4:0").valueOf()).to.equal(0.25);
// expect(Frequency("2:1:0").valueOf()).to.equal(0.25);
});

it("evalutes midi", function(){
expect(Frequency(48, "midi").eval()).to.be.closeTo(teoria.Note.fromMIDI(48).fq(), 0.0001);
expect(Frequency(69, "midi").eval()).to.be.closeTo(teoria.Note.fromMIDI(69).fq(), 0.0001);
expect(Frequency(48, "midi").valueOf()).to.be.closeTo(teoria.Note.fromMIDI(48).fq(), 0.0001);
expect(Frequency(69, "midi").valueOf()).to.be.closeTo(teoria.Note.fromMIDI(69).fq(), 0.0001);
});

it("evalutes hz", function(){
expect(Frequency(48, "hz").eval()).to.equal(48);
expect(Frequency(480, "hz").eval()).to.equal(480);
expect(Frequency(48, "hz").valueOf()).to.equal(48);
expect(Frequency(480, "hz").valueOf()).to.equal(480);
});

it("can convert notes into frequencies", function(){
expect(Frequency("C4").eval()).to.be.closeTo(teoria.note("C4").fq(), 0.0001);
expect(Frequency("D4").eval()).to.be.closeTo(teoria.note("D4").fq(), 0.0001);
expect(Frequency("Db4").eval()).to.be.closeTo(teoria.note("Db4").fq(), 0.0001);
expect(Frequency("E4").eval()).to.be.closeTo(teoria.note("E4").fq(), 0.0001);
expect(Frequency("F2").eval()).to.be.closeTo(teoria.note("F2").fq(), 0.0001);
expect(Frequency("Gb-1").eval()).to.be.closeTo(teoria.note("Gb-1").fq(), 0.0001);
expect(Frequency("A#10").eval()).to.be.closeTo(teoria.note("A#10").fq(), 0.0001);
expect(Frequency("Bb2").eval()).to.be.closeTo(teoria.note("Bb2").fq(), 0.0001);
expect(Frequency("C4").valueOf()).to.be.closeTo(teoria.note("C4").fq(), 0.0001);
expect(Frequency("D4").valueOf()).to.be.closeTo(teoria.note("D4").fq(), 0.0001);
expect(Frequency("Db4").valueOf()).to.be.closeTo(teoria.note("Db4").fq(), 0.0001);
expect(Frequency("E4").valueOf()).to.be.closeTo(teoria.note("E4").fq(), 0.0001);
expect(Frequency("F2").valueOf()).to.be.closeTo(teoria.note("F2").fq(), 0.0001);
expect(Frequency("Gb-1").valueOf()).to.be.closeTo(teoria.note("Gb-1").fq(), 0.0001);
expect(Frequency("A#10").valueOf()).to.be.closeTo(teoria.note("A#10").fq(), 0.0001);
expect(Frequency("Bb2").valueOf()).to.be.closeTo(teoria.note("Bb2").fq(), 0.0001);
});

it("handles double accidentals", function(){
expect(Frequency("Cbb4").eval()).to.be.closeTo(teoria.note("Cbb4").fq(), 0.0001);
expect(Frequency("Dx4").eval()).to.be.closeTo(teoria.note("Dx4").fq(), 0.0001);
expect(Frequency("Dbb4").eval()).to.be.closeTo(teoria.note("Dbb4").fq(), 0.0001);
expect(Frequency("Ex4").eval()).to.be.closeTo(teoria.note("Ex4").fq(), 0.0001);
expect(Frequency("Fx2").eval()).to.be.closeTo(teoria.note("Fx2").fq(), 0.0001);
expect(Frequency("Gbb-1").eval()).to.be.closeTo(teoria.note("Gbb-1").fq(), 0.0001);
expect(Frequency("Ax10").eval()).to.be.closeTo(teoria.note("Ax10").fq(), 0.0001);
expect(Frequency("Bbb2").eval()).to.be.closeTo(teoria.note("Bbb2").fq(), 0.0001);
expect(Frequency("Cbb4").valueOf()).to.be.closeTo(teoria.note("Cbb4").fq(), 0.0001);
expect(Frequency("Dx4").valueOf()).to.be.closeTo(teoria.note("Dx4").fq(), 0.0001);
expect(Frequency("Dbb4").valueOf()).to.be.closeTo(teoria.note("Dbb4").fq(), 0.0001);
expect(Frequency("Ex4").valueOf()).to.be.closeTo(teoria.note("Ex4").fq(), 0.0001);
expect(Frequency("Fx2").valueOf()).to.be.closeTo(teoria.note("Fx2").fq(), 0.0001);
expect(Frequency("Gbb-1").valueOf()).to.be.closeTo(teoria.note("Gbb-1").fq(), 0.0001);
expect(Frequency("Ax10").valueOf()).to.be.closeTo(teoria.note("Ax10").fq(), 0.0001);
expect(Frequency("Bbb2").valueOf()).to.be.closeTo(teoria.note("Bbb2").fq(), 0.0001);
});

it("can accomidate different concert tuning", function(){
Frequency.A4 = 444;
expect(Frequency("C4").eval()).to.be.closeTo(teoria.note("C4").fq(Frequency.A4), 0.0001);
expect(Frequency("D1").eval()).to.be.closeTo(teoria.note("D1").fq(Frequency.A4), 0.0001);
expect(Frequency("C4").valueOf()).to.be.closeTo(teoria.note("C4").fq(Frequency.A4), 0.0001);
expect(Frequency("D1").valueOf()).to.be.closeTo(teoria.note("D1").fq(Frequency.A4), 0.0001);
Frequency.A4 = 100;
expect(Frequency("C4").eval()).to.be.closeTo(teoria.note("C4").fq(Frequency.A4), 0.0001);
expect(Frequency("C4").valueOf()).to.be.closeTo(teoria.note("C4").fq(Frequency.A4), 0.0001);
//return it to normal
Frequency.A4 = 440;
});
Expand All @@ -128,9 +135,9 @@ define(["helper/Basic", "Test", "Tone/type/Frequency", "Tone/core/Tone", "deps/t

it ("can evaluate expressions", function(){
var a4 = teoria.note("A4").fq();
expect(Frequency("A4 * 2").eval()).to.be.closeTo(a4 * 2, 0.0001);
expect(Frequency("A4 + 2 * 2").eval()).to.be.closeTo(a4 + 4, 0.0001);
expect(Frequency("A4/3").eval()).to.be.closeTo(a4/3, 0.0001);
expect(Frequency("A4 * 2").valueOf()).to.be.closeTo(a4 * 2, 0.0001);
expect(Frequency("A4 + 2 * 2").valueOf()).to.be.closeTo(a4 + 4, 0.0001);
expect(Frequency("A4/3").valueOf()).to.be.closeTo(a4/3, 0.0001);
});

});
Expand Down Expand Up @@ -159,13 +166,13 @@ define(["helper/Basic", "Test", "Tone/type/Frequency", "Tone/core/Tone", "deps/t
context("Operators", function(){

it("can combine operations", function(){
expect(Frequency(4).mult(2).add(3).eval()).to.equal(11);
expect(Frequency(8).sub(2).div(2).mult(8).eval()).to.equal(24);
expect(Frequency(4).mult(2).add(3).valueOf()).to.equal(11);
expect(Frequency(8).sub(2).div(2).mult(8).valueOf()).to.equal(24);
});

it("can combine operations", function(){
expect(Frequency(4).mult(2).add(3).eval()).to.equal(11);
expect(Frequency(8).sub(2).div(2).mult(8).eval()).to.equal(24);
expect(Frequency(4).mult(2).add(3).valueOf()).to.equal(11);
expect(Frequency(8).sub(2).div(2).mult(8).valueOf()).to.equal(24);
});

});
Expand Down
Loading

2 comments on commit d037c28

@JackCA
Copy link
Contributor

@JackCA JackCA commented on d037c28 Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 !

@tambien
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion @JackCA! i feel like this will make working with the time classes much more elegant

Please sign in to comment.