/** * RiffDex's entry into alphaTab (https://github.com/CoderLine/alphaTab, MPL-2.0): its Guitar Pro (3–8) and MusicXML * importers and the score model they produce, without alphaTab's rendering, audio or browser set-up. * scripts/alphatab/build-importer.mjs bundles this with alphaTab's unchanged source into * src/vendor/alphatab/alphatab-importer.js; src/vendor/alphatab/README.md says how and why. * * The importers are tried in the order alphaTab's own ScoreLoader.loadScoreFromBytes tries them (Environment.buildImporters), * leaving out the ones RiffDex doesn't offer: Capella and alphaTex. */ import { Gp3To5Importer } from '@coderline/alphatab/importer/Gp3To5Importer'; import { GpxImporter } from '@coderline/alphatab/importer/GpxImporter'; import { Gp7To8Importer } from '@coderline/alphatab/importer/Gp7To8Importer'; import { MusicXmlImporter } from '@coderline/alphatab/importer/MusicXmlImporter'; import { UnsupportedFormatError } from '@coderline/alphatab/importer/UnsupportedFormatError'; import type { ScoreImporter } from '@coderline/alphatab/importer/ScoreImporter'; import { ByteBuffer } from '@coderline/alphatab/io/ByteBuffer'; import { ThrowingReadable } from '@coderline/alphatab/io/IReadable'; import { Logger, type ILogger } from '@coderline/alphatab/Logger'; import { LogLevel } from '@coderline/alphatab/LogLevel'; import type { Score } from '@coderline/alphatab/model/Score'; import { Settings } from '@coderline/alphatab/Settings'; import { VersionInfo } from '@coderline/alphatab/generated/VersionInfo'; export { Duration } from '@coderline/alphatab/model/Duration'; export { GraceType } from '@coderline/alphatab/model/GraceType'; export { SlideInType } from '@coderline/alphatab/model/SlideInType'; export { SlideOutType } from '@coderline/alphatab/model/SlideOutType'; export { HarmonicType } from '@coderline/alphatab/model/HarmonicType'; export { BendType } from '@coderline/alphatab/model/BendType'; export { VibratoType } from '@coderline/alphatab/model/VibratoType'; export { AccentuationType } from '@coderline/alphatab/model/AccentuationType'; export { PickStroke } from '@coderline/alphatab/model/PickStroke'; export { BrushType } from '@coderline/alphatab/model/BrushType'; export { WhammyType } from '@coderline/alphatab/model/WhammyType'; export { Fingers } from '@coderline/alphatab/model/Fingers'; export { Direction } from '@coderline/alphatab/model/Direction'; export { TripletFeel } from '@coderline/alphatab/model/TripletFeel'; /** The decoder the importers use in place of TextDecoder (exported so RiffDex's tests can compare it with a native one). */ export { RiffDexTextDecoder } from './text-decoder'; export const alphaTabVersion: string = VersionInfo.version; export const alphaTabCommit: string = VersionInfo.commit; /** Reads a Guitar Pro (3–8) or MusicXML file. alphaTab's warnings and errors are returned, not printed. */ export function readScore(data: Uint8Array): { score: Score; importer: string; warnings: string[] } { const warnings: string[] = []; const collect: ILogger = { debug() {}, info() {}, warning(category: string, msg: string) { warnings.push(`${category}: ${msg}`); }, error(category: string, msg: string) { warnings.push(`${category}: ${msg}`); }, }; const previous = { log: Logger.log, level: Logger.logLevel }; Logger.log = collect; Logger.logLevel = LogLevel.Warning; try { const settings = new Settings(); const importers: ScoreImporter[] = [new Gp3To5Importer(), new GpxImporter(), new Gp7To8Importer(), new MusicXmlImporter()]; const readable = new ThrowingReadable(ByteBuffer.fromBuffer(data)); for (const importer of importers) { readable.reset(); try { importer.init(readable, settings); return { score: importer.readScore(), importer: importer.name, warnings }; } catch (e) { if (!(e instanceof UnsupportedFormatError)) throw e; } } throw new UnsupportedFormatError('No compatible importer found for file'); } finally { Logger.log = previous.log; Logger.logLevel = previous.level; } }