




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
学游戏就上我学院Unity麦克风录制声音脚本〃使用麦克风可以录制音频流,但是如何将录制的音频流通过代码的形式改成3d音效如果有知道的朋友情不吝赐教usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingUnityEngine;usingSystem.Collections;usingSystem.Net;[RequireComponent(typeof(AudioSource))]publicclassMicroPhoneInput:MonoBehaviour{privatestaticMicroPhoneInputm_instance;publicfloatsensitivity=100;publicfloatloudness=0;privatestaticstring[]micArray=null;constintHEADER_SIZE=44;constintRECORD_TIME=10;//UsethisforinitializationvoidStart(){)publicstaticMicroPhoneInputgetInstance(){if(m_instance==null){micArray=Microphone.devices;if(micArray.Length==0){Debug.LogError("Microphone.devicesisnull");)foreach(stringdeviceStrinMicrophone.devices)Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划学游戏就上我学院(Debug.Log("devicename="+deviceStr);)if(micArray.Length==0)(Debug.LogError("nomicdevice");)GameObjectMicObj=newGameObject("MicObj");m_instance=MicObj.AddComponent<MicroPhoneInput>();)returnm_instance;)voidOnGUI(){//GUI.Label(newRect(10,10,200,100),"loudness="+loudness);//GUI.Label(newRect(10,210,200,100),"Microphone.GetPosition="+Microphone.GetPosition(null));)publicvoidStartRecord(){audio.Stop();if(micArray.Length==0){Debug.Log("NoRecordDevice!");return;)audio.loop=false;audio.mute=true;audio.clip=Microphone.Start(null,false,RECORD_TIME,44100);//22050while(!(Microphone.GetPosition(null)>0)){)audio.Play();Debug.Log("StartRecord");//倒计时StartCoroutine(TimeDown());)publicvoidStopRecord(){Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划学游戏就上我学院if(micArray.Length==0)(Debug.Log("NoRecordDevice!");return;)if(!Microphone.IsRecording(null))(return;)Microphone.End(null);audio.Stop();Debug.Log("StopRecord");//PlayRecord();〃调试Int16[]数据的转化与播放//PlayClipData(GetClipData());)publicByte[]GetClipData()(if(audio.clip==null)(Debug.Log("GetClipDataaudio.clipisnull");returnnull;)float[]samples=newfloat[audio.clip.samples];audio.clip.GetData(samples,0);Byte[]outData=newbyte[samples.Length*2];//Int16[]intData=newInt16[samples.Length];//convertingin2float[]stepstoInt16[],//thenInt16[]toByte[]intrescaleFactor=32767;//toconvertfloattoInt16for(inti=0;i<samples.Length;i++)(shorttemshort=(short)(samples[i]*rescaleFactor);Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划w我学院学游戏就上我学院Byte[]temdata=System.BitConverter.GetBytes(temshort);outData[i*2]=temdata[0];outData[i*2+1]=temdata[1];)if(outData==null||outData.Length<=0)(Debug.Log("GetClipDataintDataisnull");returnnull;)//returnintData;returnoutData;///<summary>///调用GOOLE语音识别引擎///</summary>///<returns></returns>privatestringGoogleSTT(byte[]voice)(stringresult=string.Empty;try(/**stringinFile="audio.wav";FileStreamfs=newFileStream(inFile,FileMode.Open);byte[]voice=newbyte[fs.Length];fs.Read(voice,0,voice.Length);fs.Close();*/HttpWebRequestrequest=null;stringurl"/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN";Uriuri=newUri(url);request=(HttpWebRequest)WebRequest.Create(uri);request.Method="POST";request.ContentType="audio/x-flac;rate=16000";request.ContentLength=voice.Length;using(StreamwriteStream=request.GetRequestStream())(Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划学游戏就上我学院writeStream.Write(voice,0,voice.Length);)using(HttpWebResponseresponse=(HttpWebResponse)request.GetResponse())(using(StreamresponseStream=response.GetResponseStream())(using(StreamReaderreadStream=newStreamReader(responseStream,Encoding.UTF8))(result=readStream.ReadToEnd();Debug.Log("result="+result);))))catch(Exceptionex)(//Console.WriteLine(ex.StackTrace);Debug.Log(ex.StackTrace);)returnresult;)publicvoidPlayClipData(Int16[]intArr)(stringaaastr=intArr.ToString();longaaalength=aaastr.Length;Debug.LogError("aaalength="+aaalength);stringaaastr1=Convert.ToString(intArr);aaalength=aaastr1.Length;Debug.LogError("aaalength="+aaalength);if(intArr.Length==0)(Debug.Log("getintarrclipdataisnull");return;)〃从Int16口到float[]Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划学游戏就上我学院float[]samples=newfloat[intArr.Length];intrescaleFactor=32767;for(inti=0;i<intArr.Length;i++)(samples[i]=(float)intArr[i]/rescaleFactor;)〃从float□到ClipAudioSourceaudioSource=this.GetComponent<AudioSource>();if(audioSource.clip==null)(audioSource.clip=AudioClip.Create("playRecordClip",intArr.Length,1,44100,false,false);)audioSource.clip.SetData(samples,0);audioSource.mute=false;audioSource.Play();)publicvoidPlayRecord()(if(audio.clip==null)(Debug.Log("audio.clip=null");return;)audio.mute=false;audio.loop=false;audio.Play();Debug.Log("PlayRecord");)publicvoidLoadAndPlayRecord()(stringrecordPath="yourpath";//SavWav.LoadAndPlay(recordPath);)publicfloatGetAveragedVolume()(Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划N我学院学游戏就上我学院float[]data=newfloat[256];floata=0;audio.GetOutputData(data,0);foreach(floatsindata)(a+=Mathf.Abs(s);)returna/256;//UpdateiscalledonceperframevoidUpdate()(loudness=GetAveragedVolume()*sensitivity;if(loudness>
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 智慧港口自动化装卸设备智能搬运机器人市场前景预测报告
- 2025年休闲食品健康化转型与食品添加剂替代品研发报告
- 【高中语文】《为了忘却的记念》课件+统编版高二语文选择性必修中册+
- 2025年汽车零部件再制造产业市场潜力与增长动力分析报告
- 2025年金融租赁公司业务创新案例分析及风险防范策略报告
- 融合创新视角下2025年广播影视行业媒体融合的跨界营销策略分析报告
- 2025年化妆品零售行业自有品牌策略与竞争优势研究报告
- 2025年在线教育平台用户满意度提升策略与教育行业政策解读报告
- 护理员培训工作管理制度
- 不合格医用设备管理制度
- 2024CSCO结直肠癌诊疗指南解读
- 封装的JEDEC标准(包含26个标准PDF附件)
- 食品购销合同模板
- 学情分析初中生物
- DnD3RCharacterCard龙与地下城3.5空白人物卡(初版)
- (正式版)QBT 2174-2024 不锈钢厨具
- 腾讯公司英文介绍
- 24春国家开放大学《乡镇行政管理》作业1-5参考答案
- (高清版)DZT 0309-2017 地质环境监测标志
- 2024年吉林省彩虹人才开发咨询服务公司招聘笔试参考题库含答案解析
- 车间员工安全用电常识培训
评论
0/150
提交评论