示例#1
0
        public void ProcessAudioFrame(ToxAvAudioFrame frame)
        {
            if (_waveOutProvider == null)
            {
                return;
            }

            //what is the length of this audio frame?
            int audioLength = ((frame.Data.Length / frame.Channels) * 1000) / frame.SamplingRate;

            //what should the length of this frame have been? (we want 20ms to send to the provider)
            int wantedDataLength = ((20 * frame.SamplingRate) / 1000) * frame.Channels;

            if (wantedDataLength != frame.Data.Length)
            {
                //if we didn't get the amount of data we wanted, we need to buffer it
                _receivedAudioBuffer.AddRange(frame.Data);
                if (_receivedAudioBuffer.Count == wantedDataLength)
                {
                    short[] shorts = _receivedAudioBuffer.ToArray();
                    byte[]  bytes  = ShortsToBytes(shorts);

                    _waveOutProvider.AddSamples(bytes, 0, bytes.Length);
                    _receivedAudioBuffer.Clear();
                }
            }
            else
            {
                byte[] bytes = ShortsToBytes(frame.Data);
                _waveOutProvider.AddSamples(bytes, 0, bytes.Length);
            }
        }
示例#2
0
        public bool SendAudioFrame(int friendNumber, ToxAvAudioFrame frame)
        {
            ToxAvErrorSendFrame error;
            var retVal = _toxAv.SendAudioFrame(friendNumber, frame, out error);

            ToxErrorViewModel.Instance.RelayError(error);
            return(retVal);
        }
示例#3
0
        private void OnAudioDeviceMicDataAvailable(short[] audioData, int sampleRate, int channels)
        {
            if (audioData == null || audioData.Length == 0)
            {
                return;
            }

            if (this.callInfo == null || !this.callInfo.CanSendAudio)
            {
                return;
            }

            var toxAudioFrame = new ToxAvAudioFrame(audioData, sampleRate, channels);

            if (toxAudioFrame == null)
            {
                Logger.Log(LogLevel.Error, "Failed to convert audio data to audio frame");
                return;
            }

            // yes, check again
            if (this.callInfo == null || !this.callInfo.CanSendAudio)
            {
                return;
            }

            if (this.callInfo.AudioBitrate <= 0)
            {
                if (this.toxAv.SetAudioBitrate(this.callInfo.FriendNumber, DefaultAudioBitrate))
                {
                    this.callInfo.AudioBitrate = DefaultAudioBitrate;
                }
            }

            this.sendAudioFrameTimer.Reset();
            this.sendAudioFrameTimer.Start();
            try
            {
                var error = ToxAvErrorSendFrame.Ok;
                do
                {
                    if (!this.toxAv.SendAudioFrame(this.callInfo.FriendNumber, toxAudioFrame, out error))
                    {
                        if (error == ToxAvErrorSendFrame.Sync)
                        {
                            Thread.Sleep(0);
                        }
                        else
                        {
                            Logger.Log(LogLevel.Error, string.Format("Failed to send audio frame {0}/{1}/{2} : {3}", toxAudioFrame.Data.Length, toxAudioFrame.SamplingRate, toxAudioFrame.Channels, error));
                        }
                    }
                }while (error == ToxAvErrorSendFrame.Sync && this.sendAudioFrameTimer.ElapsedMilliseconds < 5);

                if (error == ToxAvErrorSendFrame.Sync)
                {
                    Logger.Log(LogLevel.Error, string.Format("Failed to send audio frame {0}/{1}/{2} : Lock busy, dropping frame", toxAudioFrame.Data.Length, toxAudioFrame.SamplingRate, toxAudioFrame.Channels));
                }
            }
            finally
            {
                this.sendAudioFrameTimer.Stop();
            }
        }
示例#4
0
 public bool SendAudioFrame(int friendNumber, ToxAvAudioFrame frame)
 {
     ToxAvErrorSendFrame error;
     var retVal = _toxAv.SendAudioFrame(friendNumber, frame, out error);
     ToxErrorViewModel.Instance.RelayError(error);
     return retVal;
 }