From 127072709852a9f156abe850c4004c7c2ba32119 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Mon, 15 Aug 2016 10:22:50 -0400 Subject: [PATCH] testing to/from Array methods --- test/core/Buffer.js | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/test/core/Buffer.js b/test/core/Buffer.js index df5a7c3c..fa8e5c6a 100644 --- a/test/core/Buffer.js +++ b/test/core/Buffer.js @@ -29,6 +29,22 @@ define(["Test", "Tone/core/Buffer"], function (Test, Buffer) { }); }); + 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.equal(132301); + buffer.dispose(); + done(); + }); + }); + it("the static on('load') method is invoked", function(done){ var buffer = new Buffer(testFile); Buffer.on("load", function(){ @@ -123,5 +139,58 @@ define(["Test", "Tone/core/Buffer"], function (Test, Buffer) { done(); }); }); + + 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(); + }); + }); }); }); \ No newline at end of file