Tone.js/test/tests/Signal.js

372 lines
10 KiB
JavaScript
Raw Normal View History

2014-08-27 19:20:52 +00:00
/* global it, describe, recorderDelay, recorderDuration, maxTimeout */
2014-07-30 19:10:45 +00:00
2014-07-04 02:59:45 +00:00
define(["tests/Core", "chai", "Tone/component/Recorder", "Tone/signal/Signal", "Tone/source/Oscillator",
2014-08-24 19:46:55 +00:00
"Tone/core/Master", "Tone/signal/Threshold", "Tone/signal/Switch", "Tone/signal/Route", "Tone/signal/Selector"],
function(core, chai, Recorder, Signal, Oscillator, Master, Threshold, Switch, Route, Selector){
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(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-06-21 17:05:05 +00:00
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-07-04 03:48:27 +00:00
var signal = new Signal(0);
2014-06-21 17:05:05 +00:00
expect(signal.getValue()).to.equal(0);
2014-07-04 03:48:27 +00:00
signal.dispose();
});
it("can set a value", function(){
2014-07-04 03:48:27 +00:00
var signal = new Signal(0);
signal.setValue(10);
expect(signal.getValue()).to.equal(10);
2014-07-04 03:48:27 +00:00
signal.dispose();
});
it("can set a value in the future", function(done){
2014-07-04 03:48:27 +00:00
var sig = new Signal(10);
sig.noGC();
sig.setValueAtTime(100, "+0.1");
expect(sig.getValue()).to.equal(10);
var recorder = new Recorder();
sig.connect(recorder);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
var buffer = buffers[0];
2014-07-04 03:48:27 +00:00
var reached100 = false;
for (var i = 0; i < buffer.length; i++){
if (buffer[i] === 100){
reached100 = true;
break;
}
2014-06-18 19:42:17 +00:00
}
2014-07-04 03:48:27 +00:00
expect(reached100).to.be.true;
sig.dispose();
done();
});
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);
2014-08-27 19:20:52 +00:00
changeSignal.setValueAtTime(1, "+"+(recorderDelay));
var recorder = new Recorder();
2014-06-21 17:05:05 +00:00
changeSignal.connect(recorder);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
var buffer = buffers[0];
2014-06-18 19:42:17 +00:00
for (var i = 0; i < buffer.length; i++){
if (buffer[i] === 1){
2014-08-27 19:20:52 +00:00
expect(changeSignal.samplesToSeconds(i)).is.closeTo(0.0, 0.1);
2014-06-21 17:05:05 +00:00
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);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-06-21 17:05:05 +00:00
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-07-04 03:48:27 +00:00
var sig1 = new Signal(0);
sig1.setValue(-10);
sig1.noGC();
var recorder = new Recorder(1);
2014-07-04 03:48:27 +00:00
sig1.connect(recorder);
2014-08-27 19:20:52 +00:00
var waitTime = recorderDelay;
2014-07-04 03:48:27 +00:00
expect(sig1.getValue()).to.equal(-10);
sig1.linearRampToValueNow(1, waitTime);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-06-21 17:05:05 +00:00
var buffer = buffers[0];
for (var i = 0; i < buffer.length; i++){
if (buffer[i] === 1){
2014-08-27 19:20:52 +00:00
expect(sig1.samplesToSeconds(i)).is.closeTo(0, 0.1);
done();
2014-07-04 03:48:27 +00:00
sig1.dispose();
break;
}
}
2014-06-21 17:05:05 +00:00
});
});
2014-06-18 05:37:01 +00:00
});
2014-07-02 21:09:13 +00:00
//THRESHOLD
describe("Tone.Threshold", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-02 21:09:13 +00:00
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);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-02 21:09:13 +00:00
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);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-02 21:09:13 +00:00
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);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-02 21:09:13 +00:00
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(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-02 21:09:13 +00:00
it("can be created and disposed", function(){
var sw = new Switch();
2014-07-02 21:09:13 +00:00
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();
2014-07-02 21:09:13 +00:00
signal.connect(gate);
var recorder = new Recorder();
gate.connect(recorder);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-02 21:09:13 +00:00
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();
2014-07-02 21:09:13 +00:00
signal.connect(gate);
gate.open();
var recorder = new Recorder();
gate.connect(recorder);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-02 21:09:13 +00:00
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-07-30 19:10:45 +00:00
//Route
describe("Tone.Route", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-30 19:10:45 +00:00
it("can be created and disposed", function(){
var r = new Route();
r.dispose();
});
it("can route a signal to first output", function(done){
var signal = new Signal(10);
var route = new Route();
signal.connect(route);
2014-08-27 19:20:52 +00:00
route.select(0);
var recorder = new Recorder();
route.connect(recorder, 0, 0);
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-30 19:10:45 +00:00
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();
route.dispose();
done();
});
2014-08-27 19:20:52 +00:00
});
it("can route a signal to first output and not the second one", function(done){
var signal = new Signal(10);
var route = new Route();
signal.connect(route);
route.select(0);
var recorder = new Recorder();
route.connect(recorder, 1, 0);
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-30 19:10:45 +00:00
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);
}
2014-08-27 19:20:52 +00:00
signal.dispose();
route.dispose();
2014-07-30 19:10:45 +00:00
done();
});
});
2014-08-27 19:20:52 +00:00
it("can route a signal to second output", function(done){
2014-07-30 19:10:45 +00:00
var signal = new Signal(20);
var route = new Route();
signal.connect(route);
2014-08-27 19:20:52 +00:00
route.select(1);
var recorder = new Recorder();
route.connect(recorder, 1, 0);
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-30 19:10:45 +00:00
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
2014-08-27 19:20:52 +00:00
expect(buffer[i]).to.equal(20);
2014-07-30 19:10:45 +00:00
}
signal.dispose();
route.dispose();
done();
});
2014-08-27 19:20:52 +00:00
});
it("can route a signal to second output and not the first one", function(done){
var signal = new Signal(20);
var route = new Route();
signal.connect(route);
route.select(1);
var recorder = new Recorder();
route.connect(recorder, 0, 0);
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-30 19:10:45 +00:00
var buffer = buffers[0];
//get the left buffer and check that all values are === 1
for (var i = 0; i < buffer.length; i++){
2014-08-27 19:20:52 +00:00
expect(buffer[i]).to.equal(0);
2014-07-30 19:10:45 +00:00
}
2014-08-27 19:20:52 +00:00
signal.dispose();
route.dispose();
2014-07-30 19:10:45 +00:00
done();
});
});
});
//Selector
describe("Tone.Selector", function(){
2014-08-27 19:20:52 +00:00
this.timeout(maxTimeout);
2014-07-30 19:10:45 +00:00
it("can be created and disposed", function(){
var s = new Selector();
s.dispose();
});
it("can select the first signal", function(done){
var signal0 = new Signal(10);
var signal1 = new Signal(20);
var sel = new Selector();
signal0.connect(sel, 0, 0);
signal1.connect(sel, 0, 1);
sel.select(0);
var recorder = new Recorder();
sel.connect(recorder, 0, 0);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-30 19:10:45 +00:00
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);
}
signal0.dispose();
signal1.dispose();
sel.dispose();
done();
});
});
it("can select the second signal", function(done){
var signal0 = new Signal(11);
var signal1 = new Signal(21);
var sel = new Selector();
signal0.connect(sel, 0, 0);
signal1.connect(sel, 0, 1);
sel.select(1);
var recorder = new Recorder();
sel.connect(recorder, 0, 0);
2014-08-27 19:20:52 +00:00
recorder.record(recorderDuration, recorderDelay, function(buffers){
2014-07-30 19:10:45 +00:00
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(21);
}
signal0.dispose();
signal1.dispose();
sel.dispose();
done();
});
});
});
2014-06-18 05:37:01 +00:00
});