Tone.js/test/core/Clock.js

396 lines
10 KiB
JavaScript
Raw Normal View History

2016-03-03 06:37:44 +00:00
define(["Test", "Tone/core/Clock", "helper/Offline2"], function (Test, Clock, Offline) {
2015-08-18 20:32:59 +00:00
describe("Clock", function(){
it ("can be created and disposed", function(){
var clock = new Clock();
clock.dispose();
Test.wasDisposed(clock);
});
context("Get/Set values", function(){
it ("can get and set the frequency", function(){
var clock = new Clock(function(){}, 2);
expect(clock.frequency.value).to.equal(2);
clock.frequency.value = 0.2;
expect(clock.frequency.value).to.be.closeTo(0.2, 0.001);
clock.dispose();
});
it ("invokes the callback when started", function(done){
var clock = new Clock(function(){
clock.dispose();
done();
}, 10).start();
2015-08-18 20:32:59 +00:00
});
2015-08-18 20:32:59 +00:00
it ("can be constructed with an options object", function(done){
var clock = new Clock({
"callback" : function(){
clock.dispose();
done();
},
"frequency" : 8
}).start();
expect(clock.frequency.value).to.equal(8);
});
it ("can get and set it's values with the set/get", function(){
var clock = new Clock();
clock.set({
"frequency" : 2
2015-08-18 20:32:59 +00:00
});
var gotValues = clock.get();
expect(gotValues.frequency).to.equal(2);
clock.dispose();
});
it ("can set the lookAhead", function(){
var oldLookAhead = Clock.lookAhead;
Clock.lookAhead = 0.05;
expect(Clock.lookAhead).to.equal(0.05);
Clock.lookAhead = oldLookAhead;
});
it ("can set the updateInterval", function(){
var oldUpdateInterval = Clock.updateInterval;
Clock.updateInterval = 0.05;
expect(Clock.updateInterval).to.equal(0.05);
Clock.updateInterval = oldUpdateInterval;
});
it ("can set the latencyHint", function(){
var oldLatencyHint = Clock.latencyHint;
Clock.latencyHint = "fastest";
expect(Clock.latencyHint).to.equal("fastest");
expect(Clock.lookAhead).to.be.closeTo(0.01, 0.05);
expect(Clock.updateInterval).to.be.closeTo(0.01, 0.05);
Clock.latencyHint = oldLatencyHint;
});
2015-08-18 20:32:59 +00:00
});
context("State", function(){
it ("correctly returns the scheduled play state", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var clock = new Clock();
2015-08-18 20:32:59 +00:00
expect(clock.state).to.equal("stopped");
clock.start(0).stop(0.5);
2016-03-03 06:37:44 +00:00
expect(clock.state).to.equal("started");
tearDown(function(){
expect(clock.state).to.equal("stopped");
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
it("can start, pause, and stop", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var clock = new Clock();
2015-08-18 20:32:59 +00:00
expect(clock.state).to.equal("stopped");
clock.start(0).pause(0.2).stop(0.4);
2016-03-03 06:37:44 +00:00
expect(clock.state).to.equal("started");
testFn(function(sample, time){
if (time >= 0.2 && time < 0.4){
expect(clock.state).to.equal("paused");
} else if (time >= 0.4){
expect(clock.state).to.equal("stopped");
}
});
tearDown(function(){
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
it("can start and stop in close proximity", function(done){
Offline(function(output, testFn, tearDown){
var clock = new Clock();
expect(clock.state).to.equal("stopped");
clock.start(0).stop(0.001).start(0.002);
expect(clock.state).to.equal("started");
testFn(function(sample, time){
if (time < 0.001){
expect(clock.state).to.equal("started");
} else if (time >= 0.002){
expect(clock.state).to.equal("started");
} else {
expect(clock.state).to.equal("stopped");
}
});
tearDown(function(){
clock.dispose();
done();
});
}, 0.05);
});
2015-08-18 20:32:59 +00:00
it("can schedule multiple start and stops", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var clock = new Clock();
2015-08-18 20:32:59 +00:00
expect(clock.state).to.equal("stopped");
clock.start(0).pause(0.2).stop(0.4).start(0.6).stop(0.8);
2015-08-18 20:32:59 +00:00
expect(clock.state).to.equal("started");
2016-03-03 06:37:44 +00:00
testFn(function(sample, time){
if (time >= 0.2 && time < 0.4){
expect(clock.state).to.equal("paused");
} else if (time >= 0.4 && time < 0.6){
expect(clock.state).to.equal("stopped");
} else if (time >= 0.6 && time < 0.8){
expect(clock.state).to.equal("started");
} else if (time >= 0.8){
expect(clock.state).to.equal("stopped");
}
});
tearDown(function(){
clock.dispose();
done();
});
}, 0.9);
2015-08-18 20:32:59 +00:00
});
});
context("Scheduling", function(){
it ("passes a time to the callback", function(done){
var clock = new Clock(function(time){
expect(time).to.be.a.number;
clock.dispose();
done();
}, 10).start();
2015-08-18 20:32:59 +00:00
});
it ("invokes the callback with a time great than now", function(done){
var clock = new Clock(function(time){
clock.dispose();
expect(time).to.be.greaterThan(now);
done();
}, 10);
2015-08-18 20:32:59 +00:00
var now = clock.now();
var startTime = now + 0.1;
clock.start(startTime);
});
it ("invokes the first callback at the given start time", function(done){
var clock = new Clock(function(time){
clock.dispose();
expect(time).to.equal(startTime);
done();
}, 10);
2015-08-18 20:32:59 +00:00
var startTime = clock.now() + 0.1;
clock.start(startTime);
});
it ("can be scheduled to stop in the future", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var invokations = 0;
var clock = new Clock(function(){
invokations++;
}, 0.5).start(0);
tearDown(function(){
expect(invokations).to.equal(1);
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
it ("invokes the right number of callbacks given the duration", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var invokations = 0;
var clock = new Clock(function(){
invokations++;
}, 10).start(0).stop(0.49);
2016-03-03 06:37:44 +00:00
tearDown(function(){
expect(invokations).to.equal(5);
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
2015-08-18 20:32:59 +00:00
it ("can schedule the frequency of the clock", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var invokations = 0;
var clock = new Clock(function(){
invokations++;
}, 2);
clock.start(0).stop(1.1);
clock.frequency.setValueAtTime(4, 0.5);
tearDown(function(){
expect(invokations).to.equal(4);
clock.dispose();
done();
});
2015-08-18 20:32:59 +00:00
}, 2);
});
});
context("Ticks", function(){
it ("has 0 ticks when first created", function(){
var clock = new Clock();
expect(clock.ticks).to.equal(0);
clock.dispose();
});
it ("increments 1 tick per callback", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var ticks = 0;
var clock = new Clock(function(){
ticks++;
}, 0.05).start();
tearDown(function(){
expect(ticks).to.equal(clock.ticks);
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
it ("resets ticks on stop", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var clock = new Clock(function(){}, 20).start(0).stop(0.5);
2016-03-03 06:37:44 +00:00
testFn(function(sample, time){
if (time > 0.05 && time < 0.5){
2016-03-03 06:37:44 +00:00
expect(clock.ticks).to.be.above(0);
}
});
tearDown(function(){
expect(clock.ticks).to.equal(0);
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
it ("does not reset ticks on pause but stops incrementing", function(done){
2016-03-03 06:37:44 +00:00
Offline(function(output, testFn, tearDown){
var clock = new Clock(function(){}, 20).start(0).pause(0.3);
2016-03-03 06:37:44 +00:00
var pausedTicks = 0;
testFn(function(sample, time){
if (time > 0.05 && time < 0.3){
expect(clock.ticks).to.be.above(0);
pausedTicks = clock.ticks;
} else if (time >= 0.3){
expect(clock.ticks).to.equal(pausedTicks);
2016-03-03 06:37:44 +00:00
}
});
tearDown(function(){
clock.dispose();
done();
});
}, 0.6);
2015-08-18 20:32:59 +00:00
});
2015-08-18 22:14:26 +00:00
it ("starts incrementing where it left off after pause", function(done){
Offline(function(output, testFn, tearDown){
var clock = new Clock(function(){}, 20).start(0).pause(0.3).start(0.5);
var pausedTicks = 0;
var restarted = false;
testFn(function(sample, time){
if (time < 0.3){
expect(clock.ticks).to.be.above(0);
pausedTicks = clock.ticks;
} else if (time >= 0.7 && !restarted){
restarted = true;
expect(clock.ticks).to.equal(pausedTicks + 1);
}
});
tearDown(function(){
clock.dispose();
done();
});
}, 0.6);
});
2015-08-18 22:14:26 +00:00
it ("can start with a tick offset", function(done){
var clock = new Clock(function(){
expect(clock.ticks).to.equal(4);
clock.dispose();
done();
}, 10);
2015-08-18 22:14:26 +00:00
expect(clock.ticks).to.equal(0);
clock.start(undefined, 4);
});
2015-08-18 20:32:59 +00:00
});
2016-09-24 15:26:57 +00:00
context("Events", function(){
it ("triggers the start event on start", function(done){
var clock = new Clock(function(){}, 20);
2016-09-24 15:26:57 +00:00
var startTime = clock.now() + 0.3;
clock.on("start", function(time, offset){
2016-12-19 03:33:36 +00:00
expect(time).to.be.closeTo(startTime, 0.05);
expect(clock.now() + Clock.lookAhead).to.be.closeTo(startTime, 0.1);
2016-09-24 15:26:57 +00:00
expect(offset).to.equal(0);
clock.dispose();
done();
});
clock.start(startTime);
});
2016-09-24 15:26:57 +00:00
it ("triggers the start event with an offset", function(done){
var clock = new Clock(function(){}, 20);
2016-09-24 15:26:57 +00:00
var startTime = clock.now() + 0.3;
clock.on("start", function(time, offset){
2016-12-19 03:33:36 +00:00
expect(time).to.be.closeTo(startTime, 0.05);
expect(clock.now() + Clock.lookAhead).to.be.closeTo(startTime, 0.1);
2016-09-24 15:26:57 +00:00
expect(offset).to.equal(2);
clock.dispose();
done();
});
clock.start(startTime, 2);
});
it ("triggers stop event", function(done){
var clock = new Clock(function(){}, 20);
2016-09-24 15:26:57 +00:00
var stopTime = clock.now() + 0.3;
clock.on("stop", function(time){
2016-12-19 03:33:36 +00:00
expect(time).to.be.closeTo(stopTime, 0.05);
expect(clock.now() + Clock.lookAhead).to.be.closeTo(stopTime, 0.1);
2016-09-24 15:26:57 +00:00
clock.dispose();
done();
});
clock.start().stop(stopTime);
});
it ("triggers pause stop event", function(done){
var clock = new Clock(function(){}, 20);
2016-09-24 15:26:57 +00:00
var now = clock.now();
clock.on("pause", function(time){
2016-12-19 03:33:36 +00:00
expect(time).to.be.closeTo(now + 0.1, 0.05);
2016-09-24 15:26:57 +00:00
}).on("stop", function(time){
2016-12-19 03:33:36 +00:00
expect(time).to.be.closeTo(now + 0.2, 0.05);
2016-09-24 15:26:57 +00:00
clock.dispose();
done();
});
clock.start().pause("+0.1").stop("+0.2");
});
});
2015-08-18 20:32:59 +00:00
});
});