Started work on a beat data structure

This commit is contained in:
2021-08-06 21:42:59 +02:00
parent 3eb0eb4437
commit fdfad0b900
4 changed files with 57 additions and 0 deletions

49
src/Beat.ts Normal file
View File

@@ -0,0 +1,49 @@
import BeatUnit from "./BeatUnit";
type BeatInitOptions = {
timeSig: {
up: number,
down: number,
},
bars: number,
};
export default class Beat {
private timeSigUp: number = 4;
private timeSigDown: number = 4;
private units: BeatUnit[] = [];
constructor(options?: BeatInitOptions) {
this.setTimeSignature(options.timeSig.up, options.timeSig.down);
this.setBars(options.bars);
}
setTimeSignature(up: number, down: number): void {
if (Beat.isValidTimeSigRange(up)) {
this.timeSigUp = up | 0;
}
if (Beat.isValidTimeSigRange(down)) {
this.timeSigDown = down | 0;
}
}
setBars(barCount: number): void {
if (barCount*this.timeSigUp < this.units.length) {
this.units.splice(barCount, this.units.length - barCount);
} else if (barCount > this.bars) {
for (let i = 0; i < barCount; i++) {
this.units.push(new BeatUnit());
}
}
this.bars = barCount;
}
stringify(): string {
return "I am a Beat";
}
private static isValidTimeSigRange(sig: number): boolean {
return sig >= 2 && sig <= 64;
}
}