https://platform.openai.com/docs/api-reference/audio/createSpeech
s, _ := sjson.Set("", "model", "tts-1") // tts-1 or tts-1-hd
s, _ = sjson.Set(s, "input", text) // 要合成的文本 不超过4096个字符
s, _ = sjson.Set(s, "voice", "alloy") //声音 alloy, echo, fable, onyx, nova, and shimmer
s, _ = sjson.Set(s, "response_format", "wav") // 语音格式 mp3, opus, aac, and flac
s, _ = sjson.Set(s, "speed", speed) // 语速 0.25 to 4.0. 1.0 is the default
req, err := http.NewRequest("POST", "https://api.openai-proxy.com/v1/audio/speech", bytes.NewBuffer([]byte(s)))
req.Header.Set("Authorization", "<your key>")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logger.Errorf("Openai call error,errormsg: %v", err)
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
logger.Errorf("Openai ReadAll error,errormsg: %v", err)
}
// 保存语音文件
name := "D:/桌面/test.wav"
err := os.WriteFile(name, audio, 0644)
if err != nil {
logger.Errorf("Voice File Write Failed,errormsg:%s", err)
}