Tone.js/test/event/Loop.js

334 lines
8.2 KiB
JavaScript
Raw Normal View History

2017-02-20 01:48:22 +00:00
define(["helper/Basic", "Tone/event/Loop", "Tone/core/Tone", "Tone/core/Transport", "helper/Offline", "Test"],
function (Basic, Loop, Tone, Transport, Offline, Test) {
2015-11-03 23:38:14 +00:00
describe("Loop", function(){
Basic(Loop);
context("Constructor", function(){
it ("takes a callback and an interval", function(){
2017-02-20 01:48:22 +00:00
return Offline(function(){
var callback = function(){};
var loop = new Loop(callback, "8n");
expect(loop.callback).to.equal(callback);
expect(loop.interval).to.equal("8n");
loop.dispose();
});
2015-11-03 23:38:14 +00:00
});
it ("can be constructed with no arguments", function(){
2017-02-20 01:48:22 +00:00
return Offline(function(){
var loop = new Loop();
expect(loop.iterations).to.equal(Infinity);
loop.dispose();
});
2015-11-03 23:38:14 +00:00
});
it ("can pass in arguments in options object", function(){
2017-02-20 01:48:22 +00:00
return Offline(function(){
var callback = function(){};
var loop = new Loop({
"callback" : callback,
"iterations" : 4,
"probability" : 0.3,
"interval" : "8t"
});
expect(loop.callback).to.equal(callback);
expect(loop.interval).to.equal("8t");
expect(loop.iterations).to.equal(4);
expect(loop.probability).to.equal(0.3);
loop.dispose();
2015-11-03 23:38:14 +00:00
});
});
});
context("Get/Set", function(){
it ("can set values with object", function(){
2017-02-20 01:48:22 +00:00
return Offline(function(){
var callback = function(){};
var loop = new Loop();
loop.set({
"callback" : callback,
"iterations" : 8
});
expect(loop.callback).to.equal(callback);
expect(loop.iterations).to.equal(8);
loop.dispose();
2015-11-03 23:38:14 +00:00
});
});
2017-07-06 14:32:50 +00:00
it ("can get/set the humanize values", function(){
return Offline(function(){
var callback = function(){};
var loop = new Loop();
loop.humanize = true;
expect(loop.humanize).to.be.true;
loop.dispose();
});
});
2015-11-03 23:38:14 +00:00
it ("can set get a the values as an object", function(){
2017-02-20 01:48:22 +00:00
return Offline(function(){
var callback = function(){};
var loop = new Loop({
"callback" : callback,
"iterations" : 4,
"probability" : 0.3
});
var values = loop.get();
expect(values.iterations).to.equal(4);
expect(values.probability).to.equal(0.3);
loop.dispose();
2015-11-03 23:38:14 +00:00
});
});
});
context("Callback", function(){
2017-02-20 01:48:22 +00:00
it ("does not invoke get invoked until started", function(){
return Offline(function(Transport){
new Loop(function(){
throw new Error("shouldn't call this callback");
}, "8n");
2017-02-20 01:48:22 +00:00
Transport.start();
}, 0.3);
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("is invoked after it's started", function(){
var invoked = false;
return Offline(function(Transport){
var loop = new Loop(function(){
loop.dispose();
invoked = true;
}, 0.05).start(0);
Transport.start();
}).then(function(){
expect(invoked).to.be.true;
});
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("passes in the scheduled time to the callback", function(){
var invoked = false;
return Offline(function(Transport){
var now = Transport.now() + 0.1;
var loop = new Loop(function(time){
expect(time).to.be.a.number;
expect(time - now).to.be.closeTo(0.3, 0.01);
loop.dispose();
invoked = true;
});
Transport.start(now);
loop.start(0.3);
}, 0.5).then(function(){
expect(invoked).to.be.true;
2015-11-03 23:38:14 +00:00
});
});
2017-02-20 01:48:22 +00:00
it ("can mute the callback", function(){
return Offline(function(){
var loop = new Loop(function(){
throw new Error("shouldn't call this callback");
}, "4n").start();
loop.mute = true;
expect(loop.mute).to.be.true;
2017-02-20 01:48:22 +00:00
Transport.start();
}, 0.4);
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("can trigger with some probability", function(){
return Offline(function(Transport){
var loop = new Loop(function(){
throw new Error("shouldn't call this callback");
}, "4n").start();
loop.probability = 0;
expect(loop.probability).to.equal(0);
2017-02-20 01:48:22 +00:00
Transport.start();
}, 0.4);
2015-11-03 23:38:14 +00:00
});
});
context("Scheduling", function(){
2017-02-20 01:48:22 +00:00
it ("can be started and stopped multiple times", function(){
return Offline(function(Transport){
var loop = new Loop().start().stop(0.2).start(0.4);
2017-02-20 01:48:22 +00:00
Transport.start(0);
return function(time){
Test.whenBetween(time, 0, 0.19, function(){
expect(loop.state).to.equal("started");
2017-02-20 01:48:22 +00:00
});
Test.whenBetween(time, 0.2, 0.39, function(){
expect(loop.state).to.equal("stopped");
});
Test.whenBetween(time, 0.4, Infinity, function(){
expect(loop.state).to.equal("started");
});
};
}, 0.6);
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("restarts when transport is restarted", function(){
return Offline(function(Transport){
var note = new Loop().start(0).stop(0.4);
Transport.start(0).stop(0.5).start(0.55);
return function(sample, time){
Test.whenBetween(time, 0, 0.39, function(){
expect(note.state).to.equal("started");
});
Test.whenBetween(time, 0.4, 0.5, function(){
expect(note.state).to.equal("stopped");
});
Test.whenBetween(time, 0.55, 0.8, function(){
expect(note.state).to.equal("started");
});
};
}, 1);
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("can be cancelled", function(){
return Offline(function(Transport){
var note = new Loop().start(0);
expect(note.state).to.equal("started");
Transport.start();
var firstStop = false;
var restarted = false;
var tested = false;
return function(time){
//stop the transport
if (time > 0.2 && !firstStop){
firstStop = true;
Transport.stop();
note.cancel();
}
if (time > 0.3 && !restarted){
restarted = true;
Transport.start();
}
if (time > 0.4 && !tested){
restarted = true;
Transport.start();
expect(note.state).to.equal("stopped");
}
};
}, 0.5);
2015-11-03 23:38:14 +00:00
});
});
context("Looping", function(){
2017-02-20 01:48:22 +00:00
it ("loops", function(){
var callCount = 0;
return Offline(function(Transport){
new Loop({
"interval" : 0.1,
"callback" : function(){
callCount++;
}
}).start(0);
2017-02-20 01:48:22 +00:00
Transport.start();
}, 0.8).then(function(){
2017-07-06 14:32:50 +00:00
expect(callCount).to.equal(9);
2017-02-20 01:48:22 +00:00
});
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("loops for the specified interval", function(){
var invoked = false;
return Offline(function(Transport){
var lastCall;
new Loop({
"interval" : "8n",
"callback" : function(time){
if (lastCall){
invoked = true;
expect(time - lastCall).to.be.closeTo(0.25, 0.01);
}
lastCall = time;
2015-11-03 23:38:14 +00:00
}
2017-02-20 01:48:22 +00:00
}).start(0);
Transport.start();
}, 1).then(function(){
expect(invoked).to.be.true;
});
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("can loop a specific number of iterations", function(){
var callCount = 0;
return Offline(function(Transport){
new Loop({
"interval" : 0.1,
"iterations" : 2,
"callback" : function(){
callCount++;
}
}).start(0);
2017-02-20 01:48:22 +00:00
Transport.start();
}, 0.4).then(function(){
expect(callCount).to.equal(2);
});
2015-11-03 23:38:14 +00:00
});
2017-02-20 01:48:22 +00:00
it ("reports the progress of the loop", function(){
return Offline(function(Transport){
var loop = new Loop({
"interval" : 1,
}).start(0);
2017-02-20 01:48:22 +00:00
Transport.start();
return function(time){
expect(loop.progress).to.be.closeTo(time, 0.05);
};
}, 0.8);
2015-11-03 23:38:14 +00:00
});
});
context("playbackRate", function(){
2017-02-20 01:48:22 +00:00
it ("can adjust the playbackRate", function(){
var invoked = false;
return Offline(function(Transport){
var lastCall;
2017-07-06 14:32:50 +00:00
var loop = new Loop({
"playbackRate" : 2,
2016-04-13 16:24:34 +00:00
"interval" : 0.5,
"callback" : function(time){
if (lastCall){
2017-02-20 01:48:22 +00:00
invoked = true;
expect(time - lastCall).to.be.closeTo(0.25, 0.01);
}
lastCall = time;
2015-11-03 23:38:14 +00:00
}
}).start(0);
2017-07-06 14:32:50 +00:00
expect(loop.playbackRate).to.equal(2);
2017-02-20 01:48:22 +00:00
Transport.start();
}, 0.7).then(function(){
expect(invoked).to.be.true;
});
2015-11-03 23:38:14 +00:00
});
2017-07-06 14:32:50 +00:00
it ("can playback at a faster rate", function(){
var callCount = 0;
return Offline(function(Transport){
var loop = new Loop({
"interval" : 0.1,
"callback" : function(){
callCount++;
}
}).start(0);
loop.playbackRate = 1.5;
expect(loop.playbackRate).to.equal(1.5);
Transport.start();
}, 0.8).then(function(){
expect(callCount).to.equal(13);
});
});
2015-11-03 23:38:14 +00:00
});
});
});