diff --git a/test/test.js b/test/test.js index 2d7bb4b2..5dcb291a 100644 --- a/test/test.js +++ b/test/test.js @@ -12,7 +12,7 @@ require.config({ } }); -var allTests = ["tests/Core", "tests/Timing", "tests/Signal", "tests/Math", "tests/Transport", "tests/Sources", "tests/Components"]; +var allTests = ["tests/Core", "tests/Timing", "tests/Signal", "tests/SignalComparison", "tests/SignalMath", "tests/Math", "tests/Transport", "tests/Sources", "tests/Components"]; require(allTests, function(){ mocha.run(); diff --git a/test/tests/Signal.js b/test/tests/Signal.js index 7559665a..5dab56a1 100644 --- a/test/tests/Signal.js +++ b/test/tests/Signal.js @@ -1,7 +1,6 @@ -define(["tests/Core", "chai", "Tone/component/Recorder", "Tone/signal/Signal", "Tone/signal/Add", "Tone/signal/Multiply", - "Tone/signal/Scale", "Tone/source/Oscillator", "Tone/signal/Merge", "Tone/signal/Split","Tone/core/Master", "Tone/signal/EqualsZero", - "Tone/signal/Threshold", "Tone/signal/Switch"], -function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Merge, Split, Master, EqualsZero, Threshold, Switch){ +define(["tests/Core", "chai", "Tone/component/Recorder", "Tone/signal/Signal", "Tone/source/Oscillator", + "Tone/signal/Merge", "Tone/signal/Split","Tone/core/Master", "Tone/signal/Threshold", "Tone/signal/Gate"], +function(core, chai, Recorder, Signal, Oscillator, Merge, Split, Master, Threshold, Gate){ var expect = chai.expect; @@ -105,108 +104,6 @@ function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Merge, }); }); - //ADD - describe("Tone.Add", function(){ - this.timeout(500); - - var recorder = new Recorder(); - - after(function(){ - recorder.dispose(); - }); - - it("can be created and disposed", function(){ - var a = new Add(1); - a.dispose(); - }); - - it("correctly sums a signal and a number", function(done){ - var signal = new Signal(0); - var adder = new Add(3); - signal.connect(adder); - adder.connect(recorder); - recorder.record(0.1, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.equal(3); - } - done(); - }); - }); - - it("can handle negative values", function(done){ - var signal = new Signal(10); - var adder = new Add(-1); - signal.connect(adder); - var recorder = new Recorder(); - adder.connect(recorder); - recorder.record(0.1, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.equal(9); - } - done(); - }); - }); - }); - - //MULTIPLY - describe("Tone.Multiply", function(){ - this.timeout(500); - - it("can be created and disposed", function(){ - var m = new Multiply(1); - m.dispose(); - }); - - it("correctly multiplys a signal and a scalar", function(done){ - var signal = new Signal(2); - var mult = new Multiply(10); - signal.connect(mult); - var recorder = new Recorder(); - mult.connect(recorder); - recorder.record(0.05, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.equal(20); - } - signal.dispose(); - mult.dispose(); - done(); - }); - }); - }); - - //SCALE - describe("Tone.Scale", function(){ - this.timeout(500); - - it("can be created and disposed", function(){ - var s = new Scale(0, 10); - s.dispose(); - }); - - it("scales an input range to an output range", function(done){ - //make an oscillator to drive the signal - var osc = new Oscillator(1000); - osc.start(); - var scale = new Scale(-1, 1, 10, 20); - osc.connect(scale); - var recorder = new Recorder(); - scale.connect(recorder); - recorder.record(0.05, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.be.within(10, 20); - } - done(); - }); - }); - }); //MERGE describe("Tone.Merge", function(){ @@ -277,70 +174,6 @@ function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Merge, }); }); - //EQUALS 0 - describe("Tone.EqualsZero", function(){ - this.timeout(500); - - it("can be created and disposed", function(){ - var ez = new EqualsZero(); - ez.dispose(); - }); - - it("outputs 1 when the incoming signal is 0", function(done){ - var signal = new Signal(0); - var ez = new EqualsZero(); - signal.connect(ez); - var recorder = new Recorder(); - ez.connect(recorder); - recorder.record(0.05, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.equal(1); - } - signal.dispose(); - ez.dispose(); - done(); - }); - }); - - it("outputs 0 when the incoming signal is not 0", function(done){ - var signal = new Signal(100); - var ez = new EqualsZero(); - signal.connect(ez); - var recorder = new Recorder(); - ez.connect(recorder); - recorder.record(0.05, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.equal(0); - } - signal.dispose(); - ez.dispose(); - done(); - }); - }); - - it("is not fooled by values very close to 0", function(done){ - var signal = new Signal(0.00001); - var ez = new EqualsZero(); - signal.connect(ez); - var recorder = new Recorder(); - ez.connect(recorder); - recorder.record(0.05, 0.05, function(buffers){ - var buffer = buffers[0]; - //get the left buffer and check that all values are === 1 - for (var i = 0; i < buffer.length; i++){ - expect(buffer[i]).to.equal(0); - } - signal.dispose(); - ez.dispose(); - done(); - }); - }); - - }); //THRESHOLD describe("Tone.Threshold", function(){ @@ -407,18 +240,18 @@ function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Merge, }); - //Switch - describe("Tone.Switch", function(){ + //Gate + describe("Tone.Gate", function(){ this.timeout(500); it("can be created and disposed", function(){ - var sw = new Switch(); + var sw = new Gate(); sw.dispose(); }); it("can stop a signal from passing through", function(done){ var signal = new Signal(10); - var gate = new Switch(); + var gate = new Gate(); signal.connect(gate); var recorder = new Recorder(); gate.connect(recorder); @@ -436,7 +269,7 @@ function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Merge, it("can allow a signal to pass through", function(done){ var signal = new Signal(10); - var gate = new Switch(); + var gate = new Gate(); signal.connect(gate); gate.open(); var recorder = new Recorder(); diff --git a/test/tests/SignalComparison.js b/test/tests/SignalComparison.js new file mode 100644 index 00000000..c48fcedb --- /dev/null +++ b/test/tests/SignalComparison.js @@ -0,0 +1,253 @@ +define(["tests/Core", "chai", "Tone/component/Recorder", "Tone/signal/Signal", "Tone/core/Master", "Tone/signal/EqualZero", + "Tone/signal/Equal", "Tone/signal/GreaterThan", "Tone/signal/LessThan"], +function(core, chai, Recorder, Signal, Master, EqualZero, Equal, GreaterThan, LessThan){ + + var expect = chai.expect; + + Master.mute(); + + + //EQUALS 0 + describe("Tone.EqualZero", function(){ + this.timeout(500); + + it("can be created and disposed", function(){ + var ez = new EqualZero(); + ez.dispose(); + }); + + it("outputs 1 when the incoming signal is 0", function(done){ + var signal = new Signal(0); + var ez = new EqualZero(); + signal.connect(ez); + var recorder = new Recorder(); + ez.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(1); + } + signal.dispose(); + ez.dispose(); + done(); + }); + }); + + it("outputs 0 when the incoming signal is not 0", function(done){ + var signal = new Signal(100); + var ez = new EqualZero(); + signal.connect(ez); + var recorder = new Recorder(); + ez.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(0); + } + signal.dispose(); + ez.dispose(); + done(); + }); + }); + + it("is not fooled by values very close to 0", function(done){ + var signal = new Signal(0.00001); + var ez = new EqualZero(); + signal.connect(ez); + var recorder = new Recorder(); + ez.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(0); + } + signal.dispose(); + ez.dispose(); + done(); + }); + }); + + }); + + //EQUALS + describe("Tone.Equal", function(){ + this.timeout(500); + + it("can be created and disposed", function(){ + var eq = new Equal(3); + eq.dispose(); + }); + + it("outputs 1 when the incoming signal is equal to the value", function(done){ + var signal = new Signal(3); + var eq = new Equal(3); + signal.connect(eq); + var recorder = new Recorder(); + eq.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(1); + } + signal.dispose(); + eq.dispose(); + done(); + }); + }); + + it("outputs 0 when the incoming signal is not equal", function(done){ + var signal = new Signal(100); + var eq = new EqualZero(200); + signal.connect(eq); + var recorder = new Recorder(); + eq.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(0); + } + signal.dispose(); + eq.dispose(); + done(); + }); + }); + + }); + + //GREATER THAN + describe("Tone.GreaterThan", function(){ + this.timeout(500); + + it("can be created and disposed", function(){ + var gt = new GreaterThan(); + gt.dispose(); + }); + + it("outputs 1 when the incoming signal is greater than the value", function(done){ + var signal = new Signal(3); + var gt = new GreaterThan(2); + signal.connect(gt); + var recorder = new Recorder(); + gt.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(1); + } + signal.dispose(); + gt.dispose(); + done(); + }); + }); + + it("outputs 0 when the incoming signal less than the value", function(done){ + var signal = new Signal(-101); + var gt = new GreaterThan(-100); + signal.connect(gt); + var recorder = new Recorder(); + gt.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(0); + } + signal.dispose(); + gt.dispose(); + done(); + }); + }); + + it("set be set to a new value", function(done){ + var signal = new Signal(100); + var gt = new GreaterThan(200); + signal.connect(gt); + gt.setValue(50); + var recorder = new Recorder(); + gt.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(1); + } + signal.dispose(); + gt.dispose(); + done(); + }); + }); + }); + + //LESS THAN + describe("Tone.LessThan", function(){ + this.timeout(500); + + it("can be created and disposed", function(){ + var lt = new LessThan(2); + lt.dispose(); + }); + + it("outputs 1 when the incoming signal is less than the value", function(done){ + var signal = new Signal(0); + var lt = new LessThan(2); + signal.connect(lt); + var recorder = new Recorder(); + lt.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.lenlth; i++){ + expect(buffer[i]).to.equal(1); + } + signal.dispose(); + lt.dispose(); + done(); + }); + }); + + it("outputs 0 when the incoming signal greater than the value", function(done){ + var signal = new Signal(1.01); + var lt = new LessThan(1); + signal.connect(lt); + var recorder = new Recorder(); + lt.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.lenlth; i++){ + expect(buffer[i]).to.equal(0); + } + signal.dispose(); + lt.dispose(); + done(); + }); + }); + + it("outputs 0 when the incoming signal is equal the value", function(done){ + var signal = new Signal(-20); + var lt = new LessThan(-20); + signal.connect(lt); + var recorder = new Recorder(); + lt.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.lenlth; i++){ + expect(buffer[i]).to.equal(0); + } + signal.dispose(); + lt.dispose(); + done(); + }); + }); + + + }); + +}); \ No newline at end of file diff --git a/test/tests/SignalMath.js b/test/tests/SignalMath.js new file mode 100644 index 00000000..2b714ff0 --- /dev/null +++ b/test/tests/SignalMath.js @@ -0,0 +1,313 @@ +define(["tests/Core", "chai", "Tone/component/Recorder", "Tone/signal/Signal", "Tone/signal/Add", "Tone/signal/Multiply", + "Tone/signal/Scale", "Tone/source/Oscillator", "Tone/core/Master", "Tone/signal/Abs", "Tone/signal/Negate"], +function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Negate){ + + var expect = chai.expect; + + Master.mute(); + + //SIGNAL + describe("Tone.Signal", function(){ + this.timeout(1000); + + var signal = new Signal(0); + signal.toMaster(); + + after(function(){ + signal.dispose(); + }); + + it("can be created and disposed", function(){ + var s = new Signal(); + s.dispose(); + }); + + it("can start with a value initially", function(){ + expect(signal.getValue()).to.equal(0); + }); + + it("can set a value", function(){ + signal.setValue(10); + expect(signal.getValue()).to.equal(10); + }); + + it("can set a value in the future", function(done){ + signal.setValueAtTime(100, "+0.1"); + expect(signal.getValue()).to.equal(10); + var interval = setInterval(function(){ + if (signal.getValue() === 100){ + clearInterval(interval); + done(); + } + }, 10); + }); + + it("can change value with sample accurate timing", function(done){ + var changeSignal = new Signal(0); + var waitTime = 0.03; + changeSignal.setValueAtTime(1, "+"+waitTime);//ramp after 50ms + var recorder = new Recorder(); + changeSignal.connect(recorder); + var delayTime = 0.05; + recorder.record(0.1, delayTime, function(buffers){ + buffer = buffers[0]; + for (var i = 0; i < buffer.length; i++){ + if (buffer[i] === 1){ + expect(changeSignal.samplesToSeconds(i)).is.closeTo(delayTime - waitTime, 0.01); + changeSignal.dispose(); + recorder.dispose(); + done(); + break; + } + } + }); + }); + + it("can sync to another signal", function(done){ + var syncTo = new Signal(1); + var signalSync = new Signal(2); + signalSync.sync(syncTo); + syncTo.setValue(2); + syncTo.noGC(); + signalSync.noGC(); + var recorder = new Recorder(); + signalSync.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(4); + } + signalSync.dispose(); + recorder.dispose(); + done(); + }); + }); + + it("can ramp from the current value", function(done){ + signal.setValue(-10); + signal.noGC(); + var recorder = new Recorder(1); + signal.connect(recorder); + var waitTime = 0.03; + expect(signal.getValue()).to.equal(-10); + signal.linearRampToValueNow(1, waitTime); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + for (var i = 0; i < buffer.length; i++){ + if (buffer[i] === 1){ + expect(signal.samplesToSeconds(i)).is.closeTo(waitTime, 0.01); + done(); + break; + } + } + }); + }); + }); + + //ADD + describe("Tone.Add", function(){ + this.timeout(500); + + var recorder = new Recorder(); + + after(function(){ + recorder.dispose(); + }); + + it("can be created and disposed", function(){ + var a = new Add(1); + a.dispose(); + }); + + it("correctly sums a signal and a number", function(done){ + var signal = new Signal(0); + var adder = new Add(3); + signal.connect(adder); + adder.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(3); + } + done(); + }); + }); + + it("can handle negative values", function(done){ + var signal = new Signal(10); + var adder = new Add(-1); + signal.connect(adder); + var recorder = new Recorder(); + adder.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(9); + } + done(); + }); + }); + }); + + //MULTIPLY + describe("Tone.Multiply", function(){ + this.timeout(500); + + it("can be created and disposed", function(){ + var m = new Multiply(1); + m.dispose(); + }); + + it("correctly multiplys a signal and a scalar", function(done){ + var signal = new Signal(2); + var mult = new Multiply(10); + signal.connect(mult); + var recorder = new Recorder(); + mult.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(20); + } + signal.dispose(); + mult.dispose(); + done(); + }); + }); + }); + + //SCALE + describe("Tone.Scale", function(){ + this.timeout(500); + + it("can be created and disposed", function(){ + var s = new Scale(0, 10); + s.dispose(); + }); + + it("scales an input range to an output range", function(done){ + //make an oscillator to drive the signal + var osc = new Oscillator(1000); + osc.start(); + var scale = new Scale(-1, 1, 10, 20); + osc.connect(scale); + var recorder = new Recorder(); + scale.connect(recorder); + recorder.record(0.05, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.be.within(10, 20); + } + done(); + }); + }); + }); + + //ADD + describe("Tone.Abs", function(){ + this.timeout(500); + + var recorder = new Recorder(); + + after(function(){ + recorder.dispose(); + }); + + it("can be created and disposed", function(){ + var ab = new Abs(); + ab.dispose(); + }); + + it("outputs the same value for positive values", function(done){ + var signal = new Signal(1); + var abs = new Abs(); + signal.connect(abs); + abs.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(1); + } + signal.dispose(); + abs.dispose(); + done(); + }); + }); + + it("outputs the absolute value for negative numbers", function(done){ + var signal = new Signal(-10); + var abs = new Abs(); + signal.connect(abs); + var recorder = new Recorder(); + abs.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(10); + } + abs.dispose(); + signal.dispose(); + done(); + }); + }); + }); + + //NEGATE + describe("Tone.Negate", function(){ + this.timeout(500); + + var recorder = new Recorder(); + + after(function(){ + recorder.dispose(); + }); + + it("can be created and disposed", function(){ + var neg = new Negate(); + neg.dispose(); + }); + + it("negates a positive value", function(done){ + var signal = new Signal(1); + var neg = new Negate(); + signal.connect(neg); + neg.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(-1); + } + signal.dispose(); + neg.dispose(); + done(); + }); + }); + + it("makes a negative value positive", function(done){ + var signal = new Signal(-10); + var neg = new Negate(); + signal.connect(neg); + var recorder = new Recorder(); + neg.connect(recorder); + recorder.record(0.1, 0.05, function(buffers){ + var buffer = buffers[0]; + //get the left buffer and check that all values are === 1 + for (var i = 0; i < buffer.length; i++){ + expect(buffer[i]).to.equal(10); + } + neg.dispose(); + signal.dispose(); + done(); + }); + }); + }); + + +}); \ No newline at end of file