|
| 1 | +import { |
| 2 | + fromArrayBuffer, |
| 3 | + Instrument, |
| 4 | + Song, |
| 5 | + type FromArrayBufferOptions, |
| 6 | +} from '@encode42/nbs.js'; |
| 7 | + |
| 8 | +/** Default instruments 0–15 (NBS v5 and below). */ |
| 9 | +export const NBS_V5_FIRST_CUSTOM = 16; |
| 10 | + |
| 11 | +/** First custom instrument index in NBS v6. */ |
| 12 | +export const NBS_V6_FIRST_CUSTOM = 20; |
| 13 | + |
| 14 | +export const MAX_SUPPORTED_NBS_VERSION = 6; |
| 15 | + |
| 16 | +export class UnsupportedNbsVersionError extends Error { |
| 17 | + constructor(public readonly version: number) { |
| 18 | + super( |
| 19 | + `Unsupported NBS version: ${version}. Maximum supported version is ${MAX_SUPPORTED_NBS_VERSION}.`, |
| 20 | + ); |
| 21 | + this.name = 'UnsupportedNbsVersionError'; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// TODO: TEMP: Remove when @encode42/nbs.js ships v6 built-in instruments in Instrument.builtIn. |
| 26 | +const NBS_V6_BUILTIN_INSTRUMENTS: Instrument[] = [ |
| 27 | + new Instrument(16, { |
| 28 | + name: 'Trumpet', |
| 29 | + soundFile: 'trumpet.ogg', |
| 30 | + builtIn: true, |
| 31 | + key: 45, |
| 32 | + }), |
| 33 | + new Instrument(17, { |
| 34 | + name: 'Exposed Trumpet', |
| 35 | + soundFile: 'exposed_trumpet.ogg', |
| 36 | + builtIn: true, |
| 37 | + key: 45, |
| 38 | + }), |
| 39 | + new Instrument(18, { |
| 40 | + name: 'Weathered Trumpet', |
| 41 | + soundFile: 'weathered_trumpet.ogg', |
| 42 | + builtIn: true, |
| 43 | + key: 45, |
| 44 | + }), |
| 45 | + new Instrument(19, { |
| 46 | + name: 'Oxidized Trumpet', |
| 47 | + soundFile: 'oxidized_trumpet.ogg', |
| 48 | + builtIn: true, |
| 49 | + key: 45, |
| 50 | + }), |
| 51 | +]; |
| 52 | + |
| 53 | +export function getNbsFormatVersion(song: Song): 5 | 6 { |
| 54 | + return song.nbsVersion >= 6 ? 6 : 5; |
| 55 | +} |
| 56 | + |
| 57 | +export function isNbsV6(song: Song): boolean { |
| 58 | + return getNbsFormatVersion(song) === 6; |
| 59 | +} |
| 60 | + |
| 61 | +function findInstrumentById(song: Song, id: number): Instrument | undefined { |
| 62 | + const { loaded } = song.instruments; |
| 63 | + |
| 64 | + if (loaded[id]?.id === id) { |
| 65 | + return loaded[id]; |
| 66 | + } |
| 67 | + |
| 68 | + return loaded.find((inst) => inst?.id === id); |
| 69 | +} |
| 70 | + |
| 71 | +function cloneBuiltinInstrument( |
| 72 | + source: Instrument | undefined, |
| 73 | + fallback: Instrument, |
| 74 | + id: number, |
| 75 | +): Instrument { |
| 76 | + const base = source ?? fallback; |
| 77 | + |
| 78 | + return new Instrument(id, { |
| 79 | + name: base.meta.name, |
| 80 | + soundFile: base.meta.soundFile, |
| 81 | + key: base.key, |
| 82 | + pressKey: base.pressKey, |
| 83 | + builtIn: true, |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +function getDefaultBuiltinInstrument(id: number): Instrument { |
| 88 | + if (id < Instrument.builtIn.length) { |
| 89 | + return Instrument.builtIn[id]!; |
| 90 | + } |
| 91 | + |
| 92 | + return NBS_V6_BUILTIN_INSTRUMENTS[id - NBS_V5_FIRST_CUSTOM]!; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Rebuilds `instruments.loaded` so array indices match note instrument IDs. |
| 97 | + * nbs.js 5.0.2 leaves gaps at 16–19 for v6 files and may place customs at wrong indices. |
| 98 | + * |
| 99 | + * // TODO: TEMP: Remove when @encode42/nbs.js natively models v6. |
| 100 | + */ |
| 101 | +export function normalizeNbsSong(song: Song): Song { |
| 102 | + if (song.nbsVersion > MAX_SUPPORTED_NBS_VERSION) { |
| 103 | + throw new UnsupportedNbsVersionError(song.nbsVersion); |
| 104 | + } |
| 105 | + |
| 106 | + if (!isNbsV6(song)) { |
| 107 | + return song; |
| 108 | + } |
| 109 | + |
| 110 | + const firstCustom = song.instruments.firstCustomIndex; |
| 111 | + const newLoaded: Instrument[] = []; |
| 112 | + |
| 113 | + for (let id = 0; id < firstCustom; id++) { |
| 114 | + newLoaded[id] = cloneBuiltinInstrument( |
| 115 | + findInstrumentById(song, id), |
| 116 | + getDefaultBuiltinInstrument(id), |
| 117 | + id, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + const customs = song.instruments.loaded.filter( |
| 122 | + (inst): inst is Instrument => Boolean(inst) && !inst.builtIn, |
| 123 | + ); |
| 124 | + |
| 125 | + customs.sort((a, b) => a.id - b.id); |
| 126 | + |
| 127 | + for (const inst of customs) { |
| 128 | + const targetId = |
| 129 | + inst.id >= firstCustom ? inst.id : firstCustom + customs.indexOf(inst); |
| 130 | + |
| 131 | + newLoaded[targetId] = inst; |
| 132 | + } |
| 133 | + |
| 134 | + song.instruments.loaded = newLoaded; |
| 135 | + |
| 136 | + return song; |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Seeds obfuscated output with the source song's format version and built-in instruments. |
| 141 | + * |
| 142 | + * TODO: TEMP: Remove when @encode42/nbs.js creates v6 songs from `new Song()`. |
| 143 | + */ |
| 144 | +export function seedOutputBuiltinInstruments(source: Song, output: Song): void { |
| 145 | + output.nbsVersion = getNbsFormatVersion(source); |
| 146 | + output.instruments.firstCustomIndex = source.instruments.firstCustomIndex; |
| 147 | + |
| 148 | + const firstCustom = source.instruments.firstCustomIndex; |
| 149 | + const builtins: Instrument[] = []; |
| 150 | + |
| 151 | + for (let id = 0; id < firstCustom; id++) { |
| 152 | + builtins[id] = cloneBuiltinInstrument( |
| 153 | + findInstrumentById(source, id), |
| 154 | + getDefaultBuiltinInstrument(id), |
| 155 | + id, |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + output.instruments.loaded = builtins; |
| 160 | +} |
| 161 | + |
| 162 | +export function loadNbsFromBuffer( |
| 163 | + buffer: ArrayBuffer, |
| 164 | + options?: FromArrayBufferOptions, |
| 165 | +): Song { |
| 166 | + const song = fromArrayBuffer(buffer, options); |
| 167 | + |
| 168 | + if (song.nbsVersion > MAX_SUPPORTED_NBS_VERSION) { |
| 169 | + throw new UnsupportedNbsVersionError(song.nbsVersion); |
| 170 | + } |
| 171 | + |
| 172 | + return normalizeNbsSong(song); |
| 173 | +} |
0 commit comments