UNPKG

broken-neees

Version:

A really broken NEEES emulator that introduces glitches and random bugs on purpose!

79 lines (78 loc) 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _AudioRegisters = _interopRequireDefault(require("./AudioRegisters")); var _FrameSequencer = _interopRequireDefault(require("./FrameSequencer")); var _PulseChannel = _interopRequireDefault(require("./PulseChannel")); var _TriangleChannel = _interopRequireDefault(require("./TriangleChannel")); var _NoiseChannel = _interopRequireDefault(require("./NoiseChannel")); var _DMCChannel = _interopRequireDefault(require("./DMCChannel")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } var FREQ_APU_HZ = 894887; var APU_SAMPLE_RATE = 44100; var STEPS_PER_SAMPLE = Math.floor(FREQ_APU_HZ / APU_SAMPLE_RATE); var APU = /*#__PURE__*/function () { function APU(cpu) { _classCallCheck(this, APU); this.cpu = cpu; this.sampleCounter = 0; this.sample = 0; this.registers = new _AudioRegisters.default(this); this.frameSequencer = new _FrameSequencer.default(this); this.channels = { pulses: [new _PulseChannel.default(this, 0, "enablePulse1"), new _PulseChannel.default(this, 1, "enablePulse2")], triangle: new _TriangleChannel.default(this), noise: new _NoiseChannel.default(this), dmc: new _DMCChannel.default(this, this.cpu) }; } _createClass(APU, [{ key: "step", value: function step(onSample) { this.channels.pulses[0].step(); this.channels.pulses[1].step(); this.channels.noise.step(); this.channels.dmc.step(); this.sampleCounter++; this.frameSequencer.step(); if (this.sampleCounter === STEPS_PER_SAMPLE) { this.sampleCounter = 0; var pulse1 = this.channels.pulses[0].sample(); var pulse2 = this.channels.pulses[1].sample(); var triangle = this.channels.triangle.sample(); var noise = this.channels.noise.sample(); var dmc = this.channels.dmc.sample(); var pulseOut = 0.00752 * (pulse1 + pulse2); var tndOut = 0.00851 * triangle + 0.00494 * noise + 0.00335 * dmc; this.sample = pulseOut + tndOut; onSample(this.sample, pulse1, pulse2, triangle, noise, dmc); } } }, { key: "onQuarterFrameClock", value: function onQuarterFrameClock() { this.channels.pulses[0].quarterFrame(); this.channels.pulses[1].quarterFrame(); this.channels.triangle.quarterFrame(); this.channels.noise.quarterFrame(); } }, { key: "onHalfFrameClock", value: function onHalfFrameClock() { this.channels.pulses[0].halfFrame(); this.channels.pulses[1].halfFrame(); this.channels.triangle.halfFrame(); this.channels.noise.halfFrame(); } }]); return APU; }(); exports.default = APU;