问题
We use the standard method of recording audio in Unity:
_sendingClip = Microphone.Start(_device, true, 10, 16000);
where _sendingClip
is the AudioClip and _device
is the device name.
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.
回答1:
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();
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.
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.
回答2:
There will always be a latency when working with audio.
Are you asking to record and send a clip to the SRS every time the user stops talking? or after 'x' amount of time when no voice input has been received ? (Something to think about)
Here is a decent read on the subject : https://support.unity3d.com/hc/en-us/articles/206485253-How-do-I-get-Unity-to-playback-a-Microphone-input-in-real-time-
Sorry I can't be of more help - hope you find a solution!
来源:https://stackoverflow.com/questions/40913081/unity-microphone-check-if-silent