Exemplo n.º 1
0
        public void Update()
        {
            if (IsRecording)
            {
                _currentSamplePosition = Microphone.GetPosition(_microphoneDevice);
                _microphoneWorkingAudioClip.GetData(_currentAudioSamples, 0);

                if (IsEnabledVoiceDetection)
                {
                    bool isTalking = _voiceDetectionManager.CheckVoice(AudioClip2ByteConverter.FloatToByte(_currentAudioSamples));

                    if (!_isTalking && isTalking)
                    {
                        _isTalking = true;

                        if (BeginTalkigEvent != null)
                        {
                            BeginTalkigEvent();
                        }
                    }
                    else if (_isTalking && !isTalking)
                    {
                        _isTalking = false;

                        _latestVoiceAudioClip = MakeAudioClipFromSamples(_currentRecordingVoice.ToArray());

                        if (EndTalkigEvent != null)
                        {
                            EndTalkigEvent(_latestVoiceAudioClip);
                        }

                        _currentRecordingVoice.Clear();
                    }
                    else if (_isTalking)
                    {
                        AddVoiceSamples();
                    }
                }
                else
                {
                    AddVoiceSamples();
                }

                _previousSamplePosition = _currentSamplePosition;
            }
        }
Exemplo n.º 2
0
        public void Update()
        {
            if (IsRecording)
            {
                _currentSamplePosition = CustomMicrophone.GetPosition(MicrophoneDevice);
                CustomMicrophone.GetRawData(ref _currentAudioSamples, _microphoneWorkingAudioClip);

                if (DetectVoice)
                {
                    bool isTalking = _voiceDetectionManager.HasDetectedVoice(AudioClip2ByteConverter.FloatToByte(_currentAudioSamples));

                    if (isTalking)
                    {
                        _endTalkingDelay = 0f;
                    }
                    else
                    {
                        _endTalkingDelay += Time.deltaTime;
                    }

                    if (!_isTalking && isTalking)
                    {
                        _isTalking = true;

#if NET_2_0 || NET_2_0_SUBSET
                        if (TalkBeganEvent != null)
                        {
                            TalkBeganEvent();
                        }
#else
                        TalkBeganEvent?.Invoke();
#endif
                    }
                    else if (_isTalking && !isTalking && _endTalkingDelay >= _speechRecognitionManager.CurrentConfig.voiceDetectionEndTalkingDelay)
                    {
                        _isTalking = false;

                        LastRecordedRaw  = _currentRecordingVoice.ToArray();
                        LastRecordedClip = AudioConvert.Convert(LastRecordedRaw, _microphoneWorkingAudioClip.channels);

                        _currentRecordingVoice.Clear();
#if NET_2_0 || NET_2_0_SUBSET
                        if (TalkEnded != null)
                        {
                            TalkEnded(LatestVoiceAudioClip, LastRecordedRaw);
                        }
#else
                        TalkEndedEvent?.Invoke(LastRecordedClip, LastRecordedRaw);
#endif
                    }
                    else if (_isTalking && isTalking)
                    {
                        AddAudioSamplesIntoBuffer();
                    }
                }
                else
                {
                    AddAudioSamplesIntoBuffer();
                }

                _previousSamplePosition = _currentSamplePosition;
            }
        }