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)); }
public SpeexCodex(BandMode mode) { this.mode = mode; encoder = new NSpeex.SpeexEncoder(SpeedxMode); decoder = new NSpeex.SpeexDecoder(SpeedxMode, false); encoder.VBR = true; encoder.Quality = 4; dataSize = encoder.FrameSize * (mode == BandMode.Narrow ? 8 : mode == BandMode.Wide ? 4 : 2); encodingBuffer = new short[dataSize]; encodedBuffer = new byte[dataSize]; decodeBuffer = new short[dataSize]; decodedBuffer = new float[dataSize]; int frequency = AudioUtils.GetFrequency(mode); var time = frequency / (float)dataSize; UnityEngine.Debug.Log("SpeedCodex created mode:" + mode + " dataSize:" + dataSize + " time:" + time); }
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; }