Formatting a couple of things

This commit is contained in:
Essem 2023-07-02 19:58:43 -05:00
parent c6d1adc4b2
commit ec60a4a4ff
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
2 changed files with 153 additions and 74 deletions

View file

@ -47,45 +47,45 @@ export const FILE_TYPE_TRACKER_MODULES = [
"audio/xm", "audio/xm",
"audio/x-xm", "audio/x-xm",
"audio/it", "audio/it",
"audio/x-it" "audio/x-it",
]; ];
export const FILE_EXT_TRACKER_MODULES = [ export const FILE_EXT_TRACKER_MODULES = [
'mod', "mod",
's3m', "s3m",
'xm', "xm",
'it', "it",
'mptm', "mptm",
'stm', "stm",
'nst', "nst",
'm15', "m15",
'stk', "stk",
'wow', "wow",
'ult', "ult",
'669', "669",
'mtm', "mtm",
'med', "med",
'far', "far",
'mdl', "mdl",
'ams', "ams",
'dsm', "dsm",
'amf', "amf",
'okt', "okt",
'dmf', "dmf",
'ptm', "ptm",
'psm', "psm",
'mt2', "mt2",
'dbm', "dbm",
'digi', "digi",
'imf', "imf",
'j2b', "j2b",
'gdm', "gdm",
'umx', "umx",
'plm', "plm",
'mo3', "mo3",
'xpk', "xpk",
'ppm', "ppm",
'mmcmp' "mmcmp",
]; ];
/* /*
https://github.com/sindresorhus/file-type/blob/main/supported.js https://github.com/sindresorhus/file-type/blob/main/supported.js

View file

@ -2,14 +2,14 @@
const ChiptuneAudioContext = window.AudioContext || window.webkitAudioContext; const ChiptuneAudioContext = window.AudioContext || window.webkitAudioContext;
export function ChiptuneJsConfig (repeatCount?: number, context?: AudioContext) { export function ChiptuneJsConfig(repeatCount?: number, context?: AudioContext) {
this.repeatCount = repeatCount; this.repeatCount = repeatCount;
this.context = context; this.context = context;
} }
ChiptuneJsConfig.prototype.constructor = ChiptuneJsConfig; ChiptuneJsConfig.prototype.constructor = ChiptuneJsConfig;
export function ChiptuneJsPlayer (config: object) { export function ChiptuneJsPlayer(config: object) {
this.config = config; this.config = config;
this.audioContext = config.context || new ChiptuneAudioContext(); this.audioContext = config.context || new ChiptuneAudioContext();
this.context = this.audioContext.createGain(); this.context = this.audioContext.createGain();
@ -24,7 +24,7 @@ ChiptuneJsPlayer.prototype.constructor = ChiptuneJsPlayer;
ChiptuneJsPlayer.prototype.fireEvent = function (eventName: string, response) { ChiptuneJsPlayer.prototype.fireEvent = function (eventName: string, response) {
const handlers = this.handlers; const handlers = this.handlers;
if (handlers.length > 0) { if (handlers.length > 0) {
for(const handler of handlers) { for (const handler of handlers) {
if (handler.eventName === eventName) { if (handler.eventName === eventName) {
handler.handler(response); handler.handler(response);
} }
@ -32,7 +32,10 @@ ChiptuneJsPlayer.prototype.fireEvent = function (eventName: string, response) {
} }
}; };
ChiptuneJsPlayer.prototype.addHandler = function (eventName: string, handler: Function) { ChiptuneJsPlayer.prototype.addHandler = function (
eventName: string,
handler: Function,
) {
this.handlers.push({ eventName, handler }); this.handlers.push({ eventName, handler });
}; };
@ -41,35 +44,51 @@ ChiptuneJsPlayer.prototype.clearHandlers = function () {
}; };
ChiptuneJsPlayer.prototype.onEnded = function (handler: Function) { ChiptuneJsPlayer.prototype.onEnded = function (handler: Function) {
this.addHandler('onEnded', handler); this.addHandler("onEnded", handler);
}; };
ChiptuneJsPlayer.prototype.onError = function (handler: Function) { ChiptuneJsPlayer.prototype.onError = function (handler: Function) {
this.addHandler('onError', handler); this.addHandler("onError", handler);
}; };
ChiptuneJsPlayer.prototype.duration = function () { ChiptuneJsPlayer.prototype.duration = function () {
return libopenmpt._openmpt_module_get_duration_seconds(this.currentPlayingNode.modulePtr); return libopenmpt._openmpt_module_get_duration_seconds(
this.currentPlayingNode.modulePtr,
);
}; };
ChiptuneJsPlayer.prototype.position = function () { ChiptuneJsPlayer.prototype.position = function () {
return libopenmpt._openmpt_module_get_position_seconds(this.currentPlayingNode.modulePtr); return libopenmpt._openmpt_module_get_position_seconds(
this.currentPlayingNode.modulePtr,
);
}; };
ChiptuneJsPlayer.prototype.seek = function (position: number) { ChiptuneJsPlayer.prototype.seek = function (position: number) {
if (this.currentPlayingNode) { if (this.currentPlayingNode) {
libopenmpt._openmpt_module_set_position_seconds(this.currentPlayingNode.modulePtr, position); libopenmpt._openmpt_module_set_position_seconds(
this.currentPlayingNode.modulePtr,
position,
);
} }
}; };
ChiptuneJsPlayer.prototype.metadata = function () { ChiptuneJsPlayer.prototype.metadata = function () {
const data = {}; const data = {};
const keys = UTF8ToString(libopenmpt._openmpt_module_get_metadata_keys(this.currentPlayingNode.modulePtr)).split(';'); const keys = UTF8ToString(
libopenmpt._openmpt_module_get_metadata_keys(
this.currentPlayingNode.modulePtr,
),
).split(";");
let keyNameBuffer = 0; let keyNameBuffer = 0;
for (const key of keys) { for (const key of keys) {
keyNameBuffer = libopenmpt._malloc(key.length + 1); keyNameBuffer = libopenmpt._malloc(key.length + 1);
writeAsciiToMemory(key, keyNameBuffer); writeAsciiToMemory(key, keyNameBuffer);
data[key] = UTF8ToString(libopenmpt._openmpt_module_get_metadata(this.currentPlayingNode.modulePtr, keyNameBuffer)); data[key] = UTF8ToString(
libopenmpt._openmpt_module_get_metadata(
this.currentPlayingNode.modulePtr,
keyNameBuffer,
),
);
libopenmpt._free(keyNameBuffer); libopenmpt._free(keyNameBuffer);
} }
return data; return data;
@ -88,7 +107,7 @@ ChiptuneJsPlayer.prototype.unlock = function () {
ChiptuneJsPlayer.prototype.load = function (input) { ChiptuneJsPlayer.prototype.load = function (input) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(this.touchLocked) { if (this.touchLocked) {
this.unlock(); this.unlock();
} }
const player = this; const player = this;
@ -99,13 +118,19 @@ ChiptuneJsPlayer.prototype.load = function (input) {
}; };
reader.readAsArrayBuffer(input); reader.readAsArrayBuffer(input);
} else { } else {
window.fetch(input).then((response) => { window
response.arrayBuffer().then((arrayBuffer) => { .fetch(input)
.then((response) => {
response
.arrayBuffer()
.then((arrayBuffer) => {
resolve(arrayBuffer); resolve(arrayBuffer);
}).catch((error) => { })
.catch((error) => {
reject(error); reject(error);
}); });
}).catch((error) => { })
.catch((error) => {
reject(error); reject(error);
}); });
} }
@ -119,7 +144,10 @@ ChiptuneJsPlayer.prototype.play = function (buffer: ArrayBuffer) {
if (processNode === null) { if (processNode === null) {
return; return;
} }
libopenmpt._openmpt_module_set_repeat_count(processNode.modulePtr, this.config.repeatCount || 0); libopenmpt._openmpt_module_set_repeat_count(
processNode.modulePtr,
this.config.repeatCount || 0,
);
this.currentPlayingNode = processNode; this.currentPlayingNode = processNode;
processNode.connect(this.context); processNode.connect(this.context);
this.context.connect(this.audioContext.destination); this.context.connect(this.audioContext.destination);
@ -141,40 +169,65 @@ ChiptuneJsPlayer.prototype.togglePause = function () {
ChiptuneJsPlayer.prototype.getPattern = function () { ChiptuneJsPlayer.prototype.getPattern = function () {
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) { if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
return libopenmpt._openmpt_module_get_current_pattern(this.currentPlayingNode.modulePtr); return libopenmpt._openmpt_module_get_current_pattern(
this.currentPlayingNode.modulePtr,
);
} }
return 0; return 0;
}; };
ChiptuneJsPlayer.prototype.getRow = function () { ChiptuneJsPlayer.prototype.getRow = function () {
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) { if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
return libopenmpt._openmpt_module_get_current_row(this.currentPlayingNode.modulePtr); return libopenmpt._openmpt_module_get_current_row(
this.currentPlayingNode.modulePtr,
);
} }
return 0; return 0;
}; };
ChiptuneJsPlayer.prototype.getNumPatterns = function () { ChiptuneJsPlayer.prototype.getNumPatterns = function () {
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) { if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
return libopenmpt._openmpt_module_get_num_patterns(this.currentPlayingNode.modulePtr); return libopenmpt._openmpt_module_get_num_patterns(
this.currentPlayingNode.modulePtr,
);
} }
return 0; return 0;
}; };
ChiptuneJsPlayer.prototype.getPatternNumRows = function (pattern: number) { ChiptuneJsPlayer.prototype.getPatternNumRows = function (pattern: number) {
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) { if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
return libopenmpt._openmpt_module_get_pattern_num_rows(this.currentPlayingNode.modulePtr, pattern); return libopenmpt._openmpt_module_get_pattern_num_rows(
this.currentPlayingNode.modulePtr,
pattern,
);
} }
return 0; return 0;
}; };
ChiptuneJsPlayer.prototype.getPatternRowChannel = function (pattern: number, row: number, channel: number) { ChiptuneJsPlayer.prototype.getPatternRowChannel = function (
pattern: number,
row: number,
channel: number,
) {
if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) { if (this.currentPlayingNode && this.currentPlayingNode.modulePtr) {
return UTF8ToString(libopenmpt._openmpt_module_format_pattern_row_channel(this.currentPlayingNode.modulePtr, pattern, row, channel, 0, true)); return UTF8ToString(
libopenmpt._openmpt_module_format_pattern_row_channel(
this.currentPlayingNode.modulePtr,
pattern,
row,
channel,
0,
true,
),
);
} }
return ''; return "";
}; };
ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: object) { ChiptuneJsPlayer.prototype.createLibopenmptNode = function (
buffer,
config: object,
) {
const maxFramesPerChunk = 4096; const maxFramesPerChunk = 4096;
const processNode = this.audioContext.createScriptProcessor(2048, 0, 2); const processNode = this.audioContext.createScriptProcessor(2048, 0, 2);
processNode.config = config; processNode.config = config;
@ -182,8 +235,16 @@ ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: obje
const byteArray = new Int8Array(buffer); const byteArray = new Int8Array(buffer);
const ptrToFile = libopenmpt._malloc(byteArray.byteLength); const ptrToFile = libopenmpt._malloc(byteArray.byteLength);
libopenmpt.HEAPU8.set(byteArray, ptrToFile); libopenmpt.HEAPU8.set(byteArray, ptrToFile);
processNode.modulePtr = libopenmpt._openmpt_module_create_from_memory(ptrToFile, byteArray.byteLength, 0, 0, 0); processNode.modulePtr = libopenmpt._openmpt_module_create_from_memory(
processNode.nbChannels = libopenmpt._openmpt_module_get_num_channels(processNode.modulePtr); ptrToFile,
byteArray.byteLength,
0,
0,
0,
);
processNode.nbChannels = libopenmpt._openmpt_module_get_num_channels(
processNode.modulePtr,
);
processNode.patternIndex = -1; processNode.patternIndex = -1;
processNode.paused = false; processNode.paused = false;
processNode.leftBufferPtr = libopenmpt._malloc(4 * maxFramesPerChunk); processNode.leftBufferPtr = libopenmpt._malloc(4 * maxFramesPerChunk);
@ -239,23 +300,39 @@ ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: obje
let ended = false; let ended = false;
let error = false; let error = false;
const currentPattern = libopenmpt._openmpt_module_get_current_pattern(this.modulePtr); const currentPattern = libopenmpt._openmpt_module_get_current_pattern(
const currentRow = libopenmpt._openmpt_module_get_current_row(this.modulePtr); this.modulePtr,
);
const currentRow = libopenmpt._openmpt_module_get_current_row(
this.modulePtr,
);
if (currentPattern !== this.patternIndex) { if (currentPattern !== this.patternIndex) {
processNode.player.fireEvent('onPatternChange'); processNode.player.fireEvent("onPatternChange");
} }
processNode.player.fireEvent('onRowChange', { index: currentRow }); processNode.player.fireEvent("onRowChange", { index: currentRow });
while (framesToRender > 0) { while (framesToRender > 0) {
const framesPerChunk = Math.min(framesToRender, maxFramesPerChunk); const framesPerChunk = Math.min(framesToRender, maxFramesPerChunk);
const actualFramesPerChunk = libopenmpt._openmpt_module_read_float_stereo(this.modulePtr, this.context.sampleRate, framesPerChunk, this.leftBufferPtr, this.rightBufferPtr); const actualFramesPerChunk = libopenmpt._openmpt_module_read_float_stereo(
this.modulePtr,
this.context.sampleRate,
framesPerChunk,
this.leftBufferPtr,
this.rightBufferPtr,
);
if (actualFramesPerChunk === 0) { if (actualFramesPerChunk === 0) {
ended = true; ended = true;
// modulePtr will be 0 on openmpt: error: openmpt_module_read_float_stereo: ERROR: module * not valid or other openmpt error // modulePtr will be 0 on openmpt: error: openmpt_module_read_float_stereo: ERROR: module * not valid or other openmpt error
error = !this.modulePtr; error = !this.modulePtr;
} }
const rawAudioLeft = libopenmpt.HEAPF32.subarray(this.leftBufferPtr / 4, this.leftBufferPtr / 4 + actualFramesPerChunk); const rawAudioLeft = libopenmpt.HEAPF32.subarray(
const rawAudioRight = libopenmpt.HEAPF32.subarray(this.rightBufferPtr / 4, this.rightBufferPtr / 4 + actualFramesPerChunk); 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) { for (let i = 0; i < actualFramesPerChunk; ++i) {
outputL[framesRendered + i] = rawAudioLeft[i]; outputL[framesRendered + i] = rawAudioLeft[i];
outputR[framesRendered + i] = rawAudioRight[i]; outputR[framesRendered + i] = rawAudioRight[i];
@ -270,7 +347,9 @@ ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: obje
if (ended) { if (ended) {
this.disconnect(); this.disconnect();
this.cleanup(); this.cleanup();
error ? processNode.player.fireEvent('onError', { type: 'openmpt' }) : processNode.player.fireEvent('onEnded'); error
? processNode.player.fireEvent("onError", { type: "openmpt" })
: processNode.player.fireEvent("onEnded");
} }
}; };
return processNode; return processNode;