Tone.js/test/signal/TimelineSignal.js

172 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-02-20 21:40:41 +00:00
define(["Test", "Tone/signal/TimelineSignal", "helper/Offline", "Tone/type/Type", "helper/Supports"],
function (Test, TimelineSignal, Offline, Tone, Supports) {
2015-08-17 05:02:40 +00:00
2015-08-18 20:31:12 +00:00
describe("TimelineSignal", function(){
2015-08-17 05:02:40 +00:00
it("can be created and disposed", function(){
2015-08-18 20:31:12 +00:00
var sched = new TimelineSignal();
2015-08-17 05:02:40 +00:00
sched.dispose();
Test.wasDisposed(sched);
});
it("can schedule a change in the future", function(){
2015-08-18 20:31:12 +00:00
var sched = new TimelineSignal(1);
2015-08-17 05:02:40 +00:00
sched.setValueAtTime(2, 0.2);
sched.dispose();
});
it("can schedule a ramp in the future", function(){
2015-08-18 20:31:12 +00:00
var sched = new TimelineSignal(1);
2015-08-17 05:02:40 +00:00
sched.setValueAtTime(2, 0);
sched.linearRampToValueAtTime(0.1, 0.2);
sched.exponentialRampToValueAtTime(1, 0.4);
sched.setTargetAtTime(0.5, 0.45, 1);
sched.dispose();
});
it("can get a setValueAtTime value in the future", function(){
2015-08-18 20:31:12 +00:00
var sched = new TimelineSignal(1);
2015-08-17 05:02:40 +00:00
sched.setValueAtTime(0, 0);
sched.setValueAtTime(1, 1);
sched.setValueAtTime(2, 2);
sched.setValueAtTime(3, 3);
2015-08-18 20:31:12 +00:00
expect(sched.getValueAtTime(0)).to.equal(0);
expect(sched.getValueAtTime(1)).to.equal(1);
expect(sched.getValueAtTime(1.1)).to.equal(1);
expect(sched.getValueAtTime(2)).to.equal(2);
expect(sched.getValueAtTime(3)).to.equal(3);
expect(sched.getValueAtTime(4)).to.equal(3);
2015-08-17 05:02:40 +00:00
sched.dispose();
});
it("can get linear ramp value in the future", function(){
2015-08-18 20:31:12 +00:00
var sched = new TimelineSignal(1);
2015-08-17 05:02:40 +00:00
sched.setValueAtTime(0, 0);
sched.linearRampToValueAtTime(1, 1);
sched.linearRampToValueAtTime(0, 2);
2015-08-18 20:31:12 +00:00
expect(sched.getValueAtTime(0)).to.equal(0);
expect(sched.getValueAtTime(0.5)).to.equal(0.5);
expect(sched.getValueAtTime(1)).to.equal(1);
expect(sched.getValueAtTime(1.5)).to.equal(0.5);
expect(sched.getValueAtTime(2)).to.equal(0);
2015-08-17 05:02:40 +00:00
sched.dispose();
});
2017-02-20 21:40:41 +00:00
it("can get exponential ramp value in the future", function(){
2015-08-17 05:02:40 +00:00
var sched;
2017-02-20 21:40:41 +00:00
return Offline(function(){
sched = new TimelineSignal().toMaster();
2015-08-17 05:02:40 +00:00
sched.setValueAtTime(0.5, 0);
sched.exponentialRampToValueAtTime(1, 1);
sched.exponentialRampToValueAtTime(0.5, 2);
2017-02-20 21:40:41 +00:00
}, 2).then(function(buffer){
buffer.forEach(function(sample, time){
expect(sample).to.be.closeTo(sched.getValueAtTime(time), 0.01);
});
2015-08-17 05:02:40 +00:00
});
});
2017-02-20 21:40:41 +00:00
it("can get set target value in the future", function(){
2015-08-17 05:02:40 +00:00
var sched;
2017-02-20 21:40:41 +00:00
return Offline(function(){
sched = new TimelineSignal(1).toMaster();
2016-03-05 15:44:10 +00:00
sched.setValueAtTime(1, 0);
2015-08-17 05:02:40 +00:00
sched.setTargetAtTime(0.5, 0.5, 2);
2017-02-20 21:40:41 +00:00
}, 2).then(function(buffer){
buffer.forEach(function(sample, time){
expect(sample).to.be.closeTo(sched.getValueAtTime(time), 0.01);
});
2015-08-17 05:02:40 +00:00
});
});
2017-02-20 21:40:41 +00:00
it("can get set a curve in the future", function(){
var sched;
return Offline(function(){
sched = new TimelineSignal(1).toMaster();
sched.setValueCurveAtTime([0, 1, 0.2, 0.8, 0], 0, 1);
2017-02-20 21:40:41 +00:00
}, 1).then(function(buffer){
buffer.forEach(function(sample, time){
expect(sample).to.be.closeTo(sched.getValueAtTime(time), 0.03);
});
2017-02-20 21:40:41 +00:00
});
});
2016-03-05 15:44:10 +00:00
2017-02-20 21:40:41 +00:00
it("can scale a curve value", function(){
var sched;
return Offline(function(){
sched = new TimelineSignal(1).toMaster();
2016-03-05 15:44:10 +00:00
sched.setValueCurveAtTime([0, 1, 0], 0, 1, 0.5);
2017-02-20 21:40:41 +00:00
}, 1).then(function(buffer){
buffer.forEach(function(sample){
expect(sample).to.be.at.most(0.51);
2016-03-05 15:44:10 +00:00
});
2017-02-20 21:40:41 +00:00
});
2016-03-05 15:44:10 +00:00
});
if (Supports.ACCURATE_SIGNAL_SCHEDULING){
2017-02-20 21:40:41 +00:00
it("can match a complex scheduled curve", function(){
var sched;
2017-02-20 21:40:41 +00:00
return Offline(function(){
sched = new TimelineSignal(1).toMaster();
sched.setValueAtTime(0.2, 0.3);
sched.setTargetAtTime(0.5, 0.5, 2);
sched.setValueAtTime(0.4, 1);
sched.linearRampToValueAtTime(5, 1.4);
sched.exponentialRampToValueAtTime(2, 1.6);
sched.setValueAtTime(2.5, 2);
sched.linearRampToValueAtTime(2.4, 2.5);
sched.linearRampToValueAtTime(5, 3);
sched.setTargetAtTime(2, 3.5, 5);
sched.setValueCurveAtTime([0, 1, 0], 3.8, 0.2);
2017-02-20 21:40:41 +00:00
}, 4).then(function(buffer){
buffer.forEach(function(sample, time){
expect(sample).to.be.closeTo(sched.getValueAtTime(time), 0.01);
});
});
2015-08-17 05:02:40 +00:00
});
}
2015-08-17 05:02:40 +00:00
it("can schedule a linear ramp between two times", function(){
2015-08-18 20:31:12 +00:00
var sched = new TimelineSignal(0);
2015-08-17 05:02:40 +00:00
sched.linearRampToValueBetween(1, 1, 2);
2015-08-18 20:31:12 +00:00
expect(sched.getValueAtTime(0)).to.equal(0);
expect(sched.getValueAtTime(0.5)).to.equal(0);
expect(sched.getValueAtTime(1)).to.equal(0);
expect(sched.getValueAtTime(1.5)).to.equal(0.5);
expect(sched.getValueAtTime(2)).to.equal(1);
2015-08-17 05:02:40 +00:00
});
2017-02-20 21:40:41 +00:00
it("can get exponential ramp value between two times", function(){
2015-08-17 05:02:40 +00:00
var sched;
2017-02-20 21:40:41 +00:00
return Offline(function(){
sched = new TimelineSignal(1).toMaster();
2015-08-17 05:02:40 +00:00
sched.linearRampToValueBetween(3, 1, 2);
2017-02-20 21:40:41 +00:00
}, 3).then(function(buffer){
buffer.forEach(function(sample, time){
expect(sample).to.be.closeTo(sched.getValueAtTime(time), 0.01);
});
2015-08-17 05:02:40 +00:00
});
});
2017-02-20 21:40:41 +00:00
it("can automate values with different units", function(){
var sched;
2017-02-20 21:40:41 +00:00
return Offline(function(){
sched = new TimelineSignal(-10, Tone.Type.Decibels).toMaster();
sched.setValueAtTime(-5, 0);
sched.linearRampToValueAtTime(-12, 0.5);
sched.exponentialRampToValueBetween(-6, 1, 1.1);
2017-02-20 21:40:41 +00:00
}, 1.2).then(function(buffer){
buffer.forEach(function(sample, time){
if (time < 0.5){
expect(sample).to.be.within(sched.dbToGain(-12), sched.dbToGain(-5));
} else if (time < 1){
expect(sample).to.be.a.percentageFrom(sched.dbToGain(-12), 0.01);
} else if (time > 1.1){
expect(sample).to.be.a.percentageFrom(sched.dbToGain(-6), 0.01);
}
});
});
});
2015-08-17 05:02:40 +00:00
});
});