UNPKG

musictime

Version:

Convert musical timings (bars, beats, 16ths) from and to seconds.

150 lines (149 loc) 4.54 kB
/** * Holds sixteenthsPerBeat and beatsPerBar values. */ export interface ITimeConfig { beatsPerBar: number; sixteenthsPerBeat: number; } /** * Defines the place on the bars, beats, sixteenths grid. */ export interface IBarsBeatsSixteenths { /** The amount of bars (measures). Will always be an integer. */ bars: number; /** The amount of beats. Will always be an integer, and never equal or exceed the beatsPerBar setting. */ beats: number; /** The amount of sixteenths. Will always be an integer. */ sixteenths: number; /** The remainder (as fraction of a sixteenth) when a time does not end up exactly on the sixteenth-grid. Will be either exactly 0 or a float between 0 and 1.*/ remainingSixteenths: number; } /** * Checks * @param {string} value * @returns {boolean} */ export declare const stringIsValid: (value: string) => boolean; export default class MusicTime { private _beats; private _timeConfig; constructor(bars?: number, beats?: number, sixteenths?: number, timeConfig?: ITimeConfig); /** * Returns an object with bars, beats & sixteenths, based on the current timeConfig. * @returns {IBarsBeatsSixteenths} */ getBarsBeatsSixteenths(): IBarsBeatsSixteenths; /** * Returns the current timeConfig (beatsPerBar and sixteenthsPerBeat). * @returns {ITimeConfig} */ getTimeConfig(): ITimeConfig; /** * Sets the timeConfig. * @param {ITimeConfig} value */ setTimeConfig(value: ITimeConfig): void; /** * Convert to time in seconds, based on a given amount of beats per minute. * @param bpm * @returns {number} */ toTime(bpm: number): number; /** * Adds a musicTime to the instance. * @param {MusicTime} time * @returns {MusicTime} */ add(time: MusicTime): MusicTime; /** * Subtracts a musicTime from the instance. * @param {MusicTime} time * @returns {MusicTime} */ subtract(time: MusicTime): MusicTime; /** * Multiplies a musicTime with a scalar. * @param {number} value * @returns {MusicTime} */ multiply(value: number): MusicTime; /** * Check if two instances are equal. * @param {MusicTime} time * @returns {MusicTime} */ equals(time: MusicTime): boolean; /** * Create a clone of the instance. * @returns {MusicTime} */ clone(): MusicTime; /** * Returns the total amount of bars (can be a float). * @returns {number} */ getTotalBars(): number; /** * Returns the total amount of beats (can be a float). * @returns {number} */ getTotalBeats(): number; /** * Returns the total amount of sixteenths (can be a float). * @returns {number} */ getTotalSixteenths(): number; /** * Returns a readable string. * @returns {string} */ toString(): string; /** * Returns the amount of beats. * @returns {number} */ valueOf(): number; /** * Subtracts two MusicTimes. The timeConfig of the first time will be used in the result. * @param {MusicTime} time1 * @param {MusicTime} time2 * @returns {MusicTime} */ static subtract(time1: MusicTime, time2: MusicTime): MusicTime; /** * Returns the sum of two times. The timeConfig of the first time will be used in the result. * @param {MusicTime} time1 * @param {MusicTime} time2 * @returns {MusicTime} */ static add(time1: MusicTime, time2: MusicTime): MusicTime; /** * Multiplies the time with a value. The time's timeConfig will be used in the result. * @param {MusicTime} time * @param {number} value * @returns {MusicTime} */ static multiply(time: MusicTime, value: number): MusicTime; /** * Creates an instance from a string. * @param value * @param timeConfig * @returns {MusicTime} */ static fromString(value: string, timeConfig?: ITimeConfig): MusicTime; /** * Check if two times are equal. * @param {MusicTime} time1 * @param {MusicTime} time2 * @returns {boolean} */ static equals(time1: MusicTime, time2: MusicTime): boolean; /** * Creates a MusicTime instance from an amount of seconds. * @param {number} seconds * @param {number} bpm * @param {ITimeConfig} timeConfig * @returns {MusicTime} */ static fromTime(seconds: number, bpm: number, timeConfig?: ITimeConfig): MusicTime; }