client: add mod tracker
All checks were successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
All checks were successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
Squashed commit of the following: commit54f0b67b25
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 21:27:15 2022 +0100 use nextTick instead of setTimeout commit6998cae7e3
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 21:14:55 2022 +0100 my absolute terrible fix to the unhide issue commit79f546d150
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 21:01:35 2022 +0100 stop player on hide/unhide commit6b7f13e8ef
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 10:36:59 2022 +0100 make webkit style range slider the same commit8a267c5cdc
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 01:16:18 2022 +0100 restyling range inputs commitc39e1671b2
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:57:47 2022 +0100 make module seekable commitc1762f27ae
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:14:35 2022 +0100 remove accesskey attribs commit08f75a01f1
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:12:23 2022 +0100 v-else on play button commit9302a9faaa
Author: Puniko <me@absturztaube.ch> Date: Thu Dec 29 00:08:19 2022 +0100 replace filter with some commitbffd15daed
Author: Puniko <me@absturztaube.ch> Date: Wed Dec 28 09:13:20 2022 +0100 add chiptune2 and libopenmpt into COPYING commit794298c21c
Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 15:32:43 2022 +0100 little cleanup commitf383aec1cd
Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 15:23:25 2022 +0100 repeat only once and proper handling of track ending commitfdaa9614c9
Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 14:52:20 2022 +0100 prevent losing connection when downloading module commit6c5723c795
Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 14:45:59 2022 +0100 colours!!! 🌈 commitdba4f0a4a9
Author: Puniko <me@absturztaube.ch> Date: Tue Dec 27 13:01:06 2022 +0100 replace with i18n commit4234dfbdbc
Author: Puniko <me@absturztaube.ch> Date: Mon Dec 26 15:47:10 2022 +0100 retab commit0cc1ea8c3e
Author: Puniko <me@absturztaube.ch> Date: Mon Dec 26 15:19:28 2022 +0100 include libopenmpt tracker to foundkey commitc2437c696a
Author: Puniko <me@absturztaube.ch> Date: Mon Dec 26 12:08:49 2022 +0100 add libopenmpt Reviewed-on: #306 Changelog: Added
This commit is contained in:
parent
ed9d4023d4
commit
ed27f61a4d
9 changed files with 732 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -11,6 +11,9 @@
|
|||
# nano
|
||||
.swp
|
||||
|
||||
# vimlocal
|
||||
.vimlocal
|
||||
|
||||
# Node.js
|
||||
node_modules
|
||||
report.*.json
|
||||
|
|
8
COPYING
8
COPYING
|
@ -13,3 +13,11 @@ https://github.com/muan/emojilib/blob/master/LICENSE
|
|||
RsaSignature2017 implementation by Transmute Industries Inc
|
||||
License: MIT
|
||||
https://github.com/transmute-industries/RsaSignature2017/blob/master/LICENSE
|
||||
|
||||
Chiptune2.js by Simon Gündling
|
||||
License: MIT
|
||||
https://github.com/deskjet/chiptune2.js#license
|
||||
|
||||
libopenmpt (as part of openmpt) by OpenMPT
|
||||
License: BSD 3-Clause
|
||||
https://github.com/OpenMPT/openmpt/blob/master/LICENSE
|
||||
|
|
|
@ -36,6 +36,7 @@ html
|
|||
link(rel='prefetch' href=config.images.error)
|
||||
link(rel='stylesheet' href='/assets/fontawesome/css/all.css')
|
||||
link(rel='modulepreload' href=`/assets/${clientEntry.file}`)
|
||||
script(src='/client-assets/libopenmpt.js')
|
||||
|
||||
each href in clientEntry.css
|
||||
link(rel='preload' href=`/assets/${href}` as='style')
|
||||
|
@ -63,6 +64,7 @@ html
|
|||
script.
|
||||
var VERSION = "#{version}";
|
||||
var CLIENT_ENTRY = "#{clientEntry.file}";
|
||||
window.libopenmpt = window.Module;
|
||||
|
||||
body
|
||||
noscript: p
|
||||
|
|
1
packages/client/assets/libopenmpt.js
Normal file
1
packages/client/assets/libopenmpt.js
Normal file
File diff suppressed because one or more lines are too long
BIN
packages/client/assets/libopenmpt.wasm
Executable file
BIN
packages/client/assets/libopenmpt.wasm
Executable file
Binary file not shown.
|
@ -6,6 +6,7 @@
|
|||
<template v-for="media in mediaList.filter(media => previewable(media))">
|
||||
<XVideo v-if="media.type.startsWith('video')" :key="media.id" :video="media"/>
|
||||
<XImage v-else-if="media.type.startsWith('image')" :key="media.id" class="image" :data-id="media.id" :image="media" :raw="raw"/>
|
||||
<XModPlayer v-else-if="isModule(media)" :key="media.id" :module="media"/>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -21,7 +22,8 @@ import 'photoswipe/style.css';
|
|||
import XBanner from './media-banner.vue';
|
||||
import XImage from './media-image.vue';
|
||||
import XVideo from './media-video.vue';
|
||||
import { FILE_TYPE_BROWSERSAFE } from '@/const';
|
||||
import XModPlayer from './mod-player.vue';
|
||||
import { FILE_TYPE_BROWSERSAFE, FILE_EXT_TRACKER_MODULES } from '@/const';
|
||||
|
||||
const props = defineProps<{
|
||||
mediaList: foundkey.entities.DriveFile[];
|
||||
|
@ -123,8 +125,16 @@ onMounted(() => {
|
|||
const previewable = (file: foundkey.entities.DriveFile): boolean => {
|
||||
if (file.type === 'image/svg+xml') return true; // svgのwebpublic/thumbnailはpngなのでtrue
|
||||
// FILE_TYPE_BROWSERSAFEに適合しないものはブラウザで表示するのに不適切
|
||||
if (isModule(file)) return true;
|
||||
return (file.type.startsWith('video') || file.type.startsWith('image')) && FILE_TYPE_BROWSERSAFE.includes(file.type);
|
||||
};
|
||||
|
||||
const isModule = (file: foundkey.entities.DriveFile): boolean => {
|
||||
return FILE_EXT_TRACKER_MODULES.some((ext) => {
|
||||
return file.name.toLowerCase().endsWith("." + ext);
|
||||
});
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
|
403
packages/client/src/components/mod-player.vue
Normal file
403
packages/client/src/components/mod-player.vue
Normal file
|
@ -0,0 +1,403 @@
|
|||
<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>
|
|
@ -46,6 +46,44 @@ export const FILE_TYPE_BROWSERSAFE = [
|
|||
'audio/x-flac',
|
||||
'audio/vnd.wave',
|
||||
];
|
||||
|
||||
export const FILE_EXT_TRACKER_MODULES = [
|
||||
'mod',
|
||||
's3m',
|
||||
'xm',
|
||||
'it',
|
||||
'mptm',
|
||||
'stm',
|
||||
'nst',
|
||||
'm15',
|
||||
'stk',
|
||||
'wow',
|
||||
'ult',
|
||||
'669',
|
||||
'mtm',
|
||||
'med',
|
||||
'far',
|
||||
'mdl',
|
||||
'ams',
|
||||
'dsm',
|
||||
'amf',
|
||||
'okt',
|
||||
'dmf',
|
||||
'ptm',
|
||||
'psm',
|
||||
'mt2',
|
||||
'dbm',
|
||||
'digi',
|
||||
'imf',
|
||||
'j2b',
|
||||
'gdm',
|
||||
'umx',
|
||||
'plm',
|
||||
'mo3',
|
||||
'xpk',
|
||||
'ppm',
|
||||
'mmcmp'
|
||||
];
|
||||
/*
|
||||
https://github.com/sindresorhus/file-type/blob/main/supported.js
|
||||
https://github.com/sindresorhus/file-type/blob/main/core.js
|
||||
|
|
266
packages/client/src/scripts/chiptune2.ts
Normal file
266
packages/client/src/scripts/chiptune2.ts
Normal file
|
@ -0,0 +1,266 @@
|
|||
/* global libopenmpt UTF8ToString writeAsciiToMemory */
|
||||
|
||||
const ChiptuneAudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
|
||||
export function ChiptuneJsConfig (repeatCount: number, context: AudioContext) {
|
||||
this.repeatCount = repeatCount;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
ChiptuneJsConfig.prototype.constructor = ChiptuneJsConfig;
|
||||
|
||||
export function ChiptuneJsPlayer (config: object) {
|
||||
this.config = config;
|
||||
this.audioContext = config.context || new ChiptuneAudioContext();
|
||||
this.context = this.audioContext.createGain();
|
||||
this.currentPlayingNode = null;
|
||||
this.handlers = [];
|
||||
this.touchLocked = true;
|
||||
this.volume = 1;
|
||||
}
|
||||
|
||||
ChiptuneJsPlayer.prototype.constructor = ChiptuneJsPlayer;
|
||||
|
||||
ChiptuneJsPlayer.prototype.fireEvent = function (eventName: string, response) {
|
||||
const handlers = this.handlers;
|
||||
if (handlers.length > 0) {
|
||||
for(const handler of handlers) {
|
||||
if (handler.eventName === eventName) {
|
||||
handler.handler(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.addHandler = function (eventName: string, handler: Function) {
|
||||
this.handlers.push({ eventName, handler });
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.onEnded = function (handler: Function) {
|
||||
this.addHandler('onEnded', handler);
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.onError = function (handler: Function) {
|
||||
this.addHandler('onError', handler);
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.duration = function () {
|
||||
return libopenmpt._openmpt_module_get_duration_seconds(this.currentPlayingNode.modulePtr);
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.position = function () {
|
||||
return libopenmpt._openmpt_module_get_position_seconds(this.currentPlayingNode.modulePtr);
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.seek = function (position: number) {
|
||||
if (this.currentPlayingNode) {
|
||||
libopenmpt._openmpt_module_set_position_seconds(this.currentPlayingNode.modulePtr, position);
|
||||
}
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.metadata = function () {
|
||||
const data = {};
|
||||
const keys = UTF8ToString(libopenmpt._openmpt_module_get_metadata_keys(this.currentPlayingNode.modulePtr)).split(';');
|
||||
let keyNameBuffer = 0;
|
||||
for (const key of keys) {
|
||||
keyNameBuffer = libopenmpt._malloc(key.length + 1);
|
||||
writeAsciiToMemory(key, keyNameBuffer);
|
||||
data[key] = UTF8ToString(libopenmpt._openmpt_module_get_metadata(this.currentPlayingNode.modulePtr, keyNameBuffer));
|
||||
libopenmpt._free(keyNameBuffer);
|
||||
}
|
||||
return data;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.unlock = function () {
|
||||
const context = this.audioContext;
|
||||
const buffer = context.createBuffer(1, 1, 22050);
|
||||
const unlockSource = context.createBufferSource();
|
||||
unlockSource.buffer = buffer;
|
||||
unlockSource.connect(this.context);
|
||||
this.context.connect(context.destination);
|
||||
unlockSource.start(0);
|
||||
this.touchLocked = false;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.load = function (input) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(this.touchLocked) {
|
||||
this.unlock();
|
||||
}
|
||||
const player = this;
|
||||
if (input instanceof File) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
resolve(reader.result);
|
||||
};
|
||||
reader.readAsArrayBuffer(input);
|
||||
} else {
|
||||
window.fetch(input).then((response) => {
|
||||
response.arrayBuffer().then((arrayBuffer) => {
|
||||
resolve(arrayBuffer);
|
||||
}).catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
}).catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.play = function (buffer: ArrayBuffer) {
|
||||
this.unlock();
|
||||
this.stop();
|
||||
const processNode = this.createLibopenmptNode(buffer, this.buffer);
|
||||
if (processNode === null) {
|
||||
return;
|
||||
}
|
||||
libopenmpt._openmpt_module_set_repeat_count(processNode.modulePtr, this.config.repeatCount || 0);
|
||||
this.currentPlayingNode = processNode;
|
||||
processNode.connect(this.context);
|
||||
this.context.connect(this.audioContext.destination);
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.stop = function () {
|
||||
if (this.currentPlayingNode != null) {
|
||||
this.currentPlayingNode.disconnect();
|
||||
this.currentPlayingNode.cleanup();
|
||||
this.currentPlayingNode = null;
|
||||
}
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.togglePause = function () {
|
||||
if (this.currentPlayingNode != null) {
|
||||
this.currentPlayingNode.togglePause();
|
||||
}
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.getPattern = function () {
|
||||
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
|
||||
return libopenmpt._openmpt_module_get_current_pattern(this.currentPlayingNode.modulePtr);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.getRow = function () {
|
||||
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
|
||||
return libopenmpt._openmpt_module_get_current_row(this.currentPlayingNode.modulePtr);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.getPatternNumRows = function (pattern: number) {
|
||||
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
|
||||
return libopenmpt._openmpt_module_get_pattern_num_rows(this.currentPlayingNode.modulePtr, pattern);
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.getPatternRowChannel = function (pattern: number, row: number, channel: number) {
|
||||
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
|
||||
return UTF8ToString(libopenmpt._openmpt_module_format_pattern_row_channel(this.currentPlayingNode.modulePtr, pattern, row, channel, 0, true));
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: object) {
|
||||
const maxFramesPerChunk = 4096;
|
||||
const processNode = this.audioContext.createScriptProcessor(2048, 0, 2);
|
||||
processNode.config = config;
|
||||
processNode.player = this;
|
||||
const byteArray = new Int8Array(buffer);
|
||||
const ptrToFile = libopenmpt._malloc(byteArray.byteLength);
|
||||
libopenmpt.HEAPU8.set(byteArray, ptrToFile);
|
||||
processNode.modulePtr = libopenmpt._openmpt_module_create_from_memory(ptrToFile, byteArray.byteLength, 0, 0, 0);
|
||||
processNode.nbChannels = libopenmpt._openmpt_module_get_num_channels(processNode.modulePtr);
|
||||
processNode.patternIndex = -1;
|
||||
processNode.paused = false;
|
||||
processNode.leftBufferPtr = libopenmpt._malloc(4 * maxFramesPerChunk);
|
||||
processNode.rightBufferPtr = libopenmpt._malloc(4 * maxFramesPerChunk);
|
||||
processNode.cleanup = function () {
|
||||
if (this.modulePtr !== 0) {
|
||||
libopenmpt._openmpt_module_destroy(this.modulePtr);
|
||||
this.modulePtr = 0;
|
||||
}
|
||||
if (this.leftBufferPtr !== 0) {
|
||||
libopenmpt._free(this.leftBufferPtr);
|
||||
this.leftBufferPtr = 0;
|
||||
}
|
||||
if (this.rightBufferPtr !== 0) {
|
||||
libopenmpt._free(this.rightBufferPtr);
|
||||
this.rightBufferPtr = 0;
|
||||
}
|
||||
};
|
||||
processNode.stop = function () {
|
||||
this.disconnect();
|
||||
this.cleanup();
|
||||
};
|
||||
processNode.pause = function () {
|
||||
this.paused = true;
|
||||
};
|
||||
processNode.unpause = function () {
|
||||
this.paused = false;
|
||||
};
|
||||
processNode.togglePause = function () {
|
||||
this.paused = !this.paused;
|
||||
};
|
||||
processNode.onaudioprocess = function (e) {
|
||||
const outputL = e.outputBuffer.getChannelData(0);
|
||||
const outputR = e.outputBuffer.getChannelData(1);
|
||||
let framesToRender = outputL.length;
|
||||
if (this.ModulePtr === 0) {
|
||||
for (let i = 0; i < framesToRender; ++i) {
|
||||
outputL[i] = 0;
|
||||
outputR[i] = 0;
|
||||
}
|
||||
this.disconnect();
|
||||
this.cleanup();
|
||||
return;
|
||||
}
|
||||
if (this.paused) {
|
||||
for (let i = 0; i < framesToRender; ++i) {
|
||||
outputL[i] = 0;
|
||||
outputR[i] = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
let framesRendered = 0;
|
||||
let ended = false;
|
||||
let error = false;
|
||||
|
||||
const currentPattern = libopenmpt._openmpt_module_get_current_pattern(this.modulePtr);
|
||||
const currentRow = libopenmpt._openmpt_module_get_current_row(this.modulePtr);
|
||||
if (currentPattern !== this.patternIndex) {
|
||||
processNode.player.fireEvent('onPatternChange');
|
||||
}
|
||||
processNode.player.fireEvent('onRowChange', { index: currentRow });
|
||||
|
||||
while (framesToRender > 0) {
|
||||
const framesPerChunk = Math.min(framesToRender, maxFramesPerChunk);
|
||||
const actualFramesPerChunk = libopenmpt._openmpt_module_read_float_stereo(this.modulePtr, this.context.sampleRate, framesPerChunk, this.leftBufferPtr, this.rightBufferPtr);
|
||||
if (actualFramesPerChunk === 0) {
|
||||
ended = true;
|
||||
// modulePtr will be 0 on openmpt: error: openmpt_module_read_float_stereo: ERROR: module * not valid or other openmpt error
|
||||
error = !this.modulePtr;
|
||||
}
|
||||
const rawAudioLeft = libopenmpt.HEAPF32.subarray(this.leftBufferPtr / 4, this.leftBufferPtr / 4 + actualFramesPerChunk);
|
||||
const rawAudioRight = libopenmpt.HEAPF32.subarray(this.rightBufferPtr / 4, this.rightBufferPtr / 4 + actualFramesPerChunk);
|
||||
for (let i = 0; i < actualFramesPerChunk; ++i) {
|
||||
outputL[framesRendered + i] = rawAudioLeft[i];
|
||||
outputR[framesRendered + i] = rawAudioRight[i];
|
||||
}
|
||||
for (let i = actualFramesPerChunk; i < framesPerChunk; ++i) {
|
||||
outputL[framesRendered + i] = 0;
|
||||
outputR[framesRendered + i] = 0;
|
||||
}
|
||||
framesToRender -= framesPerChunk;
|
||||
framesRendered += framesPerChunk;
|
||||
}
|
||||
if (ended) {
|
||||
this.disconnect();
|
||||
this.cleanup();
|
||||
error ? processNode.player.fireEvent('onError', { type: 'openmpt' }) : processNode.player.fireEvent('onEnded');
|
||||
}
|
||||
};
|
||||
return processNode;
|
||||
};
|
Loading…
Reference in a new issue