#region 錄制聲音轉(zhuǎn)化為文字
private string token; //access_token
private string cuid = "隨便寫的d"; //用戶標(biāo)識(shí)
private string format = "pcm"; //語音格式
private int rate = 8000; //采樣率
private int channel = 1; //聲道數(shù)
private string speech; //語音數(shù)據(jù),進(jìn)行base64編碼
private int len; //原始語音長(zhǎng)度
private string lan = "zh"; //語種
private string grant_Type = "client_credentials";
private string client_ID = "9152186"; //百度appkey
private string client_Secret = "14c703ce0f900eae40e95b2cdd564472"; //百度Secret Key
private string baiduAPI = "http://vop.baidu.com/server_api";
private string getTokenAPIPath =
"https://aip./oauth/2.0/token?grant_type=client_credentials&client_id=ekGb1G5XHY4BIVSA8nLzX5cA&client_secret=14c703ce0f900eae40e95b2cdd564472";
private Byte[] clipByte;
/// <summary>
/// 轉(zhuǎn)換出來的TEXT
/// </summary>
public static string audioToString;
private AudioSource aud;
private int audioLength;//錄音的長(zhǎng)度
public void StartRecord()
{
Debug.Log("開始說話");
if (Microphone.devices.Length == 0) return;
Microphone.End(null);
aud.clip = Microphone.Start(null, false, 10, rate);
}
public void EndRecord()
{
Debug.Log("結(jié)束說話");
int lastPos = Microphone.GetPosition(null);
if (Microphone.IsRecording(null))
audioLength = lastPos / rate;//錄音時(shí)長(zhǎng)
else
audioLength = 10;
Microphone.End(null);
clipByte = GetClipData();
len = clipByte.Length;
speech = Convert.ToBase64String(clipByte);
StartCoroutine(GetToken(getTokenAPIPath));
StartCoroutine(GetAudioString(baiduAPI));
}
/// <summary>
/// 把錄音轉(zhuǎn)換為Byte[]
/// </summary>
/// <returns></returns>
public Byte[] GetClipData()
{
if (aud.clip == null)
{
Debug.LogError("錄音數(shù)據(jù)為空");
return null;
}
float[] samples = new float[aud.clip.samples];
aud.clip.GetData(samples, 0);
Byte[] outData = new byte[samples.Length * 2];
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i < samples.Length; i++)
{
short temshort = (short)(samples[i] * rescaleFactor);
[/i] Byte[] temdata = System.BitConverter.GetBytes(temshort);
outData[i * 2] = temdata[0];
outData[i * 2 + 1] = temdata[1];
}
if (outData == null || outData.Length <= 0)
{
Debug.LogError("錄音數(shù)據(jù)為空");
return null;
}
return outData;
}
/// <summary>
/// 獲取百度用戶令牌
/// </summary>
/// <param name="url">獲取的url</param>
/// <returns></returns>
private IEnumerator GetToken(string url)
{
WWW getTW = new WWW(url);
yield return getTW;
if (getTW.isDone)
{
if (getTW.error == null)
{
token = getTW.text;
StartCoroutine(GetAudioString(baiduAPI));
}
else
{
Debug.LogError("獲取令牌出錯(cuò)" + getTW.error);
}
}
else
{
Debug.LogError("下載出錯(cuò)" + getTW.error);
}
}
/// <summary>
/// 把語音轉(zhuǎn)換為文字
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private IEnumerator GetAudioString(string url)
{
JsonWriter jw = new JsonWriter();
jw.WriteObjectStart();
jw.WritePropertyName("format");
jw.Write(format);
jw.WritePropertyName("rate");
jw.Write(rate);
jw.WritePropertyName("channel");
jw.Write(channel);
jw.WritePropertyName("token");
jw.Write(token);
jw.WritePropertyName("cuid");
jw.Write(cuid);
jw.WritePropertyName("len");
jw.Write(len);
jw.WritePropertyName("speech");
jw.Write(speech);
jw.WriteObjectEnd();
WWW getASW = new WWW(url, Encoding.Default.GetBytes(jw.ToString()));
yield return getASW;
if (getASW.isDone)
{
if (getASW.error == null)
{
JsonData getASWJson = JsonMapper.ToObject(getASW.text);
if (getASWJson["err_msg"].ToString() == "success.")
{
audioToString = getASWJson["result"][0].ToString();
if (audioToString.Substring(audioToString.Length - 1) == ",")
audioToString = audioToString.Substring(0, audioToString.Length - 1);
Debug.Log("說話的問題是:" + audioToString);
GetAnswer(audioToString);
}
else
{
Debug.LogWarning("沒有成功:" + getASWJson["err_msg"].ToString());
}
}
else
{
Debug.LogError(getASW.error);
}
}
}
#endregion