echogarden
Version:
An easy-to-use speech toolset. Includes tools for synthesis, recognition, alignment, speech translation, language detection, source separation and more.
61 lines • 1.99 kB
JavaScript
import { logToStderr, roundToDigits } from './Utilities.js';
export class Timer {
startTime = 0;
constructor() {
this.restart();
}
restart() {
this.startTime = Timer.currentTime;
}
get elapsedTime() {
// Elapsed time (milliseconds)
return Timer.currentTime - this.startTime;
}
get elapsedTimeSeconds() {
// Elapsed time (seconds)
return this.elapsedTime / 1000;
}
getElapsedTimeAndRestart() {
const elapsedTime = this.elapsedTime;
this.restart();
return elapsedTime;
}
logAndRestart(title, timePrecision = 3) {
const elapsedTime = this.elapsedTime;
//
const message = `${title}: ${roundToDigits(elapsedTime, timePrecision)}ms`;
logToStderr(message);
//
this.restart();
return elapsedTime;
}
static get currentTime() {
if (!this.getTimestamp) {
this.createTimestampFunction();
}
return this.getTimestamp();
}
static get microsecondTimestamp() {
return Math.floor(Timer.currentTime * 1000);
}
static createTimestampFunction() {
if (typeof process === 'object' && typeof process.hrtime === 'function') {
let baseTimestamp = 0;
this.getTimestamp = () => {
const nodeTimeNanoSeconds = process.hrtime.bigint();
const nodeTimeMilliseconds = Number(nodeTimeNanoSeconds) / 1_000_000;
return baseTimestamp + nodeTimeMilliseconds;
};
baseTimestamp = Date.now() - this.getTimestamp();
}
else if (typeof performance === 'object' && performance.now) {
const baseTimestamp = Date.now() - performance.now();
this.getTimestamp = () => baseTimestamp + performance.now();
}
else {
this.getTimestamp = () => Date.now();
}
}
static getTimestamp;
}
//# sourceMappingURL=Timer.js.map