/*
- FeiZiXiao
- BaiDuAI
- BaiDuAI_BaiDuAI//百度AI,api网络调用,获取口令
- 20180115
*/
using System.Collections;
using UnityEngine;
//百度AI_API在线调用,需要先获取口令
public class BaiDuAI_Token
{
public string SecretKey { get; private set; }
public string APIKey { get; private set; }
public string Token { get; private set; }
/// <summary>
/// 百度AI,Key,secretKey
/// </summary>
/// <param name="apiKey"></param>
/// <param name="secretKey"></param>
public BaiDuAI_Token(string apiKey, string secretKey)
{
APIKey = apiKey;
SecretKey = secretKey;
}
/// <summary>
/// 获取口令
/// </summary>
/// <returns></returns>
public IEnumerator GetAccessToken()
{
var uri =
//string.Format("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}",APIKey, SecretKey);//百度语音识别
string.Format("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}", APIKey, SecretKey);//百度文字识别
// https://aip.baidubce.com / rest / 2.0 / ocr / v1 / general ?
var www = new WWW(uri);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
var result = JsonUtility.FromJson<TokenResponse>(www.text);
Token = result.access_token;
Debug.Log("Get access_token successfully");
}
else
{
Debug.LogError(www.error);
}
}
}
/// <summary>
/// 获取口令结果
/// </summary>
public class TokenResponse
{
public string access_token = null;
}
===================================
/*
- FeiZiXiao
- BaiDuAI
- BaiDuAI_ASR_百度AI:语音识别为文字脚本
- 20180115
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 百度语音识别
/// </summary>
public class Asr: BaiDuAI_Token
{
public Asr(string apiKey, string secretKey) : base(apiKey, secretKey)
{
}
private const string UrlAsr = "https://vop.baidu.com/server_api";//百度语音识别在线地址
public IEnumerator Recognize(byte[] data, Action<AsrResponse> callback)
{
var uri = string.Format("{0}?lan=zh&cuid={1}&token={2}", UrlAsr, SystemInfo.deviceUniqueIdentifier,Token);
var headers = new Dictionary<string, string> {{"Content-Type", "audio/pcm;rate=16000"}};
var www = new WWW(uri, data, headers);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
Debug.Log(www.text);
callback(JsonUtility.FromJson<AsrResponse>(www.text));
}
else
Debug.LogError(www.error);
}
/// <summary>
/// 将Unity的AudioClip数据转化为PCM格式16bit数据
/// </summary>
/// <param name="clip"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 语音语音识别结果
/// </summary>
public class AsrResponse
{
public int err_no;
public string err_msg;
public string sn;
public bool Success
{
get { return err_no == 0; }
}
public string[] result;//识别结果
}
/*
- FeiZiXiao
- BaiDuAI
- BaiDuAI_TTS_百度AI:文字合成为语音脚本
- 20180115
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using NAudio.Wave;
using UnityEngine;
/// <summary>
/// 百度语音合成
/// </summary>
public class Tts: BaiDuAI_Token
{
public enum Pronouncer
{
Female, // 0为普通女声
Male, // 1为普通男生
Duxiaoyao, // 3为情感合成-度逍遥
Duyaya // 4为情感合成-度丫丫
}
public Tts(string apiKey, string secretKey) : base(apiKey, secretKey)
{
}
private const string UrlTts = "http://tsn.baidu.com/text2audio";//百度语音合成在线地址
public IEnumerator Synthesis(string text, Action<TtsResponse> callback, int speed = 5, int pit = 5, int vol = 5,
Pronouncer per = Pronouncer.Female)
{
var param = new Dictionary<string, string>();
param.Add("tex", text);
// param.Add("tok", Token);
param.Add("tok", Token);
param.Add("cuid", SystemInfo.deviceUniqueIdentifier);
param.Add("ctp", "1");
param.Add("lan", "zh");
param.Add("spd", Mathf.Clamp(speed, 0, 9).ToString());
param.Add("pit", Mathf.Clamp(pit, 0, 9).ToString());
param.Add("vol", Mathf.Clamp(vol, 0, 15).ToString());
param.Add("per", ((int) per).ToString());
string url = UrlTts;
int i = 0;
foreach (var p in param)
{
url += i != 0 ? "&" : "?";
url += p.Key + "=" + p.Value;
i++;
}
WWW www = new WWW(url);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
var type = www.responseHeaders["Content-Type"];
Debug.Log(type);
if (type == "audio/mp3")
{
var response = new TtsResponse { result = FromMp3Data(www.bytes)};
callback(response);
}
else
{
Debug.LogError(www.text);
callback(JsonUtility.FromJson<TtsResponse>(www.text));
}
}
else
Debug.LogError(www.error);
}
/// <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;
}
}
/// <summary>
/// 百度语音合成结果
/// </summary>
public class TtsResponse
{
public int err_no;
public string err_msg;
public string sn;
//public int idx;
public bool Success
{
get { return err_no == 0; }
}
public AudioClip result;//语音合成结果
}
/*
- FeiZiXiao
- BaiDuAI
- BaiDuAI_TTS//百度文字识别,文字识别为语音应用
- 20180115
*/
using UnityEngine;
using UnityEngine.UI;
public class TtsDemo : MonoBehaviour
{
public string APIKey = "";
public string SecretKey = "";
public Button SynthesisButton;
public InputField Input;
public Text DescriptionText;
private Tts _tts;
private AudioSource _audioSource;
private bool _startPlaying;
void Start()
{
_tts = new Tts(APIKey, SecretKey);
StartCoroutine(_tts.GetAccessToken());
_audioSource = gameObject.AddComponent<AudioSource>();
DescriptionText.text = "";
SynthesisButton.onClick.AddListener(OnClickSynthesisButton);
}
private void OnClickSynthesisButton()
{
SynthesisButton.gameObject.SetActive(false);
DescriptionText.text = "合成中...";
StartCoroutine(_tts.Synthesis(Input.text, s =>
{
if (s.err_no==0)
{
DescriptionText.text = "合成成功,正在播放";
_audioSource.clip = s.result;
_audioSource.Play();
_startPlaying = true;
}
else
{
DescriptionText.text = s.err_msg;
}
}));
}
void Update()
{
if (_startPlaying)
{
if (!_audioSource.isPlaying)
{
_startPlaying = false;
DescriptionText.text = "播放完毕,可以修改文本继续测试";
SynthesisButton.gameObject.SetActive(true);
}
}
}
}
/*
- FeiZiXiao
- BaiDuAI
- BaiDuAI_ASR//百度语音识别,语音识别为文字应用
- 20180115
*/
using UnityEngine;
using UnityEngine.UI;
public class AsrDemo : MonoBehaviour
{
public string APIKey = "";
public string SecretKey = "";
public Button StartButton;
public Button StopButton;
public Text DescriptionText;
private AudioClip _clipRecord = new AudioClip();
private Asr _asr;
void Start()
{
_asr = new Asr(APIKey, SecretKey);
StartCoroutine(_asr.GetAccessToken());
StartButton.gameObject.SetActive(true);
StopButton.gameObject.SetActive(false);
DescriptionText.text = "";
StartButton.onClick.AddListener(OnClickStartButton);
StopButton.onClick.AddListener(OnClickStopButton);
}
private void OnClickStartButton()
{
StartButton.gameObject.SetActive(false);
StopButton.gameObject.SetActive(true);
DescriptionText.text = "Listening...";
_clipRecord = Microphone.Start(null, false, 30, 16000);
}
private void OnClickStopButton()
{
StartButton.gameObject.SetActive(false);
StopButton.gameObject.SetActive(false);
DescriptionText.text = "Recognizing...";
Microphone.End(null);
Debug.Log("end record");
var data = Asr.ConvertAudioClipToPCM16(_clipRecord);
StartCoroutine(_asr.Recognize(data, s =>
{
DescriptionText.text = s.result[0];
StartButton.gameObject.SetActive(true);
}));
}
}
网友评论