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 ...
随机推荐
- JuiceFS 数据加密原理
JuiceFS 作为分布文件系统,每天与海量的数据打着交道,因此数据的安全性尤为关键,今天就来介绍一下 JuiceFS 在数据加密方面所做的努力. 传输中数据加密 JuiceFS 在网络上传输时会对数 ...
- 贪心——55. 跳跃游戏 && 45.跳跃游戏II
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- SpringBoot中Post请求提交富文本数据量过大参数无法获取的问题
yml增加配置 # 开发环境配置 server: tomcat: max-http-form-post-size: -1
- JAVA微信支付——微信公众号内支付 代码
官方文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 微信PC二维码支付方式参考:https://www.cnblogs. ...
- SpringBoot 设置请求字符串格式为UTF-8
增加一个过滤器 package com.config; import com.jetsum.business.common.constant.CharsetConstant; import lombo ...
- JAVA判断某个元素是否在某个数组中
先把数组转为list 然后再利用contains方法 String[] strArr = new String[] { "a1", "b1", "c1 ...
- Android Linux vmstat 命令详解
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- hdu -4325-Flowers(离散化 线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=4325: 题目意思: 给你N个花开的时间段,然后在有M个时间点,问你在这个时间点有多少花开着. 昨天刚做的一个类似 ...
- JVM调参
今天看了下之前做的一个异步处理任务的服务,发现占用内存量比较大,达到2G,但我检查了代码,基本没有static对象.但这个服务有个特点,就是每次执行一个任务的时候,会从数据库中捞大量的数据做处理,因此 ...