Tone.js/test/core/Buffer.js

197 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-08-17 02:21:14 +00:00
define(["Test", "Tone/core/Buffer"], function (Test, Buffer) {
2016-03-04 21:25:44 +00:00
if (window.__karma__){
Buffer.baseUrl = "/base/test/";
}
2015-08-17 02:21:14 +00:00
2016-03-05 05:07:17 +00:00
var testFile = "./audio/sine.wav";
2015-08-17 02:21:14 +00:00
describe("Buffer", function(){
it ("can be created and disposed", function(){
2016-03-05 05:07:17 +00:00
var buff = new Buffer(testFile);
2015-08-17 02:21:14 +00:00
buff.dispose();
Test.wasDisposed(buff);
});
it("loads a file from a url string", function(done){
2016-03-05 05:07:17 +00:00
var buffer = new Buffer(testFile, function(buff){
2015-08-17 02:21:14 +00:00
expect(buff).to.be.instanceof(Buffer);
buffer.dispose();
done();
});
});
it("has a duration", function(done){
2016-03-05 05:07:17 +00:00
var buffer = new Buffer(testFile, function(){
expect(buffer.duration).to.be.closeTo(3, 0.01);
2015-08-17 02:21:14 +00:00
buffer.dispose();
done();
});
});
2016-08-15 14:22:50 +00:00
it("can get the number of channels", function(done){
var buffer = new Buffer(testFile, function(){
expect(buffer.numberOfChannels).to.be.equal(1);
buffer.dispose();
done();
});
});
it("can get the length of the buffer", function(done){
var buffer = new Buffer(testFile, function(){
expect(buffer.length).to.be.a.number;
expect(buffer.length).to.be.above(130000);
2016-08-15 14:22:50 +00:00
buffer.dispose();
done();
});
});
2015-11-11 17:33:11 +00:00
it("the static on('load') method is invoked", function(done){
2016-03-05 05:07:17 +00:00
var buffer = new Buffer(testFile);
2015-11-11 17:33:11 +00:00
Buffer.on("load", function(){
2015-08-17 02:21:14 +00:00
buffer.dispose();
2015-11-11 17:33:11 +00:00
Buffer.off("load");
2015-08-17 02:21:14 +00:00
done();
2015-11-11 17:33:11 +00:00
});
2015-08-17 02:21:14 +00:00
});
it("can be constructed with an options object", function(done){
var buffer = new Buffer({
2016-03-05 05:07:17 +00:00
"url" : testFile,
2015-08-17 02:21:14 +00:00
"reverse" : true,
"onload" : function(){
buffer.dispose();
done();
}
});
});
it("takes an AudioBuffer in the constructor method", function(done){
var buffer = new Buffer({
2016-03-05 05:07:17 +00:00
"url" : testFile,
2015-08-17 02:21:14 +00:00
"onload" : function(){
var testOne = new Buffer(buffer.get());
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
2015-10-21 17:52:03 +00:00
it("takes a Tone.Buffer in the constructor method", function(done){
var buffer = new Buffer({
2016-03-05 05:07:17 +00:00
"url" : testFile,
2015-10-21 17:52:03 +00:00
"onload" : function(){
var testOne = new Buffer(buffer);
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
2015-08-17 02:21:14 +00:00
it("takes Tone.Buffer in the set method", function(done){
var buffer = new Buffer({
2016-03-05 05:07:17 +00:00
"url" : testFile,
2015-08-17 02:21:14 +00:00
"onload" : function(){
2016-03-05 05:07:17 +00:00
var testOne = new Buffer(testFile);
2015-08-17 02:21:14 +00:00
testOne.set(buffer);
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("takes AudioBuffer in the set method", function(done){
var buffer = new Buffer({
2016-03-05 05:07:17 +00:00
"url" : testFile,
2015-08-17 02:21:14 +00:00
"onload" : function(){
2016-03-05 05:07:17 +00:00
var testOne = new Buffer(testFile);
2015-08-17 02:21:14 +00:00
testOne.set(buffer.get());
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
2015-11-11 17:33:11 +00:00
it("the static on('progress') method is invoked", function(done){
2016-03-05 05:07:17 +00:00
var buffer = new Buffer(testFile);
2015-11-11 17:33:11 +00:00
Buffer.on("progress", function(percent){
expect(percent).to.be.a.number;
Buffer.off("progress");
2015-08-17 02:21:14 +00:00
buffer.dispose();
done();
});
});
it("can reverse a buffer", function(done){
2016-03-05 05:07:17 +00:00
var buffer = new Buffer(testFile, function(){
2015-08-17 02:21:14 +00:00
var buffArray = buffer.get();
var lastSample = buffArray[buffArray.length - 1];
buffer.reverse = true;
expect(buffer.get()[0]).to.equal(lastSample);
buffer.dispose();
done();
});
});
2016-08-15 14:22:50 +00:00
it("can convert from an array", function(){
var buffer = new Buffer();
var arr = new Float32Array(0.5 * buffer.context.sampleRate);
arr[0] = 0.5;
buffer.fromArray(arr);
expect(buffer.duration).to.equal(0.5);
expect(buffer.numberOfChannels).to.equal(1);
//test the first sample of the first channel to see if it's the same
expect(buffer.get().getChannelData(0)[0]).to.equal(0.5);
buffer.dispose();
});
it("can convert from a multidimentional array", function(){
var buffer = new Buffer();
var arr = [new Float32Array(0.5 * buffer.context.sampleRate), new Float32Array(0.5 * buffer.context.sampleRate)];
arr[0][0] = 0.5;
buffer.fromArray(arr);
expect(buffer.duration).to.equal(0.5);
expect(buffer.numberOfChannels).to.equal(2);
expect(buffer.get().getChannelData(0)[0]).to.equal(0.5);
buffer.dispose();
});
it("can convert to and from an array", function(){
var buffer = new Buffer();
var arr = [new Float32Array(0.5 * buffer.context.sampleRate), new Float32Array(0.5 * buffer.context.sampleRate)];
arr[0][0] = 0.5;
buffer.fromArray(arr);
expect(buffer.get().getChannelData(0)[0]).to.equal(0.5);
expect(buffer.toArray()[0][0]).to.equal(0.5);
//with a selected channel
expect(buffer.toArray(0)[0]).to.equal(0.5);
buffer.dispose();
});
it("can slice a portion of the array", function(done){
var buffer = new Buffer(testFile, function(){
//original duration
expect(buffer.duration).to.be.closeTo(3, 0.01);
var sliced1 = buffer.slice(0, 1);
//does not modify the original
expect(buffer.duration).to.be.closeTo(3, 0.01);
expect(sliced1.duration).to.be.closeTo(1, 0.01);
var sliced2 = sliced1.slice(0.5);
expect(sliced2.duration).to.be.closeTo(0.5, 0.01);
buffer.dispose();
sliced1.dispose();
sliced2.dispose();
done();
});
});
2015-08-17 02:21:14 +00:00
});
});