public void StartRecording() { if (Microphone.devices.Length == 0) { return; } inputFreq = AudioUtils.GetFreqForMic(); encoder = SpeexCodex.Create(BandMode.Wide); var ratio = inputFreq / (float)AudioUtils.GetFrequency(encoder.mode); int sizeRequired = (int)(ratio * encoder.dataSize); recordingBuffer = new float[sizeRequired]; resampleBuffer = new float[encoder.dataSize]; if (AudioUtils.GetFrequency(encoder.mode) == inputFreq) { recordingBuffer = resampleBuffer; } recording = Microphone.Start(null, true, 20, inputFreq); Plugin.log.Info("Used mic sample rate: " + inputFreq + "Hz"); }
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 ? 8 : 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); }
void Update() { if (recording == null) { return; } var now = Microphone.GetPosition(null); var length = now - lastPos; if (now < lastPos) { lastPos = 0; length = now; } while (length >= recordingBuffer.Length) { if (_isListening && recording.GetData(recordingBuffer, lastPos)) { //Send.. var amplitude = AudioUtils.GetMaxAmplitude(recordingBuffer); if (amplitude >= minAmplitude) { index++; if (OnAudioGenerated != null) { //Downsample if needed. if (recordingBuffer != resampleBuffer) { AudioUtils.Resample(recordingBuffer, resampleBuffer, inputFreq, AudioUtils.GetFrequency(encoder.mode)); } var data = encoder.Encode(resampleBuffer); VoipFragment frag = new VoipFragment(0, index, data, encoder.mode); OnAudioGenerated?.Invoke(frag); } } } length -= recordingBuffer.Length; lastPos += recordingBuffer.Length; } }
void Update() { if (recording == null) { return; } var now = Microphone.GetPosition(null); var length = now - lastPos; if (now < lastPos) { lastPos = 0; length = now; } while (length >= recordingBuffer.Length) { if (recording.GetData(recordingBuffer, lastPos)) { //Send.. var amplitude = AudioUtils.GetMaxAmplitude(recordingBuffer); if (amplitude >= minAmplitude) { index++; if (OnAudioGenerated != null) { chunkCount++; //Downsample if needed. if (recordingBuffer != resampleBuffer) { AudioUtils.Resample(recordingBuffer, resampleBuffer, inputFreq, AudioUtils.GetFrequency(encoder.mode)); } var data = encoder.Encode(resampleBuffer); bytes += data.Length + 11; VoipFragment frag = new VoipFragment(0, index, data, encoder.mode); foreach (var action in OnAudioGenerated.GetInvocationList()) { try { action.DynamicInvoke(frag); } catch (Exception e) { Misc.Logger.Error($"Exception on OnAudioGenerated event in {action.Target.GetType().ToString()}.{action.Method.Name}: {e}"); } } } } } length -= recordingBuffer.Length; lastPos += recordingBuffer.Length; } UpdateStats(); }