Tone.js/test/source/BufferSource.js

268 lines
6.5 KiB
JavaScript
Raw Normal View History

2017-02-20 21:45:32 +00:00
define(["helper/Basic", "Tone/source/BufferSource", "helper/Offline", "Tone/core/Buffer", "helper/Meter"],
function (BasicTests, BufferSource, Offline, Buffer, Meter) {
if (window.__karma__){
Buffer.baseUrl = "/base/test/";
}
describe("BufferSource", function(){
var buffer = new Buffer();
beforeEach(function(done){
buffer.load("./audio/sine.wav", function(){
done();
});
});
//run the common tests
BasicTests(BufferSource, buffer);
context("Constructor", function(){
it ("can be constructed with a Tone.Buffer", function(done){
var source = new BufferSource(buffer);
expect(source.buffer).to.equal(buffer.get());
source.dispose();
done();
});
it ("can be constructed with an AudioBuffer", function(done){
var source = new BufferSource(buffer.get());
expect(source.buffer).to.equal(buffer.get());
source.dispose();
done();
});
it("can be created with an options object", function(){
var source = new BufferSource({
"buffer" : buffer,
"loop" : true
});
expect(source.loop).to.equal(true);
source.dispose();
});
});
context("Starts and Stops", function(){
it ("can be constructed with a Tone.Buffer", function(done){
var source = new BufferSource(buffer);
expect(source.buffer).to.equal(buffer.get());
source.dispose();
done();
});
it ("can be constructed with an AudioBuffer", function(done){
var source = new BufferSource(buffer.get());
expect(source.buffer).to.equal(buffer.get());
source.dispose();
done();
});
it("can be created with an options object", function(){
var source = new BufferSource({
"buffer" : buffer,
"loop" : true
});
expect(source.loop).to.equal(true);
source.dispose();
});
});
context("Looping", function(){
beforeEach(function(done){
buffer.load("./audio/short_sine.wav", function(){
done();
});
});
it("can be set to loop", function(){
var player = new BufferSource();
player.loop = true;
expect(player.loop).to.be.true;
player.dispose();
});
2017-02-20 19:06:44 +00:00
it("loops the audio", function(){
return Offline(function(){
var player = new BufferSource(buffer);
player.loop = true;
2017-02-20 19:06:44 +00:00
player.toMaster();
player.start(0);
2017-02-20 19:06:44 +00:00
}, buffer.duration * 2).then(function(buff){
buff.getRMS().forEach(function(val){
expect(val).to.be.above(0);
});
});
});
});
context("Get/Set", function(){
it("can be set with an options object", function(){
var player = new BufferSource();
expect(player.loop).to.be.false;
player.set({
"loop" : true,
"loopStart" : 0.4
});
expect(player.loop).to.be.true;
expect(player.loopStart).to.equal(0.4);
player.dispose();
});
it("can get/set the playbackRate", function(){
var player = new BufferSource();
player.playbackRate.value = 0.5;
expect(player.playbackRate.value).to.equal(0.5);
player.dispose();
});
});
context("Start/Stop Scheduling", function(){
beforeEach(function(done){
buffer.load("./audio/sine.wav", function(){
done();
});
});
2017-02-20 19:06:44 +00:00
it("can play for a specific duration", function(){
2017-02-20 21:45:32 +00:00
return Meter(function(){
var player = new BufferSource(buffer);
2017-02-20 19:06:44 +00:00
player.toMaster();
player.start(0).stop(0.1);
2017-02-20 19:06:44 +00:00
return function(time){
if (time > 0.1){
expect(player.state).to.equal("stopped");
}
2017-02-20 19:06:44 +00:00
};
}, 0.4).then(function(buffer){
buffer.forEach(function(level, time){
if (time >= 0 && time < 0.1){
expect(level).to.be.greaterThan(0);
} else if (time > 0.1){
expect(level).to.equal(0);
}
});
2017-02-20 19:06:44 +00:00
});
});
2017-02-20 19:06:44 +00:00
it("can play for a specific duration passed in the 'start' method", function(){
2017-02-20 21:45:32 +00:00
return Meter(function(){
2017-02-20 19:06:44 +00:00
var player = new BufferSource(buffer);
player.toMaster();
player.start(0, 0, 0.1);
2017-02-20 19:06:44 +00:00
return function(time){
if (time > 0.1){
expect(player.state).to.equal("stopped");
}
};
}, 0.4).then(function(buffer){
buffer.forEach(function(level, time){
if (time >= 0 && time < 0.1){
expect(level).to.be.greaterThan(0);
} else if (time > 0.1){
expect(level).to.equal(0);
}
});
});
});
2017-02-20 19:06:44 +00:00
it("reports the right state", function(){
return Offline(function(){
var player = new BufferSource(buffer).toMaster();
player.start(0.2).stop(0.4);
2017-02-20 19:06:44 +00:00
return function(time){
if (time >= 0.2 && time < 0.4){
expect(player.state).to.equal("started");
} else {
expect(player.state).to.equal("stopped");
}
2017-02-20 19:06:44 +00:00
};
}, 0.5);
});
it("schedules the onended callback", function(done){
var player = new BufferSource(buffer);
player.start().stop("+0.1");
var wasCalled = false;
player.onended = function(plyr){
expect(plyr).to.equal(player);
wasCalled = true;
};
setTimeout(function(){
expect(wasCalled).to.be.true;
expect(player.state).to.equal("stopped");
player.dispose();
done();
}, 300);
});
2017-02-20 19:06:44 +00:00
it("can be scheduled to stop", function(){
2017-02-20 21:45:32 +00:00
return Meter(function(){
var player = new BufferSource(buffer).toMaster();
player.start(0).stop(0.1);
2017-02-20 19:06:44 +00:00
}, 0.6).then(function(rms){
rms.forEach(function(level, time){
if (time > 0.01 && time < 0.1){
2017-02-20 19:06:44 +00:00
expect(level).to.be.gt(0);
} else if (time > 0.11){
2017-02-20 19:06:44 +00:00
expect(level).to.equal(0);
}
});
2017-02-20 19:06:44 +00:00
});
});
2017-02-20 19:06:44 +00:00
it("can be scheduled to stop with a ramp", function(){
2017-02-20 21:45:32 +00:00
return Meter(function(){
var player = new BufferSource(buffer).toMaster();
player.start(0).stop(0.1, 0.1);
2017-02-20 19:06:44 +00:00
}, 0.6).then(function(rms){
rms.forEach(function(level, time){
if (time > 0.01 && time < 0.2){
2017-02-20 19:06:44 +00:00
expect(level).to.be.gt(0);
} else if (time > 0.21){
2017-02-20 19:06:44 +00:00
expect(level).to.equal(0);
}
});
2017-02-20 19:06:44 +00:00
});
});
2017-02-20 19:06:44 +00:00
it("can be scheduled to start at a lower gain", function(){
return Offline(function(){
var player = new BufferSource(buffer).toMaster();
player.start(0, 0, undefined, 0.5);
2017-02-20 19:06:44 +00:00
}, 0.5).then(function(buffer){
buffer.forEach(function(sample){
expect(sample).to.be.lte(0.5);
});
2017-02-20 19:06:44 +00:00
});
});
2017-02-20 19:06:44 +00:00
it("can be scheduled to start with a ramp", function(){
return Offline(function(){
var player = new BufferSource(buffer).toMaster();
player.start(0, 0, undefined, 1, 0.1);
2017-02-20 19:06:44 +00:00
}, 0.5).then(function(buffer){
buffer.forEach(function(sample, time){
if (time < 0.1){
expect(sample).to.be.lte(time * 10);
}
});
2017-02-20 19:06:44 +00:00
});
});
});
});
});