美文网首页
无标题文章

无标题文章

作者: 萧非子 | 来源:发表于2017-11-08 18:41 被阅读28次

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LitJson;
using System.Text;
using UnityEngine.Networking;
using System.IO;
using System.Net;

public class BaiduYY : MonoBehaviour {
public static BaiduYY _instanve;

//百度语音识别参数_必填
private string format = "wav";                  //语音格式,pcm,wav.amr,推荐pcm
private int rate = 16000;                        //采样率;8000,16000,推荐16000;
private int channel = 1;                        //声道数

//private string cuid;
private string token;                           //access_token
//_选填_参数
private  string lan = "zh";                      //语种
private  string url;                             //可下载的语音地址与callback连一起使用,确保百度服务器可以访问
private  string calback;                         //用户服务器的识别结果回调地址
private  string speech;                          //本地语音文件的的二进制语音数据 ,需要进行base64 编码。与len参数连一起使用。
private int len;                                //原始语音长度/本地语音文件的的字节数,单位字节

// private string len; //原始语音长度/本地语音文件的的字节数,单位字节

//百度语音合成参数_必填
//private string tex="好好学习,天天向上";                        //合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节

//private string lan="zh";                       //语言选择。中英混合模式
private string tok;                             //开放平台获取到的开发者access_token
private int ctp =1;                              //客户端类型选择,web端填写固定值1
//private string cuid;
//选填_参数
private string spd = "5";//语速0-9,默认=5;
private string pit = "5";//音调0-9,默认=5;
private string vol = "5";//音量0-15,默认=5;
private string per ="0";//发音人选择,0为普通女声,1为普通男声,3为情感合成-度逍遥,4为情感合成-度YY,默认为普通女声;


/***

*应用名称:BDAIX
*AppID:10313629
API Key;Lgk3vC7FXQfQGW76zVg4pZKO
*Secret Key;bB8kbhl1Pnef3NG6MfG5AaYmI3SV53YC

  • 百度语音
    应用名称:BaiduYY
    AppID:10307503
    API Key:a3qqZofX0vWh3XkukfxRDsTl
    Secret Key:8gT9nzQIOGeZkpPBKZs4tvKlptgdI25w

语音识别
请求地址:
http://vop.baidu.com/server_api
语音合成
请求地址
http://tsn.baidu.com/text2audio
在线口令地址
https://openapi.baidu.com/oauth/2.0/token
*/
private string grant_Type = "client_credentials"; //授权类型
private string cuid = "10313629"; //百度App ID//用户唯一标识,
private string client_ID = "Lgk3vC7FXQfQGW76zVg4pZKO"; //百度appkey
private string client_Secret = "bB8kbhl1Pnef3NG6MfG5AaYmI3SV53YC"; //百度Secret Key
private string baiduAPI_vop = "http://vop.baidu.com/server_api"; //百度语音——在线识别
private string baiduAPI_tsn = "http://tsn.baidu.com/text2audio"; //百度语音——在线合成
private string getTokenAPIPath = "https://openapi.baidu.com/oauth/2.0/token";//百度语音——在线口令地址
private string tex = ""; //合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节

private Byte[] clipByte;
//private string path = Application.dataPath + "/Resources/";//合成的语音文件保存的地址
//private  string name = "xxmymp3.mp3";//合成的语音文件保存的文件名

//public Button start;
//public Button end;
//public Button quit;
public Text debugText;//显示百度语音识别的文字,
public InputField inputField;//显示提示信息
public Text inputField_Text;
private bool isTTA = false;//是否语音合成=否
/// <summary>
/// 语音转换出来的文字
/// </summary>
public static string audioToString;

public static AudioClip textToAudio;

private AudioSource aud;
private int audioLength;//录音的长度

// private static string mp3path = Application.streamingAssetsPath + "/";//文本转成语音,文件存储路径
// private static string mp3name = "mymp3.mp3";//文本转成语音后,文件存储名称

private void Awake()
{
    _instanve = this;
    if (GetComponent<AudioSource>() == null)
        aud = gameObject.AddComponent<AudioSource>();
    else
        aud = gameObject.GetComponent<AudioSource>();
    aud.playOnAwake = false;
}
void Update () {

    debugText.text = audioToString;

    tex = inputField_Text.text;
}
/// <summary>
/// 开始录音
/// </summary>
public void StartMic()
{
    if (Microphone.devices.Length == 0) return;
    Microphone.End(null);
    Debug.Log("开始录音");
    aud.clip = Microphone.Start(null, false, 10, Convert.ToInt32(rate));//麦克风开始录音;参数:1,麦克风名称。2,循环.3,10秒录音计划长度,4,录音频率,一般44100
}
/// <summary>
/// 结束录音,并且,百度语音识别
/// </summary>
public void EndMic()
{
    int lastPos = Microphone.GetPosition(null);
    if (Microphone.IsRecording(null))
    {
        audioLength = lastPos / Convert.ToInt32(rate);//录音实际时长,string转int
       // audioLength = lastPos / int.Parse(rate);//录音实际时长,string转int

    }
    else
        audioLength = 10;//录音实际时长=录音计划时长
    Debug.Log("结束录音");
    Microphone.End(null);//麦克风停止录音

    clipByte = GetClipData();//吧录音转换为Byte[];
    //len = Convert.ToString(clipByte.Length);//语音长度,int转string;
    len =clipByte.Length;//语音长度,int转string;

    speech = Convert.ToBase64String(clipByte);
    //StartCoroutine(GetToken(getTokenAPIPath));//开始协成,//获取百度语音口令//百度语音识别
    Debug.Log("len"+len);//语音实际长度《语音计划长度
    Debug.Log("audioLength" + audioLength);
}
/// <summary>
/// 百度语音识别
/// </summary>
public void BaiduATT()
{
    Debug.Log("百度录音识别");
    isTTA = false;
    StartCoroutine(GetToken(getTokenAPIPath));//开始协成,//获取百度语音口令//百度语音识别
}
/// <summary>
/// 百度语音合成
/// </summary>
public void BaiduTTA()
{
    Debug.Log("百度录音合成");
    isTTA = true;
    StartCoroutine(GetToken(getTokenAPIPath));//开始协成,//获取百度语音口令//百度语音识别

}

/// <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;
}

/// <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)//如果成功获得百度用户口令,就可以开始使用百度语音,识别/合成
        {
            if (isTTA)
            {
                Debug.Log("开始百度录音合成");

                tok = JsonMapper.ToObject(getTW.text)["access_token"].ToString();
                Debug.Log(tok);
                 StartCoroutine(GetAudioClip(baiduAPI_tsn));//百度语音合成  
                
                //GetAudioClip();
            }
            else
            {
                Debug.Log("开始百度录音识别");

                token = JsonMapper.ToObject(getTW.text)["access_token"].ToString();
                Debug.Log(token);
                StartCoroutine(GetTextString(baiduAPI_vop));//百度语音识别
            }
        }
        else
            Debug.LogError("获取口令错误");
    }
}
/// <summary>
/// 把语音转换为文字
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
/// 
private IEnumerator GetTextString(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 getJT = new WWW(url, Encoding.Default.GetBytes(jw.ToString()));
    yield return getJT;
    Debug.Log(getJT.text);
    if (getJT.isDone)
    {
        if (getJT.error == null)
        {
         //成功返回数据,为json数据
            JsonData getJTJson = JsonMapper.ToObject(getJT.text);

            if (getJTJson["err_msg"].ToString() == "success.")
            {
                audioToString = getJTJson["result"][0].ToString();//显示语音识别结果
                Debug.Log("开始百度录音识别成功");

                if (audioToString.Substring(audioToString.Length - 1) == ",")
                    audioToString = audioToString.Substring(0, audioToString.Length - 1);
                Debug.Log(audioToString);
            }
        }
        else
        {
            Debug.LogError(getJT.error);
        }
    }
}

/// <summary>
/// 把文字转换为语音
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private IEnumerator GetAudioClip(string url)

//private void GetAudioClip()
{
//string tex = "好好学习,天天向上"; //合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节
Encoding.UTF8.GetBytes(tex);
string path = Application.dataPath + "/Resources/";//合成的语音文件保存的地址
string name = "xxmymp3.mp3";//合成的语音文件保存的文件名

// WWWForm form = new WWWForm();
//form.AddField("tex", tex);
//form.AddField("lan", lan);
//form.AddField("tok", tok);
//form.AddField("ctp", ctp);
//form.AddField("cuid", cuid);
//form.AddField("spd", spd);
//form.AddField("pit", pit);
//form.AddField("vol", vol);
//form.AddField("per", per);
//WWW www = new WWW(url,form);


//JsonWriter jw = new JsonWriter();
//jw.WriteObjectStart();
//jw.WritePropertyName("tex");
//jw.Write(tex);
//jw.WritePropertyName("lan");
//jw.Write(lan);
//jw.WritePropertyName("tok");
//jw.Write(tok);
//jw.WritePropertyName("ctp");
//jw.Write(ctp);
//jw.WritePropertyName("cuid");
//jw.Write(cuid);
//jw.WritePropertyName("spd");
//jw.Write(spd);
//jw.WritePropertyName("pit");
//jw.Write(pit);
//jw.WritePropertyName("vol");
//jw.Write(vol);
//jw.WritePropertyName("per");
//jw.Write(per);
//jw.WriteObjectEnd();

//WWW www = new WWW(url, Encoding.UTF8.GetBytes(jw.ToString()));
//yield return www;
//if (www.error == null)
//{
//    Debug.Log(www);
//    // byte[] byt = www.bytes;
//    // string strTempTime = System.DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);//当前时间,把图片命名为当前时间
//    //// string path=Application.dataPath + "/Resources/"+ "xx.mp3";//把文件保存在程序内,Resources文件夹中,名字为当前时间.mp3
//    // string path = Application.persistentDataPath +"/"+ strTempTime + ".mp3";// 把文件保存在缓存区(),名字为当前时间.mp3
//    // System.IO.File.WriteAllBytes(path, byt);//把二进制文件bye保存在path;

//    // UnityEditor.AssetDatabase.Refresh();//刷新,使刚创建的图片立刻导入。接下来才可以被使用

//    // Debug.Log(path);
//}
//else
//{
//    Debug.LogError(www.error);
//}


//List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
//formData.Add(new MultipartFormDataSection("tex", "tex"));
//formData.Add(new MultipartFormFileSection("lan", "lan"));
//formData.Add(new MultipartFormFileSection("tok", "tok"));
//formData.Add(new MultipartFormFileSection("ctp", "ctp"));
//formData.Add(new MultipartFormFileSection("cuid", "cuid"));
//formData.Add(new MultipartFormFileSection("spd", "spd"));
//formData.Add(new MultipartFormFileSection("pit", "pit"));
//formData.Add(new MultipartFormFileSection("vol", "vol"));
//formData.Add(new MultipartFormFileSection("per", "per"));


//UnityWebRequest www = UnityWebRequest.Post(url, formData);
//yield return www.Send();

//if (www.isError)
//{
//    Debug.Log(www.error);
//}
//else
//{
//    Debug.Log("Form upload complete!");
//    Debug.Log(www);
//    Debug.Log(www.downloadHandler.data);

//    byte[] myByte = www.downloadHandler.data;


//将string转为byte数组
//byte[] array = Encoding.UTF8.GetBytes(content);

//string path = Server.MapPath("/test.txt");
//创建一个文件流
//FileStream fs = new FileStream(path+name, FileMode.Create);

////将byte数组写入文件中
//fs.Write(myByte, 0, myByte.Length);
////所有流类型都要关闭流,否则会出现内存泄露问题
//fs.Close();

//    //创建本地文件写入流
//    Stream stream = new FileStream(path+name, FileMode.Create);
//    byte[] bArr = new byte[1024];
//    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
//    while (size > 0)
//    {
//        stream.Write(bArr, 0, size);
//        size = responseStream.Read(bArr, 0, (int)bArr.Length);
//    }
//    stream.Close();
//    responseStream.Close();

//    UnityEditor.AssetDatabase.Refresh();//刷新,使刚创建的立刻导入。接下来才可以被使用


//string path = Application.streamingAssetsPath + "/";
//string path = Application.dataPath + "/Resources/";

//string name = "xxmymp3.mp3";


string rest = "tex={0}&lan={1}&tok={2}&ctp={3}&cuid={4}&spd={5}&pit={6}&vol={7}&per={8}";

    string strUpdateData = string.Format(rest, tex, lan, tok, ctp, cuid, spd, pit, vol, per);
    HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = Encoding.UTF8.GetByteCount(strUpdateData);

    using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
    {
        sw.Write(strUpdateData);
    }
    HttpWebResponse res = req.GetResponse() as HttpWebResponse;

    yield return res;

    // 直到request.GetResponse()程序才开始向目标网页发送Post请求
    //服务器返回一个流文件
    Stream responseStream = res.GetResponseStream();
   // 创建本地文件写入流
    FileStream stream = new FileStream(path + name, FileMode.Create);
    byte[] bArr = new byte[1024];
    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    while (size > 0)
    {
        stream.Write(bArr, 0, size);//流文件写入
        size = responseStream.Read(bArr, 0, (int)bArr.Length);
        Debug.Log("百度语音合成文件存入本地");

    }
    stream.Close();
    responseStream.Close();
    UnityEditor.AssetDatabase.Refresh();//刷新,使刚创建的图片立刻导入。接下来才可以被使用
    PlayMp3();
}
public void PlayMp3()
{
    string name = "xxmymp3";//合成的语音文件保存的文件名,不需要后缀
    Debug.Log("百度语音合成文件播放");
    textToAudio = Resources.Load(name, typeof(AudioClip))as AudioClip;
    aud.clip = textToAudio;
    aud.Play();
}

//播放语音文件
//IEnumerator LoadMusic()
//{

//}
//public static byte[] Mp3(string inFile)
//    {
//        //mp3 -> mp3 mono 8000 samples per second example
//        int sps = 8000;
//        short mono = 1;

    //    Mp3Reader mr = new Mp3Reader(File.OpenRead(inFile));
    //    IntPtr formatMp3 = mr.ReadFormat();
    //    byte[] dataMp3 = mr.ReadData();
    //    mr.Close();
    //    IntPtr formatPcm = AudioCompressionManager.GetCompatibleFormat(formatMp3, AudioCompressionManager.PcmFormatTag);
    //    //mp3 -> pcm
    //    byte[] dataPcm = AudioCompressionManager.Convert(formatMp3, formatPcm, dataMp3, false);
    //    WaveFormat wf = AudioCompressionManager.GetWaveFormat(formatPcm);
    //    IntPtr formatPcm8000 = AudioCompressionManager.GetPcmFormat(mono, wf.wBitsPerSample, sps);
    //    //pcm -> pcm 8000 sps
    //    byte[] dataPcm8000 = AudioCompressionManager.Convert(formatPcm, formatPcm8000, dataPcm, false);
    //    return dataPcm8000;

    //    textToAudio = www.GetAudioClip();
    //aud.clip = textToAudio;
    //aud.Play();

// }
//IEnumerator LoadMp3()
//{
// string path = Application.dataPath + "/Resources/";//合成的语音文件保存的地址
// string name = "xxmymp3.mp3";//合成的语音文件保存的文件名

//     WWW www = new WWW(path+name);

//    yield return www;
//    Debug.Log("百度语音合成语音开始播放");

//    textToAudio = www.GetAudioClip();
//    aud.Play();
//}

//    IEnumerator wwwLoad(string _path)
//    {

//#if UNITY_EDITOR || UNITY_IOS
//        _path = "file://" + _path;
//#endif

//        WWW www = new WWW(_path);

//        yield return www;
//       textToAudio = www.GetAudioClip();
//        aud.Play();

//    }



//    public void QuitGame()
//    {
//        Application.Quit();
//    
/// 将 byte[] 转成 Stream
//public Stream BytesToStream(byte[] bytes)
//{
//    Stream stream = new MemoryStream(bytes);
//    return stream;
//}

}

相关文章

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章 无标题文章 无标题文章无标题文章 无标题文章 无标题文章

网友评论

      本文标题:无标题文章

      本文链接:https://www.haomeiwen.com/subject/sciymxtx.html