Skip to content

Instantly share code, notes, and snippets.

@Earu
Created April 17, 2026 11:08
Show Gist options
  • Select an option

  • Save Earu/cd89d3deb3091375cecce74ccc9821e5 to your computer and use it in GitHub Desktop.

Select an option

Save Earu/cd89d3deb3091375cecce74ccc9821e5 to your computer and use it in GitHub Desktop.
Dead simple garry's mod TTS.
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