Started work on a beat data structure
This commit is contained in:
49
src/Beat.ts
Normal file
49
src/Beat.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/BeatUnit.ts
Normal file
8
src/BeatUnit.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export default class BeatUnit {
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
stringify(): string {
|
||||||
|
return "U";
|
||||||
|
}
|
||||||
|
}
|
||||||
0
src/tests.ts
Normal file
0
src/tests.ts
Normal file
0
src/ui/DrumRows.svelte
Normal file
0
src/ui/DrumRows.svelte
Normal file
Reference in New Issue
Block a user