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 ...
随机推荐
- xml数据结构处理
<data> <country name="Liechtenstein"> <rank updated="yes">2< ...
- 可以通过外键的.id直接传值
可以通过外键的.id直接传值 如<input type="text" name="user.department.id" value="1&qu ...
- react 结合antd 实现分页效果
import React, { useState, useEffect } from "react"; // antd import { Pagination } from &qu ...
- c++计算 char数组CRC算法
!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 我使用的OS:win7. 我使用的开发环境:VS2010 + sp1 算法源码: 1 uns ...
- 自定义C语言CVector
CVector.h // // cvector.h // GKApp // // Created by 王明辉 on 16/4/15. // Copyright (c) 2016年 GK. All r ...
- 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...
- 【剑指Offer】变态跳台阶 解题报告(Python)
题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级 ...
- Grids
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- 【stm32】基于hal库使用野火指南者esp8266 WIFI模块进行TCP传输
UART.c #include "stm32f1xx_it.h" #include "LED.h" #include "UART.h" #i ...
- DEFENSE-GAN: PROTECTING CLASSIFIERS AGAINST ADVERSARIAL ATTACKS USING GENERATIVE MODELS
目录 概 主要内容 Samangouei P, Kabkab M, Chellappa R, et al. Defense-GAN: Protecting Classifiers Against Ad ...