Tone.js/test/event/Pattern.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

import Basic from "helper/Basic";
import Pattern from "Tone/event/Pattern";
import Tone from "Tone/core/Tone";
import Transport from "Tone/core/Transport";
import Offline from "helper/Offline";
import Time from "Tone/type/Time";
2015-11-04 00:15:55 +00:00
describe("Pattern", function(){
2015-11-04 00:15:55 +00:00
Basic(Pattern);
2015-11-04 00:15:55 +00:00
context("Constructor", function(){
2015-11-04 00:15:55 +00:00
it("takes a callback, an array of values and a pattern name", function(){
return Offline(function(){
var callback = function(){};
var pattern = new Pattern(callback, [0, 1, 2, 3], "down");
expect(pattern.callback).to.equal(callback);
expect(pattern.values).to.deep.equal([0, 1, 2, 3]);
expect(pattern.pattern).to.equal("down");
pattern.dispose();
});
});
2015-11-04 00:15:55 +00:00
it("can be constructed with no arguments", function(){
return Offline(function(){
var pattern = new Pattern();
pattern.dispose();
});
});
2015-11-04 00:15:55 +00:00
it("can pass in arguments in options object", function(){
return Offline(function(){
var callback = function(){};
var pattern = new Pattern({
"callback" : callback,
"iterations" : 4,
"probability" : 0.3,
"interval" : "8t",
"values" : [1, 2, 3],
"pattern" : "upDown"
2015-11-04 00:15:55 +00:00
});
expect(pattern.callback).to.equal(callback);
expect(pattern.interval.valueOf()).to.equal(Time("8t").valueOf());
expect(pattern.iterations).to.equal(4);
expect(pattern.values).to.deep.equal([1, 2, 3]);
expect(pattern.probability).to.equal(0.3);
expect(pattern.pattern).to.equal("upDown");
pattern.dispose();
2015-11-04 00:15:55 +00:00
});
});
});
2015-11-04 00:15:55 +00:00
context("Get/Set", function(){
2015-11-04 00:15:55 +00:00
it("can set values with object", function(){
return Offline(function(){
var callback = function(){};
var pattern = new Pattern();
pattern.set({
"callback" : callback,
"values" : ["a", "b", "c"],
"index" : 2
2015-11-04 00:15:55 +00:00
});
expect(pattern.callback).to.equal(callback);
expect(pattern.values).to.deep.equal(["a", "b", "c"]);
expect(pattern.index).to.equal(2);
pattern.dispose();
});
});
2015-11-04 00:15:55 +00:00
it("can set get a the values as an object", function(){
return Offline(function(){
var callback = function(){};
var pattern = new Pattern({
"callback" : callback,
"pattern" : "random",
"probability" : 0.3,
2015-11-04 00:15:55 +00:00
});
var values = pattern.get();
expect(values.pattern).to.equal("random");
values.pattern = "upDown";
expect(values.pattern).to.equal("upDown");
expect(values.probability).to.equal(0.3);
pattern.dispose();
2015-11-04 00:15:55 +00:00
});
});
});
2015-11-04 00:15:55 +00:00
context("Callback", function(){
2015-11-04 00:15:55 +00:00
it("is invoked after it's started", function(){
var invoked = false;
return Offline(function(Transport){
var index = 0;
var pattern = new Pattern(function(){
invoked = true;
expect(pattern.value).to.equal(index);
index++;
}, [0, 1, 2]).start(0);
Transport.start();
}, 0.2).then(function(){
expect(invoked).to.be.true;
});
});
2015-11-04 00:15:55 +00:00
it("passes in the scheduled time and pattern index to the callback", function(){
var invoked = false;
return Offline(function(Transport){
var startTime = 0.05;
var pattern = new Pattern(function(time, note){
expect(time).to.be.a.number;
expect(time - startTime).to.be.closeTo(0.3, 0.01);
expect(note).to.be.equal("a");
invoked = true;
}, ["a"], "up");
Transport.start(startTime);
pattern.start(0.3);
}, 0.4).then(function(){
expect(invoked).to.be.true;
2015-11-04 00:15:55 +00:00
});
});
2015-11-04 00:15:55 +00:00
it("passes in the next note of the pattern", function(){
var counter = 0;
return Offline(function(Transport){
var pattern = new Pattern(function(time, note){
expect(note).to.equal(counter % 3);
counter++;
}, [0, 1, 2], "up").start(0);
pattern.interval = "16n";
Transport.start(0);
}, 0.7).then(function(){
expect(counter).to.equal(6);
});
2017-12-30 16:26:29 +00:00
});
2015-11-04 00:15:55 +00:00
});
});