Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Visualizer for when a Note Falls out of Range #2

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/core/PianoRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ let overlayCtx: CanvasRenderingContext2D;
const notePositions: Map<Audio.Note, { x: number; y: number }> = new Map();
const noteSize = 0.7;

let drawOutOfRange: boolean = true;
const outOfRangeNoteSize = 10;

let whiteKeyWidth: number;
let blackKeyWidth: number;

Expand Down Expand Up @@ -80,9 +83,51 @@ export function drawKeys(from: Audio.Note, to: Audio.Note) {
}
}

export function drawNote(note: Audio.Note, volume: number, color: string) {
export function drawNote(
note: Audio.Note,
range: [Audio.Note, Audio.Note],
volume: number,
color: string
) {
const middlePos = notePositions.get(note);
if (!middlePos) {
if (!drawOutOfRange) return;
overlayCtx.fillStyle = color;
if (note < range[0]) {
//Path for a Triangle ◀
overlayCtx.beginPath();
overlayCtx.moveTo(outOfRangeNoteSize, overlayCanvas.height - 2);
overlayCtx.lineTo(
outOfRangeNoteSize,
overlayCanvas.height - 2 - outOfRangeNoteSize
);
overlayCtx.lineTo(
0,
overlayCanvas.height - 2 - outOfRangeNoteSize / 2
);
overlayCtx.lineTo(outOfRangeNoteSize, overlayCanvas.height - 2);
} else if (note > range[1]) {
//Path for a Triangle ▶
overlayCtx.beginPath();
overlayCtx.moveTo(
overlayCanvas.width - outOfRangeNoteSize,
overlayCanvas.height - 2
);
overlayCtx.lineTo(
overlayCanvas.width - outOfRangeNoteSize,
overlayCanvas.height - 2 - outOfRangeNoteSize
);
overlayCtx.lineTo(
overlayCanvas.width,
overlayCanvas.height - 2 - outOfRangeNoteSize / 2
);
overlayCtx.lineTo(
overlayCanvas.width - outOfRangeNoteSize,
overlayCanvas.height - 2
);
}
overlayCtx.fill();
overlayCtx.closePath();
return;
}

Expand Down Expand Up @@ -142,3 +187,7 @@ export function resize(yPos: number, width: number, height: number) {
overlayCanvas.height = height;
overlayCanvas.style.top = `${yPos}px`;
}

export function setDrawOutOfRange(value: boolean) {
drawOutOfRange = value;
}
23 changes: 23 additions & 0 deletions src/core/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function render() {

PianoRenderer.drawNote(
Math.round(note.note),
noteRange,
note.volume,
colors[i]
);
Expand All @@ -110,6 +111,28 @@ export function alignNotesToPiano(value: boolean) {
TimelineRenderer.alignNotesToPiano(value);
}

export function setOutOfRangeBehaviour(value: string) {
switch (value) {
default:
case "On Keys":
PianoRenderer.setDrawOutOfRange(true);
TimelineRenderer.setDrawOutOfRange(false);
break;
case "On Timeline":
PianoRenderer.setDrawOutOfRange(false);
TimelineRenderer.setDrawOutOfRange(true);
break;
case "On Both":
PianoRenderer.setDrawOutOfRange(true);
TimelineRenderer.setDrawOutOfRange(true);
break;
case "Off":
PianoRenderer.setDrawOutOfRange(false);
TimelineRenderer.setDrawOutOfRange(false);
break;
}
}

export function setPianoRange(value: [number, number]) {
noteRange = value;
PianoRenderer.drawKeys(noteRange[0], noteRange[1]);
Expand Down
77 changes: 77 additions & 0 deletions src/core/TimelineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SynthState } from "./SynthState";
import { Audio } from "nitro-fs";

let alignNotes = true;
let drawOutOfRange = false;

export class TimelineRenderer {
constructor(canvas: HTMLCanvasElement) {
Expand Down Expand Up @@ -58,6 +59,78 @@ export class TimelineRenderer {
continue;
}

if (
note.note < noteRange[0] ||
noteRange[1] < note.note
) {
if (
!drawOutOfRange ||
note.state !== Audio.EnvelopeState.Attack
)
continue;

const outOfRangeNoteSize = 10;
const noteY =
invLerp(
absoluteTimeRange[0],
absoluteTimeRange[1],
s.time
) * this.canvas.height;

if (note.note < noteRange[0]) {
//Path for a Triangle ◀
this.ctx.beginPath();
this.ctx.moveTo(
outOfRangeNoteSize,
this.canvas.height - noteY
);
this.ctx.lineTo(
outOfRangeNoteSize,
this.canvas.height -
noteY -
outOfRangeNoteSize
);
this.ctx.lineTo(
0,
this.canvas.height -
noteY -
outOfRangeNoteSize / 2
);
this.ctx.lineTo(
outOfRangeNoteSize,
this.canvas.height - noteY
);
this.ctx.fill();
this.ctx.closePath();
} else if (note.note > noteRange[0]) {
//Path for a Triangle ▶
this.ctx.beginPath();
this.ctx.moveTo(
this.canvas.width - outOfRangeNoteSize,
this.canvas.height - noteY
);
this.ctx.lineTo(
this.canvas.width - outOfRangeNoteSize,
this.canvas.height -
noteY -
outOfRangeNoteSize
);
this.ctx.lineTo(
this.canvas.width,
this.canvas.height -
noteY -
outOfRangeNoteSize / 2
);
this.ctx.lineTo(
this.canvas.width - outOfRangeNoteSize,
this.canvas.height - noteY
);
this.ctx.fill();
this.ctx.closePath();
}
continue;
}

const noteWidth =
(this.canvas.width / noteRangeCount) * note.volume;

Expand Down Expand Up @@ -114,6 +187,10 @@ export class TimelineRenderer {
static alignNotesToPiano(value: boolean) {
alignNotes = value;
}

static setDrawOutOfRange(value: boolean) {
drawOutOfRange = value;
}
}

function invLerp(a: number, b: number, v: number) {
Expand Down
17 changes: 16 additions & 1 deletion src/ui/ConfigSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
ConfigGrid,
ConfigGridCheckbox,
ConfigGridMinMax,
ConfigGridNumber
ConfigGridNumber,
ConfigGridSelect
} from "./ConfigGrid";
import * as Renderer from "../core/Renderer";
import { Collapsible } from "./Collapsible";
Expand Down Expand Up @@ -55,6 +56,20 @@ export function ConfigSection() {
Renderer.setPianoRange(value)
}
/>
<ConfigGridSelect
label="Draw Out of Range Notes"
storageTag="drawOutOfRangeType"
defaultValue="On Keys"
options={[
"On Both",
"On Keys",
"On Timeline",
"Off"
]}
onChange={(value: string) => {
Renderer.setOutOfRangeBehaviour(value);
}}
/>
</ConfigGrid>
</Collapsible>
</section>
Expand Down