• 本文首发于“洪流学堂”公众号。 洪流学堂,让你快人几步 源码地址 https://github.com/zhenghongzhi/WitBaiduAip 功能概述 1 语音识别 从麦克风录制音频 AudioClip的音频数据...百度语音合成Restf...

    本文首发于“洪流学堂”公众号。
    洪流学堂,让你快人几步!你好,我是你的技术探路者郑洪智,你可以叫我大智(vx: zhz11235)。

    源码地址

    https://github.com/zhenghongzhi/WitBaiduAip

    功能概述

    1 语音识别

    1. 从麦克风录制音频
    2. AudioClip的音频数据转换为百度语音识别的PCM16格式
    3. 百度语音识别Restful接口的封装以及一个测试场景

    2 语音合成

    1. 百度语音合成Restful接口的封装以及一个测试场景
    2. mp3格式运行时转为AudioClip进行播放

    为什么不使用百度的C# SDK
    百度的C# SDK使用了一些Unity不支持的特性,直接导入unity不能用
    而且百度C# SDK只是封装了Restful的接口,功能上并没有增多
    自己编写更简洁

    更新说明

    2018-08-22更新

    根据百度API的更新,语音合成性能优化,直接使用原生格式,移除第三方插件

    2018-03-28更新

    加入平台判断,更好的支持android和ios

    2018-01-11更新

    在工程中加入了语音合成

    2018-01-02更新

    应广大小伙伴的要求,对工程进行了重构,放出github源码
    https://github.com/zhenghongzhi/WitBaiduAip

    2017-12-23更新

    教程首发


    洪流学堂,让你快人几步
    欢迎关注“洪流学堂”微信公众号

    展开全文
  • BaiduVoiceTestUnity3d使用百度的Rest Api实现语音交互,可以将语音转化为文本,也...该平台上由于编译环境的问题接入SDK会出现种种问题,所以只能使用百度语音的RestApi来实现。 使用RestApi就可以不受平台的限制了。

    BaiduVoiceTest

    Unity3d使用百度的Rest Api实现语音交互,可以将语音转化为文本,也可以将文本转化为语音。
    这个项目原本是为一个HoloLens眼镜的虚拟角色语音交互做准备。该平台上由于编译环境的问题接入SDK会出现种种问题,所以只能使用百度语音的RestApi来实现。 使用RestApi就可以不受平台的限制了。

    语音解析

    百度语音的RestApi接口实现语音转化为文本,只需要通过http请求发送本地录音所生成的音频文件,只不过请求需要的是pcm格式,采样率为1600的文件,录音完毕以后需要进行相应转码。

    语音合成

    在使用语音合成接口实现将文本转化为合成语音时,由于百度语音合成接口返回的音频文件是mp3格式的,出现了比较棘手的问题就是Unity3D不支持网络获取到的mp3格式的音频文件,这里还需要使用到一个NAudio插件,来实现将mp3音频转换成为unity3d可以直接播放的AudioClip。


    整个项目文件结构如下:
    image
    把代码重构了一下,将整个工具类做成了单例,==在使用时需要将需要将BaiduApiToolkit脚本绑定到有AudioSource组件的物体上才能播放语音==。然后Wav脚本与Plugins中的NAudio都是音频转换过程中需要的。
    下面是调用UIManger中调用Baidu接口的代码:

    private void Update()
        {
            if (Input.GetKeyDown(KeyCode.S))
            {
               BaiduApiToolkit.Instance.StartRecord();          
                titleText.text = "正在聆听……";
            }
            if (Input.GetKeyUp(KeyCode.S))
            {
                titleText.text = "请摁住”S“键开始说话";
                resultText.text = "正在识别……";
                BaiduApiToolkit.Instance.StopRecord(delegate(string result)//通过回调获取协程中通过网络传回的语音文本
                {
                    if (result==null)//如果返回null代表解析失败
                    {
                        this.resultText.text = "抱歉,不能识别语音,请重复一遍!";
                    }
                    else
                    {
                        this.resultText.text = result;
                    }
                    string answer = BaiduApiToolkit.Instance.AnswerQuestion(result);
                    answerText.text = "回答:" + answer;
                    BaiduApiToolkit.Instance.ReadText(answer);
                });
            }
            if (Input.GetKeyDown(KeyCode.A))
            {
                BaiduApiToolkit.Instance.ReadText("你好呀");
            }
        }

    之前本想在嵌入一个只能回复的接口,不过目前还没有实现,只是在BaiduApiToolkit脚本中写死了一个简单的回答。

    关于接口的调用

    在使用时只需要通过StratRecord()开始录音,然后再通过StopRecord()结束录音,不过StopRecord()方法会有一个异步回调函数,用于将解析完成后的文本返回到回调函数中。
    此外就是ReadText(string text)方法,这里的text参数就是要合成语音文本,传入文本之后,接口会自动播放语音。

    还有点小问题

    在语音解析过程中,由于录音最大时长是写死的,并且无论说了多长时间,哪怕说了一秒钟,最后也按照最大时长生成音频并传输,所以解析效率有点低,原本想把音频文件压缩一下再发送,不过有点犯懒就不想写了 = =。不过百度语音的语音合成速度还是相当迅速的!还是相当令人满意的。

    展开全文
  • unity3d百度在线语音转文字,文字转语音。可以跨平台运行。https://blog.csdn.net/luoyikun/article/details/81212838
  • using (Stream stream = response.GetResponseStream()) { buffer2 = new byte[stream.Length]; stream.Read(buffer2, 0, buffer2.Length); }stream.Length失败解决方案 1、用unity自带...
      using (Stream stream = response.GetResponseStream())
            {
                buffer2 = new byte[stream.Length];
                stream.Read(buffer2, 0, buffer2.Length);

            }


    stream.Length失败



    解决方案 

    1、用unity自带的www类

    2、https://bbs.csdn.net/topics/360163784

    byte[] result;
    byte[] buffer = new byte[4096];
     
    WebRequest wr = WebRequest.Create(someUrl);
     
    using(WebResponse response = wr.GetResponse())
    {
       using(Stream responseStream = response.GetResponseStream())
       {
          using(MemoryStream memoryStream = new MemoryStream())
          {
             int count = 0;
             do
             {
                count = responseStream.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
     
             } while(count != 0);
     
             result = memoryStream.ToArray();
     
          }
       }
    }

    展开全文
  • 百度语音识别 using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Xml; using LitJson; using System.Text; using System; using UnityEngine.UI; using ...

    直接上unity的C#脚本代码

    百度语音识别

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Xml;
    using LitJson;
    using System.Text;
    using System;
    using UnityEngine.UI;
    using System.IO;
    
    public class showVoiceResult1 : MonoBehaviour {
    
        private string token;                           //access_token
        private string cuid = "liang";                  //用户标识
        private string format = "wav";                  //语音格式
        private int rate = 8000;                        //采样率
        private int channel = 1;                        //声道数
        private string speech;                          //语音数据,进行base64编码
        private int len;                                //原始语音长度
        private string lan = "zh";                      //语种
    
        private string grant_Type = "client_credentials";
        private string client_ID = "********";                       //百度appkey
        private string client_Secret = "******";                   //百度Secret Key
    
        private string baiduAPI = "http://vop.baidu.com/server_api";
        private string getTokenAPIPath = "https://openapi.baidu.com/oauth/2.0/token";
    
        private byte[] clipByte;
        public Text debugText;
    
        /// <summary>
        /// 
        /// 转换出来的TEXT
        /// </summary>
        public static string audioToString;
    
        private AudioSource aud;
        private int audioLength;//录音的长度
    
        void Start () {
    
        }
    
        // Update is called once per frame
        void Update () {
            debugText.text = audioToString;
        }
        /// <summary>
        /// 获取百度用户令牌
        /// </summary>
        /// <param name="url">获取的url</param>
        /// <returns></returns>
        private IEnumerator GetToken(string url)
        {
            WWWForm getTForm = new WWWForm();
            getTForm.AddField("grant_type", grant_Type);
            getTForm.AddField("client_id", client_ID);
            getTForm.AddField("client_secret", client_Secret);
    
            WWW getTW = new WWW(url, getTForm);
            yield return getTW;
            if (getTW.isDone)
            {
                if (getTW.error == null)
                {
                    token = JsonMapper.ToObject(getTW.text)["access_token"].ToString();
                    StartCoroutine(GetAudioString(baiduAPI));
                }
                else
                    Debug.LogError(getTW.error);
            }
        }
    
        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();
            WWWForm w = new WWWForm();
    
    
            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);
                    }
                }
                else
                {
                    Debug.LogError(getASW.error);
                }
            }
        }
    
        public void StartMic()
        {
    
            if (Microphone.devices.Length == 0) {
                Debug.Log ("no devices");
                return;
            } 
            Microphone.End(null);
            Debug.Log("Start");
            Debug.Log(Microphone.devices);
            aud.clip = Microphone.Start("Built-in Microphone", false, 10, rate);
        }
    
        /// <summary>
        /// 结束录音
        /// </summary>
        public void EndMic()
        {
            int lastPos = Microphone.GetPosition(null);
            if (Microphone.IsRecording(null))
                audioLength = lastPos / rate;//录音时长  
            else
                audioLength = 10;
            Debug.Log("Stop");
            Microphone.End(null);
    
            clipByte = GetClipData();
            len = clipByte.Length;
            speech = Convert.ToBase64String(clipByte);
            StartCoroutine(GetToken(getTokenAPIPath));
            Debug.Log(len);
            Debug.Log(audioLength);
        }
    
        /// <summary>
        /// 把录音转换为Byte[]
        /// </summary>
        /// <returns></returns>
        public byte[] GetClipData()
        {
            if (aud.clip == null)
            {
                Debug.LogError("录音数据为空");
                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);
    
                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("录音数据为空");
                return null;
            }
    
            //return SubByte(outData, 0, audioLength * 8000 * 2);
            return outData;
        }
    }
    

    百度语音主要借鉴了另一篇文章,地址忘了

    百度语音合成部分

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Xml;
    using LitJson;
    using System.Text;
    using System;
    using UnityEngine.UI;
    using System.IO;
    
    public class showTextTTSResult : MonoBehaviour {
    
        private string text;  //user input text
        private string token;                           //access_token
        private string cuid = "***";                  //current user id
        private int ctp = 1; // client type choose, web is only value 1
        private string lan = "zh"; 
        private int spd = 5;
        private int pit = 5;
        private int vol = 5;
        private int per = 3;    //person voice 
    
        private string grant_Type = "client_credentials";
        private string client_ID = "****";                       //百度appkey
        private string client_Secret = "****";                   //百度Secret Key
    
        private string baiduAPI = "http://tsn.baidu.com/text2audio";
        private string getTokenAPIPath = "https://openapi.baidu.com/oauth/2.0/token";
    
        private byte[] clipByte;
        public Text debugText;
        public Text debugMsg;
    
        /// <summary>
        /// 
        /// 转换出来的TEXT
        /// </summary>
        public static string audioToString;
    
        private AudioSource aud;
        private int audioLength;//录音的长度
        private string filePath;
        void Start () {
    
        }
    
        // Update is called once per frame
        void Update () {
            /*if (audioToString != null) {
                debugText.text = audioToString;
            }*/
    
    
        }
        /// <summary>
        /// get token
        /// </summary>
        /// <param name="url">url</param>
        /// <returns></returns>
        private IEnumerator GetToken(string url)
        {
            WWWForm getTForm = new WWWForm();
            getTForm.AddField("grant_type", grant_Type);
            getTForm.AddField("client_id", client_ID);
            getTForm.AddField("client_secret", client_Secret);
    
            WWW getTW = new WWW(url, getTForm);
            yield return getTW;
    
            Debug.Log (getTW.text);
            if (getTW.isDone)
            {
                if (getTW.error == null)
                {
                    token = JsonMapper.ToObject(getTW.text)["access_token"].ToString();
                    Debug.Log (token);
                    debugMsg.text += "token:"+token+"\n";
                    //StartCoroutine(GetAudioString(baiduAPI));
                    StartCoroutine(GetTextAudio(baiduAPI));
                }
                else
                    Debug.LogError(getTW.error);
            }
        }
    
        private IEnumerator GetTextAudio(string url){
            //url?lan ctp  cuid  tok tex vol per spd pit
            WWWForm getTForm = new WWWForm();
            getTForm.AddField ("lan", lan);
            getTForm.AddField ("ctp", ctp);
            getTForm.AddField ("cuid", cuid);
            getTForm.AddField ("tok", token);
            getTForm.AddField ("tex", /*WWW.EscapeURL(*/debugText.text/*)*/);
            getTForm.AddField ("vol",vol);
            getTForm.AddField ("per", per);
            getTForm.AddField ("spd", spd);
            getTForm.AddField ("pit", pit);
    
            WWW getTW = new WWW (url,getTForm);
            yield return getTW;
            byte[] s = getTW.bytes;
            filePath = Application.persistentDataPath+"/1.mp3";
            //filePath = "/data/data/com.example.baiduTTS/1.mp3";
            File.Delete (filePath);
            if (writeFile (s, filePath)) {
                debugMsg.text += "success to translate txt to voice\n";
                debugMsg.text += "the voice byte[] length:"+s.Length+"\n";
            } else {
                debugMsg.text = "fail";
            }
            WWW w = new WWW ("file://"+filePath);
            aud.clip = w.GetAudioClip (false, false, AudioType.MPEG);
    
            Debug.Log (debugText.text);
            //debugMsg.text += "txt source:" + debugText.text+"\n";
            Debug.Log (s.Length);
            if (getTW.isDone) {
                if (getTW.error == null) {
                    //debugMsg.text = "合成成功 音频字节长度为"+getTW.bytesDownloaded;
                    //Debug.Log (getTW.bytesDownloaded);
                    //JsonData getASWJson = JsonMapper.ToObject (getTW.text);
                    //Debug.Log (getASWJson.Count);
                    //Debug.Log (getASWJson["result"]);
                }else{
                    Debug.Log (getTW.error);
                }
            }
    
        }
    
    
        private bool writeFile(byte[] readByte,string fileName){
            FileStream pFileStream = null;
            try{
                pFileStream = new FileStream(fileName,FileMode.OpenOrCreate);
                pFileStream.Write(readByte,0,readByte.Length);
            }catch{
                return false;
            }finally{
                if (pFileStream != null) {
                    pFileStream.Close ();
                }
            }
            return true;
        }
        public void startTTS()
        {
            debugMsg.text = "";
            StartCoroutine(GetToken(getTokenAPIPath));
        }
    
        public void playAud(){
    
            aud.Play ();
            /*if (!aud.isPlaying) {
                aud.Play ();
            }*/
            debugMsg.text += "play the audio:"+aud.isPlaying+"\n";
            debugMsg.text += "the audio useful:"+aud.enabled+"\n";
    
        }
    }
    

    仿照百度语音识别脚本写的,里面重点主要是获取的音频无法在unity直接播放,主要是文件夹权限问题,unity可读写文件夹和Android不一样,有固定的对应文件夹,Application.persistentDataPath是一个可读写文件夹,相关知识有博客,可自行搜索,地址忘了。aud.clip = w.GetAudioClip (false, false, AudioType.MPEG);是将MP3文件赋给unity的音频对象。


    时隔几个月,现在使用此脚本的时候报错,¥_¥上周在项目中还能用
    补充下暂时的情况:经过给评论区同学答疑,发现麦克风设备只能有一个,如果是两个就无法Start。但现在麦克风能启动,但数据获取不到,解决中。。。@_@

    最近事情多,找到解决方案后再进行说明。。。
    展开全文
  • using NAudio.Wave; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using UnityEngine;...public class AsrR...
    using NAudio.Wave;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    using UnityEngine;
    using Xfrog.Net;
    public class AsrResponse
    {
        public int err_no;
        public string err_msg;
        public string sn;
        public string[] result;
        public static AsrResponse CreateFromJSON(string jsonString)
        {
            return JsonUtility.FromJson<AsrResponse>(jsonString);
        }
    }
    public class record : MonoBehaviour
    {
        AudioClip audioClip;
        AudioSource audioSource;
    
        public int recordTime=5;
        //// Use this for initialization
        void Start()
        {   audioSource = GameObject.Find("Canvas/Audio Source").GetComponent<AudioSource>();
            string[] md = Microphone.devices;
            int mdl = md.Length;
            if (mdl == 0)
            {
                Debug.Log("no microphone found");
            }
        }
    
        //// Update is called once per frame
        //void Update () {
    
        //}
        public void StartRecordAudio()
        {
       
            Microphone.End(null);
            audioClip = Microphone.Start(null, false, recordTime, 16000);
            Debug.Log("开始录音.....");
            // if(Microphone.GetPosition())
            if (!Microphone.IsRecording(null))
            {
                Debug.Log("没有声音.....");
                return;
            }
            Microphone.GetPosition(null);
    
        }
    
        public void StopRecordAudio()
        {
            /***文件读取为字节流***
            FileInfo fi = new FileInfo("d:\\1.wav");
            FileStream fs = new FileStream("d:\\1.wav", FileMode.Open);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            ***/
            Microphone.End(null);
    
    
    
            //*************使用语音识别api
            byte[] buffer = ConvertAudioClipToPCM16(audioClip);
           //byte[] buffer = GetClipData();    
            HttpWebRequest request = null;
         //request = (HttpWebRequest)HttpWebRequest.Create("https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=ZH-CN&format=detailed");
          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");
           request.SendChunked = true;
            request.Accept = @"application/json;text/xml";
            request.Method = "POST";
           request.ProtocolVersion = HttpVersion.Version11;
           // request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000";
            request.ContentType = @"audio/pcm; rate = 16000";
           request.Headers["Ocp-Apim-Subscription-Key"] = "e8cd273d62c347cb9f64d6b94b94435d";
            request.ContentLength = buffer.Length;
            // Send an audio file by 1024 byte chunks
            /*
            * Open a request stream and write 1024 byte chunks in the stream one at a time.
            */
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(buffer, 0, buffer.Length);
            }
            Debug.Log("Response:");
            string responseString;
            WebResponse response = request.GetResponse();
            Debug.Log(((HttpWebResponse)response).StatusCode);
            StreamReader sr = new StreamReader(response.GetResponseStream());
            responseString = sr.ReadToEnd();
            responseString = AsrResponse.CreateFromJSON(responseString).result[0];
            Debug.Log(responseString);
    
    
    
            //************图灵api得到回答
            string url = "http://www.tuling123.com/openapi/api";
            string key = "7c664d28fa0b472ab9833c2679c431f5";
            string postDataStr = "key=" + key + "&info=" + responseString;
            string result = HttpGet(url, postDataStr);
            JsonObject newObj = new JsonObject(result);
            string info = newObj["text"].Value.ToString();
            Debug.Log(info);
    
            ////**************合成语音不支持linux和window只支持移动端,据说mp3版权问题,无法直接在window上播放,也就是无法从网上或者文件里读取播放。场景里的应该被转编码了。
            //string url_speaker = "http://tsn.baidu.com/text2audio";
            //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";
            //string req = url_speaker + "?" + postDataStr_speaker;
            //WWW www = new WWW(req);  // start a download of the given URL
            //audioSource.clip = www.GetAudioClip(true, false, AudioType.MPEG); // 2D, streaming
            //audioSource.Play();
    
    
    
            //**************合成语音
            string url2 = "http://tsn.baidu.com/text2audio";
            byte[] buffer2 = null;
            string postDataStr2 = "tex=" + info + "&lan=zh&cuid=B8-81-98-41-3E-E9&ctp=1&tok=24.d1ba8c1f1efa8a3de68678e5404d55a4.2592000.1523629153.282335-10681472";
    
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url2 + "?" + postDataStr2);
            request2.Method = "GET";
            request2.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
    
            using (Stream stream = response2.GetResponseStream())
            {
                //buffer2 = new byte[stream.Length];  ////////////////报错*******无法取得响应流,应该用什么先盛放一下
    
                // long length = request2.ContentLength;
                //buffer2 = new byte[length];///数字溢出
                // stream.Read(buffer2, 0, (int)length);
                //BinaryReader br = new BinaryReader(stream);
                //    buffer2 = br.ReadBytes((int)stream.Length);
             
                    byte[] buffer3 = new byte[16*1096];
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        int count = 0;
                        do
                        {
                            count = stream.Read(buffer3, 0, buffer3.Length);
                            memoryStream.Write(buffer3, 0, count);
    
                        } while (count != 0);
    
                        buffer2 = memoryStream.ToArray();
    
                    }
              
    
    
            }
            audioSource.clip = FromMp3Data(buffer2);
            audioSource.Play();
        }
        /// <summary>
        /// 将mp3格式的字节数组转换为audioclip
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
    
        public static AudioClip FromMp3Data(byte[] data)
        {
            // Load the data into a stream  
            MemoryStream mp3stream = new MemoryStream(data);
            // Convert the data in the stream to WAV format  
            Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);
    
            WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio);
            // Convert to WAV data  
            Wav wav = new Wav(AudioMemStream(waveStream).ToArray());
    
            AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false);
            audioClip.SetData(wav.LeftChannel, 0);
            // Return the clip  
            return audioClip;
        }
    
        private static MemoryStream AudioMemStream(WaveStream waveStream)
        {
            MemoryStream outputStream = new MemoryStream();
            using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
            {
                byte[] bytes = new byte[waveStream.Length];
                waveStream.Position = 0;
                waveStream.Read(bytes, 0, Convert.ToInt32(waveStream.Length));
                waveFileWriter.Write(bytes, 0, bytes.Length);
                waveFileWriter.Flush();
            }
            return outputStream;
        }
        //**********audioClip格式转成字节流
        public static byte[] ConvertAudioClipToPCM16(AudioClip clip)
        {
            var samples = new float[clip.samples * clip.channels];
            clip.GetData(samples, 0);
            var samples_int16 = new short[samples.Length];
    
            for (var index = 0; index < samples.Length; index++)
            {
                var f = samples[index];
                samples_int16[index] = (short)(f * short.MaxValue);
            }
    
            var byteArray = new byte[samples_int16.Length * 2];
            Buffer.BlockCopy(samples_int16, 0, byteArray, 0, byteArray.Length);
    
            return byteArray;
    
        }
        ////************把mp3转换成audioclip
        //public static AudioClip FromMp3Data(byte[] data)
        //{
        //    // Load the data into a stream  
        //    MemoryStream mp3stream = new MemoryStream(data);
        //    // Convert the data in the stream to WAV format  
        //    Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);
    
        //    WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio);
        //    // Convert to WAV data  
        //    Wav wav = new Wav(AudioMemStream(waveStream).ToArray());
    
        //    AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false);
        //    audioClip.SetData(wav.LeftChannel, 0);
        //    // Return the clip  
        //    return audioClip;
        //}
    
        //用于http get请求
        public static string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
    
    
        }
        public void PlayRecordAudio()
        {
            Microphone.End(null);
            audioSource.clip = audioClip;
            audioSource.Play();
        }
        public void EndPlayRecordAudio()
        {
            Microphone.End(null);
            audioSource.Stop();
        }
    
    
    
    
        /// <summary>
        /// 把录音转换为Byte[]
        /// </summary>
        /// <returns></returns>
        //public byte[] GetClipData()
        //{
        //    if (audioClip == null)
        //    {
        //        //Debug.LogError("录音数据为空");
        //        Debug.Log("录音数据为空");
        //        return null;
        //    }
    
        //    float[] samples = new float[audioClip.samples];
    
        //    audioClip.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);
    
        //        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("录音数据为空");
        //        Debug.Log("录音数据为空");
        //        return null;
        //    }
    
        //    //return SubByte(outData, 0, audioLength * 8000 * 2);
        //    return outData;
        //}
    }
    

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

    using NAudio.Wave;

    转换只在windows上起效。


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

    展开全文
  • 查阅unity api unity5.5.0以上版本可使用win10自带语音识别功能,语音合成采用百度语音api
  • Unity 文字转语音 实时播放 整体很简单,只需要简单的搭载使就用就可以了。 废话不多说了 直接上代码。 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine....
  • 1.背景现在我们要给我们的游戏接入百度语音SDK,让我们的游戏可以被声控,当我们说:上,游戏就响应上,下,游戏就响应下,左,游戏就响应左, 右,游戏就响应右。或者听到玩家频繁 说我靠、靠等就知道玩家不耐烦了...
  • Unity3D平台使用图片识别+语音合成技术的一个Demo 主要功能是通过手机摄像头获取拍摄画面,在通过图片...百度语音合成 EasyAR 百度安卓API工程可用Android Studio直接打开,也可以直接使用编译好的DLL直接导入...
  • eclipse这边推荐下载一个ADT_bundle对应自己的系统版本(里面有自带的android sdk),不然自己下载的eclipse可能会没有 android application project 让你创建,我的就是刚开始没有 adt_bundle_win_64下载地...
  • 当时做了一个“极简版”的(其实是在百度API的Demo的基础上改了改而已),只有一个按钮,把语音合成和保存的功能都挤到一个函数里实现了。反正是自己用,也没那么讲究了,功能过的去就好了。 但是用着用着,问题...
  • using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Windows.Speech;//引入命名空间 利用 public class speechKey : MonoBehaviour { ... private PhraseR...
  • unity3d 各大插件评测

    2018-01-30 15:00:43
    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院 引言:想用Unity3D制作优秀的游戏,插件是必不可少的。工欲善其事必先利其器。本文主旨是告诉使用Unity3D引擎的同学们如何根据需求选择适当的工具...
  • 最近被迫做一些前端开发...1.在IOS调Microphone.Start(),报错问题(程序卡死),见描述: http://answers.unity3d.com/questions/711211/crash-on-microphonestart-in-audiomanagergetrecordp.html  解决:首先确实
  • 废话不多说先上视频,各位小伙伴要的是这种效果就可以继续看下去了。 Unity实现语音识别 占坑。稍后展开。 ...
  • 该Demo是用Unity原生的录音系统来实现聊天语音室,语音识别,语音合成; 原生的录音存在这录音时间固定,内存大小固定的问题,该工程解决录音以上的问题,可以将音频保存本地,或者服务器上面,目前功能已经实现,...
  • 百度人工智能平台(AIP)有很多种产品,这里使用的是语音技术产品在Unity中实现语音识别功能。 百度语音识别产品共分为两种,标准版和极速版,都是以上传一段不超过60s的录音的方式进行语音识别,详细差别请查阅...
  • 使用百度API进行在线文字换语音,语音合成
  • # -*- coding=utf-8 -*- import urllib.request import json import sys import webbrowser #reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入sys...#这个是解决合成中文文
1 2 3 4 5 ... 7
收藏数 138
精华内容 55