Example #1
0
 public void Dispose()
 {
     if (this.pushCallback != null && this.audioOutCapture != null)
     {
         this.audioOutCapture.OnAudioFrame -= this.AudioOutCaptureOnOnAudioFrame;
     }
     UnityMicrophone.End(this.device);
     if (this.destroyGameObjectOnStop && this.audioSource != null)
     {
         UnityEngine.Object.Destroy(this.audioSource.gameObject);
     }
 }
 public MicWrapper(string device, int suggestedFrequency, ILogger logger)
 {
     try
     {
         this.device = device;
         this.logger = logger;
         if (UnityMicrophone.devices.Length < 1)
         {
             Error = "No microphones found (UnityMicrophone.devices is empty)";
             logger.LogError("[PV] MicWrapper: " + Error);
             return;
         }
         if (!string.IsNullOrEmpty(device) && !UnityMicrophone.devices.Contains(device))
         {
             logger.LogError(string.Format("[PV] MicWrapper: \"{0}\" is not a valid Unity microphone device, falling back to default one", device));
             device = null;
         }
         int minFreq;
         int maxFreq;
         logger.LogInfo("[PV] MicWrapper: initializing microphone '{0}', suggested frequency = {1}).", device, suggestedFrequency);
         UnityMicrophone.GetDeviceCaps(device, out minFreq, out maxFreq);
         var frequency = suggestedFrequency;
         //        minFreq = maxFreq = 44100; // test like android client
         if (suggestedFrequency < minFreq || maxFreq != 0 && suggestedFrequency > maxFreq)
         {
             logger.LogWarning("[PV] MicWrapper does not support suggested frequency {0} (min: {1}, max: {2}). Setting to {2}",
                               suggestedFrequency, minFreq, maxFreq);
             frequency = maxFreq;
         }
         this.mic = UnityMicrophone.Start(device, true, 1, frequency);
         logger.LogInfo("[PV] MicWrapper: microphone '{0}' initialized, frequency = {1}, channels = {2}.", device, this.mic.frequency, this.mic.channels);
     }
     catch (Exception e)
     {
         Error = e.ToString();
         if (Error == null) // should never happen but since Error used as validity flag, make sure that it's not null
         {
             Error = "Exception in MicWrapper constructor";
         }
         logger.LogError("[PV] MicWrapper: " + Error);
     }
 }
        public bool Read(float[] buffer)
        {
            if (Error != null)
            {
                return(false);
            }
            int micPos = UnityMicrophone.GetPosition(this.device);

            // loop detection
            if (micPos < micPrevPos)
            {
                micLoopCnt++;
            }
            micPrevPos = micPos;

            var micAbsPos = micLoopCnt * this.mic.samples + micPos;

            if (mic.channels == 0)
            {
                Error = "Number of channels is 0 in Read()";
                logger.LogError("[PV] MicWrapper: " + Error);
                return(false);
            }
            var bufferSamplesCount = buffer.Length / mic.channels;

            var nextReadPos = this.readAbsPos + bufferSamplesCount;

            if (nextReadPos < micAbsPos)
            {
                this.mic.GetData(buffer, this.readAbsPos % this.mic.samples);
                this.readAbsPos = nextReadPos;
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public void Dispose()
 {
     UnityMicrophone.End(this.device);
 }
Example #5
0
        public MicWrapperPusher(string device, Transform parentTransform, int suggestedFrequency, ILogger lg, bool destroyOnStop = true)
        {
            try
            {
                this.logger = lg;
                this.device = device;
                this.destroyGameObjectOnStop = destroyOnStop;
                if (UnityMicrophone.devices.Length < 1)
                {
                    this.Error = "No microphones found (Microphone.devices is empty)";
                    this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error);
                    return;
                }
                if (!string.IsNullOrEmpty(device) && !UnityMicrophone.devices.Contains(device))
                {
                    this.logger.LogError("[PV] MicWrapperPusher: \"{0}\" is not a valid Unity microphone device, falling back to default one", device);
                    device = UnityMicrophone.devices[0];
                }
                this.sampleRate = AudioSettings.outputSampleRate;
                switch (AudioSettings.speakerMode)
                {
                case AudioSpeakerMode.Mono: this.channels = 1; break;

                case AudioSpeakerMode.Stereo: this.channels = 2; break;

                default:
                    this.Error = string.Concat("Only Mono and Stereo project speaker mode supported. Current mode is ", AudioSettings.speakerMode);
                    this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error);
                    return;
                }
                int minFreq;
                int maxFreq;
                this.logger.LogInfo("[PV] MicWrapperPusher: initializing microphone '{0}', suggested frequency = {1}).", device, suggestedFrequency);
                UnityMicrophone.GetDeviceCaps(device, out minFreq, out maxFreq);
                int frequency = suggestedFrequency;
                //        minFreq = maxFreq = 44100; // test like android client
                if (suggestedFrequency < minFreq || maxFreq != 0 && suggestedFrequency > maxFreq)
                {
                    this.logger.LogWarning("[PV] MicWrapperPusher does not support suggested frequency {0} (min: {1}, max: {2}). Setting to {2}",
                                           suggestedFrequency, minFreq, maxFreq);
                    frequency = maxFreq;
                }
                GameObject gO = new GameObject("[PV] MicWrapperPusher: AudioSource + AudioOutCapture");
                if (!parentTransform || parentTransform == null)
                {
                    this.logger.LogWarning("[PV] MicWrapperPusher: Parent transform passed is destroyed or null. Creating AudioSource GameObject at root.");
                }
                else
                {
                    gO.transform.SetParent(parentTransform, false);
                    if (!gO.activeSelf)
                    {
                        this.logger.LogWarning("[PV] MicWrapperPusher: AudioSource GameObject inactive, activating it.");
                        this.audioSource.gameObject.SetActive(true);
                    }
                    if (!gO.activeInHierarchy)
                    {
                        this.Error = "AudioSource GameObject is not active in hierarchy, audio input can't work.";
                        this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error);
                        return;
                    }
                }
                this.audioSource        = gO.AddComponent <AudioSource>();
                this.audioOutCapture    = this.audioSource.gameObject.AddComponent <AudioOutCapture>();
                this.mic                = UnityMicrophone.Start(device, true, 1, frequency);
                this.audioSource.mute   = true;
                this.audioSource.volume = 0f;
                this.audioSource.clip   = this.mic;
                this.audioSource.loop   = true;
                this.audioSource.Play();
                this.logger.LogInfo("[PV] MicWrapperPusher: microphone '{0}' initialized, frequency = in:{1}|out:{2}, channels = in:{3}|out:{4}.", device, this.mic.frequency, this.SamplingRate, this.mic.channels, this.Channels);
            }
            catch (Exception e)
            {
                this.Error = e.ToString();
                if (this.Error == null) // should never happen but since Error used as validity flag, make sure that it's not null
                {
                    this.Error = "Exception in MicWrapperPusher constructor";
                }
                this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error);
            }
        }