Created
April 17, 2026 11:08
-
-
Save Earu/cd89d3deb3091375cecce74ccc9821e5 to your computer and use it in GitHub Desktop.
Dead simple garry's mod TTS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local TTS = {} | |
| local panel = nil | |
| local cvarVolume = GetConVar("volume") | |
| local function EnsurePanel() | |
| if IsValid(panel) then return end | |
| panel = vgui.Create("DHTML") | |
| panel:SetSize(1, 1) | |
| panel:SetPos(-10, -10) | |
| panel:SetHTML([[<!DOCTYPE html><html><body><script> | |
| function speak(text, rate, pitch, volume) { | |
| if (!window.speechSynthesis) return; | |
| var utt = new SpeechSynthesisUtterance(text); | |
| utt.rate = rate; | |
| utt.pitch = pitch; | |
| utt.volume = volume; | |
| window.speechSynthesis.cancel(); | |
| window.speechSynthesis.speak(utt); | |
| } | |
| </script></body></html>]]) | |
| end | |
| -- rate: 0.1 – 10 (default 1) | |
| -- pitch: 0 – 2 (default 1) | |
| -- volume: 0 – 1 (default 1) | |
| function TTS.Say(text, rate, pitch, volume) | |
| EnsurePanel() | |
| local escaped = text | |
| :gsub("\\", "\\\\") | |
| :gsub("\"", "\\\"") | |
| :gsub("\r", "") | |
| :gsub("\n", "\\n") | |
| panel:QueueJavascript(string.format( | |
| "speak(\"%s\", %g, %g, %g);", | |
| escaped, | |
| rate or 1, | |
| pitch or 1, | |
| (volume or 1) * cvarVolume:GetFloat() * 10 | |
| )) | |
| end | |
| function TTS.Stop() | |
| if IsValid(panel) then | |
| panel:QueueJavascript("window.speechSynthesis.cancel();") | |
| end | |
| end | |
| function TTS.Destroy() | |
| if IsValid(panel) then | |
| panel:Remove() | |
| panel = nil | |
| end | |
| end | |
| _G.TTS = TTS | |
| return TTS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment