Tone.js/test/tests/Signal.js

457 lines
12 KiB
JavaScript
Raw Normal View History

2014-06-21 17:05:05 +00:00
define(["tests/Core", "chai", "Tone/component/Recorder", "Tone/signal/Signal", "Tone/signal/Add", "Tone/signal/Multiply",
2014-07-02 21:09:13 +00:00
"Tone/signal/Scale", "Tone/source/Oscillator", "Tone/signal/Merge", "Tone/signal/Split","Tone/core/Master", "Tone/signal/EqualsZero",
"Tone/signal/Threshold", "Tone/signal/Switch"],
function(core, chai, Recorder, Signal, Add, Multiply, Scale, Oscillator, Merge, Split, Master, EqualsZero, Threshold, Switch){
2014-06-18 05:37:01 +00:00
var expect = chai.expect;
2014-06-21 17:05:05 +00:00
Master.mute();
2014-06-18 05:37:01 +00:00
//SIGNAL
describe("Tone.Signal", function(){
this.timeout(1000);
2014-06-21 17:05:05 +00:00
var signal = new Signal(0);
signal.toMaster();
after(function(){
signal.dispose();
});
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var s = new Signal();
s.dispose();
});
it("can start with a value initially", function(){
2014-06-21 17:05:05 +00:00
expect(signal.getValue()).to.equal(0);
});
it("can set a value", function(){
signal.setValue(10);
expect(signal.getValue()).to.equal(10);
});
it("can set a value in the future", function(done){
2014-06-21 17:05:05 +00:00
signal.setValueAtTime(100, "+0.1");
expect(signal.getValue()).to.equal(10);
var interval = setInterval(function(){
2014-06-21 17:05:05 +00:00
if (signal.getValue() === 100){
clearInterval(interval);
2014-06-21 17:05:05 +00:00
done();
2014-06-18 19:42:17 +00:00
}
}, 10);
2014-06-18 05:37:01 +00:00
});
2014-06-21 17:05:05 +00:00
it("can change value with sample accurate timing", function(done){
2014-06-21 17:05:05 +00:00
var changeSignal = new Signal(0);
var waitTime = 0.03;
changeSignal.setValueAtTime(1, "+"+waitTime);//ramp after 50ms
var recorder = new Recorder();
2014-06-21 17:05:05 +00:00
changeSignal.connect(recorder);
var delayTime = 0.05;
recorder.record(0.1, delayTime, function(buffers){
buffer = buffers[0];
2014-06-18 19:42:17 +00:00
for (var i = 0; i < buffer.length; i++){
if (buffer[i] === 1){
2014-06-21 17:05:05 +00:00
expect(changeSignal.samplesToSeconds(i)).is.closeTo(delayTime - waitTime, 0.01);
changeSignal.dispose();
recorder.dispose();
2014-06-18 19:42:17 +00:00
done();
break;
2014-06-18 05:37:01 +00:00
}
2014-06-18 19:42:17 +00:00
}
2014-06-21 17:05:05 +00:00
});
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){
2014-06-21 17:05:05 +00:00
var syncTo = new Signal(1);
var signalSync = new Signal(2);
signalSync.sync(syncTo);
syncTo.setValue(2);
syncTo.noGC();
signalSync.noGC();
2014-06-18 19:42:17 +00:00
var recorder = new Recorder();
2014-06-21 17:05:05 +00:00
signalSync.connect(recorder);
recorder.record(0.1, 0.05, function(buffers){
var buffer = buffers[0];
2014-06-18 19:42:17 +00:00
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(4);
}
2014-06-21 17:05:05 +00:00
signalSync.dispose();
recorder.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){
2014-06-21 17:05:05 +00:00
signal.setValue(-10);
signal.noGC();
var recorder = new Recorder(1);
signal.connect(recorder);
var waitTime = 0.03;
expect(signal.getValue()).to.equal(-10);
signal.linearRampToValueNow(1, waitTime);
2014-06-21 17:05:05 +00:00
recorder.record(0.1, 0.05, function(buffers){
var buffer = buffers[0];
for (var i = 0; i < buffer.length; i++){
if (buffer[i] === 1){
expect(signal.samplesToSeconds(i)).is.closeTo(waitTime, 0.01);
done();
break;
}
}
2014-06-21 17:05:05 +00:00
});
});
2014-06-18 05:37:01 +00:00
});
//ADD
describe("Tone.Add", function(){
this.timeout(500);
2014-06-20 04:38:14 +00:00
2014-06-21 17:05:05 +00:00
var recorder = new Recorder();
after(function(){
recorder.dispose();
});
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var a = new Add(1);
a.dispose();
});
2014-06-18 05:37:01 +00:00
it("correctly sums a signal and a number", function(done){
var signal = new Signal(0);
var adder = new Add(3);
signal.connect(adder);
2014-06-18 19:42:17 +00:00
adder.connect(recorder);
2014-06-21 17:05:05 +00:00
recorder.record(0.1, 0.05, function(buffers){
var buffer = buffers[0];
2014-06-18 19:42:17 +00:00
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(3);
}
done();
2014-06-21 17:05:05 +00:00
});
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 handle negative values", function(done){
var signal = new Signal(10);
var adder = new Add(-1);
signal.connect(adder);
2014-06-18 19:42:17 +00:00
var recorder = new Recorder();
adder.connect(recorder);
2014-06-21 17:05:05 +00:00
recorder.record(0.1, 0.05, function(buffers){
var buffer = buffers[0];
2014-06-18 19:42:17 +00:00
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(9);
}
done();
2014-06-21 17:05:05 +00:00
});
2014-06-18 05:37:01 +00:00
});
});
//MULTIPLY
describe("Tone.Multiply", function(){
this.timeout(500);
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var m = new Multiply(1);
m.dispose();
});
2014-06-18 05:37:01 +00:00
it("correctly multiplys a signal and a scalar", function(done){
var signal = new Signal(2);
var mult = new Multiply(10);
signal.connect(mult);
2014-06-18 19:42:17 +00:00
var recorder = new Recorder();
mult.connect(recorder);
2014-06-21 17:05:05 +00:00
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
2014-06-18 19:42:17 +00:00
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(20);
}
2014-06-21 17:05:05 +00:00
signal.dispose();
mult.dispose();
2014-06-18 19:42:17 +00:00
done();
2014-07-02 21:09:13 +00:00
});
2014-06-18 05:37:01 +00:00
});
});
//SCALE
describe("Tone.Scale", function(){
this.timeout(500);
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var s = new Scale(0, 10);
s.dispose();
});
2014-06-18 05:37:01 +00:00
it("scales an input range to an output range", function(done){
//make an oscillator to drive the signal
var osc = new Oscillator(1000);
osc.start();
var scale = new Scale(-1, 1, 10, 20);
osc.connect(scale);
2014-06-18 19:42:17 +00:00
var recorder = new Recorder();
scale.connect(recorder);
2014-06-21 17:05:05 +00:00
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
2014-06-18 19:42:17 +00:00
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.be.within(10, 20);
}
done();
2014-06-21 17:05:05 +00:00
});
2014-06-18 05:37:01 +00:00
});
});
2014-06-18 19:42:17 +00:00
//MERGE
2014-06-18 05:37:01 +00:00
describe("Tone.Merge", function(){
this.timeout(500);
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var mer = new Merge();
mer.dispose();
});
2014-06-18 05:37:01 +00:00
it("merge two signal into one stereo signal", function(done){
//make an oscillator to drive the signal
var sigL = new Signal(1);
var sigR = new Signal(2);
var merger = new Merge();
sigL.connect(merger.left);
sigR.connect(merger.right);
2014-06-18 19:42:17 +00:00
var recorder = new Recorder(2);
merger.connect(recorder);
2014-06-21 17:05:05 +00:00
recorder.record(0.05, 0.05, function(buffers){
2014-06-18 19:42:17 +00:00
var lBuffer = buffers[0];
var rBuffer = buffers[1];
//get the left buffer and check that all values are === 1
for (var i = 0; i < lBuffer.length; i++){
expect(lBuffer[i]).to.equal(1);
expect(rBuffer[i]).to.equal(2);
}
done();
2014-06-21 17:05:05 +00:00
});
2014-06-18 05:37:01 +00:00
});
});
//SCALE
describe("Tone.Split", function(){
this.timeout(500);
2014-06-20 04:38:14 +00:00
it("can be created and disposed", function(){
var split = new Split();
split.dispose();
});
2014-06-18 05:37:01 +00:00
it("merges two signal into one stereo signal and then split them back into two signals", function(done){
//make an oscillator to drive the signal
var sigL = new Signal(1);
var sigR = new Signal(2);
var merger = new Merge();
var split = new Split();
sigL.connect(merger.left);
sigR.connect(merger.right);
merger.connect(split);
2014-06-18 19:42:17 +00:00
var recorderL = new Recorder();
var recorderR = new Recorder();
split.left.connect(recorderL);
split.right.connect(recorderR);
2014-06-21 22:39:01 +00:00
recorderL.record(0.05, 0.1, function(buffers){
2014-06-21 17:05:05 +00:00
var lBuffer = buffers[0];
2014-06-18 19:42:17 +00:00
for (var i = 0; i < lBuffer.length; i++){
expect(lBuffer[i]).to.equal(1);
}
2014-06-21 17:05:05 +00:00
});
recorderR.record(0.05, 0.1, function(buffers){
2014-06-18 19:42:17 +00:00
var rBuffer = recorderR.getFloat32Array()[0];
for (var j = 0; j < rBuffer.length; j++){
expect(rBuffer[j]).to.equal(2);
}
done();
2014-06-21 17:05:05 +00:00
});
2014-06-18 05:37:01 +00:00
});
});
2014-07-02 19:46:58 +00:00
//EQUALS 0
describe("Tone.EqualsZero", function(){
this.timeout(500);
it("can be created and disposed", function(){
var ez = new EqualsZero();
ez.dispose();
});
it("outputs 1 when the incoming signal is 0", function(done){
var signal = new Signal(0);
var ez = new EqualsZero();
signal.connect(ez);
var recorder = new Recorder();
ez.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(1);
}
signal.dispose();
ez.dispose();
done();
2014-07-02 21:09:13 +00:00
});
2014-07-02 19:46:58 +00:00
});
it("outputs 0 when the incoming signal is not 0", function(done){
var signal = new Signal(100);
var ez = new EqualsZero();
signal.connect(ez);
var recorder = new Recorder();
ez.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(0);
}
signal.dispose();
ez.dispose();
done();
2014-07-02 21:09:13 +00:00
});
2014-07-02 19:46:58 +00:00
});
it("is not fooled by values very close to 0", function(done){
var signal = new Signal(0.00001);
var ez = new EqualsZero();
signal.connect(ez);
var recorder = new Recorder();
ez.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(0);
}
signal.dispose();
ez.dispose();
done();
2014-07-02 21:09:13 +00:00
});
});
});
//THRESHOLD
describe("Tone.Threshold", function(){
this.timeout(500);
it("can be created and disposed", function(){
var thresh = new Threshold();
thresh.dispose();
});
it("thresholds an incoming signal to 0 when it is below the thresh", function(done){
var signal = new Signal(0.1);
var thresh = new Threshold(0.5);
signal.connect(thresh);
var recorder = new Recorder();
thresh.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(0);
}
signal.dispose();
thresh.dispose();
done();
});
});
it("thresholds an incoming signal to 1 when it is above the thresh", function(done){
var signal = new Signal(0.8);
var thresh = new Threshold(0.5);
signal.connect(thresh);
var recorder = new Recorder();
thresh.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(1);
}
signal.dispose();
thresh.dispose();
done();
});
});
it("thresholds an incoming signal that is very close to the thresh", function(done){
var signal = new Signal(0.4999);
var thresh = new Threshold(0.5);
signal.connect(thresh);
var recorder = new Recorder();
thresh.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(0);
}
signal.dispose();
thresh.dispose();
done();
});
});
});
//Switch
describe("Tone.Switch", function(){
this.timeout(500);
it("can be created and disposed", function(){
var sw = new Switch();
sw.dispose();
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 = new Signal(10);
var gate = new Switch();
signal.connect(gate);
var recorder = new Recorder();
gate.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(0);
}
signal.dispose();
gate.dispose();
done();
});
});
it("can allow a signal to pass through", function(done){
var signal = new Signal(10);
var gate = new Switch();
signal.connect(gate);
gate.open();
var recorder = new Recorder();
gate.connect(recorder);
recorder.record(0.05, 0.05, function(buffers){
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
expect(buffer[i]).to.equal(10);
}
signal.dispose();
gate.dispose();
done();
});
});
2014-07-02 19:46:58 +00:00
});
2014-06-18 05:37:01 +00:00
});