FoundKey/packages/client/src/components/mod-player.vue

404 lines
8.9 KiB
Vue
Raw Normal View History

client: add mod tracker Squashed commit of the following: commit 54f0b67b25bc6064b5c0ab3982e20943859aff76 Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 21:27:15 2022 +0100 use nextTick instead of setTimeout commit 6998cae7e3a706b00c1b63320750dab279f13e3d Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 21:14:55 2022 +0100 my absolute terrible fix to the unhide issue commit 79f546d1509185c315a5db7e12985e01ed430b08 Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 21:01:35 2022 +0100 stop player on hide/unhide commit 6b7f13e8ef48d74edb92441144051e4225b27f71 Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 10:36:59 2022 +0100 make webkit style range slider the same commit 8a267c5cdc5c1e98d6dd1e038571d92d01985a98 Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 01:16:18 2022 +0100 restyling range inputs commit c39e1671b2957326ff91746da44091d728f58e82 Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:57:47 2022 +0100 make module seekable commit c1762f27ae2e4d342ede88018f777c75b8b31d4a Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:14:35 2022 +0100 remove accesskey attribs commit 08f75a01f1c2359799e4c2ec734a391d3abfc07c Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:12:23 2022 +0100 v-else on play button commit 9302a9faaa5a75c33314befb3f8e0f414e9d886b Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:08:19 2022 +0100 replace filter with some commit bffd15daedf55f7623d6ebd777a12384274b89d6 Author: Puniko <me@absturztaube.ch> Date: Wed Dec 28 09:13:20 2022 +0100 add chiptune2 and libopenmpt into COPYING commit 794298c21c6fe09909136d141fd2a6b83eb64a89 Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 15:32:43 2022 +0100 little cleanup commit f383aec1cd5a9f8a44539d9f802dee2d1332f64c Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 15:23:25 2022 +0100 repeat only once and proper handling of track ending commit fdaa9614c993de306789ecafc38d781d350fe1ab Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 14:52:20 2022 +0100 prevent losing connection when downloading module commit 6c5723c795a3610558111f5d0cd6f39dcfd06965 Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 14:45:59 2022 +0100 colours!!! 🌈 commit dba4f0a4a909b956d928bfb7e4f0321291a096e0 Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 13:01:06 2022 +0100 replace with i18n commit 4234dfbdbc7f9b73fe07348af1fe2590db4e811d Author: Puniko <me@absturztaube.ch> Date: Mon Dec 26 15:47:10 2022 +0100 retab commit 0cc1ea8c3ec14fde81936ec3c9eed4d06ad52d95 Author: Puniko <me@absturztaube.ch> Date: Mon Dec 26 15:19:28 2022 +0100 include libopenmpt tracker to foundkey commit c2437c696a5dabb8581fd81f7852cdd6091fe00f Author: Puniko <me@absturztaube.ch> Date: Mon Dec 26 12:08:49 2022 +0100 add libopenmpt Reviewed-on: https://akkoma.dev/FoundKeyGang/FoundKey/pulls/306 Changelog: Added
2022-12-29 20:34:57 +00:00
<template>
<div class="mod-player-disabled" v-if="hide" @click="toggleVisible()">
<div>
<b><i class="fas fa-exlamation-triangle"></i> {{ i18n.ts.sensitive }}</b>
<span>{{ i18n.ts.clickToShow }}</span>
</div>
</div>
<div class="mod-player-enabled" v-else>
<div class="pattern-display">
<canvas class="pattern-canvas" ref="displayCanvas"></canvas>
</div>
<div class="controls">
<button class="play" @click="playPause()">
<i class="fas fa-pause" v-if="playing"></i>
<i class="fas fa-play" v-else></i>
</button>
<button class="stop" @click="stop()">
<i class="fas fa-stop"></i>
</button>
<input class="progress" type="range" min="0" max="1" v-model="position" step="0.1" ref="progress" @mousedown="initSeek()" @mouseup="performSeek()"/>
<input type="range" min="0" max="1" v-model="player.context.gain.value" step="0.1"/>
<a class="download" :title="i18n.ts.download" :href="module.url" target="_blank">
<i class="fas fa-download"></i>
</a>
</div>
<i class="fas fa-eye-slash" @click="toggleVisible()"></i>
</div>
</template>
<script lang="ts" setup>
import { ref, nextTick } from 'vue';
import * as foundkey from 'foundkey-js';
import { i18n } from '@/i18n';
import { defaultStore } from '@/store';
import { ChiptuneJsPlayer, ChiptuneJsConfig } from '@/scripts/chiptune2';
const CHAR_WIDTH = 6;
const CHAR_HEIGHT = 12;
const ROW_OFFSET_Y = 10;
const colours = {
background: '#000000',
default: {
active: '#ffffff',
inactive: '#808080'
},
quarter: {
active: '#ffff00',
inactive: '#ffe135'
},
instr: {
active: '#80e0ff',
inactive: '#0099cc'
},
volume: {
active: '#80ff80',
inactive: '#008000'
},
fx: {
active: '#ff80e0',
inactive: '#800060'
},
operant: {
active: '#ffe080',
inactive: '#806000'
}
};
const props = defineProps<{
module: foundkey.entities.DriveFile
}>();
let hide = ref((defaultStore.state.nsfw === 'force') ? true : props.module.isSensitive && (defaultStore.state.nsfw !== 'ignore'));
let playing = ref(false);
let displayCanvas = ref<HTMLCanvasElement>(null);
let progress = ref<HTMLProgressElement>(null);
let position = ref(0);
const player = ref(new ChiptuneJsPlayer(new ChiptuneJsConfig()));
const rowBuffer = 24;
let buffer = null;
let isSeeking = false;
player.value.load(props.module.url).then((result) => {
buffer = result;
try {
player.value.play(buffer);
display();
} catch (e) {
console.warn(e);
}
player.value.stop();
}).catch((error) => {
console.error(error);
});
function playPause() {
player.value.addHandler('onRowChange', () => {
progress.value.max = player.value.duration();
if (!isSeeking) {
position.value = player.value.position() % player.value.duration();
}
display();
});
player.value.addHandler('onEnded', () => {
stop();
});
if (player.value.currentPlayingNode === null) {
player.value.play(buffer);
player.value.seek(position.value);
playing.value = true;
} else {
player.value.togglePause();
playing.value = !player.value.currentPlayingNode.paused;
}
}
function stop(noDisplayUpdate = false) {
player.value.stop();
playing.value = false;
if (!noDisplayUpdate) {
try {
player.value.play(buffer);
display();
} catch (e) {
console.warn(e);
}
}
player.value.stop();
position.value = 0;
player.value.handlers = [];
}
function initSeek() {
isSeeking = true;
}
function performSeek() {
player.value.seek(position.value);
display();
isSeeking = false;
}
function toggleVisible() {
hide.value = !hide.value;
nextTick(() => { stop(hide.value); });
}
function display() {
if (!displayCanvas.value) {
stop();
return;
}
const canvas = displayCanvas.value;
const pattern = player.value.getPattern();
const row = player.value.getRow();
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;
canvas.height = 12 * rowBuffer;
}
const nbRows = player.value.getPatternNumRows(pattern);
const ctx = canvas.getContext('2d');
ctx.font = '10px monospace';
ctx.fillStyle = colours.background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = colours.default.inactive;
for (let rowOffset = 0; rowOffset < rowBuffer; rowOffset++) {
const rowToDraw = row - rowBuffer / 2 + rowOffset;
if (rowToDraw >= 0 && rowToDraw < nbRows) {
const active = (rowToDraw === row) ? 'active' : 'inactive';
let rowText = parseInt(rowToDraw).toString(16);
if (rowText.length === 1) {
rowText = '0' + rowText;
}
ctx.fillStyle = colours.default[active];
if (rowToDraw % 4 === 0) {
ctx.fillStyle = colours.quarter[active];
}
ctx.fillText(rowText, 0, 10 + rowOffset * 12);
for (let channel = 0; channel < nbChannels; channel++) {
const part = player.value.getPatternRowChannel(pattern, rowToDraw, channel);
const baseOffset = (2 + (part.length + 1) * channel) * CHAR_WIDTH;
const baseRowOffset = ROW_OFFSET_Y + rowOffset * CHAR_HEIGHT;
ctx.fillStyle = colours.default[active];
ctx.fillText("|", baseOffset, baseRowOffset);
const note = part.substring(0, 3);
ctx.fillStyle = colours.default[active];
ctx.fillText(note, baseOffset + CHAR_WIDTH, baseRowOffset);
const instr = part.substring(4, 6);
ctx.fillStyle = colours.instr[active];
ctx.fillText(instr, baseOffset + CHAR_WIDTH * 5, baseRowOffset);
const volume = part.substring(6, 9);
ctx.fillStyle = colours.volume[active];
ctx.fillText(volume, baseOffset + CHAR_WIDTH * 7, baseRowOffset);
const fx = part.substring(10, 11);
ctx.fillStyle = colours.fx[active];
ctx.fillText(fx, baseOffset + CHAR_WIDTH * 11, baseRowOffset);
const op = part.substring(11, 13);
ctx.fillStyle = colours.operant[active];
ctx.fillText(op, baseOffset + CHAR_WIDTH * 12, baseRowOffset);
}
}
}
}
</script>
<style lang="scss" scoped>
.mod-player-enabled {
position: relative;
display: flex;
flex-direction: column;
> i {
display: block;
position: absolute;
border-radius: 6px;
background-color: var(--fg);
color: var(--accentLighten);
font-size: 14px;
opacity: .5;
padding: 3px 6px;
text-align: center;
cursor: pointer;
top: 12px;
right: 12px;
}
> .pattern-display {
width: 100%;
height: 100%;
overflow-x: scroll;
overflow-y: hidden;
background-color: black;
text-align: center;
.pattern-canvas {
background-color: black;
height: 100%;
}
}
> .controls {
display: flex;
width: 100%;
background-color: var(--bg);
> * {
padding: 4px 8px;
}
> button, a {
border: none;
background-color: transparent;
color: var(--accent);
cursor: pointer;
&:hover {
background-color: var(--fg);
}
}
> input[type=range] {
height: 21px;
-webkit-appearance: none;
width: 90px;
padding: 0;
margin: 4px 8px;
overflow-x: hidden;
&:focus {
outline: none;
&::-webkit-slider-runnable-track {
background: var(--bg);
}
&::-ms-fill-lower, &::-ms-fill-upper {
background: var(--bg);
}
}
&::-webkit-slider-runnable-track {
width: 100%;
height: 100%;
cursor: pointer;
border-radius: 0;
animate: 0.2s;
background: var(--bg);
border: 1px solid var(--fg);
overflow-x: hidden;
}
&::-webkit-slider-thumb {
border: none;
height: 100%;
width: 14px;
border-radius: 0;
background: var(--accentLighten);
cursor: pointer;
-webkit-appearance: none;
box-shadow: calc(-100vw - 14px) 0 0 100vw var(--accent);
clip-path: polygon(1px 0, 100% 0, 100% 100%, 1px 100%, 1px calc(50% + 10.5px), -100vw calc(50% + 10.5px), -100vw calc(50% - 10.5px), 0 calc(50% - 10.5px));
z-index: 1;
}
&::-moz-range-track {
width: 100%;
height: 100%;
cursor: pointer;
border-radius: 0;
animate: 0.2s;
background: var(--bg);
border: 1px solid var(--fg);
}
&::-moz-range-progress {
cursor: pointer;
height: 100%;
background: var(--accent);
}
&::-moz-range-thumb {
border: none;
height: 100%;
border-radius: 0;
width: 14px;
background: var(--accentLighten);
cursor: pointer;
}
&::-ms-track {
width: 100%;
height: 100%;
cursor: pointer;
border-radius: 0;
animate: 0.2s;
background: transparent;
border-color: transparent;
color: transparent;
}
&::-ms-fill-lower {
background: var(--accent);
border: 1px solid var(--fg);
border-radius: 0;
}
&::-ms-fill-upper {
background: var(--bg);
border: 1px solid var(--fg);
border-radius: 0;
}
&::-ms-thumb {
margin-top: 1px;
border: none;
height: 100%;
width: 14px;
border-radius: 0;
background: var(--accentLighten);
cursor: pointer;
}
&.progress {
flex-grow: 1;
min-width: 0;
}
}
}
}
.mod-player-disabled {
display: flex;
justify-content: center;
align-items: center;
background: #111;
color: #fff;
> div {
display: table-cell;
text-align: center;
font-size: 12px;
> b {
display: block;
}
}
}
</style>