本篇文章演示在unity引擎中接入百度语音API的语音合成(适用于Windows平台,其它平台的后续更新)
因为在上一篇发布的文章中已经有一些准备步骤,这一篇就直接进入正题。
(1)搭建好一个UI面板
(2)编写脚本
①获取Access Token的方式和语音识别一样,这里我直接复制过来
///
/// 获取accessToken请求令牌
///
///
IEnumerator _GetAccessToken()
{
var uri =
string.Format(
"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}",
api_key, secret_Key);
UnityWebRequest unityWebRequest = UnityWebRequest.Get(uri);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isDone)
{
Match match = Regex.Match(unityWebRequest.downloadHandler.text, @"access_token.:.(.*?).,");
if (match.Success)
{
Debug.Log("Token已经匹配");
accessToken = match.Groups[1].ToString();
}
else
{
Debug.Log("验证错误,获取AccessToken失败!!!");
}
}
}
然后就编写代码直接调用接口进行语音合成
///
/// 请求语音合成
///
///
///
///
public IEnumerator StratTTS(string text, Action<TtsResponse> callback)
{
var url = "http://tsn.baidu.com/text2audio";
var param = new Dictionary<string, string>();
param.Add("tex", text);
param.Add("tok", accessToken);
param.Add("cuid", SystemInfo.deviceUniqueIdentifier);
param.Add("ctp", "1");
param.Add("lan", "zh");
param.Add("spd", "5");
param.Add("pit", "5");
param.Add("vol", "10");
param.Add("per", "1");
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
param.Add("aue", "6"); //Windows设置为wav格式,移动端需要mp3格式
#endif
int i = 0;
foreach (var p in param)
{
url += i != 0 ? "&" : "?";
url += p.Key + "=" + p.Value;
i++;
}
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP //根据不同平台,获取不同类型的音频格式
var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV);
#else
var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
#endif
Debug.Log("[WitBaiduAip]" + www.url);
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError)
Debug.LogError(www.error);
else
{
var type = www.GetResponseHeader("Content-Type");
Debug.Log("[WitBaiduAip]response type: " + type);
if (type.Contains("audio"))
{
var response = new TtsResponse { clip = DownloadHandlerAudioClip.GetContent(www) };
callback(response);
}
else
{
var textBytes = www.downloadHandler.data;
var errorText = Encoding.UTF8.GetString(textBytes);
}
}
}
对返回的语音进行播放
StartCoroutine(StratTTS(ttsText.text, s =>
{
source.clip = s.clip;
source.Play();
}));
全篇的代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class TTS : MonoBehaviour
{
//自行填写自己的百度语音识别相关key
public string api_key;
public string secret_Key;
string accessToken = string.Empty;
Button StartTTS;
InputField ttsText;
AudioSource source;
void Start()
{
StartTTS = GameObject.Find("Canvas/Start").GetComponent<Button>();
ttsText = GameObject.Find("Canvas/panel/tts").GetComponent<InputField>();
source = transform.GetComponent<AudioSource>();
StartCoroutine(_GetAccessToken());
StartTTS.onClick.AddListener(()=> {
StartCoroutine(StratTTS(ttsText.text, s =>
{
source.clip = s.clip;
source.Play();
}));
});
}
///
/// 返回的语音合成结果
///
public class TtsResponse
{
public int error_index;
public string error_msg;
public string sn;
public int idx;
public bool Success
{
get { return error_index == 0; }
}
public AudioClip clip;
}
///
/// 请求语音合成
///
///
///
///
public IEnumerator StratTTS(string text, Action<TtsResponse> callback)
{
var url = "http://tsn.baidu.com/text2audio";
var param = new Dictionary<string, string>();
param.Add("tex", text);
param.Add("tok", accessToken);
param.Add("cuid", SystemInfo.deviceUniqueIdentifier);
param.Add("ctp", "1");
param.Add("lan", "zh");
param.Add("spd", "5");
param.Add("pit", "5");
param.Add("vol", "10");
param.Add("per", "1");
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
param.Add("aue", "6"); //Windows设置为wav格式,移动端需要mp3格式
#endif
int i = 0;
foreach (var p in param)
{
url += i != 0 ? "&" : "?";
url += p.Key + "=" + p.Value;
i++;
}
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP //根据不同平台,获取不同类型的音频格式
var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV);
#else
var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
#endif
Debug.Log("[WitBaiduAip]" + www.url);
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError)
Debug.LogError(www.error);
else
{
var type = www.GetResponseHeader("Content-Type");
Debug.Log("[WitBaiduAip]response type: " + type);
if (type.Contains("audio"))
{
var response = new TtsResponse { clip = DownloadHandlerAudioClip.GetContent(www) };
callback(response);
}
else
{
var textBytes = www.downloadHandler.data;
var errorText = Encoding.UTF8.GetString(textBytes);
}
}
}
///
/// 获取accessToken请求令牌
///
///
IEnumerator _GetAccessToken()
{
var uri =
string.Format(
"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}",
api_key, secret_Key);
UnityWebRequest unityWebRequest = UnityWebRequest.Get(uri);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isDone)
{
Match match = Regex.Match(unityWebRequest.downloadHandler.text, @"access_token.:.(.*?).,");
if (match.Success)
{
Debug.Log("Token已经匹配");
accessToken = match.Groups[1].ToString();
}
else
{
Debug.Log("验证错误,获取AccessToken失败!!!");
}
}
}
}
演示效果:
成功播放合成的声音,本篇结束。这只是最基本的调用百度API的操作,下一篇就是调用百度的自然处理语言的API实现智能对话,之后还会对Unity关于调用Android SDK的操作进行总结,使用unity制作一个智能语音助手(陪聊)的软件。