unity3d百度语音+图灵机器人
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上起效。
unity3d百度语音+图灵机器人的更多相关文章
- 基于百度ai,图灵机器人,Flask 实现的网站语音智能问答
准备以下模块中的函数 from aip import AipSpeech import time import os import requests APP_ID = '15420654' API_K ...
- ros实例_百度语音+图灵
1 百度语音模块 参考http://blog.csdn.net/u011118482/article/details/55001444 1.1 百度语音识别包 git clonehttps://git ...
- [初识]使用百度AI接口,图灵机器人实现简单语音对话
一.准备 1.百度ai开放平台提供了优质的接口资源https://ai.baidu.com/ (基本免费) 2.在语音识别的接口中, 对中文来说, 讯飞的接口是很好的选择https://www.xf ...
- 人工智能-调百度AI接口+图灵机器人
1.登陆百度AI的官网 1.注册:没有账号注册 2.创建应用 3.创建应用 4.查看应用的ID 5.Python代码 from aip import AipSpeech APP_ID = " ...
- flask 第六章 人工智能 百度语音合成 识别 NLP自然语言处理+simnet短文本相似度 图灵机器人
百度智能云文档链接 : https://cloud.baidu.com/doc/SPEECH/index.html 1.百度语音合成 概念: 顾名思义,就是将你输入的文字合成语音,例如: from a ...
- 人工智能-基于百度baidu-ai和图灵机器人实现学说话机器人
本文引用了2个js文件,这里提供下CDN资源,! <script type="application/javascript" src="https://cdn.bo ...
- Flask&&人工智能AI -- 6 人工智能初识,百度AI,图灵机器人
一.人工智能初识,百度AI的创建与应用 参考连接:https://www.cnblogs.com/xiao987334176/p/9620113.html 1. 百度ai开放平台 2. AipSpee ...
- python 全栈开发,Day123(图灵机器人,web录音实现自动化交互问答)
昨日内容回顾 . 百度ai开放平台 . AipSpeech技术,语言合成,语言识别 . Nlp技术,短文本相似度 . 实现一个简单的问答机器人 . 语言识别 ffmpeg (目前所有音乐,视频领域,这 ...
- 图灵机器人,web录音实现自动化交互问答
一.图灵机器人 介绍 图灵机器人 是以语义技术为核心驱动力的人工智能公司,致力于“让机器理解世界”,产品服务包括机器人开放平台.机器人OS和场景方案. 官方地址为: http://www.tuling ...
随机推荐
- 使用 juqery.media.js 实现 pdf 预览
作用:可以实现在指定的位置预览PDF 缺点: (1)在iPad上只能预览一页PDF.(问题是iPad会将PDF转为img呈现,试了将img宽度设置为100%方法但并不好使) (2)在安卓上不能预览,依 ...
- 模块化和webpack模块化打包
模块化和webpack模块化打包: 一.❀ 模块化 [导入import-----导出export] 1.为什么需要模块化? JavaScript 发展初期,代码简单地堆积在一起,只要能顺利地从上往下一 ...
- c++指针常量和常量指针概述
个人理解,欢迎指正 这个简单,简单,简单(不要有心里压力:认为很难) 本文将会解决: A.变与不变 B.判断指针常量和常量指针. C.常量指针指针常量.本文不涉及. 1.概述 A.指针: 说到底,还 ...
- c++代码编译错误查找方法之宏
1.关于 本文演示环境: win10+vs2017 好久不用这法子了,都快忘了 排查错误,思路很重要,且一定要思路清晰(由于自己思路不清晰,查找错误耽误了不少时间,其实问题很简单,只是你要找到他需要不 ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- Lucky Substrings
而在26以内且属于fibonacci数列的数为1,2,3,5,8,13,21时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if ...
- RSA非对称加密算法实现:Java
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- centos6.5-搭建Apache-地址限制和用户访问
对于企业内部的WEB管理平台,例如日志分析系统(Awstats)为了更好实现访问控制,在通过客户端访问时,提交一些用户密码或对客户IP地址进行授权,避免服务器的安全隐患. 常见的访问控制方式:应用于配 ...