Skip to content

Commit

Permalink
Add a box that automatically shifts notes up or down octaves if they'…
Browse files Browse the repository at this point in the history
…re out of range
  • Loading branch information
reimu committed Oct 5, 2023
1 parent b302ab2 commit 99d7447
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
Halfsteps to shift by
<input id="shift" type="number" value="0" />
</label>
<label>
Autoshift if outside of range
<input id="autoshift" type="checkbox" />
</label>
<span id="error"> </span>
</section>
<section id="notes">
Expand Down
22 changes: 14 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const inputEle = document.getElementById("input") as HTMLTextAreaElement;
const stepsEle = document.getElementById("shift") as HTMLInputElement;
const outputEle = document.getElementById("output") as HTMLOutputElement;
const errorEle = document.getElementById("error") as HTMLSpanElement;
const autoshiftEle = document.getElementById("autoshift") as HTMLInputElement;

function t(notes: string, steps: number): string {
// TODO: handle extended keys
Expand All @@ -81,16 +82,21 @@ function t(notes: string, steps: number): string {
`detected invalid key: ${key}` +
`, at position: ${i};${notes.slice(i - 5, i + 5)}`
);
else
return key; // probably a non-note symbol
const newPos = pos + steps;
else return key; // probably a non-note symbol
let newPos = pos + steps;
if (newPos < 0 || newPos >= keys.length) {
throw new Error(
`detected key outside of range: ${key}` +
`, at position: ${i};${notes.slice(i - 5, i + 5)}`
);
if (!autoshiftEle.checked)
throw new Error(
`detected key outside of range: ${key}` +
`, at position: ${i};${notes.slice(i - 5, i + 5)}`
);
else {
while (newPos < 0 || newPos >= keys.length) {
newPos += 16 * (steps > 0 ? -1 : 1);
}
}
}
return keys[pos + steps];
return keys[newPos];
})
.join("");

Expand Down
2 changes: 1 addition & 1 deletion src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ section {
}

.notes {
width: 50%;
flex: 50%;
}

#toolbar {
Expand Down

0 comments on commit 99d7447

Please sign in to comment.