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