mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 22:53:09 +02:00
Optimization (frontend): MkModPlayer Don't waste memory on a static pattern
Fix: Inconsistent pattern height offset.
This commit is contained in:
parent
452c5668ea
commit
00e118c0ce
1 changed files with 182 additions and 67 deletions
|
@ -7,7 +7,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-else class="mod-player-enabled">
|
<div v-else class="mod-player-enabled">
|
||||||
<div class="pattern-display" @click="togglePattern()">
|
<div class="pattern-display" @click="togglePattern()" @scroll="scrollHandler" @scrollend="scrollEndHandle">
|
||||||
<div v-if="patternHide" class="pattern-hide">
|
<div v-if="patternHide" class="pattern-hide">
|
||||||
<b><i class="ph-eye ph-bold ph-lg"></i> Pattern Hidden</b>
|
<b><i class="ph-eye ph-bold ph-lg"></i> Pattern Hidden</b>
|
||||||
<span>{{ i18n.ts.clickToShow }}</span>
|
<span>{{ i18n.ts.clickToShow }}</span>
|
||||||
|
@ -17,6 +17,7 @@
|
||||||
<canvas ref="displayCanvas" class="pattern-canvas"></canvas>
|
<canvas ref="displayCanvas" class="pattern-canvas"></canvas>
|
||||||
</div>
|
</div>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
|
<input v-if="patternScrollSliderShow" ref="patternScrollSlider" v-model="patternScrollSliderPos" class="pattern-slider" type="range" min="0" max="100" step="0.01" style=""/>
|
||||||
<button class="play" @click="playPause()">
|
<button class="play" @click="playPause()">
|
||||||
<i v-if="playing" class="ph-pause ph-bold ph-lg"></i>
|
<i v-if="playing" class="ph-pause ph-bold ph-lg"></i>
|
||||||
<i v-else class="ph-play ph-bold ph-lg"></i>
|
<i v-else class="ph-play ph-bold ph-lg"></i>
|
||||||
|
@ -35,11 +36,12 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, nextTick, computed } from 'vue';
|
import { ref, nextTick, computed, watch } from 'vue';
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
import { defaultStore } from '@/store.js';
|
import { defaultStore } from '@/store.js';
|
||||||
import { ChiptuneJsPlayer, ChiptuneJsConfig } from '@/scripts/chiptune2.js';
|
import { ChiptuneJsPlayer, ChiptuneJsConfig } from '@/scripts/chiptune2.js';
|
||||||
|
import { isTouchUsing } from '@/scripts/touch.js';
|
||||||
|
|
||||||
const colours = {
|
const colours = {
|
||||||
background: '#000000',
|
background: '#000000',
|
||||||
|
@ -69,6 +71,9 @@ let playing = ref(false);
|
||||||
let displayCanvas = ref<HTMLCanvasElement>();
|
let displayCanvas = ref<HTMLCanvasElement>();
|
||||||
let progress = ref<HTMLProgressElement>();
|
let progress = ref<HTMLProgressElement>();
|
||||||
let position = ref(0);
|
let position = ref(0);
|
||||||
|
let patternScrollSlider = ref<HTMLProgressElement>();
|
||||||
|
let patternScrollSliderShow = ref(false);
|
||||||
|
let patternScrollSliderPos = ref(0);
|
||||||
const player = ref(new ChiptuneJsPlayer(new ChiptuneJsConfig()));
|
const player = ref(new ChiptuneJsPlayer(new ChiptuneJsConfig()));
|
||||||
|
|
||||||
const maxRowNumbers = 0xFF;
|
const maxRowNumbers = 0xFF;
|
||||||
|
@ -179,20 +184,22 @@ function toggleVisible() {
|
||||||
|
|
||||||
function togglePattern() {
|
function togglePattern() {
|
||||||
patternHide.value = !patternHide.value;
|
patternHide.value = !patternHide.value;
|
||||||
if (!patternHide.value) {
|
handleScrollBarEnable();
|
||||||
|
|
||||||
if (player.value.getRow() === 0) {
|
if (player.value.getRow() === 0) {
|
||||||
try {
|
try {
|
||||||
player.value.play(buffer);
|
player.value.play(buffer);
|
||||||
display();
|
display(true);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
console.warn(err);
|
||||||
}
|
}
|
||||||
player.value.stop();
|
player.value.stop();
|
||||||
}
|
} else {
|
||||||
|
display(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function DrawPattern() {
|
function drawPattern() {
|
||||||
if (!displayCanvas.value) return;
|
if (!displayCanvas.value) return;
|
||||||
const canvas = displayCanvas.value;
|
const canvas = displayCanvas.value;
|
||||||
|
|
||||||
|
@ -202,8 +209,6 @@ function DrawPattern() {
|
||||||
const halfbuf = rowBuffer / 2;
|
const halfbuf = rowBuffer / 2;
|
||||||
const minRow = row - halfbuf;
|
const minRow = row - halfbuf;
|
||||||
const maxRow = row + halfbuf;
|
const maxRow = row + halfbuf;
|
||||||
const spacer = 11;
|
|
||||||
const space = ' ';
|
|
||||||
|
|
||||||
let rowDif = 0;
|
let rowDif = 0;
|
||||||
|
|
||||||
|
@ -215,8 +220,8 @@ function DrawPattern() {
|
||||||
rowDif = row - lastDrawnRow;
|
rowDif = row - lastDrawnRow;
|
||||||
} else {
|
} else {
|
||||||
alreadyDrawn = [];
|
alreadyDrawn = [];
|
||||||
canvas.width = 12 + 84 * nbChannels + 2;
|
if (canvas.width !== (12 + 84 * nbChannels + 2)) canvas.width = 12 + 84 * nbChannels + 2;
|
||||||
canvas.height = 12 * nbRows;
|
if (canvas.height !== (12 * nbRows)) canvas.height = 12 * nbRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx = canvas.getContext('2d', { alpha: false }) as CanvasRenderingContext2D;
|
const ctx = canvas.getContext('2d', { alpha: false }) as CanvasRenderingContext2D;
|
||||||
|
@ -235,43 +240,9 @@ function DrawPattern() {
|
||||||
if (alreadyDrawn[rowToDraw] === true) continue;
|
if (alreadyDrawn[rowToDraw] === true) continue;
|
||||||
|
|
||||||
if (rowToDraw >= 0 && rowToDraw < nbRows) {
|
if (rowToDraw >= 0 && rowToDraw < nbRows) {
|
||||||
let seperators = '';
|
|
||||||
let note = '';
|
|
||||||
let instr = '';
|
|
||||||
let volume = '';
|
|
||||||
let fx = '';
|
|
||||||
let op = '';
|
|
||||||
for (let channel = 0; channel < nbChannels; channel++) {
|
|
||||||
const part = player.value.getPatternRowChannel(pattern, rowToDraw, channel);
|
|
||||||
|
|
||||||
seperators += '|' + space.repeat( spacer + 2 );
|
|
||||||
note += part.substring(0, 3) + space.repeat( spacer );
|
|
||||||
instr += part.substring(4, 6) + space.repeat( spacer + 1 );
|
|
||||||
volume += part.substring(6, 9) + space.repeat( spacer );
|
|
||||||
fx += part.substring(10, 11) + space.repeat( spacer + 2 );
|
|
||||||
op += part.substring(11, 13) + space.repeat( spacer + 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseOffset = 2 * CHAR_WIDTH;
|
const baseOffset = 2 * CHAR_WIDTH;
|
||||||
const baseRowOffset = ROW_OFFSET_Y + rowToDraw * CHAR_HEIGHT;
|
const baseRowOffset = ROW_OFFSET_Y + rowToDraw * CHAR_HEIGHT;
|
||||||
|
drawRow(ctx, rowToDraw, nbChannels, pattern, baseOffset, baseRowOffset);
|
||||||
ctx.fillStyle = colours.foreground.default;
|
|
||||||
ctx.fillText(seperators, baseOffset, baseRowOffset);
|
|
||||||
|
|
||||||
ctx.fillStyle = colours.foreground.default;
|
|
||||||
ctx.fillText(note, baseOffset + CHAR_WIDTH, baseRowOffset);
|
|
||||||
|
|
||||||
ctx.fillStyle = colours.foreground.instr;
|
|
||||||
ctx.fillText(instr, baseOffset + CHAR_WIDTH * 5, baseRowOffset);
|
|
||||||
|
|
||||||
ctx.fillStyle = colours.foreground.volume;
|
|
||||||
ctx.fillText(volume, baseOffset + CHAR_WIDTH * 7, baseRowOffset);
|
|
||||||
|
|
||||||
ctx.fillStyle = colours.foreground.fx;
|
|
||||||
ctx.fillText(fx, baseOffset + CHAR_WIDTH * 11, baseRowOffset);
|
|
||||||
|
|
||||||
ctx.fillStyle = colours.foreground.operant;
|
|
||||||
ctx.fillText(op, baseOffset + CHAR_WIDTH * 12, baseRowOffset);
|
|
||||||
|
|
||||||
alreadyDrawn[rowToDraw] = true;
|
alreadyDrawn[rowToDraw] = true;
|
||||||
}
|
}
|
||||||
|
@ -281,30 +252,155 @@ function DrawPattern() {
|
||||||
lastPattern = pattern;
|
lastPattern = pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
function display() {
|
function drawPetternPreview() {
|
||||||
if (!displayCanvas.value) {
|
if (!displayCanvas.value) return;
|
||||||
|
const canvas = displayCanvas.value;
|
||||||
|
|
||||||
|
const pattern = player.value.getPattern();
|
||||||
|
const nbRows = player.value.getPatternNumRows(pattern);
|
||||||
|
const row = player.value.getRow();
|
||||||
|
const halfbuf = rowBuffer / 2;
|
||||||
|
|
||||||
|
let nbChannels = 0;
|
||||||
|
if (player.value.currentPlayingNode) {
|
||||||
|
nbChannels = player.value.currentPlayingNode.nbChannels;
|
||||||
|
}
|
||||||
|
if (canvas.width !== (12 + 84 * nbChannels + 2)) canvas.width = 12 + 84 * nbChannels + 2;
|
||||||
|
if (canvas.height !== (12 * rowBuffer)) canvas.height = 12 * rowBuffer;
|
||||||
|
|
||||||
|
const ctx = canvas.getContext('2d', { alpha: false }) as CanvasRenderingContext2D;
|
||||||
|
ctx.font = '10px monospace';
|
||||||
|
ctx.imageSmoothingEnabled = false;
|
||||||
|
ctx.fillStyle = colours.background;
|
||||||
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
|
ctx.drawImage( numberRowCanvas, 0, (halfbuf - row) * CHAR_HEIGHT );
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.default;
|
||||||
|
for (let rowOffset = 0; rowOffset < rowBuffer; rowOffset++) {
|
||||||
|
const rowToDraw = rowOffset + row - halfbuf;
|
||||||
|
|
||||||
|
if (rowToDraw >= 0 && rowToDraw < nbRows) {
|
||||||
|
const baseOffset = 2 * CHAR_WIDTH;
|
||||||
|
const baseRowOffset = ROW_OFFSET_Y + rowOffset * CHAR_HEIGHT;
|
||||||
|
drawRow(ctx, rowToDraw, nbChannels, pattern, baseOffset, baseRowOffset);
|
||||||
|
} else if (rowToDraw >= 0) {
|
||||||
|
const baseRowOffset = ROW_OFFSET_Y + rowOffset * CHAR_HEIGHT;
|
||||||
|
ctx.fillStyle = colours.background;
|
||||||
|
ctx.fillRect(0, baseRowOffset - CHAR_HEIGHT, CHAR_WIDTH * 2, baseRowOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastPattern = -1;
|
||||||
|
lastDrawnRow = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawRow(ctx: CanvasRenderingContext2D, row: number, channels: number, pattern: number, drawX = (2 * CHAR_WIDTH), drawY = ROW_OFFSET_Y) {
|
||||||
|
const spacer = 11;
|
||||||
|
const space = ' ';
|
||||||
|
let seperators = '';
|
||||||
|
let note = '';
|
||||||
|
let instr = '';
|
||||||
|
let volume = '';
|
||||||
|
let fx = '';
|
||||||
|
let op = '';
|
||||||
|
for (let channel = 0; channel < channels; channel++) {
|
||||||
|
const part = player.value.getPatternRowChannel(pattern, row, channel);
|
||||||
|
|
||||||
|
seperators += '|' + space.repeat( spacer + 2 );
|
||||||
|
note += part.substring(0, 3) + space.repeat( spacer );
|
||||||
|
instr += part.substring(4, 6) + space.repeat( spacer + 1 );
|
||||||
|
volume += part.substring(6, 9) + space.repeat( spacer );
|
||||||
|
fx += part.substring(10, 11) + space.repeat( spacer + 2 );
|
||||||
|
op += part.substring(11, 13) + space.repeat( spacer + 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.default;
|
||||||
|
ctx.fillText(seperators, drawX, drawY);
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.default;
|
||||||
|
ctx.fillText(note, drawX + CHAR_WIDTH, drawY);
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.instr;
|
||||||
|
ctx.fillText(instr, drawX + CHAR_WIDTH * 5, drawY);
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.volume;
|
||||||
|
ctx.fillText(volume, drawX + CHAR_WIDTH * 7, drawY);
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.fx;
|
||||||
|
ctx.fillText(fx, drawX + CHAR_WIDTH * 11, drawY);
|
||||||
|
|
||||||
|
ctx.fillStyle = colours.foreground.operant;
|
||||||
|
ctx.fillText(op, drawX + CHAR_WIDTH * 12, drawY);
|
||||||
|
}
|
||||||
|
|
||||||
|
function display(skipOptimizationChecks = false) {
|
||||||
|
if (!displayCanvas.value || !displayCanvas.value.parentElement) {
|
||||||
stop();
|
stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (patternHide.value) return;
|
if (patternHide.value && !skipOptimizationChecks) return;
|
||||||
|
|
||||||
if (firstFrame) {
|
if (firstFrame) {
|
||||||
firstFrame = false;
|
// Changing it to false should enable pattern display by default.
|
||||||
// Change the next line to false in order to show the pattern by default.
|
|
||||||
patternHide.value = true;
|
patternHide.value = true;
|
||||||
|
handleScrollBarEnable();
|
||||||
|
firstFrame = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const row = player.value.getRow();
|
const row = player.value.getRow();
|
||||||
const pattern = player.value.getPattern();
|
const pattern = player.value.getPattern();
|
||||||
|
|
||||||
if ( row === lastDrawnRow && pattern === lastPattern ) return;
|
if ( row === lastDrawnRow && pattern === lastPattern && !skipOptimizationChecks) return;
|
||||||
|
|
||||||
DrawPattern();
|
// Size vs speed
|
||||||
|
if (patternHide.value) drawPetternPreview();
|
||||||
|
else drawPattern();
|
||||||
|
|
||||||
displayCanvas.value.style.top = 'calc( 50% - ' + (row * CHAR_HEIGHT) + 'px )';
|
displayCanvas.value.style.top = !patternHide.value ? 'calc( 50% - ' + (row * CHAR_HEIGHT) + 'px )' : '0%';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let suppressScrollSliderWatcher = false;
|
||||||
|
|
||||||
|
function scrollHandler() {
|
||||||
|
suppressScrollSliderWatcher = true;
|
||||||
|
|
||||||
|
if (!patternScrollSlider.value) return;
|
||||||
|
if (!displayCanvas.value) return;
|
||||||
|
if (!displayCanvas.value.parentElement) return;
|
||||||
|
|
||||||
|
patternScrollSliderPos.value = (displayCanvas.value.parentElement.scrollLeft) / (displayCanvas.value.width - displayCanvas.value.parentElement.offsetWidth) * 100;
|
||||||
|
patternScrollSlider.value.style.opacity = '1';
|
||||||
|
}
|
||||||
|
|
||||||
|
function scrollEndHandle() {
|
||||||
|
suppressScrollSliderWatcher = false;
|
||||||
|
|
||||||
|
if (!patternScrollSlider.value) return;
|
||||||
|
patternScrollSlider.value.style.opacity = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleScrollBarEnable() {
|
||||||
|
patternScrollSliderShow.value = (!patternHide.value && !isTouchUsing);
|
||||||
|
if (patternScrollSliderShow.value !== true) return;
|
||||||
|
|
||||||
|
if (!displayCanvas.value) return;
|
||||||
|
if (!displayCanvas.value.parentElement) return;
|
||||||
|
if (firstFrame) {
|
||||||
|
patternScrollSliderShow.value = (12 + 84 * player.value.getPatternNumRows(player.value.getPattern()) + 2 > displayCanvas.value.parentElement.offsetWidth);
|
||||||
|
} else {
|
||||||
|
patternScrollSliderShow.value = (displayCanvas.value.width > displayCanvas.value.parentElement.offsetWidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(patternScrollSliderPos, () => {
|
||||||
|
if (suppressScrollSliderWatcher) return;
|
||||||
|
if (!displayCanvas.value) return;
|
||||||
|
if (!displayCanvas.value.parentElement) return;
|
||||||
|
|
||||||
|
displayCanvas.value.parentElement.scrollLeft = (displayCanvas.value.width - displayCanvas.value.parentElement.offsetWidth) * patternScrollSliderPos.value / 100;
|
||||||
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
@ -347,6 +443,12 @@ function display() {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
max-height: 312px; /* magic_number = CHAR_HEIGHT * rowBuffer, needs to be in px */
|
max-height: 312px; /* magic_number = CHAR_HEIGHT * rowBuffer, needs to be in px */
|
||||||
|
|
||||||
|
scrollbar-width: none;
|
||||||
|
|
||||||
|
&::-webkit-scrollbar {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
.pattern-canvas {
|
.pattern-canvas {
|
||||||
position: relative;
|
position: relative;
|
||||||
background-color: black;
|
background-color: black;
|
||||||
|
@ -357,36 +459,37 @@ function display() {
|
||||||
|
|
||||||
.patternShadowTop {
|
.patternShadowTop {
|
||||||
background: #00000080;
|
background: #00000080;
|
||||||
pointer-events: none;
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 50%;
|
height: calc( 50% - 14px );
|
||||||
translate: 0 -100%;
|
translate: 0 -100%;
|
||||||
top: calc( 50% - 13px );
|
top: calc( 50% - 14px );
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
pointer-events: none;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
.patternShadowBottom {
|
.patternShadowBottom {
|
||||||
background: #00000080;
|
background: #00000080;
|
||||||
pointer-events: none;
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: calc( 50% + 12px );
|
height: calc( 50% - 12px );
|
||||||
top: calc( 50% - 1px );
|
top: calc( 50% - 1px );
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
pointer-events: none;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
.pattern-hide {
|
.pattern-hide {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
background: rgba(64, 64, 64, 0.3);
|
background: rgba(64, 64, 64, 0.3);
|
||||||
backdrop-filter: blur(2em);
|
backdrop-filter: var(--modalBgFilter);
|
||||||
color: #fff;
|
color: #fff;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
|
|
||||||
position: absolute;
|
position: absolute;
|
||||||
z-index: 3;
|
z-index: 4;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|
||||||
|
@ -400,7 +503,7 @@ function display() {
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
background-color: var(--bg);
|
background-color: var(--bg);
|
||||||
z-index: 3;
|
z-index: 5;
|
||||||
|
|
||||||
> * {
|
> * {
|
||||||
padding: 4px 8px;
|
padding: 4px 8px;
|
||||||
|
@ -425,6 +528,18 @@ function display() {
|
||||||
margin: 4px 8px;
|
margin: 4px 8px;
|
||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
|
|
||||||
|
&.pattern-slider {
|
||||||
|
position: absolute;
|
||||||
|
width: calc( 100% - 8px * 2 );
|
||||||
|
top: calc( 100% - 21px * 3 );
|
||||||
|
opacity: 0%;
|
||||||
|
transition: opacity 0.2s;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
opacity: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
&:focus {
|
&:focus {
|
||||||
outline: none;
|
outline: none;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue