diff --git a/jsnes/src/sample.js b/jsnes/src/sample.js index a1b0886..7b8802c 100644 --- a/jsnes/src/sample.js +++ b/jsnes/src/sample.js @@ -38,14 +38,22 @@ Sample.prototype = { this.index += this.increment / clocksPerPeriod; }, reset: function() { this.index = 0; }, + freqFromTimer: function(timer) { + // timer -> frequency + // clocksPerPeriodTriangle = 8, clocksPerPeriodPulse = 4 + // fTriangle = fCPU/(32*(t + 1)) + // fpulse = fCPU/(16*(t+1) + return 1789773 / (4 * this.clocksPerPeriod * (timer + 1)); + }, sample: function(timer, maxTimer, volume) { const timerIncreasing = maxTimer - timer; if (maxTimer !== this.prevMaxTimer) { if (this.prevMaxTimer) { - var ratio = this.prevMaxTimer < maxTimer ? maxTimer / this.prevMaxTimer : this.prevMaxTimer / maxTimer; - var semitoneInterval = 1.059463; - if (ratio >= semitoneInterval * 2 || this.clocksSinceMaxTimerChange > 210000 || this.index >= this.samples.length) { - this.index = 0; + var ratio = this.freqFromTimer(this.prevMaxTimer) / this.freqFromTimer(maxTimer); + if (ratio < 1) { ratio = 1/ratio }; + var significantNoteChange = 1.1; + if ((this.clocksSinceMaxTimerChange > 130000) || ratio >= significantNoteChange) { + this.reset(); } } this.prevMaxTimer = maxTimer;