File size: 15,515 Bytes
b0bfea8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
import { readBytesAsString } from "../utils/byte_functions/string.js";
import { SpessaSynthGroup, SpessaSynthGroupEnd, SpessaSynthInfo, SpessaSynthWarn } from "../utils/loggin.js";
import { consoleColors } from "../utils/other.js";
import { readBytesAsUintBigEndian } from "../utils/byte_functions/big_endian.js";
import { readVariableLengthQuantity } from "../utils/byte_functions/variable_length_quantity.js";
import { RMIDINFOChunks } from "./rmidi_writer.js";
import { inflateSync } from "../externals/fflate/fflate.min.js";
import { IndexedByteArray } from "../utils/indexed_array.js";
/**
* @enum {number}
*/
const metadataTypes = {
XMFFileType: 0,
nodeName: 1,
nodeIDNumber: 2,
resourceFormat: 3,
filenameOnDisk: 4,
filenameExtensionOnDisk: 5,
macOSFileTypeAndCreator: 6,
mimeType: 7,
title: 8,
copyrightNotice: 9,
comment: 10,
autoStart: 11, // Node Name of the FileNode containing the SMF image to autostart when the XMF file loads
preload: 12, // Used to preload specific SMF and DLS file images.
contentDescription: 13, // RP-42a (https://amei.or.jp/midistandardcommittee/Recommended_Practice/e/rp42.pdf)
ID3Metadata: 14 // RP-47 (https://amei.or.jp/midistandardcommittee/Recommended_Practice/e/rp47.pdf)
};
/**
* @enum {number}
*/
const referenceTypeIds = {
inLineResource: 1,
inFileResource: 2,
inFileNode: 3,
externalFile: 4,
externalXMF: 5,
XMFFileURIandNodeID: 6
};
/**
* @enum {number}
*/
const resourceFormatIDs = {
StandardMIDIFile: 0,
StandardMIDIFileType1: 1,
DLS1: 2,
DLS2: 3,
DLS22: 4,
mobileDLS: 5
};
/**
* @enum {number}
*/
const formatTypeIDs = {
standard: 0,
MMA: 1,
registered: 2,
nonRegistered: 3
};
/**
* @enum {number}
*/
const unpackerIDs = {
none: 0,
MMAUnpacker: 1,
registered: 2,
nonRegistered: 3
};
class XMFNode
{
/**
* @type {number}
*/
length;
/**
* 0 means it's a file node
* @type {number}
*/
itemCount;
/**
* @type {number}
*/
metadataLength;
/**
* @type {Object<string, any>}
*/
metadata = {};
/**
* @type {IndexedByteArray}
*/
nodeData;
/**
* @type {XMFNode[]}
*/
innerNodes = [];
packedContent = false;
nodeUnpackers = [];
/**
* @type {"StandardMIDIFile"|
* "StandardMIDIFileType1"|
* "DLS1"|
* "DLS2"|
* "DLS22"|
* "mobileDLS"|
* "unknown"|"folder"}
*/
resourceFormat = "unknown";
/**
* @param binaryData {IndexedByteArray}
*/
constructor(binaryData)
{
let nodeStartIndex = binaryData.currentIndex;
this.length = readVariableLengthQuantity(binaryData);
this.itemCount = readVariableLengthQuantity(binaryData);
// header length
const headerLength = readVariableLengthQuantity(binaryData);
const readBytes = binaryData.currentIndex - nodeStartIndex;
const remainingHeader = headerLength - readBytes;
const headerData = binaryData.slice(
binaryData.currentIndex,
binaryData.currentIndex + remainingHeader
);
binaryData.currentIndex += remainingHeader;
this.metadataLength = readVariableLengthQuantity(headerData);
const metadataChunk = headerData.slice(
headerData.currentIndex,
headerData.currentIndex + this.metadataLength
);
headerData.currentIndex += this.metadataLength;
/**
* @type {metadataTypes|string|number}
*/
let fieldSpecifier;
let key;
while (metadataChunk.currentIndex < metadataChunk.length)
{
const firstSpecifierByte = metadataChunk[metadataChunk.currentIndex];
if (firstSpecifierByte === 0)
{
metadataChunk.currentIndex++;
fieldSpecifier = readVariableLengthQuantity(metadataChunk);
if (Object.values(metadataTypes).indexOf(fieldSpecifier) === -1)
{
SpessaSynthWarn(`Unknown field specifier: ${fieldSpecifier}`);
key = `unknown_${fieldSpecifier}`;
}
else
{
key = Object.keys(metadataTypes).find(k => metadataTypes[k] === fieldSpecifier);
}
}
else
{
// this is the length of string
const stringLength = readVariableLengthQuantity(metadataChunk);
fieldSpecifier = readBytesAsString(metadataChunk, stringLength);
key = fieldSpecifier;
}
const numberOfVersions = readVariableLengthQuantity(metadataChunk);
if (numberOfVersions === 0)
{
const dataLength = readVariableLengthQuantity(metadataChunk);
const contentsChunk = metadataChunk.slice(
metadataChunk.currentIndex,
metadataChunk.currentIndex + dataLength
);
metadataChunk.currentIndex += dataLength;
const formatID = readVariableLengthQuantity(contentsChunk);
// text only
if (formatID < 4)
{
this.metadata[key] = readBytesAsString(contentsChunk, dataLength - 1);
}
else
{
this.metadata[key] = contentsChunk.slice(contentsChunk.currentIndex);
}
}
else
{
// throw new Error ("International content is not supported.");
// Skip the number of versions
SpessaSynthWarn(`International content: ${numberOfVersions}`);
// Length in bytes
// Skip the whole thing!
metadataChunk.currentIndex += readVariableLengthQuantity(metadataChunk);
}
}
const unpackersStart = headerData.currentIndex;
const unpackersLength = readVariableLengthQuantity(headerData);
const unpackersData = headerData.slice(headerData.currentIndex, unpackersStart + unpackersLength);
headerData.currentIndex = unpackersStart + unpackersLength;
if (unpackersLength > 0)
{
this.packedContent = true;
while (unpackersData.currentIndex < unpackersLength)
{
const unpacker = {};
unpacker.id = readVariableLengthQuantity(unpackersData);
switch (unpacker.id)
{
case unpackerIDs.nonRegistered:
case unpackerIDs.registered:
SpessaSynthGroupEnd();
throw new Error(`Unsupported unpacker ID: ${unpacker.id}`);
default:
SpessaSynthGroupEnd();
throw new Error(`Unknown unpacker ID: ${unpacker.id}`);
case unpackerIDs.none:
unpacker.standardID = readVariableLengthQuantity(unpackersData);
break;
case unpackerIDs.MMAUnpacker:
let manufacturerID = unpackersData[unpackersData.currentIndex++];
// one or three byte form, depending on if the first byte is zero
if (manufacturerID === 0)
{
manufacturerID <<= 8;
manufacturerID |= unpackersData[unpackersData.currentIndex++];
manufacturerID <<= 8;
manufacturerID |= unpackersData[unpackersData.currentIndex++];
}
const manufacturerInternalID = readVariableLengthQuantity(unpackersData);
unpacker.manufacturerID = manufacturerID;
unpacker.manufacturerInternalID = manufacturerInternalID;
break;
}
unpacker.decodedSize = readVariableLengthQuantity(unpackersData);
this.nodeUnpackers.push(unpacker);
}
}
binaryData.currentIndex = nodeStartIndex + headerLength;
/**
* @type {referenceTypeIds|number}
*/
this.referenceTypeID = readVariableLengthQuantity(binaryData);
this.nodeData = binaryData.slice(binaryData.currentIndex, nodeStartIndex + this.length);
binaryData.currentIndex = nodeStartIndex + this.length;
switch (this.referenceTypeID)
{
case referenceTypeIds.inLineResource:
break;
case referenceTypeIds.externalXMF:
case referenceTypeIds.inFileNode:
case referenceTypeIds.XMFFileURIandNodeID:
case referenceTypeIds.externalFile:
case referenceTypeIds.inFileResource:
SpessaSynthGroupEnd();
throw new Error(`Unsupported reference type: ${this.referenceTypeID}`);
default:
SpessaSynthGroupEnd();
throw new Error(`Unknown reference type: ${this.referenceTypeID}`);
}
// read the data
if (this.isFile)
{
if (this.packedContent)
{
const compressed = this.nodeData.slice(2, this.nodeData.length);
SpessaSynthInfo(
`%cPacked content. Attemting to deflate. Target size: %c${this.nodeUnpackers[0].decodedSize}`,
consoleColors.warn,
consoleColors.value
);
try
{
this.nodeData = new IndexedByteArray(inflateSync(compressed).buffer);
}
catch (e)
{
SpessaSynthGroupEnd();
throw new Error(`Error unpacking XMF file contents: ${e.message}.`);
}
}
/**
* interpret the content
* @type {number[]}
*/
const resourceFormat = this.metadata["resourceFormat"];
if (resourceFormat === undefined)
{
SpessaSynthWarn("No resource format for this file node!");
}
else
{
const formatTypeID = resourceFormat[0];
if (formatTypeID !== formatTypeIDs.standard)
{
SpessaSynthWarn(`Non-standard formatTypeID: ${resourceFormat}`);
this.resourceFormat = resourceFormat.toString();
}
const resourceFormatID = resourceFormat[1];
if (Object.values(resourceFormatIDs).indexOf(resourceFormatID) === -1)
{
SpessaSynthWarn(`Unrecognized resource format: ${resourceFormatID}`);
}
else
{
this.resourceFormat = Object.keys(resourceFormatIDs)
.find(k => resourceFormatIDs[k] === resourceFormatID);
}
}
}
else
{
// folder node
this.resourceFormat = "folder";
while (this.nodeData.currentIndex < this.nodeData.length)
{
const nodeStartIndex = this.nodeData.currentIndex;
const nodeLength = readVariableLengthQuantity(this.nodeData);
const nodeData = this.nodeData.slice(nodeStartIndex, nodeStartIndex + nodeLength);
this.nodeData.currentIndex = nodeStartIndex + nodeLength;
this.innerNodes.push(new XMFNode(nodeData));
}
}
}
get isFile()
{
return this.itemCount === 0;
}
}
/**
* @param midi {MIDI}
* @param binaryData {IndexedByteArray}
* @returns {IndexedByteArray} the file byte array
*/
export function loadXMF(midi, binaryData)
{
midi.bankOffset = 0;
// https://amei.or.jp/midistandardcommittee/Recommended_Practice/e/xmf-v1a.pdf
// https://wiki.multimedia.cx/index.php?title=Extensible_Music_Format_(XMF)
const sanityCheck = readBytesAsString(binaryData, 4);
if (sanityCheck !== "XMF_")
{
SpessaSynthGroupEnd();
throw new SyntaxError(`Invalid XMF Header! Expected "_XMF", got "${sanityCheck}"`);
}
SpessaSynthGroup("%cParsing XMF file...", consoleColors.info);
const version = readBytesAsString(binaryData, 4);
SpessaSynthInfo(
`%cXMF version: %c${version}`,
consoleColors.info, consoleColors.recognized
);
// https://amei.or.jp/midistandardcommittee/Recommended_Practice/e/rp43.pdf
// version 2.00 has additional bytes
if (version === "2.00")
{
const fileTypeId = readBytesAsUintBigEndian(binaryData, 4);
const fileTypeRevisionId = readBytesAsUintBigEndian(binaryData, 4);
SpessaSynthInfo(
`%cFile Type ID: %c${fileTypeId}%c, File Type Revision ID: %c${fileTypeRevisionId}`,
consoleColors.info,
consoleColors.recognized,
consoleColors.info,
consoleColors.recognized
);
}
// file length
readVariableLengthQuantity(binaryData);
const metadataTableLength = readVariableLengthQuantity(binaryData);
// skip metadata
binaryData.currentIndex += metadataTableLength;
// skip to tree root
binaryData.currentIndex = readVariableLengthQuantity(binaryData);
const rootNode = new XMFNode(binaryData);
/**
* @type {IndexedByteArray}
*/
let midiArray;
/**
* find the stuff we care about
* @param node {XMFNode}
*/
const searchNode = node =>
{
const checkMeta = (xmf, rmid) =>
{
if (node.metadata[xmf] !== undefined && typeof node.metadata[xmf] === "string")
{
midi.RMIDInfo[rmid] = node.metadata[xmf];
}
};
// meta
checkMeta("nodeName", RMIDINFOChunks.name);
checkMeta("title", RMIDINFOChunks.name);
checkMeta("copyrightNotice", RMIDINFOChunks.copyright);
checkMeta("comment", RMIDINFOChunks.comment);
if (node.isFile)
{
switch (node.resourceFormat)
{
default:
return;
case "DLS1":
case "DLS2":
case "DLS22":
case "mobileDLS":
SpessaSynthInfo("%cFound embedded DLS!", consoleColors.recognized);
midi.embeddedSoundFont = node.nodeData.buffer;
break;
case "StandardMIDIFile":
case "StandardMIDIFileType1":
SpessaSynthInfo("%cFound embedded MIDI!", consoleColors.recognized);
midiArray = node.nodeData;
break;
}
}
else
{
for (const n of node.innerNodes)
{
searchNode(n);
}
}
};
searchNode(rootNode);
SpessaSynthGroupEnd();
return midiArray;
} |