コード例 #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;
            }
        }
コード例 #2
0
        public void Update()
        {
            _soundContainer.Update();

            if (_soundContainer.IsPlaying || !IsListening)
            {
                return;
            }

            if (_isPlayingAudio)
            {
                _delayTimer += Time.deltaTime;

                if (_delayTimer >= _speechRepeater.config.delayBeforeStartListening)
                {
                    _isPlayingAudio = false;

                    if (EndAudioPlayingEvent != null)
                    {
                        EndAudioPlayingEvent();
                    }

                    _delayTimer = 0f;
                }
            }

            if (IsListening && !_isPlayingAudio)
            {
                _currentSamplePosition = Microphone.GetPosition(_microphoneDevice);
                _microphoneWorkingAudioClip.GetData(_currentAudioSamples, 0);

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

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

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

                    ClearCache();

                    _latestVoiceAudioClip = MakeAudioClipFromSamples(_currentRecordingVoice.ToArray());

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

                    _currentRecordingVoice.Clear();

                    _soundContainer.ConnectAudioClip(_latestVoiceAudioClip);

                    _isPlayingAudio = true;
                }
                else if (_isTalking)
                {
                    AddVoiceSamples();
                }

                _previousSamplePosition = _currentSamplePosition;
            }
            else
            {
                _previousSamplePosition = Microphone.GetPosition(_microphoneDevice);
            }
        }