Beispiel #1
0
        void Setup()
        {
            if (Microphone.devices.Length == 0)
            {
                return;
            }
            if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
            {
                return;
            }

            var inputMode = AudioUtils.GetMode();

            var mode = inputMode;

            if (inputMode > max)
            {
                mode = max;
            }
            encoder = SpeexCodex.Create(mode);

            var ratio        = AudioUtils.GetFrequency(inputMode) / AudioUtils.GetFrequency(encoder.mode);
            int sizeRequired = ratio * encoder.dataSize;

            recordingBuffer = new float[sizeRequired];
            resampleBuffer  = new float[encoder.dataSize];

            Debug.Log("inputMode:" + inputMode + " encodingMode:" + encoder.mode + " ratio:" + ratio + " record:" + recordingBuffer.Length + " resample:" + resampleBuffer.Length);
            if (encoder.mode == inputMode)
            {
                recordingBuffer = resampleBuffer;
            }

            recording = Microphone.Start(null, true, 10, AudioUtils.GetFrequency(inputMode));
        }
Beispiel #2
0
        public void Recieve(VoipFragment audio)
        {
            bytes += audio.data.Length + 11;
            chunkCount++;

            //If params have changed or decoder not set..
            if (decoder == null || decoder.mode != audio.mode)
            {
                //Create decoder.
                decoder = SpeexCodex.Create(audio.mode);
                //Create a buffer.
                buffer = new VoipBuffer(decoder.dataSize, BufferChunks);
                //Create clip.
                var frequency = AudioUtils.GetFrequency(audio.mode);
                recording = AudioClip.Create("Inbound", frequency * 10, 1, frequency, false);
                //Create source.
                source = gameObject.GetComponent <AudioSource>();
                if (source == null)
                {
                    source = gameObject.AddComponent <AudioSource>();
                }
                source.clip = recording;
                source.Play();
                source.loop = true;
            }

            //Decode.
            var decoded = decoder.Decode(audio.data);

            //Apply effects.
            if (Equalize)
            {
                ApplyGain(decoded);
            }
            //Write to voip buffer.
            buffer.Write(audio.index, decoded);
            //Update write cursor.
            lastWrittenCursor = audio.index > lastWrittenCursor ? audio.index : lastWrittenCursor;
        }