Example #1
0
        private void StopRecord()
        {
            if (!CustomMicrophone.HasConnectedMicrophoneDevices())
            {
                return;
            }

            if (!CustomMicrophone.IsRecording(selectedDevice))
            {
                return;
            }

            CustomMicrophone.End(selectedDevice);

            if (makeCopy)
            {
                recordedClips.Add(MakeCopy($"copy{recordedClips.Count}", recordingTime, frequency, _workingClip));
                audioSource.clip = recordedClips.Last();
            }
            else
            {
                audioSource.clip = _workingClip;
            }

            audioSource.Play();
        }
    private IEnumerator RecordingHandler()
    {
        //Log.Debug("STT.RecordingHandler()", "devices: {0}", Microphone.devices);
        _recording = CustomMicrophone.Start(_microphoneID, true, _recordingBufferSize, _recordingHZ);

        yield return(null);      // let _recordingRoutine get set..

        if (_recording == null)
        {
            StopRecording();
            yield break;
        }

        bool bFirstBlock = true;
        int  midPoint    = _recording.samples / 2;

        float[] samples = null;

        while (_recordingRoutine != 0 && _recording != null)
        {
            int writePos = CustomMicrophone.GetPosition(_microphoneID);
            if (writePos > _recording.samples || !CustomMicrophone.IsRecording(_microphoneID))
            {
                Log.Error("STT.RecordingHandler()", "Microphone disconnected.");
                StopRecording();
                yield break;
            }

            if ((bFirstBlock && writePos >= midPoint) ||
                (!bFirstBlock && writePos < midPoint))
            {
                // front block is recorded, make a RecordClip and pass it onto our callback.
                samples = new float[midPoint];
                _recording.GetData(samples, bFirstBlock ? 0 : midPoint);

                AudioData record = new AudioData();
                record.MaxLevel = Mathf.Max(Mathf.Abs(Mathf.Min(samples)), Mathf.Max(samples));
                record.Clip     = AudioClip.Create("Recording", midPoint, _recording.channels, _recordingHZ, false);
                record.Clip.SetData(samples, 0);

                _service.OnListen(record);

                bFirstBlock = !bFirstBlock;
            }
            else
            {
                // calculate the number of samples remaining until we ready for a block of audio,
                // and wait that amount of time it will take to record.
                int   remaining     = bFirstBlock ? (midPoint - writePos) : (_recording.samples - writePos);
                float timeRemaining = (float)remaining / (float)_recordingHZ;

                yield return(new WaitForSeconds(timeRemaining));
            }
        }

        yield break;
    }
Example #3
0
        private void Update()
        {
            permissionStatusText.text = string.Format("Microphone permission: {1} for '{0}'", selectedDevice,
                                                      CustomMicrophone.HasMicrophonePermission() ? "<color=green>granted</color>" : "<color=red>denined</color>");

            if (CustomMicrophone.devices.Length > 0)
            {
                recordingStatusText.text = string.Format("Microphone status: {0}",
                                                         CustomMicrophone.IsRecording(selectedDevice) ? "<color=green>recording</color>" : "<color=yellow>idle</color>");
            }
        }
Example #4
0
        /// <summary>
        /// Starts recording of microphone
        /// </summary>
        public void StartRecord()
        {
            if (CustomMicrophone.IsRecording(_microphoneDevice) || !CustomMicrophone.HasConnectedMicrophoneDevices())
            {
                RecordFailedEvent?.Invoke("record already started or no microphone device conencted");
                return;
            }

            recording = true;

            _workingClip = CustomMicrophone.Start(_microphoneDevice, true, Constants.RecordingTime, Constants.SampleRate);

            RecordStartedEvent?.Invoke();
        }
Example #5
0
        private void StopRecord()
        {
            if (!CustomMicrophone.HasConnectedMicrophoneDevices())
            {
                return;
            }

            if (!CustomMicrophone.IsRecording(CustomMicrophone.devices[0]))
            {
                return;
            }

            CustomMicrophone.End(CustomMicrophone.devices[0]);
        }
    // Start is called before the first frame update
    void Start()
    {
        var voice = GameObject.Find("[PeerJS]VoiceChat");

        if (voice != null)
        {
            Destroy(voice);
        }

        var microphone = GameObject.Find("[FG]Microphone");

        if (microphone != null)
        {
            if (CustomMicrophone.IsRecording(CustomMicrophone.devices[0]))
            {
                CustomMicrophone.End(CustomMicrophone.devices[0]);
            }
            Destroy(microphone);
        }



        // request microphone permissions at the start of the menu
        if (!CustomMicrophone.HasMicrophonePermission())
        {
            CustomMicrophone.RequestMicrophonePermission();
        }
        if (!CustomMicrophone.HasConnectedMicrophoneDevices())
        {
            CustomMicrophone.RefreshMicrophoneDevices();
        }
        //Debug.Log(CustomMicrophone.devices.Length + " microphone devices found");

        // destroys game tracker from previous game
        if (GameObject.FindGameObjectWithTag("GameTracker") != null)
        {
            Destroy(GameObject.FindGameObjectWithTag("GameTracker"));
        }

        // disconnects the player if they were already connected
        if (PhotonNetwork.IsConnected)
        {
            PhotonNetwork.Disconnect();
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible   = true;
        }
        PhotonNetwork.ConnectUsingSettings();
        //Debug.Log(PhotonNetwork.PhotonServerSettings);
    }
Example #7
0
        /// <summary>
        /// Stops recording of microphone
        /// </summary>
        public void StopRecord()
        {
            if (!CustomMicrophone.IsRecording(_microphoneDevice))
            {
                return;
            }

            recording = false;

            if (CustomMicrophone.HasConnectedMicrophoneDevices())
            {
                CustomMicrophone.End(_microphoneDevice);
            }

            if (_workingClip != null)
            {
                Destroy(_workingClip);
            }

            RecordEndedEvent?.Invoke();
        }
Example #8
0
        private void Update()
        {
            try
            {
                if (CustomMicrophone.IsRecording(string.Empty))
                {
                    float[] array = new float[0];
                    CustomMicrophone.GetRawData(ref array);

                    if (_buffer.position != CustomMicrophone.GetPosition(CustomMicrophone.devices[0]) && array.Length > 0)
                    {
                        int lastPosition = _buffer.position;
                        _buffer.position = CustomMicrophone.GetPosition(CustomMicrophone.devices[0]);

                        if (lastPosition > _buffer.position)
                        {
                            _buffer.data.AddRange(array.ToList().GetRange(lastPosition, array.Length - lastPosition));
                            _buffer.data.AddRange(array.ToList().GetRange(0, _buffer.position));
                        }
                        else
                        {
                            _buffer.data.AddRange(array.ToList().GetRange(lastPosition, _buffer.position - lastPosition));
                        }
                    }

                    _audioClipReadyToUse = _buffer.data.Count >= _sampleRate * _recordingTime;

                    if (_playing)
                    {
                        _delay -= Time.deltaTime;

                        if (_delay <= 0)
                        {
                            _playing = false;
                        }
                    }
                    else
                    {
                        if (_audioClipReadyToUse)
                        {
                            List <float> chunk;

                            if (_buffer.data.Count >= _sampleRate)
                            {
                                chunk = _buffer.data.GetRange(0, _sampleRate);
                                _buffer.data.RemoveRange(0, _sampleRate);
                            }
                            else
                            {
                                chunk = _buffer.data;
                                _buffer.data.Clear();
                                for (int i = chunk.Count; i < _sampleRate; i++)
                                {
                                    chunk.Add(0);
                                }
                            }

                            _audioSource.clip.SetData(chunk.ToArray(), 0);
                            _audioSource.Play();

                            _delay   = _recordingTime;
                            _playing = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.Message + " | " + ex.StackTrace);
            }
        }