Formatting a couple of things
This commit is contained in:
parent
c6d1adc4b2
commit
ec60a4a4ff
2 changed files with 153 additions and 74 deletions
|
@ -47,45 +47,45 @@ export const FILE_TYPE_TRACKER_MODULES = [
|
|||
"audio/xm",
|
||||
"audio/x-xm",
|
||||
"audio/it",
|
||||
"audio/x-it"
|
||||
"audio/x-it",
|
||||
];
|
||||
|
||||
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'
|
||||
"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
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
const ChiptuneAudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
|
||||
export function ChiptuneJsConfig (repeatCount?: number, context?: AudioContext) {
|
||||
export function ChiptuneJsConfig(repeatCount?: number, context?: AudioContext) {
|
||||
this.repeatCount = repeatCount;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
ChiptuneJsConfig.prototype.constructor = ChiptuneJsConfig;
|
||||
|
||||
export function ChiptuneJsPlayer (config: object) {
|
||||
export function ChiptuneJsPlayer(config: object) {
|
||||
this.config = config;
|
||||
this.audioContext = config.context || new ChiptuneAudioContext();
|
||||
this.context = this.audioContext.createGain();
|
||||
|
@ -24,7 +24,7 @@ ChiptuneJsPlayer.prototype.constructor = ChiptuneJsPlayer;
|
|||
ChiptuneJsPlayer.prototype.fireEvent = function (eventName: string, response) {
|
||||
const handlers = this.handlers;
|
||||
if (handlers.length > 0) {
|
||||
for(const handler of handlers) {
|
||||
for (const handler of handlers) {
|
||||
if (handler.eventName === eventName) {
|
||||
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 });
|
||||
};
|
||||
|
||||
|
@ -41,35 +44,51 @@ ChiptuneJsPlayer.prototype.clearHandlers = function () {
|
|||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.onEnded = function (handler: Function) {
|
||||
this.addHandler('onEnded', handler);
|
||||
this.addHandler("onEnded", handler);
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.onError = function (handler: Function) {
|
||||
this.addHandler('onError', handler);
|
||||
this.addHandler("onError", handler);
|
||||
};
|
||||
|
||||
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 () {
|
||||
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) {
|
||||
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 () {
|
||||
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;
|
||||
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));
|
||||
data[key] = UTF8ToString(
|
||||
libopenmpt._openmpt_module_get_metadata(
|
||||
this.currentPlayingNode.modulePtr,
|
||||
keyNameBuffer,
|
||||
),
|
||||
);
|
||||
libopenmpt._free(keyNameBuffer);
|
||||
}
|
||||
return data;
|
||||
|
@ -88,7 +107,7 @@ ChiptuneJsPlayer.prototype.unlock = function () {
|
|||
|
||||
ChiptuneJsPlayer.prototype.load = function (input) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(this.touchLocked) {
|
||||
if (this.touchLocked) {
|
||||
this.unlock();
|
||||
}
|
||||
const player = this;
|
||||
|
@ -99,13 +118,19 @@ ChiptuneJsPlayer.prototype.load = function (input) {
|
|||
};
|
||||
reader.readAsArrayBuffer(input);
|
||||
} else {
|
||||
window.fetch(input).then((response) => {
|
||||
response.arrayBuffer().then((arrayBuffer) => {
|
||||
window
|
||||
.fetch(input)
|
||||
.then((response) => {
|
||||
response
|
||||
.arrayBuffer()
|
||||
.then((arrayBuffer) => {
|
||||
resolve(arrayBuffer);
|
||||
}).catch((error) => {
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
}).catch((error) => {
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
}
|
||||
|
@ -119,7 +144,10 @@ ChiptuneJsPlayer.prototype.play = function (buffer: ArrayBuffer) {
|
|||
if (processNode === null) {
|
||||
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;
|
||||
processNode.connect(this.context);
|
||||
this.context.connect(this.audioContext.destination);
|
||||
|
@ -141,40 +169,65 @@ ChiptuneJsPlayer.prototype.togglePause = function () {
|
|||
|
||||
ChiptuneJsPlayer.prototype.getPattern = function () {
|
||||
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;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.getRow = function () {
|
||||
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;
|
||||
};
|
||||
|
||||
ChiptuneJsPlayer.prototype.getNumPatterns = function () {
|
||||
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;
|
||||
};
|
||||
|
||||
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 libopenmpt._openmpt_module_get_pattern_num_rows(
|
||||
this.currentPlayingNode.modulePtr,
|
||||
pattern,
|
||||
);
|
||||
}
|
||||
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) {
|
||||
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 processNode = this.audioContext.createScriptProcessor(2048, 0, 2);
|
||||
processNode.config = config;
|
||||
|
@ -182,8 +235,16 @@ ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: obje
|
|||
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.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);
|
||||
|
@ -239,23 +300,39 @@ ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: obje
|
|||
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);
|
||||
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("onPatternChange");
|
||||
}
|
||||
processNode.player.fireEvent('onRowChange', { index: currentRow });
|
||||
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);
|
||||
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);
|
||||
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];
|
||||
|
@ -270,7 +347,9 @@ ChiptuneJsPlayer.prototype.createLibopenmptNode = function (buffer, config: obje
|
|||
if (ended) {
|
||||
this.disconnect();
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue