1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-22 14:33:51 +00:00

use requestAnimationFrame(); fixed too many audio contexts

This commit is contained in:
Steven Hugg 2017-05-10 07:57:06 -04:00
parent 59cd5f78fa
commit 8df7102b69
2 changed files with 12 additions and 3 deletions

View File

@ -2,8 +2,8 @@
var MasterAudio = function() {
this.master = new MasterChannel();
this.looper = new AudioLooper(512);
this.start = function() {
this.looper = new AudioLooper(512);
this.looper.setChannel(this.master);
this.looper.activate();
}

View File

@ -201,7 +201,8 @@ var RAM = function(size) {
var AnimationTimer = function(frequencyHz, callback) {
var intervalMsec = 1000.0 / frequencyHz;
var running;
var useReqAnimFrame = false; // TODO: enable?
var lastts = 0;
var useReqAnimFrame = true;
function scheduleFrame() {
if (useReqAnimFrame)
@ -210,7 +211,14 @@ var AnimationTimer = function(frequencyHz, callback) {
setTimeout(nextFrame, intervalMsec);
}
var nextFrame = function(ts) {
callback();
if (!useReqAnimFrame || ts - lastts > intervalMsec/2) {
if (ts - lastts < intervalMsec*30) {
lastts += intervalMsec;
} else {
lastts = ts;
}
callback();
}
if (running) {
scheduleFrame();
}
@ -221,6 +229,7 @@ var AnimationTimer = function(frequencyHz, callback) {
this.start = function() {
if (!running) {
running = true;
lastts = 0;
scheduleFrame();
}
}