using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Baidu.Aip.Speech;//百度语音识别
using System.IO;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Text;
using System.Collections;
using UnityEngine.EventSystems;
//百度语音识别——语音转成文本,百度语音合成--文本转成语音
[RequireComponent(typeof(AudioSource))]//要求有AudioSource组件
public class YYTest1 : MonoBehaviour,IPointerDownHandler, IPointerUpHandler
{
private static YYTest _instance;//单例
private static string[] micArray = null;//硬件设备——麦克风
private AudioSource _AudioSource;//声音组件
private AudioClip audioClip;//声音片段
private string path="myR";
private DateTime beginTime;//开始录音时间
private int lengthSec = 30;//录音长度30秒
private int frequency = 44100;//录音频率
private Asr _asrClient;//百度语音识别
private Tts _ttsClient;//百度语音合成
//private JObject result;// 接收返回的结果
public Text debugInfo; // 显示debug信息
private string infoLog = "";//日志信息
private string ApiKey = "a3qqZofX0vWh3XkukfxRDsTl";
private string SecretKey = "8gT9nzQIOGeZkpPBKZs4tvKlptgdI25w";
//private string ApiKey = "Lgk3vC7FXQfQGW76zVg4pZKO";
//private string SecretKey = "bB8kbhl1Pnef3NG6MfG5AaYmI3SV53YC";
/***
*应用名称:BDAIX
*AppID:10313629
API Key;Lgk3vC7FXQfQGW76zVg4pZKO
*Secret Key;bB8kbhl1Pnef3NG6MfG5AaYmI3SV53YC
- 百度语音
*应用名称:BaiduYY
*AppID:10307503
*API Key:a3qqZofX0vWh3XkukfxRDsTl
*Secret Key:8gT9nzQIOGeZkpPBKZs4tvKlptgdI25w
*/
public static YYTest getInstance()
{
if (_instance == null)
{
micArray = Microphone.devices;
if (micArray.Length == 0)
{
Debug.LogError("找不到麦克风设备");
}
foreach (string deviceStr in Microphone.devices)
{
Debug.Log("device name = " + deviceStr);
}
GameObject MicObj = new GameObject("MicObj");
_instance = MicObj.AddComponent<YYTest>();
}
return _instance;
}
private void Awake()
{
_asrClient = new Asr(ApiKey, SecretKey);
_ttsClient = new Tts(ApiKey, SecretKey);
_AudioSource = GetComponent<AudioSource>();
}
void Start()
{
}
void Update()
{
debugInfo.text = infoLog;
}
// 获取麦克风设备
public void GetMicrophoneDevice()
{
micArray = Microphone.devices;
if (micArray.Length == 0)
{
Debug.LogError("找不到麦克风设备!");
ShowInfoLog("找不到麦克风设备!");
return;
}
else
{
Debug.Log("麦克风已经就绪,可以录音!");
ShowInfoLog("麦克风已经就绪,可以录音!!");
//return;
}
}
//开始录音
public void StartRecord()
{
GetMicrophoneDevice();//获取麦克风设备
_AudioSource.Stop();//开始录音前,先停止上一个
_AudioSource.loop = false;
_AudioSource.mute = true;
_AudioSource.clip = Microphone.Start(null, false, lengthSec, frequency);
beginTime = DateTime.Now;
audioClip = _AudioSource.clip;
while (!(Microphone.GetPosition(null) > 0))
{
}
_AudioSource.Play();
Debug.Log("开始录音!");
ShowInfoLog("开始录音!");
//倒计时——开始协程
//StartCoroutine(TimeDown());
}
//协程--倒计时
IEnumerator TimeDown()
{
Debug.Log("协程--倒计时");
int time = 0;
while (time < lengthSec)
{
if (!Microphone.IsRecording(null))
{ //如果没有录制
Debug.Log("录音失败");
yield break;
}
Debug.Log("yield return new WaitForSeconds " + time);
yield return new WaitForSeconds(1);
time++;
}
if (time >= lengthSec)
{
Debug.Log("时间到,停止录音!");
ShowInfoLog("时间到,停止录音!");
StopRecord();//停止录音
//SaveAudioClip();//保存录音
}
yield return 0;
//int ShowTimeUI;
//yield return new WaitForSeconds(0f);
//for (float timer = 60; timer >= 0; timer -= Time.deltaTime)
//{
// if (timer <= 60)
// {
// ShowTimeUI = (int)timer + 1;
// ShowUI.text = ShowTimeUI.ToString() + "秒";
// }
// yield return 0;
//}
}
//停止录音
public void StopRecord()
{
if (micArray.Length == 0)
{
return;
}
if (!Microphone.IsRecording(null))
{
return;
}
Microphone.End(null);
_AudioSource.Stop();
Debug.Log("停止录音!");
ShowInfoLog("停止录音!");
//SaveAudioClip();//保存录音
}
// 回放录音
public void PlayRecordAudio()
{
if (Microphone.IsRecording(null))
return;
if (_AudioSource.clip == null)
return;
_AudioSource.mute = false;
_AudioSource.loop = false;
_AudioSource.Play();
Debug.Log("回放录音");
ShowInfoLog("回放录音");
}
//播放录音 (3个参数)
public void PlayRecord()
{
PlayRecord(audioClip);
}
public void PlayRecord(AudioClip clip)
{
PlayRecord(clip, Vector3.zero);
}
public void PlayRecord(AudioClip clip, Vector3 pos)
{
if (clip == null)
{
Debug.Log("录音为空");
return;
}
//三个参数,俩个参数也可以,
AudioSource.PlayClipAtPoint(clip, pos,1); //(?? 声音播放低????,3个参数,声音大小)
Debug.Log("播放录音");
ShowInfoLog("播放录音");
}
//保存
public void Save2()
{
SaveWav.Save(path,audioClip);
Debug.Log("保存录音");
ShowInfoLog("保存录音");
}
// 百度语音识别本地文件
public void AsrData()
{
//var path1 = Application.dataPath + "/yy01.wav";
//var data = File.ReadAllBytes(path1);
var path = Application.dataPath + "/test.pcm";
var data = File.ReadAllBytes(path);
//result = _asrClient.Recognize(data, "wav", 16000);
var result = _asrClient.Recognize(data, "pcm", 16000);
debugInfo.text = result.ToString();
Debug.Log("百度语音识别");
ShowInfoLog("百度语音识别");
}
// 识别URL中的语音文件
public void AsrUrl()
{
var result = _asrClient.Recoginze(
"http://xxx.com/待识别的pcm文件地址",
"http://xxx.com/识别结果回调地址",
"pcm",
16000);
debugInfo.text = result.ToString();
}
// 百度语音合成
public void Tts()
{
string path = Application.dataPath + "/Resources/";//合成的语音文件保存的地址
string name = "xxmymp3.mp3";//合成的语音文件保存的文件名
// 可选参数
var option = new Dictionary<string, object>()
{
{"spd", 5}, // 语速
{"vol", 7}, // 音量
{"per", 4} // 发音人,4:情感度丫丫童声
};
var result1 = _ttsClient.Synthesis("众里寻他千百度", option);
if (result1.ErrorCode == 0) // 或 result.Success
{
File.WriteAllBytes(path+name, result1.Data);
//AudioClip ac = result.date;
//var path = Application.dataPath + "/合成的语音.mp3";
var data = Resources.Load<AudioClip>("xxmymp3");
audioClip = data;
_AudioSource.Play();
}
Debug.Log("百度语音合成");
ShowInfoLog("百度语音合成");
}
//显示日志提示信息
void ShowInfoLog(string info)
{
debugInfo.text ="";
infoLog += info;
infoLog += "\r\n";
}
//按钮按下
public void OnPointerDown(PointerEventData eventData)
{
StartRecord();//开始录音
}
//按钮抬起
public void OnPointerUp(PointerEventData eventData)
{
StopRecord();//停止录音
}
}
网友评论