Example #1
0
        public AudioRecorderViewModel(Microphone microphone, byte[] buffer, OpusRuntimeComponent opusComponent, Action <double> decibelValueChangedCallback, Action recordingStoppedCallback, Action recordingStoppedWithTimeoutCallback)
        {
            AudioRecorderViewModel recorderViewModel = this;

            this._microphone    = microphone;
            this._buffer        = buffer;
            this._opusComponent = opusComponent;
            this._decibelValueChangedCallback = decibelValueChangedCallback;
            this._recordingStoppedCallback    = recordingStoppedCallback;
            this._asyncDispatcher             = new XnaAsyncDispatcher((Action)(() =>
            {
                recorderViewModel._recordFinishTime = DateTime.Now;
                TimeSpan timeSpan = recorderViewModel._recordFinishTime - recorderViewModel._recordStartTime;
                recorderViewModel.RecordDurationStr = timeSpan.ToString(timeSpan.Hours > 0 ? "h\\:m\\:ss" : "m\\:ss");
                recorderViewModel.RecordDuration = (int)timeSpan.TotalMilliseconds;
                if (recorderViewModel.RecordDuration < AudioRecorderViewModel.GetMaxDuration())
                {
                    return;
                }
                recorderViewModel.StopRecording();
                Action action = recordingStoppedWithTimeoutCallback;
                if (action == null)
                {
                    return;
                }
                action();
            }));
        }
Example #2
0
        private void Microphone_OnBufferReady(object sender, EventArgs eventArgs)
        {
            int data = this._microphone.GetData(this._buffer);
            int num1 = data / 1920;

            this._stream.Write(this._buffer, 0, this._buffer.Length);
            for (int index = 0; index < num1; ++index)
            {
                int length = 1920 * (index + 1) > this._buffer.Length ? this._buffer.Length - 1920 * index : 1920;
                this._opusComponent.WriteFrame(AudioRecorderViewModel.SubArray <byte>(this._buffer, 1920 * index, length), length);
            }
            List <short> shortList   = new List <short>();
            int          startIndex1 = 0;

            while (startIndex1 < this._buffer.Length)
            {
                short int16 = BitConverter.ToInt16(this._buffer, startIndex1);
                shortList.Add(int16);
                startIndex1 += 2;
            }
            double num2 = 0.0;

            try
            {
                long num3 = this._samplesCount + (long)(data / 2);
                int  num4 = (int)((double)this._samplesCount / (double)num3 * (double)this._recordSamples.Length);
                int  num5 = this._recordSamples.Length - num4;
                if (num4 != 0)
                {
                    float num6 = (float)this._recordSamples.Length / (float)num4;
                    float num7 = 0.0f;
                    for (int index = 0; index < num4; ++index)
                    {
                        this._recordSamples[index] = this._recordSamples[(int)num7];
                        num7 += num6;
                    }
                }
                int   index1      = num4;
                float num8        = 0.0f;
                float num9        = (float)data / 2f / (float)num5;
                int   startIndex2 = 0;
                for (int index2 = 0; index2 < data / 2; ++index2)
                {
                    short  int16 = BitConverter.ToInt16(this._buffer, startIndex2);
                    double num6  = (double)int16 / 32768.0;
                    num2 += num6 * num6;
                    if (index2 == (int)num8 && index1 < this._recordSamples.Length)
                    {
                        this._recordSamples[index1] = int16;
                        num8 += num9;
                        ++index1;
                    }
                    startIndex2 += 2;
                }
                this._samplesCount = num3;
            }
            catch (Exception ex)
            {
                Logger.Instance.Error("Audio record failure", ex);
            }
            double          num10 = Math.Max(0.0, Math.Min(1.0, 1.0 - Math.Abs(AudioRecorderViewModel.ConvertAmplitudeToDb(Math.Sqrt(num2 / ((double)data / 2.0)))) / Math.Abs(-50.0)));
            Action <double> valueChangedCallback = this._decibelValueChangedCallback;

            if (valueChangedCallback == null)
            {
                return;
            }
            double num11 = num10;

            valueChangedCallback(num11);
        }