Tone.js/Tone/core/context/ToneAudioBuffers.test.ts

177 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-07-22 16:29:50 +00:00
import { expect } from "chai";
import { ToneAudioBuffer } from "./ToneAudioBuffer.js";
import { ToneAudioBuffers } from "./ToneAudioBuffers.js";
2019-07-22 16:29:50 +00:00
const testFile = "./test/audio/sine.wav";
const testFile2 = "./test/audio/hh.wav";
2019-07-22 16:29:50 +00:00
describe("ToneAudioBuffers", () => {
it("can be created and disposed", () => {
const buff = new ToneAudioBuffers();
buff.dispose();
});
it("loads a file from an object string", (done) => {
const buffer = new ToneAudioBuffers(
{
sine: testFile,
},
() => {
expect(buffer).to.be.instanceof(ToneAudioBuffers);
buffer.dispose();
done();
}
);
2019-07-22 16:29:50 +00:00
});
it("can get a buffer loaded from an object", (done) => {
const buffer = new ToneAudioBuffers(
{
kick: testFile2,
sine: testFile,
},
() => {
expect(buffer.get("kick")).to.be.instanceof(ToneAudioBuffer);
buffer.dispose();
done();
}
);
2019-07-22 16:29:50 +00:00
});
it("throws an error when it tries to get an object that doesnt exist", (done) => {
const buffer = new ToneAudioBuffers(
{
sine: testFile,
},
() => {
expect(() => {
buffer.get("nope");
}).throws(Error);
buffer.dispose();
done();
}
);
2019-07-22 16:29:50 +00:00
});
it("tests if it has a buffer", (done) => {
const buffer = new ToneAudioBuffers(
{
kick: testFile2,
sine: testFile,
},
() => {
expect(buffer.has("kick")).to.be.true;
expect(buffer.has("sine")).to.be.true;
expect(buffer.has("nope")).to.be.false;
buffer.dispose();
done();
}
);
2019-07-22 16:29:50 +00:00
});
it("can pass in buffers as object and options object in second arg", (done) => {
2019-07-22 16:29:50 +00:00
const buffer = new ToneAudioBuffers({
baseUrl: "./test/audio/",
2019-07-22 16:29:50 +00:00
onload(): void {
expect(buffer.has("sine")).to.be.true;
buffer.dispose();
done();
},
urls: {
2019-09-16 03:32:40 +00:00
sine: "sine.wav",
2019-07-22 16:29:50 +00:00
},
});
});
it("invokes onerror if it cant load the url", (done) => {
const buffer = new ToneAudioBuffers({
onerror(): void {
buffer.dispose();
done();
},
urls: {
test: "nosuchfile.wav",
},
});
});
it("reports itself as loaded", (done) => {
const buffer = new ToneAudioBuffers(
{
kick: testFile2,
sine: testFile,
},
() => {
expect(buffer.loaded).to.be.true;
buffer.dispose();
done();
}
);
2019-07-22 16:29:50 +00:00
expect(buffer.loaded).to.be.false;
});
it("can load from a base url", (done) => {
const buffer = new ToneAudioBuffers(
{
hat: "hh.wav",
},
() => {
expect(buffer.get("hat")).to.be.instanceof(ToneAudioBuffer);
buffer.dispose();
done();
},
"./test/audio/"
);
2019-07-22 16:29:50 +00:00
});
it("can add a buffer", (done) => {
2019-07-22 16:29:50 +00:00
const buffer = new ToneAudioBuffers();
buffer.add("name", testFile, () => {
expect(buffer.get("name")).to.be.instanceof(ToneAudioBuffer);
buffer.dispose();
done();
});
});
it("can add a buffer url", (done) => {
2019-07-22 16:29:50 +00:00
const buffer = new ToneAudioBuffers();
buffer.add("name", testFile, () => {
expect(buffer.get("name")).to.be.instanceof(ToneAudioBuffer);
buffer.dispose();
done();
});
});
it("throws an error if no buffer exists with that name or index", () => {
const buffer = new ToneAudioBuffers();
expect(() => {
buffer.get("nope");
}).to.throw(Error);
buffer.dispose();
});
it("can add a ToneAudioBuffer", () => {
2019-07-22 16:29:50 +00:00
const buff = new ToneAudioBuffer();
const buffer = new ToneAudioBuffers();
buffer.add("name", buff);
expect(buffer.get("name").get()).to.equal(buff.get());
2019-07-22 16:29:50 +00:00
});
it("can add an AudioBuffer", (done) => {
2019-07-22 16:29:50 +00:00
ToneAudioBuffer.load(testFile).then((buff) => {
const buffer = new ToneAudioBuffers();
buffer.add("name", buff);
expect(buffer.get("name").get()).to.equal(buff);
done();
});
});
it("can be constructed with ToneAudioBuffers", () => {
2019-07-22 16:29:50 +00:00
const buff = new ToneAudioBuffer();
const buffer = new ToneAudioBuffers({
buff,
});
expect(buffer.get("buff").get()).to.equal(buff.get());
2019-07-22 16:29:50 +00:00
});
});