Skip to content

Commit bd72d60

Browse files
committed
move scale note utils to utils and implement ScaleInfo
1 parent 321f41f commit bd72d60

6 files changed

Lines changed: 41 additions & 31 deletions

File tree

src/frets/index.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import { Interval, Note, Scale } from "tonal";
2-
3-
function getScaleNoteIntervalMap(scale) {
4-
return scale.notes.reduce((map, note, index) => {
5-
map[note] = scale.intervals[index];
6-
return map;
7-
}, {});
8-
}
9-
10-
// Create a map of note chromas to scale note names for label normalization
11-
function getScaleNoteLabelMap(scale) {
12-
return scale.notes.reduce((map, note) => {
13-
const noteObj = Note.get(note);
14-
map[noteObj.chroma] = note;
15-
return map;
16-
}, {});
17-
}
2+
import { getScaleNoteIntervalMap, getScaleNoteLabelMap } from '$lib/utils';
183

194
export default function frets(
205
tuning = ["E2", "A2", "D3", "G3", "B3", "E4"],

src/lib/FretBoard.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { onMount } from "svelte";
33
import { scaleBand, scalePoint, scaleLinear } from "d3-scale";
44
import { range } from "d3-array";
5-
import Note from "./Note.svelte";
5+
import FretNote from "./FretNote.svelte";
66
77
let { fretData } = $props();
88
@@ -58,7 +58,7 @@
5858
<g
5959
transform="translate({fretX(stringNote.fret) +
6060
(fretX.bandwidth() / 2 - margin.left)}, 0)">
61-
<Note note={stringNote} />
61+
<FretNote note={stringNote} />
6262
</g>
6363
{/each}
6464
</g>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<script>
22
let { note } = $props();
3+
4+
$effect(() => {
5+
console.log("FretNote note:", note);
6+
});
37
</script>
48

59
<g

src/lib/ScaleInfo.svelte

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22
import { onMount } from "svelte";
33
import { scalePoint, range } from "d3";
44
import { Note } from "tonal";
5+
import FretNote from "./FretNote.svelte";
56
67
let { scale } = $props();
78
9+
let notes = $derived(
10+
scale.notes.map((note, index) => {
11+
const noteObj = Note.get(note);
12+
return {
13+
...noteObj,
14+
label: noteObj.name.replace("b", "").replace("#", ""),
15+
interval: scale.intervals[index],
16+
};
17+
}),
18+
);
19+
820
let container = $state(null);
921
let width = $state(0);
1022
let height = $state(45);
@@ -16,8 +28,6 @@
1628
.range([margin.left, width - margin.right]),
1729
);
1830
19-
const notes = $derived(scale.notes.map((n) => Note.get(n)));
20-
2131
onMount(() => {
2232
width = container.clientWidth;
2333
height = 45;
@@ -28,21 +38,13 @@
2838
<svg class="h-12" viewBox="0 0 {width} {height}">
2939
{#each notes as note, i}
3040
<g transform="translate({dotX(i)}, {20})">
31-
<circle
32-
r="12"
33-
class={note.interval === "1P" ? "fill-red-500" : "fill-green-500"} />
34-
<text
35-
text-anchor="middle"
36-
dy="4"
37-
font-size="10"
38-
class="text-white"
39-
fill="currentColor">{note.name}</text>
41+
<FretNote {note} />
4042
<text
4143
text-anchor="middle"
4244
dy="25"
4345
font-size="10"
4446
class="text-black"
45-
fill="currentColor">{scale.intervals[i]}</text>
47+
fill="currentColor">{note.interval}</text>
4648
</g>
4749
{/each}
4850
</svg>

src/lib/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
import { Note } from "tonal";
2+
13
export const delay = (duration, value) => {
24
return new Promise(function (resolve) {
35
setTimeout(function () {
46
resolve(value);
57
}, duration);
68
});
79
};
10+
11+
// Create a map of note names to their scale intervals
12+
export function getScaleNoteIntervalMap(scale) {
13+
return scale.notes.reduce((map, note, index) => {
14+
map[note] = scale.intervals[index];
15+
return map;
16+
}, {});
17+
}
18+
19+
// Create a map of note chromas to scale note names for label normalization
20+
export function getScaleNoteLabelMap(scale) {
21+
return scale.notes.reduce((map, note) => {
22+
const noteObj = Note.get(note);
23+
map[noteObj.chroma] = note;
24+
return map;
25+
}, {});
26+
}

src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<div>
3434
<ScaleSelector bind:value={scale} />
3535
</div>
36-
<div class="flex-1">
36+
<div class="">
3737
<ScaleInfo scale={scaleObj} />
3838
</div>
3939
</div>

0 commit comments

Comments
 (0)