mozhi/utils/tts.go

154 lines
2.9 KiB
Go
Raw Normal View History

2023-07-06 10:54:45 +00:00
package utils
import (
2023-07-22 14:24:15 +00:00
"context"
"encoding/base64"
2023-07-06 10:54:45 +00:00
"github.com/carlmjohnson/requests"
"github.com/google/go-querystring/query"
2023-07-06 10:54:45 +00:00
"os"
)
type ReversoTTS struct {
2023-07-22 14:24:15 +00:00
Id string
2023-07-06 10:54:45 +00:00
Voice string
}
func TTSGoogle(lang string, text string) string {
type Options struct {
2023-07-22 14:24:15 +00:00
Lang string `url:"tl"`
Text string `url:"q"`
Client string `url:"client"`
}
2023-07-22 14:24:15 +00:00
opt := Options{lang, text, "tw-ob"}
v, _ := query.Values(opt)
2023-07-06 10:54:45 +00:00
var file string
2023-07-22 14:24:15 +00:00
url := "https://translate.google.com/translate_tts?" + v.Encode()
2023-07-06 10:54:45 +00:00
err := requests.
URL(url).
ToString(&file).
Fetch(context.Background())
if err != nil {
file = ""
}
return file
}
func TTSReverso(lang string, text string) string {
2023-07-06 10:54:45 +00:00
var TTSData = []ReversoTTS{
// http://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetAvailableVoices with randomized deduplication
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "ar",
2023-07-06 10:54:45 +00:00
Voice: "Mehdi22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "zh",
2023-07-06 10:54:45 +00:00
Voice: "Lulu22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "cz",
2023-07-06 10:54:45 +00:00
Voice: "Eliska22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "dk",
2023-07-06 10:54:45 +00:00
Voice: "Mette22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "nl",
2023-07-06 10:54:45 +00:00
Voice: "Daan22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "en",
2023-07-06 10:54:45 +00:00
Voice: "Will22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "fr",
2023-07-06 10:54:45 +00:00
Voice: "Margaux22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "de",
2023-07-06 10:54:45 +00:00
Voice: "Andreas22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "gr",
2023-07-06 10:54:45 +00:00
Voice: "Dimitris22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "heb",
2023-07-06 10:54:45 +00:00
Voice: "he-IL-Asaf",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "it",
2023-07-06 10:54:45 +00:00
Voice: "Chiara22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "jp",
2023-07-06 10:54:45 +00:00
Voice: "Sakura22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "kr",
2023-07-06 10:54:45 +00:00
Voice: "Minji22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "pl",
2023-07-06 10:54:45 +00:00
Voice: "Monika22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "pt",
2023-07-06 10:54:45 +00:00
Voice: "Celia22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "ro",
2023-07-06 10:54:45 +00:00
Voice: "ro-RO-Andrei",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "ru",
2023-07-06 10:54:45 +00:00
Voice: "Alyona22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "es",
2023-07-06 10:54:45 +00:00
Voice: "Antonio22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "se",
2023-07-06 10:54:45 +00:00
Voice: "Erik22k",
},
ReversoTTS{
2023-07-22 14:24:15 +00:00
Id: "tr",
2023-07-06 10:54:45 +00:00
Voice: "Ipek22k",
},
}
// https://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetVoiceStream/voiceName=Lulu22k?voiceSpeed=80&inputText=6K%20V6aqM Base64 input text
text2 := base64.StdEncoding.EncodeToString([]byte(text))
2023-07-22 14:24:15 +00:00
var voice string
for _, s := range TTSData {
if s.Id == lang {
voice = s.Voice
break
}
}
2023-07-06 10:54:45 +00:00
type Options struct {
2023-07-22 14:24:15 +00:00
VoiceSpeed int `url:"voiceSpeed"`
Text string `url:"inputText"`
}
2023-07-22 14:24:15 +00:00
opt := Options{80, text2}
v, _ := query.Values(opt)
2023-07-06 10:54:45 +00:00
var file string
2023-07-22 14:24:15 +00:00
url := "https://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetVoiceStream/voiceName=" + voice + "?" + v.Encode()
2023-07-06 10:54:45 +00:00
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
if !ok {
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
err := requests.
URL(url).
ToString(&file).
UserAgent(UserAgent).
Fetch(context.Background())
if err != nil {
file = ""
}
return file
}