Tweaking audio and web yield logic.

This commit is contained in:
Brad Nelson
2022-11-17 22:55:21 -08:00
parent 279b391a28
commit dd607401e4
3 changed files with 36 additions and 27 deletions

View File

@ -281,6 +281,7 @@ WEB_BOOT = $(COMMON_PHASE1e) \
web/platform.fs \ web/platform.fs \
common/ansi.fs \ common/ansi.fs \
$(COMMON_PHASE2) \ $(COMMON_PHASE2) \
common/tasks.fs \
web/fini.fs web/fini.fs
$(GEN)/web_boot.js: tools/source_to_string.js $(WEB_BOOT) | $(GEN) $(GEN)/web_boot.js: tools/source_to_string.js $(WEB_BOOT) | $(GEN)
$< -web boot $(VERSION) $(REVISION) $(WEB_BOOT) >$@ $< -web boot $(VERSION) $(REVISION) $(WEB_BOOT) >$@

View File

@ -728,38 +728,43 @@ JSWORD: release { handle }
context.ReleaseHandle(handle); context.ReleaseHandle(handle);
~ ~
JSWORD: newAudioContext { -- h } r~
var i = context.AllotHandle(); context.audio_contexts = [];
context.handles[i] = new AudioContext(); context.newAudio = function() {
return i; 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 } JSWORD: ms-ticks { -- ms }
var i = context.AllotHandle(); return Date.now();
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;
~ ~
forth definitions web forth definitions web
: bye 0 terminate ; : bye 0 terminate ;
: ms-ticks ms-ticks ;
forth definitions forth definitions

View File

@ -593,7 +593,10 @@ var globalObj = getGlobalObj();
var module = VM(globalObj, ffi, heap); var module = VM(globalObj, ffi, heap);
function run() { function run() {
module.run(); var lastTimeout = Date.now();
while (Date.now() - lastTimeout < 50) {
module.run();
}
setTimeout(run, 0); setTimeout(run, 0);
} }
if (globalObj.write) { if (globalObj.write) {