Mercurial Hosting > lang
comparison 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 |
comparison
equal
deleted
inserted
replaced
45:fef7a5c65cfb | 46:cc20eebaa74a |
---|---|
1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | 2 local error = Luan.error |
3 local Parsers = require "luan:Parsers.luan" | 3 local Parsers = require "luan:Parsers.luan" |
4 local xml_encode = Parsers.xml_encode or error() | 4 local json_string = Parsers.json_string or error() |
5 local Io = require "luan:Io.luan" | 5 local Io = require "luan:Io.luan" |
6 local uri = Io.uri or error() | 6 local uri = Io.uri or error() |
7 local Http = require "luan:http/Http.luan" | 7 local Http = require "luan:http/Http.luan" |
8 local Config = require "site:/private/Config.luan" | 8 local Config = require "site:/private/Config.luan" |
9 local key = Config.chatgpt.key or error() | |
10 local Logging = require "luan:logging/Logging.luan" | |
11 local logger = Logging.logger "tts.js" | |
9 | 12 |
10 | 13 |
11 -- https://learn.microsoft.com/en-us/azure/ai-services/speech-service/index-text-to-speech | 14 -- https://platform.openai.com/docs/guides/text-to-speech |
12 | 15 |
13 local region = Config.azure_tts.region or error() | 16 local url = "https://api.openai.com/v1/audio/speech" |
14 local url = "https://"..region..".tts.speech.microsoft.com/cognitiveservices/v1" | |
15 local headers = { | 17 local headers = { |
16 ["Ocp-Apim-Subscription-Key"] = Config.azure_tts.key or error() | 18 Authorization = "Bearer "..key |
17 ["Content-Type"] = "application/ssml+xml" | 19 ["Content-Type"] = "application/json" |
18 ["X-Microsoft-OutputFormat"] = "audio-16khz-128kbitrate-mono-mp3" | |
19 } | 20 } |
20 | 21 |
21 local function text_to_speech(lang,voice,text) | 22 local function text_to_speech(voice,instructions,text) |
22 local xml = `%> | |
23 <speak version='1.0' xml:lang='<%=lang%>'> | |
24 <voice name='<%=voice%>'> | |
25 <%= xml_encode(text) %> | |
26 </voice> | |
27 </speak> | |
28 <% ` | |
29 local options = { | 23 local options = { |
30 method = "POST" | 24 method = "POST" |
31 headers = headers | 25 headers = headers |
32 content = xml | 26 content = json_string{ |
27 model = "gpt-4o-mini-tts" | |
28 voice = voice | |
29 input = text | |
30 instructions = instructions | |
31 } | |
33 } | 32 } |
34 return uri(url,options) | 33 return uri(url,options) |
35 end | 34 end |
36 | 35 |
37 return function() | 36 return function() |
38 local lang = Http.request.parameters.lang or error() | |
39 local voice = Http.request.parameters.voice or error() | 37 local voice = Http.request.parameters.voice or error() |
38 local instructions = Http.request.parameters.instructions or error() | |
40 local text = Http.request.parameters.text or error() | 39 local text = Http.request.parameters.text or error() |
41 local input = text_to_speech(lang,voice,text) | 40 local input = text_to_speech(voice,instructions,text) |
42 Http.response.binary_writer().write_from(input) | 41 Http.response.binary_writer().write_from(input) |
43 end | 42 end |