Mercurial Hosting > lang
diff src/tts.mp3.luan @ 46:cc20eebaa74a
use openai tts
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Aug 2025 11:27:34 +0900 |
parents | src/stt.js.luan@d3f5448743bf |
children |
line wrap: on
line diff
--- a/src/tts.mp3.luan Wed Aug 13 10:31:24 2025 +0900 +++ b/src/tts.mp3.luan Thu Aug 14 11:27:34 2025 +0900 @@ -1,43 +1,42 @@ local Luan = require "luan:Luan.luan" local error = Luan.error local Parsers = require "luan:Parsers.luan" -local xml_encode = Parsers.xml_encode or error() +local json_string = Parsers.json_string or error() local Io = require "luan:Io.luan" local uri = Io.uri or error() local Http = require "luan:http/Http.luan" local Config = require "site:/private/Config.luan" +local key = Config.chatgpt.key or error() +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "tts.js" --- https://learn.microsoft.com/en-us/azure/ai-services/speech-service/index-text-to-speech +-- https://platform.openai.com/docs/guides/text-to-speech -local region = Config.azure_tts.region or error() -local url = "https://"..region..".tts.speech.microsoft.com/cognitiveservices/v1" +local url = "https://api.openai.com/v1/audio/speech" local headers = { - ["Ocp-Apim-Subscription-Key"] = Config.azure_tts.key or error() - ["Content-Type"] = "application/ssml+xml" - ["X-Microsoft-OutputFormat"] = "audio-16khz-128kbitrate-mono-mp3" + Authorization = "Bearer "..key + ["Content-Type"] = "application/json" } -local function text_to_speech(lang,voice,text) - local xml = `%> -<speak version='1.0' xml:lang='<%=lang%>'> - <voice name='<%=voice%>'> -<%= xml_encode(text) %> - </voice> -</speak> -<% ` +local function text_to_speech(voice,instructions,text) local options = { method = "POST" headers = headers - content = xml + content = json_string{ + model = "gpt-4o-mini-tts" + voice = voice + input = text + instructions = instructions + } } return uri(url,options) end return function() - local lang = Http.request.parameters.lang or error() local voice = Http.request.parameters.voice or error() + local instructions = Http.request.parameters.instructions or error() local text = Http.request.parameters.text or error() - local input = text_to_speech(lang,voice,text) + local input = text_to_speech(voice,instructions,text) Http.response.binary_writer().write_from(input) end