UNPKG

vgm-conv

Version:
84 lines (83 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YM2608ClockConverter = exports.YM2612ClockConverter = exports.YM2203ClockConverter = exports.OPNClockConverterBase = void 0; const vgm_converter_1 = require("./vgm-converter"); const vgm_parser_1 = require("vgm-parser"); const ay8910_clock_converter_1 = require("./ay8910-clock-converter"); class OPNClockConverterBase extends vgm_converter_1.VGMClockConverter { _ratio; _regs = [new Uint8Array(256), new Uint8Array(256)]; _target; _ssgConverter = null; constructor(target, from, toClock, opts) { super(from, toClock, 3579545); this._target = target; this._ratio = this.to.clock / from.clock; if (this.from.chip === "ym2608" || this.from.chip === "ym2203") { if (this.from.subModule == null || this.from.subModule === "ssg") { this._ssgConverter = new ay8910_clock_converter_1.AY8910ClockConverter(from, this.to.clock, opts); } } } convertWriteDataCommand(cmd) { const { port, addr, data } = cmd; if (this._ratio !== 1.0) { const regs = this._regs[port]; regs[addr] = data; if (0xa0 <= addr && addr < 0xb0) { const al = 0xa0 + (addr & 3) + (addr & 8); const ah = al + 4; const fnum = ((regs[ah] & 7) << 8) | regs[al]; let new_fnum = Math.round(fnum / this._ratio); let new_blk = (regs[ah] >> 3) & 7; while (new_fnum > 0x7ff) { new_fnum >>= 1; new_blk++; } if (new_blk > 7) { new_blk = 7; new_fnum = 0x7ff; } const dl = new_fnum & 0xff; const dh = (new_blk << 3) | (new_fnum >> 8); return [cmd.copy({ addr: ah, data: dh }), cmd.copy({ addr: al, data: dl })]; } } return [cmd]; } convertCommand(cmd) { const convertFM = this.from.subModule == null || this.from.subModule === "fm"; if (cmd instanceof vgm_parser_1.VGMWriteDataCommand && cmd.chip === this._target && cmd.index === this.from.index) { if (cmd.addr < 0x10) { if (this._ssgConverter) { return this._ssgConverter.convert(cmd); } } else { if (convertFM) { return this.convertWriteDataCommand(cmd); } } } return [cmd]; } } exports.OPNClockConverterBase = OPNClockConverterBase; class YM2203ClockConverter extends OPNClockConverterBase { constructor(from, toClock, opts) { super("ym2203", from, toClock, 4000000); } } exports.YM2203ClockConverter = YM2203ClockConverter; class YM2612ClockConverter extends OPNClockConverterBase { constructor(from, toClock, opts) { super("ym2612", from, toClock, 7670454); } } exports.YM2612ClockConverter = YM2612ClockConverter; class YM2608ClockConverter extends OPNClockConverterBase { constructor(from, toClock, opts) { super("ym2608", from, toClock, 7987200); } } exports.YM2608ClockConverter = YM2608ClockConverter;