Skip to content

Commit 98185c0

Browse files
authored
Add support for Dot Matrix Display (#199)
1 parent b8af388 commit 98185c0

File tree

6 files changed

+257
-75
lines changed

6 files changed

+257
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "SpessaSynth",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"type": "module",
55
"private": true,
66
"scripts": {

src/website/changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Note to self: make sure to update this
33
*/
4-
export const WHATS_NEW: string[] = [`Updated libraries`] as const;
4+
export const WHATS_NEW: string[] = [`GS/XG display dots support!`] as const;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { Renderer } from "./renderer.ts";
2+
3+
const DOT_MATRIX_PIXEL_WIDTH = 40;
4+
const DOT_MATRIX_PIXEL_HEIGHT = 16;
5+
const DOT_MATRIX_SIZE = 16;
6+
const DOT_MATRIX_MARGIN = 2;
7+
const DOT_MATRIX_BG_PADDING = 2;
8+
9+
const DOT_MATRIX_WIDTH =
10+
(DOT_MATRIX_PIXEL_WIDTH + DOT_MATRIX_MARGIN) * DOT_MATRIX_SIZE;
11+
const DOT_MATRIX_HEIGHT =
12+
(DOT_MATRIX_PIXEL_HEIGHT + DOT_MATRIX_MARGIN) * DOT_MATRIX_SIZE;
13+
14+
const DOT_MATRIX_ACTIVE = "hsl(0,0%,10%)";
15+
const DOT_MATRIX_BG_GS = "hsl(30, 100%, 50%)";
16+
const DOT_MATRIX_INACTIVE_GS = "hsl(30, 100%, 40%)";
17+
18+
const DOT_MATRIX_BG_XG = "hsl(75, 100%, 50%)";
19+
const DOT_MATRIX_INACTIVE_XG = "hsl(75, 100%, 40%)";
20+
const SCALE_FACTOR = 800;
21+
22+
export function drawDotMatrix(this: Renderer) {
23+
const canvasWidth = this.canvas.width;
24+
const canvasHeight = this.canvas.height;
25+
const scale = Math.min(canvasWidth, canvasHeight) / SCALE_FACTOR;
26+
const matrixWidth = DOT_MATRIX_WIDTH * scale;
27+
const matrixHeight = DOT_MATRIX_HEIGHT * scale;
28+
const matrixStartX = canvasWidth / 2 - matrixWidth / 2;
29+
const matrixStartY = canvasHeight / 2 - matrixHeight / 2;
30+
const bgPadding = scale * DOT_MATRIX_BG_PADDING;
31+
const dotWidth = scale * DOT_MATRIX_PIXEL_WIDTH;
32+
const dotHeight = scale * DOT_MATRIX_PIXEL_HEIGHT;
33+
const dotMargin = scale * DOT_MATRIX_MARGIN;
34+
35+
const isGS = this.showDisplayMatrix === "gs";
36+
this.drawingContext.fillStyle = isGS ? DOT_MATRIX_BG_GS : DOT_MATRIX_BG_XG;
37+
this.drawingContext.fillRect(
38+
matrixStartX - bgPadding,
39+
matrixStartY - bgPadding,
40+
matrixWidth + 2 * bgPadding,
41+
matrixHeight + 2 * bgPadding
42+
);
43+
44+
for (let row = 0; row < DOT_MATRIX_SIZE; row++) {
45+
for (let col = 0; col < DOT_MATRIX_SIZE; col++) {
46+
this.drawingContext.fillStyle = this.displayMatrix[row][col]
47+
? DOT_MATRIX_ACTIVE
48+
: isGS
49+
? DOT_MATRIX_INACTIVE_GS
50+
: DOT_MATRIX_INACTIVE_XG;
51+
this.drawingContext.fillRect(
52+
matrixStartX + col * (dotWidth + dotMargin),
53+
matrixStartY + row * (dotHeight + dotMargin),
54+
dotWidth,
55+
dotHeight
56+
);
57+
}
58+
}
59+
}

src/website/js/renderer/render.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export function render(this: Renderer, auto = true, force = false) {
5858
}
5959
}
6060

61+
// Draw dot matrix
62+
if (this.showDisplayMatrix !== null) {
63+
this.drawDotMatrix();
64+
}
65+
6166
// Calculate fps
6267
const timeSinceLastFrame = performance.now() - this.frameTimeStart;
6368
this.frameTimeStart = performance.now();

src/website/js/renderer/renderer.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import {
1313
renderWaveforms
1414
} from "./render_waveforms.js";
1515
import { consoleColors } from "../utils/console_colors.js";
16-
import { BasicMIDI, midiMessageTypes } from "spessasynth_core";
16+
import {
17+
BasicMIDI,
18+
midiMessageTypes,
19+
type SynthSystem
20+
} from "spessasynth_core";
1721
import type { Sequencer } from "spessasynth_lib";
1822
import type { LocaleManager } from "../locale/locale_manager.ts";
1923
import type { Synthesizer } from "../utils/synthesizer.ts";
24+
import { drawDotMatrix } from "./draw_dot_matrix.ts";
2025

2126
/**
2227
* Renderer.js
@@ -44,6 +49,8 @@ export const rendererModes = {
4449

4550
export type RendererMode = (typeof rendererModes)[keyof typeof rendererModes];
4651

52+
const DISPLAY_MATRIX_TIMEOUT = 2880;
53+
4754
// Analysers
4855
const CHANNEL_ANALYSER_FFT = 1024;
4956
const DRUMS_ANALYSER_FFT = 4096;
@@ -90,6 +97,12 @@ export class Renderer {
9097
public sideways = false;
9198
public readonly updateFftSize = updateFftSize.bind(this);
9299
public readonly render = render.bind(this);
100+
/**
101+
* For XG/GS display matrix
102+
*/
103+
public readonly displayMatrix = Array.from({ length: 16 }, () =>
104+
new Array<boolean>(16).fill(false)
105+
);
93106
protected version: string;
94107
protected _notesFall = true;
95108
protected canvas: HTMLCanvasElement;
@@ -120,12 +133,15 @@ export class Renderer {
120133
protected readonly disconnectChannelAnalysers =
121134
disconnectChannelAnalysers.bind(this);
122135
protected readonly renderWaveforms = renderWaveforms.bind(this);
136+
protected readonly drawDotMatrix = drawDotMatrix.bind(this);
123137
protected readonly renderSingleWaveform = renderSingleWaveform.bind(this);
124138
protected readonly renderSingleFft = renderSingleFft.bind(this);
125139
protected readonly renderBigFft = renderBigFft.bind(this);
126140
protected readonly inputNode: AudioNode;
127141
protected readonly workerMode: boolean;
128142
protected readonly sampleRateFactor: number;
143+
protected showDisplayMatrix: SynthSystem | null = null;
144+
private displayMatrixTimeout = 0;
129145

130146
/**
131147
* Creates a new midi renderer for rendering notes visually.
@@ -248,12 +264,12 @@ export class Renderer {
248264

249265
protected _stabilizeWaveforms = true;
250266

251-
// noinspection JSUnusedGlobalSymbols
252-
253267
public get stabilizeWaveforms() {
254268
return this._stabilizeWaveforms;
255269
}
256270

271+
// noinspection JSUnusedGlobalSymbols
272+
257273
public set stabilizeWaveforms(val: boolean) {
258274
this._stabilizeWaveforms = val;
259275
this.updateFftSize();
@@ -316,6 +332,14 @@ export class Renderer {
316332
this._notesFall = val === "down";
317333
}
318334

335+
public updateDisplayMatrix(mode: SynthSystem) {
336+
this.showDisplayMatrix = mode;
337+
clearTimeout(this.displayMatrixTimeout);
338+
this.displayMatrixTimeout = window.setTimeout(() => {
339+
this.showDisplayMatrix = null;
340+
}, DISPLAY_MATRIX_TIMEOUT);
341+
}
342+
319343
public setRendererMode(mode: RendererMode) {
320344
this.rendererMode = mode;
321345
this.updateFftSize();

0 commit comments

Comments
 (0)