UNPKG

nes-emu

Version:

A NES emulator

45 lines (39 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _config = _interopRequireDefault(require("../../config")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const TRIANGLE_SEQUENCE = [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; /** A triangle wave generator. */ class TriangleOscillator { constructor() { this.amplitude = 1; this._frequency = 0; } /** Generates a new sample. */ sample(time) { const period = 1 / this._frequency; const phase = time % period; const step = Math.floor(phase / (period / 32)); return TRIANGLE_SEQUENCE[step] * _config.default.TRIANGLE_CHANNEL_VOLUME; } /** Returns a snapshot of the current state. */ getSaveState() { return { amplitude: this.amplitude, frequency: this._frequency }; } /** Restores state from a snapshot. */ setSaveState(saveState) { this.amplitude = saveState.amplitude; this.frequency = saveState.frequency; } /** Sets the frequency, but only if the change is above a minimum threshold (this sounds better). */ set frequency(value) { if (Math.abs(this._frequency - value) > _config.default.MIN_FREQUENCY_CHANGE) this._frequency = value; } } exports.default = TriangleOscillator;