Tone.js/test/source/MultiPlayer.js

151 lines
4 KiB
JavaScript
Raw Normal View History

2016-02-08 18:44:44 +00:00
define(["helper/Basic", "Tone/source/MultiPlayer", "helper/Offline", "helper/SourceTests",
2017-02-20 21:45:32 +00:00
"Tone/core/Buffer", "helper/OutputAudioStereo", "helper/Meter"],
2017-02-20 19:06:44 +00:00
function (BasicTests, MultiPlayer, Offline, SourceTests, Buffer, OutputAudioStereo, Meter) {
2016-02-08 18:44:44 +00:00
2016-08-09 05:12:46 +00:00
if (window.__karma__){
Buffer.baseUrl = "/base/test/";
}
2016-02-08 18:44:44 +00:00
describe("MultiPlayer", function(){
var buffer = new Buffer();
beforeEach(function(done){
buffer.load("./audio/sine.wav", function(){
done();
});
});
//run the common tests
BasicTests(MultiPlayer);
context("Constructor", function(){
it ("can be constructed with a Tone.Buffer", function(){
var player = new MultiPlayer({
"buffer" : buffer
});
player.dispose();
});
it ("can be constructed with no arguments", function(){
var player = new MultiPlayer();
player.dispose();
});
it ("can be constructed an options object", function(){
var player = new MultiPlayer({
urls : ["./audio/sine.wav"],
fadeOut : 0.1
2016-02-08 18:44:44 +00:00
});
expect(player.fadeOut).to.equal(0.1);
player.dispose();
2016-02-08 18:44:44 +00:00
});
it("invokes callback when a single buffer is added", function(done){
var player = new MultiPlayer().add("sine", "./audio/sine.wav", function(){
2016-02-08 18:44:44 +00:00
player.dispose();
done();
});
});
});
context("Makes Sound", function(){
2017-02-20 19:06:44 +00:00
it("produces sound in both channels", function(){
return OutputAudioStereo(function(){
var player = new MultiPlayer().add("buffer", buffer);
player.toMaster();
2016-02-08 18:44:44 +00:00
player.start("buffer");
});
});
2017-02-20 19:06:44 +00:00
it("be scheduled to start in the future", function(){
return Offline(function(){
var player = new MultiPlayer().add("buffer", buffer).toMaster();
2016-02-08 18:44:44 +00:00
player.start("buffer", 0.1);
2017-02-20 19:06:44 +00:00
}, 0.3).then(function(buffer){
buffer.forEach(function(sample, time){
if (sample > 0){
expect(time).to.be.at.least(0.099);
2017-02-20 19:06:44 +00:00
}
});
2016-02-08 18:44:44 +00:00
});
});
2017-02-20 19:06:44 +00:00
it("can be repitched", function(){
return Meter(function(){
var player = new MultiPlayer().add("buffer", buffer).toMaster();
player.start("buffer", 0, 0, 0.3, -1);
2017-02-20 19:06:44 +00:00
}, 0.3).then(function(buffer){
buffer.forEach(function(level, time){
if (time > 0){
expect(level).to.be.at.least(0.1);
}
});
});
});
2017-02-20 19:06:44 +00:00
it("can be played at a different gain", function(){
return Offline(function(){
var player = new MultiPlayer().add("buffer", buffer).toMaster();
player.start("buffer", 0, 0, 0.3, 0, 0.1);
2017-02-20 19:06:44 +00:00
}, 0.3).then(function(buffer){
expect(buffer.max()).to.be.at.most(0.1);
});
});
2017-02-20 19:06:44 +00:00
it("can be stopped", function(){
return Meter(function(){
var player = new MultiPlayer().add("buffer", buffer);
player.toMaster();
player.start("buffer", 0).stop("buffer", 0.1);
2017-02-20 19:06:44 +00:00
}, 0.3).then(function(rms){
rms.forEach(function(level, time){
if (time > 0 && time < 0.09){
2017-02-20 19:06:44 +00:00
expect(level).to.be.at.least(0.1);
} else if (time > 0.11){
expect(level).to.equal(0);
}
});
});
});
2017-02-20 19:06:44 +00:00
it("can stop all sources", function(){
return Meter(function(){
var player = new MultiPlayer().add("buffer", buffer);
player.toMaster();
player.start("buffer", 0).start("buffer", 0.02).stopAll(0.1);
2017-02-20 19:06:44 +00:00
}, 0.3).then(function(rms){
rms.forEach(function(level, time){
if (time > 0 && time < 0.09){
2017-02-20 19:06:44 +00:00
expect(level).to.be.at.least(0.1);
} else if (time > 0.12){
expect(level).to.equal(0);
}
});
});
});
2017-02-20 19:06:44 +00:00
it("can start and stop a loop", function(){
var stopTime = buffer.duration * 1.1;
return Meter(function(){
var player = new MultiPlayer().add("buffer", buffer);
2017-02-20 19:06:44 +00:00
player.toMaster();
player.startLoop("buffer", 0).stop("buffer", stopTime);
2017-02-20 19:06:44 +00:00
}, buffer.duration * 1.5).then(function(rms){
rms.forEach(function(level, time){
if (time > 0 && time < stopTime - 0.01){
2017-02-20 19:06:44 +00:00
expect(level).to.be.at.least(0.1);
} else if (time > stopTime + 0.01){
2017-02-20 19:06:44 +00:00
expect(level).to.equal(0);
}
});
2017-02-20 19:06:44 +00:00
});
});
2016-02-08 18:44:44 +00:00
});
});
});