Tone.js/test/tests/Sources.js

219 lines
5.2 KiB
JavaScript
Raw Normal View History

/* global it, describe, after, maxTimeout, recorderDelay, recorderDuration */
2014-07-30 19:10:45 +00:00
define(["chai", "Tone/source/Player", "Tone/core/Master", "Tone/source/Oscillator", "Tone/component/Recorder", "Tone/source/Noise", "tests/Core"],
2014-06-20 04:25:00 +00:00
function(chai, Player, Master, Oscillator, Recorder, Noise){
2014-06-19 05:40:16 +00:00
2014-06-19 02:33:08 +00:00
var expect = chai.expect;
2014-06-19 05:40:16 +00:00
Master.mute();
describe("Tone.Player", function(){
this.timeout(maxTimeout);
var player = new Player("./testAudio/kick.mp3");
2014-06-20 01:48:16 +00:00
player.toMaster();
2014-06-19 05:40:16 +00:00
after(function(){
player.dispose();
});
2014-06-20 04:25:00 +00:00
it("can be created and disposed", function(){
var p = new Player();
p.dispose();
});
it("loads a file", function(done){
2014-06-19 05:40:16 +00:00
expect(player.state).to.equal("stopped");
2014-06-21 22:39:01 +00:00
player.load("./testAudio/kick.mp3", function(){
done();
});
});
2014-06-19 05:40:16 +00:00
it("has a duration", function(){
2014-06-21 17:05:05 +00:00
expect(player.duration).to.be.closeTo(0.23, 0.01);
});
2014-06-19 05:40:16 +00:00
it("invokes a callback onend", function(done){
player.onended = function(){
expect(player.state).to.equal("stopped");
done();
};
player.start();
2014-06-19 05:40:16 +00:00
expect(player.state).to.equal("started");
});
it("can be scheduled", function(done){
player.onended = function(){
var diff = player.now() - now - player.duration;
expect(diff).to.be.closeTo(0.5, 0.1);
done();
};
var now = player.now();
player.start("+.5");
});
it("can handle multiple restarts", function(){
2014-06-20 01:48:16 +00:00
expect(player.state).to.equal("stopped");
2014-06-19 05:40:16 +00:00
player.start();
player.start();
player.stop();
player.stop();
2014-06-20 01:48:16 +00:00
expect(player.state).to.equal("stopped");
});
});
2014-06-19 05:40:16 +00:00
describe("Tone.Oscillator", function(){
this.timeout(maxTimeout);
2014-06-19 05:40:16 +00:00
var oscillator = new Oscillator();
2014-06-20 01:48:16 +00:00
oscillator.toMaster();
2014-06-19 05:40:16 +00:00
after(function(){
oscillator.dispose();
});
2014-06-20 01:48:16 +00:00
2014-06-20 04:25:00 +00:00
it("can be created and disposed", function(){
var o = new Oscillator();
o.dispose();
});
2014-06-19 05:40:16 +00:00
it("starts and stops", function(done){
expect(oscillator.state).to.equal("stopped");
oscillator.start();
expect(oscillator.state).to.equal("started");
setTimeout(function(){
oscillator.stop();
done();
}, 100);
});
it("can be scheduled to stop", function(done){
expect(oscillator.state).to.equal("stopped");
oscillator.start();
expect(oscillator.state).to.equal("started");
2014-06-20 01:48:16 +00:00
oscillator.stop("+0.05");
2014-06-19 05:40:16 +00:00
setTimeout(function(){
expect(oscillator.state).to.equal("stopped");
done();
2014-06-20 01:48:16 +00:00
}, 200);
2014-06-19 05:40:16 +00:00
});
it("won't start again before stopping", function(){
expect(oscillator.state).to.equal("stopped");
oscillator.start();
oscillator.start();
oscillator.stop();
oscillator.stop();
2014-06-20 01:48:16 +00:00
expect(oscillator.state).to.equal("stopped");
});
it("be scheduled to start in the future", function(done){
var recorder = new Recorder();
oscillator.connect(recorder);
var startOffset = 0.05;
oscillator.start("+"+(recorderDelay + startOffset));
recorder.record(recorderDuration + startOffset, recorderDelay, function(buffers){
2014-06-20 01:48:16 +00:00
oscillator.stop();
2014-06-21 17:05:05 +00:00
var buffer = buffers[0];
2014-06-20 01:48:16 +00:00
for (var i = 0; i < buffer.length; i++){
if (buffer[i] !== 0){
expect(oscillator.samplesToSeconds(i)).to.be.closeTo(startOffset, 0.1);
2014-06-20 01:48:16 +00:00
break;
}
}
done();
2014-06-21 17:05:05 +00:00
});
2014-06-19 05:40:16 +00:00
});
it("can set the frequency", function(done){
oscillator.setFrequency(110);
expect(oscillator.frequency.getValue()).to.equal(110);
oscillator.start();
oscillator.setFrequency(220, 0.05);
setTimeout(function(){
expect(oscillator.frequency.getValue()).to.equal(220);
2014-06-20 01:48:16 +00:00
oscillator.stop();
2014-06-19 05:40:16 +00:00
done();
}, 100);
});
});
2014-06-20 04:25:00 +00:00
describe("Tone.Noise", function(){
this.timeout(maxTimeout);
2014-06-20 04:25:00 +00:00
var noise = new Noise();
noise.toMaster();
after(function(){
noise.dispose();
});
it("can be created and disposed", function(){
var n = new Noise();
n.dispose();
});
it("starts and stops", function(done){
expect(noise.state).to.equal("stopped");
noise.start();
expect(noise.state).to.equal("started");
setTimeout(function(){
noise.stop();
done();
}, 100);
});
it("can be scheduled to stop", function(done){
expect(noise.state).to.equal("stopped");
noise.start();
expect(noise.state).to.equal("started");
noise.stop("+0.05");
setTimeout(function(){
expect(noise.state).to.equal("stopped");
done();
}, 100);
});
it("won't start again before stopping", function(){
expect(noise.state).to.equal("stopped");
noise.start();
noise.start();
noise.stop();
noise.stop();
expect(noise.state).to.equal("stopped");
});
it("be scheduled to start in the future", function(done){
var recorder = new Recorder();
noise.connect(recorder);
var startOffset = 0.1;
noise.start("+"+(recorderDelay + startOffset));
recorder.record(recorderDuration + startOffset, recorderDelay, function(buffers){
2014-06-20 04:25:00 +00:00
noise.stop();
2014-06-21 17:05:05 +00:00
var buffer = buffers[0];
2014-06-20 04:25:00 +00:00
for (var i = 0; i < buffer.length; i++){
if (buffer[i] !== 0){
expect(noise.samplesToSeconds(i)).to.be.closeTo(startOffset, 0.1);
2014-06-20 04:25:00 +00:00
break;
}
}
done();
2014-06-21 17:05:05 +00:00
});
2014-06-20 04:25:00 +00:00
});
it("can set the noise types", function(){
noise.setType("brown");
noise.setType("white");
noise.setType("pink");
//even after started
noise.start();
noise.setType("brown");
noise.setType("white");
noise.setType("pink");
noise.stop();
});
});
2014-06-20 01:48:16 +00:00
});