Tone.js/test/tests/Signal.js

324 lines
7.7 KiB
JavaScript
Raw Normal View History

/* global it, describe, maxTimeout */
2014-07-30 19:10:45 +00:00
define(["tests/Core", "chai", "Tone/signal/Signal", "Tone/source/Oscillator",
"Tone/signal/Threshold", "Tone/signal/Switch", "Tone/signal/Route", "Tone/signal/Select", "tests/Common"],
function(core, chai, Signal, Oscillator, Threshold, Switch, Route, Select, Test){
2014-06-18 05:37:01 +00:00
var expect = chai.expect;
describe("Tone.Signal", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var s = new Signal();
s.dispose();
Test.wasDisposed(s, expect);
2014-06-20 04:38:14 +00:00
});
it("can start with a value initially", function(){
2014-07-04 03:48:27 +00:00
var signal = new Signal(0);
2014-06-21 17:05:05 +00:00
expect(signal.getValue()).to.equal(0);
2014-07-04 03:48:27 +00:00
signal.dispose();
});
it("can set a value", function(){
2014-07-04 03:48:27 +00:00
var signal = new Signal(0);
signal.setValue(10);
expect(signal.getValue()).to.equal(10);
2014-07-04 03:48:27 +00:00
signal.dispose();
});
it("can set a value in the future with sample accurate timing", function(done){
var sig;
Test.offlineTest(0.1, function(dest){
sig = new Signal(10);
sig.setValueAtTime(100, "+0.1");
expect(sig.getValue()).to.equal(10);
sig.connect(dest);
}, function(sample, time){
if (sample === 100){
expect(time).is.closeTo(0.1, 0.001);
2014-06-18 19:42:17 +00:00
}
}, function(){
2014-07-04 03:48:27 +00:00
sig.dispose();
done();
});
2014-06-18 05:37:01 +00:00
});
2014-06-21 17:05:05 +00:00
2014-06-18 05:37:01 +00:00
it("can sync to another signal", function(done){
var syncTo, signalSync;
Test.offlineTest(0.1, function(dest){
syncTo = new Signal(1);
signalSync = new Signal(2);
signalSync.sync(syncTo);
syncTo.setValue(2);
signalSync.connect(dest);
}, function(sample){
expect(sample).to.equal(4);
}, function(){
syncTo.dispose();
2014-06-21 17:05:05 +00:00
signalSync.dispose();
2014-06-18 19:42:17 +00:00
done();
2014-06-21 17:05:05 +00:00
});
});
it("can ramp from the current value", function(done){
var sig;
Test.offlineTest(0.1, function(dest){
sig = new Signal(0);
sig.setValue(-10);
sig.linearRampToValueNow(1, "+0.1");
expect(sig.getValue()).to.equal(-10);
sig.connect(dest);
}, function(sample, time){
if (sample === 1){
expect(time).is.closeTo(0.1, 0.001);
}
}, function(){
sig.dispose();
done();
2014-06-21 17:05:05 +00:00
});
});
2014-09-10 17:51:15 +00:00
it("can ramp exponentially from the current value now", function(){
var sig = new Signal(1);
2014-09-11 15:48:18 +00:00
sig.exponentialRampToValueNow(10, 0.5);
2014-09-10 17:51:15 +00:00
sig.dispose();
});
it("can ramp exponentially from the current value in the future", function(){
var sig = new Signal(1);
sig.exponentialRampToValueAtTime(10, 0.5);
sig.dispose();
});
2014-06-18 05:37:01 +00:00
});
2014-07-02 21:09:13 +00:00
describe("Tone.Threshold", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-02 21:09:13 +00:00
it("can be created and disposed", function(){
var thresh = new Threshold();
thresh.dispose();
Test.wasDisposed(thresh, expect);
2014-07-02 21:09:13 +00:00
});
it("thresholds an incoming signal to 0 when it is below the thresh", function(done){
var signal, thresh;
Test.offlineTest(0.2, function(dest){
signal = new Signal(0.1);
thresh = new Threshold(0.5);
signal.connect(thresh);
thresh.connect(dest);
}, function(sample){
expect(sample).to.equal(0);
}, function(){
2014-07-02 21:09:13 +00:00
signal.dispose();
thresh.dispose();
done();
});
});
it("thresholds an incoming signal to 1 when it is above the thresh", function(done){
var signal, thresh;
Test.offlineTest(0.2, function(dest){
signal = new Signal(0.8);
thresh = new Threshold(0.75);
signal.connect(thresh);
thresh.connect(dest);
}, function(sample){
expect(sample).to.equal(1);
}, function(){
2014-07-02 21:09:13 +00:00
signal.dispose();
thresh.dispose();
done();
});
});
it("outputs 1 when the values are the same", function(done){
var signal, thresh;
Test.offlineTest(0.2, function(dest){
signal = new Signal(0.8);
thresh = new Threshold(0.8);
signal.connect(thresh);
thresh.connect(dest);
}, function(sample){
expect(sample).to.equal(1);
}, function(){
2014-07-02 21:09:13 +00:00
signal.dispose();
thresh.dispose();
done();
});
});
});
describe("Tone.Switch", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-02 21:09:13 +00:00
it("can be created and disposed", function(){
var sw = new Switch();
2014-07-02 21:09:13 +00:00
sw.dispose();
Test.wasDisposed(sw, expect);
2014-07-02 19:46:58 +00:00
});
2014-07-02 21:09:13 +00:00
it("can stop a signal from passing through", function(done){
var signal, gate;
Test.offlineTest(0.2, function(dest){
signal = new Signal(10);
gate = new Switch();
signal.connect(gate);
gate.connect(dest);
}, function(sample){
expect(sample).to.equal(0);
}, function(){
2014-07-02 21:09:13 +00:00
signal.dispose();
gate.dispose();
done();
});
});
it("can allow a signal to pass through", function(done){
var signal, gate;
Test.offlineTest(0.2, function(dest){
signal = new Signal(10);
gate = new Switch();
signal.connect(gate);
gate.open();
gate.connect(dest);
}, function(sample){
expect(sample).to.equal(10);
}, function(){
2014-07-02 21:09:13 +00:00
signal.dispose();
gate.dispose();
done();
});
});
2014-07-02 19:46:58 +00:00
});
2014-07-30 19:10:45 +00:00
describe("Tone.Route", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-30 19:10:45 +00:00
it("can be created and disposed", function(){
var r = new Route();
r.dispose();
Test.wasDisposed(r, expect);
2014-07-30 19:10:45 +00:00
});
it("can route a signal to first output", function(done){
var signal, route;
Test.offlineTest(0.2, function(dest){
signal = new Signal(10);
route = new Route();
signal.connect(route);
route.select(0);
route.connect(dest, 0, 0);
}, function(sample){
expect(sample).to.equal(10);
}, function(){
2014-07-30 19:10:45 +00:00
signal.dispose();
route.dispose();
done();
});
2014-08-27 19:20:52 +00:00
});
it("can route a signal to first output and not the second one", function(done){
var signal, route;
Test.offlineTest(0.2, function(dest){
signal = new Signal(10);
route = new Route();
signal.connect(route);
route.select(0);
route.connect(dest, 1, 0);
}, function(sample){
expect(sample).to.equal(0);
}, function(){
2014-08-27 19:20:52 +00:00
signal.dispose();
route.dispose();
2014-07-30 19:10:45 +00:00
done();
});
});
2014-08-27 19:20:52 +00:00
it("can route a signal to second output", function(done){
var signal, route;
Test.offlineTest(0.2, function(dest){
signal = new Signal(20);
route = new Route();
signal.connect(route);
route.select(1);
route.connect(dest, 1, 0);
}, function(sample){
expect(sample).to.equal(20);
}, function(){
2014-07-30 19:10:45 +00:00
signal.dispose();
route.dispose();
done();
});
2014-08-27 19:20:52 +00:00
});
it("can route a signal to second output and not the first one", function(done){
var signal, route;
Test.offlineTest(0.2, function(dest){
signal = new Signal(20);
route = new Route();
signal.connect(route);
route.select(1);
route.connect(dest, 0, 0);
}, function(sample){
expect(sample).to.equal(0);
}, function(){
2014-08-27 19:20:52 +00:00
signal.dispose();
route.dispose();
2014-07-30 19:10:45 +00:00
done();
});
});
});
2014-09-08 01:42:31 +00:00
describe("Tone.Select", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-30 19:10:45 +00:00
it("can be created and disposed", function(){
2014-09-08 01:42:31 +00:00
var s = new Select();
2014-07-30 19:10:45 +00:00
s.dispose();
Test.wasDisposed(s, expect);
2014-07-30 19:10:45 +00:00
});
it("can select the first signal", function(done){
var signal0, signal1, select;
Test.offlineTest(0.2, function(dest){
signal0 = new Signal(10);
signal1 = new Signal(20);
select = new Select();
signal0.connect(select, 0, 0);
signal1.connect(select, 0, 1);
select.select(0);
select.connect(dest);
}, function(sample){
expect(sample).to.equal(10);
}, function(){
2014-07-30 19:10:45 +00:00
signal0.dispose();
signal1.dispose();
select.dispose();
2014-07-30 19:10:45 +00:00
done();
});
});
it("can select the second signal", function(done){
var signal0, signal1, select;
Test.offlineTest(0.2, function(dest){
signal0 = new Signal(11);
signal1 = new Signal(21);
select = new Select();
signal0.connect(select, 0, 0);
signal1.connect(select, 0, 1);
select.select(1);
select.connect(dest);
}, function(sample){
expect(sample).to.equal(21);
}, function(){
2014-07-30 19:10:45 +00:00
signal0.dispose();
signal1.dispose();
select.dispose();
2014-07-30 19:10:45 +00:00
done();
});
});
});
2014-06-18 05:37:01 +00:00
});