From dd607401e4187cb81cb634c668078fcec395063f Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Thu, 17 Nov 2022 22:55:21 -0800 Subject: [PATCH] Tweaking audio and web yield logic. --- Makefile | 1 + web/platform.fs | 57 ++++++++++++++++++++++++--------------------- web/web.template.js | 5 +++- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 63b3c31..ba4c6d4 100644 --- a/Makefile +++ b/Makefile @@ -281,6 +281,7 @@ WEB_BOOT = $(COMMON_PHASE1e) \ web/platform.fs \ common/ansi.fs \ $(COMMON_PHASE2) \ + common/tasks.fs \ web/fini.fs $(GEN)/web_boot.js: tools/source_to_string.js $(WEB_BOOT) | $(GEN) $< -web boot $(VERSION) $(REVISION) $(WEB_BOOT) >$@ diff --git a/web/platform.fs b/web/platform.fs index 1c11ba0..a180312 100644 --- a/web/platform.fs +++ b/web/platform.fs @@ -728,38 +728,43 @@ JSWORD: release { handle } context.ReleaseHandle(handle); ~ -JSWORD: newAudioContext { -- h } - var i = context.AllotHandle(); - context.handles[i] = new AudioContext(); - return i; +r~ +context.audio_contexts = []; +context.newAudio = function() { + if (context.audio_contexts.length < 30) { + var ctx = new AudioContext(); + context.audio_contexts.push(ctx); + return ctx; + } + var ctx = context.audio_contexts[0]; + context.audio_contexts.splice(0, 1); + context.audio_contexts.push(ctx); + return ctx; +}; +~ jseval + +JSWORD: tone { pitch duration volume -- } + var audio = context.newAudio(); + var oscillator = audio.createOscillator(); + oscillator.type = 'sine'; + oscillator.frequency.setValueAtTime(27.5 * Math.pow(2, (pitch - 21) / 12), audio.currentTime); + var gain = audio.createGain(); + gain.gain.setValueAtTime(0, audio.currentTime); + gain.gain.linearRampToValueAtTime(volume / 100, audio.currentTime + duration / 1000 * 0.1); + gain.gain.linearRampToValueAtTime(volume / 100, audio.currentTime + duration / 1000 * 0.9); + gain.gain.linearRampToValueAtTime(0, audio.currentTime + duration / 1000); + oscillator.connect(gain); + gain.connect(audio.destination); + oscillator.start(); ~ -JSWORD: createOscillator { audio_ctx -- h } - var i = context.AllotHandle(); - context.handles[i] = context.handles[audio_ctx].createOscillator(); - return i; -~ - -JSWORD: createGain { audio_ctx -- h } - var i = context.AllotHandle(); - context.handles[i] = context.handles[audio_ctx].createGain(); - return i; -~ - -JSWORD: createBiquadFilter { audio_ctx -- h } - var i = context.AllotHandle(); - context.handles[i] = context.handles[audio_ctx].createBiquadFilter(); - return i; -~ - -JSWORD: createBufferSource { audio_ctx -- h } - var i = context.AllotHandle(); - context.handles[i] = context.handles[audio_ctx].createBufferSource(); - return i; +JSWORD: ms-ticks { -- ms } + return Date.now(); ~ forth definitions web : bye 0 terminate ; +: ms-ticks ms-ticks ; forth definitions diff --git a/web/web.template.js b/web/web.template.js index 13fc557..46645cf 100644 --- a/web/web.template.js +++ b/web/web.template.js @@ -593,7 +593,10 @@ var globalObj = getGlobalObj(); var module = VM(globalObj, ffi, heap); function run() { - module.run(); + var lastTimeout = Date.now(); + while (Date.now() - lastTimeout < 50) { + module.run(); + } setTimeout(run, 0); } if (globalObj.write) {