UNPKG

nes-emu

Version:

A NES emulator

56 lines (51 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _constants = _interopRequireDefault(require("../../constants")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** A volume envelope. It starts at a volume of 15 and decrements every time the unit is clocked. */ class VolumeEnvelope { constructor() { // input this.startFlag = false; // output this.dividerCount = 0; this.volume = 0; } /** Decrements `volume` every `period` clocks. If `loop`, it resets the countdown to 15. */ clock(period, loop) { if (!this.startFlag) { if (this.dividerCount === 0) { this.dividerCount = period; if (this.volume === 0) { if (loop) this.volume = _constants.default.APU_MAX_VOLUME; } else { this.volume--; } } else { this.dividerCount--; } } else { this.startFlag = false; this.volume = _constants.default.APU_MAX_VOLUME; this.dividerCount = period; } } /** Returns a snapshot of the current state. */ getSaveState() { return { startFlag: this.startFlag, dividerCount: this.dividerCount, volume: this.volume }; } /** Restores state from a snapshot. */ setSaveState(saveState) { this.startFlag = saveState.startFlag; this.dividerCount = saveState.dividerCount; this.volume = saveState.volume; } } exports.default = VolumeEnvelope;