vgm-conv
Version:
Chip-type and clock converter for VGM.
52 lines (51 loc) • 2.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.YM2413ClockConverter = void 0;
const vgm_converter_1 = require("./vgm-converter");
const vgm_parser_1 = require("vgm-parser");
const vgm_write_data_buffer_1 = __importDefault(require("./vgm-write-data-buffer"));
class YM2413ClockConverter extends vgm_converter_1.VGMClockConverter {
_ratio;
_regs = new Uint8Array(256);
_buf = new vgm_write_data_buffer_1.default(256, 1);
constructor(from, toClock, opts) {
super(from, toClock, 3579545);
this._ratio = this.to.clock / from.clock;
}
convertWriteDataCommand(cmd) {
if (this._ratio !== 1.0 && cmd.addr != null) {
if (0x10 <= cmd.addr && cmd.addr < 0x30) {
this._regs[cmd.addr] = cmd.data;
const al = (cmd.addr & 0xf) + 0x10;
const ah = (cmd.addr & 0xf) + 0x20;
const fnum = ((this._regs[ah] & 1) << 8) | this._regs[al];
let new_fnum = Math.round(fnum / this._ratio);
let new_blk = (this._regs[ah] & 0xe) >> 1;
while (new_fnum > 0x1ff) {
new_fnum >>= 1;
new_blk++;
}
if (new_blk > 7) {
new_blk = 7;
new_fnum = 0x1ff;
}
const dl = new_fnum & 0xff;
const dh = (this._regs[ah] & 0xf0) | (new_blk << 1) | (new_fnum >> 8);
this._buf.push(cmd.copy({ addr: ah, data: dh }));
this._buf.push(cmd.copy({ addr: al, data: dl }));
return this._buf.commit();
}
}
return [cmd];
}
convertCommand(cmd) {
if (cmd instanceof vgm_parser_1.VGMWriteDataCommand && cmd.chip === "ym2413" && cmd.index === this.from.index) {
return this.convertWriteDataCommand(cmd);
}
return [cmd];
}
}
exports.YM2413ClockConverter = YM2413ClockConverter;