comparison src/tts.wav.luan @ 73:60ebb333b40c

chunked encoding
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 28 Aug 2025 05:16:32 -0600
parents
children
comparison
equal deleted inserted replaced
72:5d274f7a4637 73:60ebb333b40c
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local Parsers = require "luan:Parsers.luan"
4 local json_string = Parsers.json_string 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 local key = Config.chatgpt.key or error()
10 local Logging = require "luan:logging/Logging.luan"
11 local logger = Logging.logger "tts.js"
12
13
14 -- https://platform.openai.com/docs/guides/text-to-speech
15
16 local url = "https://api.openai.com/v1/audio/speech"
17 local headers = {
18 Authorization = "Bearer "..key
19 ["Content-Type"] = "application/json"
20 }
21
22 local function text_to_speech(voice,instructions,text)
23 local options = {
24 method = "POST"
25 headers = headers
26 content = json_string{
27 model = "gpt-4o-mini-tts"
28 voice = voice
29 input = text
30 instructions = instructions
31 response_format = "wav"
32 }
33 }
34 return uri(url,options)
35 end
36
37 return function()
38 local voice = Http.request.parameters.voice or error()
39 local instructions = Http.request.parameters.instructions or error()
40 local text = Http.request.parameters.text or error()
41 local input = text_to_speech(voice,instructions,text)
42 Http.response.write_chunked_binary(function(writer)
43 writer.write_from(input)
44 end)
45 end