Tone.js/Tone/core/context/OfflineContext.ts

89 lines
2 KiB
TypeScript
Raw Normal View History

import { createOfflineAudioContext } from "../context/AudioContext";
2019-05-23 18:00:49 +00:00
import { Context } from "../context/Context";
import { Seconds } from "../type/Units";
2019-08-19 17:01:37 +00:00
import { isOfflineAudioContext } from "../util/AdvancedTypeCheck";
2019-05-23 18:00:49 +00:00
/**
2019-08-30 16:02:18 +00:00
* Wrapper around the OfflineAudioContext
2019-08-26 17:44:43 +00:00
* @category Core
2019-05-23 18:00:49 +00:00
*/
export class OfflineContext extends Context {
2019-09-04 23:18:44 +00:00
readonly name: string = "OfflineContext";
2019-05-23 18:00:49 +00:00
/**
2019-09-14 20:39:18 +00:00
* A private reference to the duration
2019-05-23 18:00:49 +00:00
*/
private readonly _duration: Seconds;
/**
2019-09-14 20:39:18 +00:00
* An artificial clock source
2019-05-23 18:00:49 +00:00
*/
private _currentTime: Seconds = 0;
/**
* Private reference to the OfflineAudioContext.
*/
protected _context!: OfflineAudioContext;
2019-08-26 17:44:43 +00:00
/**
* @param channels The number of channels to render
* @param duration The duration to render in seconds
* @param sampleRate the sample rate to render at
2019-08-26 17:44:43 +00:00
*/
constructor(
2019-08-19 16:59:07 +00:00
channels: number,
duration: Seconds, sampleRate: number,
2019-08-19 16:59:07 +00:00
);
2019-08-30 16:02:18 +00:00
constructor(context: OfflineAudioContext);
2019-08-19 16:59:07 +00:00
constructor() {
2019-05-23 18:00:49 +00:00
super({
clockSource: "offline",
2019-08-19 16:59:07 +00:00
context: isOfflineAudioContext(arguments[0]) ?
arguments[0] : createOfflineAudioContext(arguments[0], arguments[1] * arguments[2], arguments[2]),
2019-05-23 18:00:49 +00:00
lookAhead: 0,
2019-08-19 16:59:07 +00:00
updateInterval: isOfflineAudioContext(arguments[0]) ?
2019-09-16 03:32:40 +00:00
128 / arguments[0].sampleRate : 128 / arguments[2],
2019-05-23 18:00:49 +00:00
});
2019-08-19 16:59:07 +00:00
this._duration = isOfflineAudioContext(arguments[0]) ?
arguments[0].length / arguments[0].sampleRate : arguments[1];
2019-05-23 18:00:49 +00:00
}
/**
2019-09-14 20:39:18 +00:00
* Override the now method to point to the internal clock time
2019-05-23 18:00:49 +00:00
*/
now(): Seconds {
return this._currentTime;
}
/**
* Same as this.now()
*/
get currentTime(): Seconds {
return this._currentTime;
}
/**
2019-09-14 20:39:18 +00:00
* Render the output of the OfflineContext
2019-05-23 18:00:49 +00:00
*/
render(): Promise<AudioBuffer> {
while (this._duration - this._currentTime >= 0) {
// invoke all the callbacks on that time
this.emit("tick");
// increment the clock in 5ms chunks
2019-08-19 17:01:37 +00:00
this._currentTime += 128 / this.sampleRate;
2019-05-23 18:00:49 +00:00
}
return this._context.startRendering();
}
/**
2019-09-14 20:39:18 +00:00
* Close the context
2019-05-23 18:00:49 +00:00
*/
close(): Promise<void> {
return Promise.resolve();
2019-05-23 18:00:49 +00:00
}
}