Unity-麦克风检查是否静音 [英] Unity - Microphone check if silent

查看:461
本文介绍了Unity-麦克风检查是否静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用在Unity中录制音频的标准方法:

We use the standard method of recording audio in Unity:

_sendingClip = Microphone.Start(_device, true, 10, 16000);

其中_sendingClip是AudioClip,_device是设备名称.

where _sendingClip is the AudioClip and _device is the device name.

我想知道用户何时停止讲话,这种情况可能会在2秒甚至10秒后发生.

I'd like to know when the user stops speaking, which can happen after 2 seconds, or even 10.

我已经查看了不同的来源来找到答案,但是找不到答案:

I've looked at different sources to find an answer, but could not find one:

  • https://forum.unity3d.com/threads/check-current-microphone-input-volume.133501/
  • http://answers.unity3d.com/questions/137170/how-to-check-if-the-user-speak-to-microphone.html (but this one is over 5 years old already)
  • http://answers.unity3d.com/questions/1113690/microphone-input-in-unity-5x.html

这个想法是,当用户停止讲话时,音频将被无延迟地发送到语音识别服务器,并且当用户仍在讲话时不会中断音频.

The idea is that when a user stops talking, the audio is send to a speech recognition server without a delay and without audio getting cut off when the user is still speaking.

解决方案不必采用代码格式.一般看哪里的方向会很好.

Solutions don't need to be in code format. A general direction of where to look would be nice.

推荐答案

您可以将录制的音频剪辑发送到AudioSource并使用以下方式播放:

You can send the recording audioclip to an AudioSource and play it using:

audioSource.clip = Microphone.Start(_device, true, 60, 16000);
while (!(Microphone.GetPosition(null) > 0)) { }
audioSource.Play();

在播放时,您可以从音频中获取SpectrumData.当用户讲话时,光谱数据将显示更多峰.您可以检查SpectrumData音频的平均值,以确定是否有人在说话.您应该设置某种最低级别,因为录音中可能会有些杂音.如果频谱数据的平均值高于确定的水平,则表示有人在讲话,如果低于该水平,则用户停止讲话.

When it is playing, you can get the SpectrumData from the audio. When the user is speaking the spectrumdata will show more peaks. You can check the average of the SpectrumData audio to determine if someone is speaking. You should set some sort of minimum level, as you will probably have some noise in the recordings. If the average of the spectrumdata is above the determined level, someone is speaking, if it's below that, the user stopped speaking.

float[] clipSampleData = new float[1024];
bool isSpeaking=false;

void Update(){
   audioSource.GetSpectrumData(clipSampleData, 0, FFTWindow.Rectangular);
   float currentAverageVolume = clipSampleData.Average();

   if(currentAverageVolume>minimumLevel){ 
      isSpeaking=true 
   } 
   else if(isSpeaking){
      isSpeaking=false;
      //volume below level, but user was speaking before. So user stopped speaking
   }
}

您可以在更新"方法中添加该复选框,光谱数据将是最后一帧的光谱数据.因此,它将接近实时.

You can put that check in the Update method, the spectrumdata will be the spectrumdata of the last frame. So it will be close to realtime.

最低音量可以通过录制静音来确定,您可以在用户需要讲话之前以某种方式录制,也可以通过设置的方式来实现.

The minimum level can be determined by just recording something silent, you can do that before the user needs to speak, or in a set-up kind of way.

使用此解决方案,用户将听到自己的声音,您可以将音频源的输出设置为混音器,然后将其音量设置为-80.因此它仍然可以识别数据,但不会将声音输出给用户.在audioSource上将音量设置为0会得到0频谱数据,因此在这种情况下请使用audiomixer.

With this solution the user will hear itself speak, you can set the output of the audiosource to the audiomixer, and put that volume to -80. So it will still recognize the data, but doesn't output the sound to the user. Setting the volume to 0 on the audioSource will give 0 spectrumdata, so use the audiomixer in that case.

这篇关于Unity-麦克风检查是否静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆