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

80 lines
2.3 KiB
TypeScript
Raw Normal View History

import { TestAudioBuffer } from "@tonejs/plot";
2019-06-17 18:04:17 +00:00
import { expect } from "chai";
import { ToneOscillatorNode } from "Tone/source/oscillator/OscillatorNode";
2019-06-17 18:04:17 +00:00
import { noOp } from "../util/Interface";
import { Offline } from "./Offline";
import { ToneAudioBuffer } from "./ToneAudioBuffer";
describe("Offline", () => {
it("accepts a callback and a duration", () => {
return Offline(noOp, 0.01);
2019-06-17 18:04:17 +00:00
});
it("returns a promise", () => {
const ret = Offline(noOp, 0.01);
expect(ret).to.have.property("then");
return ret;
2019-06-17 18:04:17 +00:00
});
it("generates a buffer", () => {
return Offline(noOp, 0.01).then((buffer) => {
2019-06-17 18:04:17 +00:00
expect(buffer).to.be.instanceOf(ToneAudioBuffer);
});
});
it("silent by default", () => {
return Offline(noOp, 0.01, 1).then((buffer) => {
2019-06-17 18:04:17 +00:00
const isSilent = buffer.toArray().every(sample => sample === 0);
expect(isSilent).to.equal(true);
});
});
it("records the master output", () => {
return Offline(() => {
new ToneOscillatorNode().toDestination().start();
}, 0.01).then((buffer) => {
const testBuff = new TestAudioBuffer(buffer.get() as AudioBuffer);
expect(testBuff.isSilent()).is.equal(false);
});
});
2019-06-17 18:04:17 +00:00
it("returning a promise defers the rendering till the promise resolves", () => {
let wasInvoked = false;
return Offline(() => {
new ToneOscillatorNode().toDestination().start();
return new Promise((done) => {
setTimeout(done, 100);
}).then(() => {
wasInvoked = true;
});
}, 0.01).then((buffer) => {
const testBuff = new TestAudioBuffer(buffer.get() as AudioBuffer);
expect(wasInvoked).is.equal(true);
expect(testBuff.isSilent()).to.equal(false);
});
});
2019-06-17 18:04:17 +00:00
it("can schedule specific timing outputs", () => {
return Offline(() => {
new ToneOscillatorNode().toDestination().start(0.05);
}, 0.1).then((buffer) => {
const testBuff = new TestAudioBuffer(buffer.get() as AudioBuffer);
expect(testBuff.getTimeOfFirstSound()).to.be.closeTo(0.05, 0.0001);
});
});
2019-06-17 18:04:17 +00:00
// it("can schedule Transport events", () => {
// return Offline(function (Transport) {
// var osc = new Oscillator().toDestination();
2019-06-17 18:04:17 +00:00
// Transport.schedule(function (time) {
// osc.start(time);
// }, 0.05);
// Transport.start(0);
// }, 0.1).then((buffer) => {
// BufferTest(buffer);
// expect(buffer.getFirstSoundTime()).to.be.closeTo(0.05, 0.001);
// });
// });
});