当前位置:   article > 正文

unity3d百度语音+图灵机器人_unity+语音绘画

unity+语音绘画
  1. using NAudio.Wave;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. using UnityEngine;
  9. using Xfrog.Net;
  10. public class AsrResponse
  11. {
  12. public int err_no;
  13. public string err_msg;
  14. public string sn;
  15. public string[] result;
  16. public static AsrResponse CreateFromJSON(string jsonString)
  17. {
  18. return JsonUtility.FromJson<AsrResponse>(jsonString);
  19. }
  20. }
  21. public class record : MonoBehaviour
  22. {
  23. AudioClip audioClip;
  24. AudioSource audioSource;
  25. public int recordTime=5;
  26. Use this for initialization
  27. void Start()
  28. { audioSource = GameObject.Find("Canvas/Audio Source").GetComponent<AudioSource>();
  29. string[] md = Microphone.devices;
  30. int mdl = md.Length;
  31. if (mdl == 0)
  32. {
  33. Debug.Log("no microphone found");
  34. }
  35. }
  36. Update is called once per frame
  37. //void Update () {
  38. //}
  39. public void StartRecordAudio()
  40. {
  41. Microphone.End(null);
  42. audioClip = Microphone.Start(null, false, recordTime, 16000);
  43. Debug.Log("开始录音.....");
  44. // if(Microphone.GetPosition())
  45. if (!Microphone.IsRecording(null))
  46. {
  47. Debug.Log("没有声音.....");
  48. return;
  49. }
  50. Microphone.GetPosition(null);
  51. }
  52. public void StopRecordAudio()
  53. {
  54. /***文件读取为字节流***
  55. FileInfo fi = new FileInfo("d:\\1.wav");
  56. FileStream fs = new FileStream("d:\\1.wav", FileMode.Open);
  57. byte[] buffer = new byte[fs.Length];
  58. fs.Read(buffer, 0, buffer.Length);
  59. fs.Close();
  60. ***/
  61. Microphone.End(null);
  62. //*************使用语音识别api
  63. byte[] buffer = ConvertAudioClipToPCM16(audioClip);
  64. //byte[] buffer = GetClipData();
  65. HttpWebRequest request = null;
  66. //request = (HttpWebRequest)HttpWebRequest.Create("https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=ZH-CN&format=detailed");
  67. request = (HttpWebRequest)HttpWebRequest.Create("http://vop.baidu.com/server_api?lan=zh&cuid=B8-81-98-41-3E-E9&token=24.91d00cdafeef1490ec706f7e2f2659e1.2592000.1524029061.282335-10681472");
  68. request.SendChunked = true;
  69. request.Accept = @"application/json;text/xml";
  70. request.Method = "POST";
  71. request.ProtocolVersion = HttpVersion.Version11;
  72. // request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000";
  73. request.ContentType = @"audio/pcm; rate = 16000";
  74. request.Headers["Ocp-Apim-Subscription-Key"] = "e8cd273d62c347cb9f64d6b94b94435d";
  75. request.ContentLength = buffer.Length;
  76. // Send an audio file by 1024 byte chunks
  77. /*
  78. * Open a request stream and write 1024 byte chunks in the stream one at a time.
  79. */
  80. using (Stream requestStream = request.GetRequestStream())
  81. {
  82. requestStream.Write(buffer, 0, buffer.Length);
  83. }
  84. Debug.Log("Response:");
  85. string responseString;
  86. WebResponse response = request.GetResponse();
  87. Debug.Log(((HttpWebResponse)response).StatusCode);
  88. StreamReader sr = new StreamReader(response.GetResponseStream());
  89. responseString = sr.ReadToEnd();
  90. responseString = AsrResponse.CreateFromJSON(responseString).result[0];
  91. Debug.Log(responseString);
  92. //************图灵api得到回答
  93. string url = "http://www.tuling123.com/openapi/api";
  94. string key = "7c664d28fa0b472ab9833c2679c431f5";
  95. string postDataStr = "key=" + key + "&info=" + responseString;
  96. string result = HttpGet(url, postDataStr);
  97. JsonObject newObj = new JsonObject(result);
  98. string info = newObj["text"].Value.ToString();
  99. Debug.Log(info);
  100. **************合成语音不支持linux和window只支持移动端,据说mp3版权问题,无法直接在window上播放,也就是无法从网上或者文件里读取播放。场景里的应该被转编码了。
  101. //string url_speaker = "http://tsn.baidu.com/text2audio";
  102. //string postDataStr_speaker = "tex=" + info + "&lan=zh&cuid=B8-81-98-41-3E-E9&ctp=1&tok=24.d1ba8c1f1efa8a3de68678e5404d55a4.2592000.1523629153.282335-10681472&ctp=1&cuid=10681472";
  103. //string req = url_speaker + "?" + postDataStr_speaker;
  104. //WWW www = new WWW(req); // start a download of the given URL
  105. //audioSource.clip = www.GetAudioClip(true, false, AudioType.MPEG); // 2D, streaming
  106. //audioSource.Play();
  107. //**************合成语音
  108. string url2 = "http://tsn.baidu.com/text2audio";
  109. byte[] buffer2 = null;
  110. string postDataStr2 = "tex=" + info + "&lan=zh&cuid=B8-81-98-41-3E-E9&ctp=1&tok=24.d1ba8c1f1efa8a3de68678e5404d55a4.2592000.1523629153.282335-10681472";
  111. HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url2 + "?" + postDataStr2);
  112. request2.Method = "GET";
  113. request2.ContentType = "text/html;charset=UTF-8";
  114. HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
  115. using (Stream stream = response2.GetResponseStream())
  116. {
  117. //buffer2 = new byte[stream.Length]; 报错*******无法取得响应流,应该用什么先盛放一下
  118. // long length = request2.ContentLength;
  119. //buffer2 = new byte[length];///数字溢出
  120. // stream.Read(buffer2, 0, (int)length);
  121. //BinaryReader br = new BinaryReader(stream);
  122. // buffer2 = br.ReadBytes((int)stream.Length);
  123. byte[] buffer3 = new byte[16*1096];
  124. using (MemoryStream memoryStream = new MemoryStream())
  125. {
  126. int count = 0;
  127. do
  128. {
  129. count = stream.Read(buffer3, 0, buffer3.Length);
  130. memoryStream.Write(buffer3, 0, count);
  131. } while (count != 0);
  132. buffer2 = memoryStream.ToArray();
  133. }
  134. }
  135. audioSource.clip = FromMp3Data(buffer2);
  136. audioSource.Play();
  137. }
  138. /// <summary>
  139. /// 将mp3格式的字节数组转换为audioclip
  140. /// </summary>
  141. /// <param name="data"></param>
  142. /// <returns></returns>
  143. public static AudioClip FromMp3Data(byte[] data)
  144. {
  145. // Load the data into a stream
  146. MemoryStream mp3stream = new MemoryStream(data);
  147. // Convert the data in the stream to WAV format
  148. Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);
  149. WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio);
  150. // Convert to WAV data
  151. Wav wav = new Wav(AudioMemStream(waveStream).ToArray());
  152. AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false);
  153. audioClip.SetData(wav.LeftChannel, 0);
  154. // Return the clip
  155. return audioClip;
  156. }
  157. private static MemoryStream AudioMemStream(WaveStream waveStream)
  158. {
  159. MemoryStream outputStream = new MemoryStream();
  160. using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
  161. {
  162. byte[] bytes = new byte[waveStream.Length];
  163. waveStream.Position = 0;
  164. waveStream.Read(bytes, 0, Convert.ToInt32(waveStream.Length));
  165. waveFileWriter.Write(bytes, 0, bytes.Length);
  166. waveFileWriter.Flush();
  167. }
  168. return outputStream;
  169. }
  170. //**********audioClip格式转成字节流
  171. public static byte[] ConvertAudioClipToPCM16(AudioClip clip)
  172. {
  173. var samples = new float[clip.samples * clip.channels];
  174. clip.GetData(samples, 0);
  175. var samples_int16 = new short[samples.Length];
  176. for (var index = 0; index < samples.Length; index++)
  177. {
  178. var f = samples[index];
  179. samples_int16[index] = (short)(f * short.MaxValue);
  180. }
  181. var byteArray = new byte[samples_int16.Length * 2];
  182. Buffer.BlockCopy(samples_int16, 0, byteArray, 0, byteArray.Length);
  183. return byteArray;
  184. }
  185. ************把mp3转换成audioclip
  186. //public static AudioClip FromMp3Data(byte[] data)
  187. //{
  188. // // Load the data into a stream
  189. // MemoryStream mp3stream = new MemoryStream(data);
  190. // // Convert the data in the stream to WAV format
  191. // Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);
  192. // WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio);
  193. // // Convert to WAV data
  194. // Wav wav = new Wav(AudioMemStream(waveStream).ToArray());
  195. // AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false);
  196. // audioClip.SetData(wav.LeftChannel, 0);
  197. // // Return the clip
  198. // return audioClip;
  199. //}
  200. //用于http get请求
  201. public static string HttpGet(string Url, string postDataStr)
  202. {
  203. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
  204. request.Method = "GET";
  205. request.ContentType = "text/html;charset=UTF-8";
  206. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  207. Stream myResponseStream = response.GetResponseStream();
  208. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
  209. string retString = myStreamReader.ReadToEnd();
  210. myStreamReader.Close();
  211. myResponseStream.Close();
  212. return retString;
  213. }
  214. public void PlayRecordAudio()
  215. {
  216. Microphone.End(null);
  217. audioSource.clip = audioClip;
  218. audioSource.Play();
  219. }
  220. public void EndPlayRecordAudio()
  221. {
  222. Microphone.End(null);
  223. audioSource.Stop();
  224. }
  225. /// <summary>
  226. /// 把录音转换为Byte[]
  227. /// </summary>
  228. /// <returns></returns>
  229. //public byte[] GetClipData()
  230. //{
  231. // if (audioClip == null)
  232. // {
  233. // //Debug.LogError("录音数据为空");
  234. // Debug.Log("录音数据为空");
  235. // return null;
  236. // }
  237. // float[] samples = new float[audioClip.samples];
  238. // audioClip.GetData(samples, 0);
  239. // byte[] outData = new byte[samples.Length * 2];
  240. // int rescaleFactor = 32767; //to convert float to Int16
  241. // for (int i = 0; i < samples.Length; i++)
  242. // {
  243. // short temshort = (short)(samples[i] * rescaleFactor);
  244. // byte[] temdata = System.BitConverter.GetBytes(temshort);
  245. // outData[i * 2] = temdata[0];
  246. // outData[i * 2 + 1] = temdata[1];
  247. // }
  248. // if (outData == null || outData.Length <= 0)
  249. // {
  250. // //Debug.LogError("录音数据为空");
  251. // Debug.Log("录音数据为空");
  252. // return null;
  253. // }
  254. // //return SubByte(outData, 0, audioLength * 8000 * 2);
  255. // return outData;
  256. //}
  257. }

项目失败,unity3d windows和ubuntu不支持mp3流,

using NAudio.Wave;

转换只在windows上起效。


所以这份代码只在windows上和移动平台,安卓,ios上起效。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/127055
推荐阅读
相关标签
  

闽ICP备14008679号