From fdfad0b900855cc86bd0efef148f33a8c1037729 Mon Sep 17 00:00:00 2001 From: dLedda Date: Fri, 6 Aug 2021 21:42:59 +0200 Subject: [PATCH] Started work on a beat data structure --- src/Beat.ts | 49 ++++++++++++++++++++++++++++++++++++++++++ src/BeatUnit.ts | 8 +++++++ src/tests.ts | 0 src/ui/DrumRows.svelte | 0 4 files changed, 57 insertions(+) create mode 100644 src/Beat.ts create mode 100644 src/BeatUnit.ts create mode 100644 src/tests.ts create mode 100644 src/ui/DrumRows.svelte diff --git a/src/Beat.ts b/src/Beat.ts new file mode 100644 index 0000000..88d24cc --- /dev/null +++ b/src/Beat.ts @@ -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; + } +} \ No newline at end of file diff --git a/src/BeatUnit.ts b/src/BeatUnit.ts new file mode 100644 index 0000000..b0b4677 --- /dev/null +++ b/src/BeatUnit.ts @@ -0,0 +1,8 @@ +export default class BeatUnit { + constructor() { + } + + stringify(): string { + return "U"; + } +} diff --git a/src/tests.ts b/src/tests.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/ui/DrumRows.svelte b/src/ui/DrumRows.svelte new file mode 100644 index 0000000..e69de29