diff --git a/packages/client/src/components/MidiInfo.svelte b/packages/client/src/components/MidiInfo.svelte index 73e2d238..910e61ee 100644 --- a/packages/client/src/components/MidiInfo.svelte +++ b/packages/client/src/components/MidiInfo.svelte @@ -1,14 +1,39 @@
@@ -25,14 +50,33 @@
- + handleRangeChanged('min', e)} + /> - - + handleRangeChanged('max', e)} + />
+ {#if rangeError} +
{rangeError}
+ {/if}
+
+ +
+ +
+
@@ -41,8 +85,11 @@ .midi-body { display: grid; gap: 0.5rem; - grid-template-columns: 25% 25%; + grid-template-columns: 25% 25% 25%; grid-template-rows: auto; align-items: center; } + .error { + @apply text-xs text-red-500; + } diff --git a/packages/client/src/routes/(site)/+layout.svelte b/packages/client/src/routes/(site)/+layout.svelte index 295758d4..39d25c1a 100644 --- a/packages/client/src/routes/(site)/+layout.svelte +++ b/packages/client/src/routes/(site)/+layout.svelte @@ -3,7 +3,7 @@
-
+
diff --git a/packages/client/src/stores/game.ts b/packages/client/src/stores/game.ts index f28b4d52..ea4a4931 100644 --- a/packages/client/src/stores/game.ts +++ b/packages/client/src/stores/game.ts @@ -39,7 +39,17 @@ export const gameActions = { const range = get(midiRange) console.log('range', range) for (let i = 0; i < amount; i += 1) { - notes.push(range[0] + Math.floor(Math.random() * (range[1] - range[0]))) + let attempts = 0, + val: number = range[1] + // Try having all values unique + while (attempts < 5) { + attempts += 1 + val = range[0] + Math.floor(Math.random() * (range[1] - range[0])) + if (!notes.includes(val)) { + attempts = 5 + } + } + notes.push(val) } const game = new GuessGame(notes) console.log('new game ', notes) diff --git a/packages/client/src/stores/midi.ts b/packages/client/src/stores/midi.ts index 1963398e..6f1d44ef 100644 --- a/packages/client/src/stores/midi.ts +++ b/packages/client/src/stores/midi.ts @@ -25,5 +25,7 @@ export const midiActions = { }) .catch(err => ({ err: err.toString(), code: 403 })) }, - setMidiRange() {} + setMidiRange(range: [number, number]) { + midiRange.set(range) + } } diff --git a/packages/client/src/utils/midi.ts b/packages/client/src/utils/midi.ts index 3d6e2bba..0732eca5 100644 --- a/packages/client/src/utils/midi.ts +++ b/packages/client/src/utils/midi.ts @@ -1,3 +1,5 @@ +import type { Result } from '@/types' + export const C_MAJOR_NOTES = { 0: { note: 'C', steps: 0, sharp: false, flat: false }, 1: { note: 'C♯', steps: 0, sharp: true, flat: false }, @@ -22,3 +24,22 @@ export function getNote(value: number) { const note = C_MAJOR_NOTES[(semitonesFromC0 % 12) as keyof typeof C_MAJOR_NOTES] return { ...note, absolute: `${note.note}${octave}` } } + +export function parseNote(val: string): Result { + if (val.length === 2 || val.length === 3) { + const note = val.slice(0, val.length - 1).toUpperCase() + let octave: number | undefined + try { + octave = parseInt(val[val.length - 1]) + } catch (err) { + return { err: `Couldn't parse note "${val}" octave`, code: 400 } + } + const found = Object.values(C_MAJOR_NOTES).find(n => n.note === note) + if (!found) { + return { err: `Note "${val}" not found in scale`, code: 400 } + } + return { data: 12 + octave * 7 + found.steps } + } else { + return { err: `Unrecognized note "${val}"`, code: 400 } + } +}