写一个unity的文字合成语音的API这里是API的接口地址和参数对应_msg为下面参考示例中参数请求的api地址: httpsgenshinvoicetopapispeaker=胡桃&msg=你好&format=wav&length=1&noise=05&noisew=09&sdp_ratio=02 下方是示例请求 using System;using SystemCollections;usi
此代码是一个Unity的文字合成语音的API,用于将文字转化为语音。以下是API的接口地址和参数:
接口地址:https://genshinvoice.top/api 参数:
- speaker:语音合成的演讲者,例如“胡桃”
- msg:要转化为语音的文字内容,例如“你好”
- format:输出的音频格式,例如“wav”
- length:语音长度,例如“1”
- noise:噪声的强度,例如“0.5”
- noisew:噪声的频率,例如“0.9”
- sdp_ratio:sdp的比率,例如“0.2”
下面是一个示例请求的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class WhisperSpeechToText : STT
{
private void Awake()
{
m_SpeechRecognizeURL = GetPostUrl();
}
// 其他代码...
/// <summary>
/// 发送数据到api
/// </summary>
/// <param name="audioBytes"></param>
/// <param name="_callback"></param>
/// <returns></returns>
private IEnumerator SendAudioData(byte[] audioBytes, Action<string> _callback)
{
stopwatch.Restart();
WWWForm form = new WWWForm();
form.AddBinaryData("audio_file", audioBytes, "test.mp3", "audio/mpeg");
UnityWebRequest www = UnityWebRequest.Post(m_SpeechRecognizeURL, form);
www.SetRequestHeader("accept", "application/json");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error sending audio file: " + www.error);
}
else
{
string _responseText = www.downloadHandler.text;
Response _response = ResponseSetting(_responseText);
_callback(_response.text);
}
stopwatch.Stop();
Debug.Log("Whisper语音识别耗时:" + stopwatch.Elapsed.TotalSeconds);
}
// 其他代码...
#region 数据定义
[Serializable]
public class Response
{
[SerializeField] public string text = string.Empty;
[SerializeField] public List<Segment> segments = new List<Segment>();
[SerializeField] public string language = string.Empty;
}
[Serializable]
public class Segment
{
[SerializeField] public int id;
[SerializeField] public int seek;
[SerializeField] public int start;
[SerializeField] public int end;
[SerializeField] public string text = string.Empty;
[SerializeField] public int temperature;
}
public enum OutputType
{
txt,
json,
vtt,
srt,
tsv
}
#endregion
}
原文地址: https://www.cveoy.top/t/topic/i9rh 著作权归作者所有。请勿转载和采集!