UNPKG

@azgaar/tone

Version:

A fork of Web Audio framework for making interactive music in the browser.

52 lines 1.87 kB
import { __awaiter } from "tslib"; import { TestAudioBuffer } from "@tonejs/plot"; import { OfflineContext } from "Tone/core/context/OfflineContext"; import { getContext, setContext } from "Tone/core/Global"; import { isArray, isFunction } from "Tone/core/util/TypeCheck"; export function Offline(callback, duration = 0.1, channels = 1, sampleRate = 44100) { return __awaiter(this, void 0, void 0, function* () { const originalContext = getContext(); const offline = new OfflineContext(channels, duration + 1 / sampleRate, sampleRate); setContext(offline); try { let retFunction = callback(offline); if (retFunction instanceof Promise) { retFunction = yield retFunction; } if (isFunction(retFunction)) { const fn = retFunction; offline.on("tick", () => fn(offline.now())); } else if (isArray(retFunction)) { // each element in the array is a timing callback retFunction.forEach(fn => { offline.on("tick", () => fn(offline.now())); }); } } catch (e) { throw e; } finally { setContext(originalContext); const buffer = yield offline.render(); return new TestAudioBuffer(buffer.get()); } }); } export function whenBetween(value, start, stop, callback) { if (value >= start && value < stop) { callback(); } } // invoked only once export function atTime(when, callback) { let wasInvoked = false; return (time) => { if (time >= when && !wasInvoked) { callback(time); wasInvoked = true; } }; } //# sourceMappingURL=Offline.js.map