using LitJson; using System; using UnityEngine; using System.Collections.Generic; public class SpeechTranslate : Singleton { public const int AUDIO_TIMELIMIT = 8;//秒 static readonly string serverURL = "http://vop.baidu.com/server_api"; static string token = ""; static readonly string apiKey = "qb8RdIh80K9LsGtNS0S1f2b9"; static readonly string secretKey = "1f6050e7c508e8bb8c598bac1cea067a"; static readonly string cuid = "123456789"; bool m_IsGetToken = false; public bool gotToken { get { return m_IsGetToken; } } bool m_Busy = false; public bool busy { get { return m_Busy; } } public event Action translateResultEvent = null; Queue translateQueue = new Queue(); public void RequestGetToken() { //var url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + apiKey + "&client_secret=" + secretKey; //HttpRequest.Instance.RequestHttpGet(url, HttpRequest.defaultHttpContentType, 2, OnGetToken); } private void OnGetToken(bool _ok, string _result) { if (_ok) { if (!string.IsNullOrEmpty(_result)) { JsonData data = JsonMapper.ToObject(_result); token = data["access_token"].ToString(); m_IsGetToken = true; } } } public void ReqeustVoiceTranslate(int _voiceInstanceId, byte[] _data) { if (m_IsGetToken && _data != null) { JsonData json = new JsonData(); json["format"] = "wav"; json["rate"] = VoiceSettings.frequency; json["channel"] = 1; json["token"] = token; json["cuid"] = cuid; json["len"] = _data.Length; json["speech"] = Convert.ToBase64String(_data); translateQueue.Enqueue(_voiceInstanceId); HttpRequest.Instance.RequestHttpPost(serverURL, json.ToJson(), HttpRequest.jsonHttpContentType, 1, OnGetTranslateResult); Debug.Log("serverURL" + serverURL); m_Busy = true; } } private void OnGetTranslateResult(bool _ok, string _result) { m_Busy = false; var instanceId = translateQueue.Dequeue(); if (!_ok) { if (translateResultEvent != null) { translateResultEvent(instanceId, false, string.Empty); } return; } JsonData data = JsonMapper.ToObject(_result); if (data["err_msg"].ToString() == "success.") { var content = data["result"][0].ToString(); if (content.EndsWith(",")) { content = content.Substring(0, content.Length - 1); } if (translateResultEvent != null) { translateResultEvent(instanceId, true, content); } } else { if (translateResultEvent != null) { translateResultEvent(instanceId, false, string.Empty); } Debug.Log("当前声音质量不合格:"+ data["err_msg"].ToString()); } } static float[] samples = new float[VoiceSettings.frequency * AUDIO_TIMELIMIT]; public static byte[] AudioCompress(AudioClip _clip, int _seconds) { if (_clip == null) { Debug.LogError("录音数据为空"); return null; } _clip.GetData(samples, 0); byte[] outData = new byte[VoiceSettings.frequency * _seconds * 2]; int rescaleFactor = 32767; //to convert float to Int16 for (int i = 0; i < VoiceSettings.frequency * _seconds; i++) { short temshort = (short)(samples[i] * rescaleFactor); if (temshort == 0) { outData[i * 2] = 0; outData[i * 2 + 1] = 0; } else { byte[] temdata = BitConverter.GetBytes(temshort); outData[i * 2] = temdata[0]; outData[i * 2 + 1] = temdata[1]; } } if (outData == null || outData.Length <= 0) { Debug.LogError("录音数据为空"); return null; } return outData; } public void AudioCompress(int _instanceId, AudioClip _clip) { if (_clip == null || VoiceCodec.IsBusy) { Debug.LogError("录音数据为空"); return; } _clip.GetData(samples, 0); var _instance = _instanceId; VoiceCodec.Encode(samples, (byte[] encode) => { VoiceWarehouse.SaveVoice(_instance, encode); }); } }