Code: Select all
; Speech module, written by Quin, March/April 2024.
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
DeclareModule Speech
EnableExplicit
;{ Declares
Declare Init()
Declare Speak(Text$)
Declare SetWebTTS(State.b)
;} End declares
EndDeclareModule
Module Speech
EnableExplicit
;{ Globals
Global WebTTS.b = #False
! var synth, aria;
;} End Globals
;{ Procedures
Procedure Init()
! synth = window.speechSynthesis;
! aria = document.createElement("div");
! aria.id = "speech";
! aria.style = "position: absolute; left: 0px; top: -400px";
! aria.setAttribute("aria-live", "assertive");
! document.body.appendChild(aria);
EndProcedure
Procedure Speak(text$)
If WebTTS
! const utterThis = new SpeechSynthesisUtterance(v_text$);
! if (typeof(synth.stop) !== "undefined") synth.stop();
! synth.speak(utterThis);
Else
! aria.innerHTML = "";
! const para = document.createElement("p");
! para.appendChild(document.createTextNode(v_text$));
! aria.appendChild(para) ;
EndIf
EndProcedure
Procedure SetWebTTS(State.b)
WebTTS = State
EndProcedure
;} End Procedures
EndModule
;{ Test
CompilerIf #PB_Compiler_IsMainFile
Global UseWebTTS.b = #False
Procedure ButtonCallback()
Speech::Speak("Hi!")
UseWebTTS ! 1
Speech::SetWebTTS(UseWebTTS)
EndProcedure
Speech::Init()
OpenWindow(0, 10, 10, 200, 100, "Speech Test")
ButtonGadget(0, 15, 15, 190, 90, "Say hi!")
BindGadgetEvent(0, @ButtonCallback())
CompilerEndIf
;} End test
Enjoy
