Пример #1
0
        private void Test_Click(object sender, EventArgs e)
        {
            btnTest.Enabled = false;

            string res = "OK";

            try
            {
                Program.FfmpegMutex.WaitOne();
                var    afr    = new AudioFileReader();
                string source = cmbFFMPEGURL.Text;
                int    i      = source.IndexOf("://", StringComparison.Ordinal);
                if (i > -1)
                {
                    source = source.Substring(0, i).ToLower() + source.Substring(i);
                }

                afr.Timeout         = Mic.settings.timeout;
                afr.AnalyzeDuration = (int)numAnalyseDuration.Value;
                afr.Open(source);
                afr.ReadAudioFrame();
                Mic.settings.channels = afr.Channels;
                Mic.settings.samples  = afr.SampleRate;
                Mic.settings.bits     = 16;

                afr.Dispose();
                afr = null;
            }
            catch (Exception ex)
            {
                res = ex.Message;
            }
            finally
            {
                try
                {
                    Program.FfmpegMutex.ReleaseMutex();
                }
                catch (ObjectDisposedException)
                {
                    //can happen on shutdown
                }
            }
            MessageBox.Show(res);

            btnTest.Enabled = true;
        }
Пример #2
0
        private void FfmpegListener()
        {
            _reasonToStop = ReasonToFinishPlaying.StoppedByUser;
            _afr          = null;
            bool   open   = false;
            string errmsg = "";

            try
            {
                Program.FFMPEGMutex.WaitOne();
                _afr = new AudioFileReader();
                int i = _source.IndexOf("://", StringComparison.Ordinal);
                if (i > -1)
                {
                    _source = _source.Substring(0, i).ToLower() + _source.Substring(i);
                }
                _afr.Timeout         = Timeout;
                _afr.AnalyzeDuration = AnalyseDuration;
                _afr.Open(_source);

                open = true;
            }
            catch (Exception ex)
            {
                MainForm.LogExceptionToFile(ex, "FFMPEG");
            }
            finally
            {
                try
                {
                    Program.FFMPEGMutex.ReleaseMutex();
                }
                catch (ObjectDisposedException)
                {
                    //can happen on shutdown
                }
            }

            if (_afr == null || !_afr.IsOpen || !open)
            {
                ShutDown("Could not open audio stream" + ": " + _source);
                return;
            }


            RecordingFormat = new WaveFormat(_afr.SampleRate, 16, _afr.Channels);
            _waveProvider   = new BufferedWaveProvider(RecordingFormat)
            {
                DiscardOnBufferOverflow = true, BufferDuration = TimeSpan.FromMilliseconds(500)
            };

            _sampleChannel = new SampleChannel(_waveProvider);
            _sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;

            int    mult = _afr.BitsPerSample / 8;
            double btrg = Convert.ToDouble(_afr.SampleRate * mult * _afr.Channels);

            LastFrame = DateTime.UtcNow;
            bool realTime = !IsFileSource;

            try
            {
                DateTime req = DateTime.UtcNow;
                while (!_stopEvent.WaitOne(10, false) && !MainForm.Reallyclose)
                {
                    byte[] data = _afr.ReadAudioFrame();
                    if (data == null || data.Equals(0))
                    {
                        if (!realTime)
                        {
                            break;
                        }
                    }
                    if (data != null && data.Length > 0)
                    {
                        LastFrame = DateTime.UtcNow;
                        var da = DataAvailable;
                        if (da != null)
                        {
                            //forces processing of volume level without piping it out
                            _waveProvider.AddSamples(data, 0, data.Length);

                            var sampleBuffer = new float[data.Length];
                            _sampleChannel.Read(sampleBuffer, 0, data.Length);

                            da(this, new DataAvailableEventArgs((byte[])data.Clone()));

                            if (WaveOutProvider != null && Listening)
                            {
                                WaveOutProvider.AddSamples(data, 0, data.Length);
                            }
                        }

                        if (realTime)
                        {
                            if (_stopEvent.WaitOne(30, false))
                            {
                                break;
                            }
                        }
                        else
                        {
                            //
                            double f = (data.Length / btrg) * 1000;
                            if (f > 0)
                            {
                                var span = DateTime.UtcNow.Subtract(req);
                                var msec = Convert.ToInt32(f - (int)span.TotalMilliseconds);
                                if ((msec > 0) && (_stopEvent.WaitOne(msec, false)))
                                {
                                    break;
                                }
                                req = DateTime.UtcNow;
                            }
                        }
                    }
                    else
                    {
                        if ((DateTime.UtcNow - LastFrame).TotalMilliseconds > Timeout)
                        {
                            throw new Exception("Audio source timeout");
                        }
                        if (_stopEvent.WaitOne(30, false))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MainForm.LogExceptionToFile(e, "FFMPEG");
                errmsg = e.Message;
            }

            if (_sampleChannel != null)
            {
                _sampleChannel.PreVolumeMeter -= SampleChannelPreVolumeMeter;
                _sampleChannel = null;
            }

            if (_waveProvider != null)
            {
                if (_waveProvider.BufferedBytes > 0)
                {
                    _waveProvider.ClearBuffer();
                }
            }

            ShutDown(errmsg);
        }
Пример #3
0
        private void Test_Click(object sender, EventArgs e)
        {
            btnTest.Enabled = false;

            string res = "OK";
            try
            {
                Program.FfmpegMutex.WaitOne();
                var afr = new AudioFileReader();
                string source = cmbFFMPEGURL.Text;
                int i = source.IndexOf("://", StringComparison.Ordinal);
                if (i > -1)
                {
                    source = source.Substring(0, i).ToLower() + source.Substring(i);
                }

                afr.Timeout = Mic.settings.timeout;
                afr.AnalyzeDuration = (int)numAnalyseDuration.Value;
                afr.Open(source);
                afr.ReadAudioFrame();
                Mic.settings.channels = afr.Channels;
                Mic.settings.samples = afr.SampleRate;
                Mic.settings.bits = 16;

                afr.Dispose();
                afr = null;
            }
            catch (Exception ex)
            {
                res = ex.Message;
            }
            finally
            {
                try
                {
                    Program.FfmpegMutex.ReleaseMutex();
                }
                catch (ObjectDisposedException)
                {
                    //can happen on shutdown
                }
            }
            MessageBox.Show(res);

            btnTest.Enabled = true;
        }
Пример #4
0
        private void FfmpegListener()
        {
            _reasonToStop = ReasonToFinishPlaying.StoppedByUser;
            _afr = null;
            bool open = false;
            string errmsg = "";

            try
            {
                Program.FFMPEGMutex.WaitOne();
                _afr = new AudioFileReader();
                int i = _source.IndexOf("://", StringComparison.Ordinal);
                if (i > -1)
                {
                    _source = _source.Substring(0, i).ToLower() + _source.Substring(i);
                }
                _afr.Timeout = Timeout;
                _afr.AnalyzeDuration = AnalyseDuration;
                _afr.Open(_source);

                open = true;
            }
            catch (Exception ex)
            {
                MainForm.LogExceptionToFile(ex, "FFMPEG");
            }
            finally
            {
                try
                {
                    Program.FFMPEGMutex.ReleaseMutex();
                }
                catch (ObjectDisposedException)
                {
                    //can happen on shutdown
                }
            }

            if (_afr == null || !_afr.IsOpen || !open)
            {
                ShutDown("Could not open audio stream" + ": " + _source);
                return;
            }

            RecordingFormat = new WaveFormat(_afr.SampleRate, 16, _afr.Channels);
            _waveProvider = new BufferedWaveProvider(RecordingFormat) { DiscardOnBufferOverflow = true, BufferDuration = TimeSpan.FromMilliseconds(500) };

            _sampleChannel = new SampleChannel(_waveProvider);
            _sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;

            int mult = _afr.BitsPerSample / 8;
            double btrg = Convert.ToDouble(_afr.SampleRate * mult * _afr.Channels);
            LastFrame = DateTime.UtcNow;
            bool realTime = !IsFileSource;

            try
            {
                DateTime req = DateTime.UtcNow;
                while (!_stopEvent.WaitOne(10, false) && !MainForm.ShuttingDown)
                {
                    byte[] data = _afr.ReadAudioFrame();
                    if (data == null || data.Equals(0))
                    {
                        if (!realTime)
                        {
                            break;
                        }
                    }
                    if (data != null && data.Length > 0)
                    {
                        LastFrame = DateTime.UtcNow;
                        var da = DataAvailable;
                        if (da != null)
                        {
                            //forces processing of volume level without piping it out
                            _waveProvider.AddSamples(data, 0, data.Length);

                            var sampleBuffer = new float[data.Length];
                            _sampleChannel.Read(sampleBuffer, 0, data.Length);

                            da(this, new DataAvailableEventArgs((byte[])data.Clone()));

                            if (WaveOutProvider != null && Listening)
                            {
                                WaveOutProvider.AddSamples(data, 0, data.Length);
                            }

                        }

                        if (realTime)
                        {
                            if (_stopEvent.WaitOne(30, false))
                                break;
                        }
                        else
                        {
                            //
                            double f = (data.Length / btrg) * 1000;
                            if (f > 0)
                            {
                                var span = DateTime.UtcNow.Subtract(req);
                                var msec = Convert.ToInt32(f - (int)span.TotalMilliseconds);
                                if ((msec > 0) && (_stopEvent.WaitOne(msec, false)))
                                    break;
                                req = DateTime.UtcNow;
                            }
                        }
                    }
                    else
                    {
                        if ((DateTime.UtcNow - LastFrame).TotalMilliseconds > Timeout)
                        {
                            throw new Exception("Audio source timeout");
                        }
                        if (_stopEvent.WaitOne(30, false))
                            break;
                    }

                }

            }
            catch (Exception e)
            {
                MainForm.LogExceptionToFile(e, "FFMPEG");
                errmsg = e.Message;
            }

            if (_sampleChannel != null)
            {
                _sampleChannel.PreVolumeMeter -= SampleChannelPreVolumeMeter;
                _sampleChannel = null;
            }

            if (_waveProvider != null)
            {
                if (_waveProvider.BufferedBytes > 0)
                    _waveProvider.ClearBuffer();
            }

            ShutDown(errmsg);
        }
Пример #5
0
        private void FfmpegListener()
        {
            _reasonToStop = ReasonToFinishPlaying.StoppedByUser;
            _afr          = null;
            bool   open   = false;
            string errmsg = "";

            try
            {
                Program.FfmpegMutex.WaitOne();
                _afr = new AudioFileReader();
                int i = _source.IndexOf("://", StringComparison.Ordinal);
                if (i > -1)
                {
                    _source = _source.Substring(0, i).ToLower() + _source.Substring(i);
                }
                _afr.Timeout         = Timeout;
                _afr.AnalyzeDuration = AnalyseDuration;
                _afr.Headers         = Headers;
                _afr.Cookies         = Cookies;
                _afr.UserAgent       = UserAgent;
                _afr.Open(_source);
                open = true;
            }
            catch (Exception ex)
            {
                MainForm.LogExceptionToFile(ex, "FFMPEG");
            }
            finally
            {
                try
                {
                    Program.FfmpegMutex.ReleaseMutex();
                }
                catch (ObjectDisposedException)
                {
                    //can happen on shutdown
                }
            }

            if (_afr == null || !_afr.IsOpen || !open)
            {
                ShutDown("Could not open audio stream" + ": " + _source);
                return;
            }


            RecordingFormat = new WaveFormat(_afr.SampleRate, 16, _afr.Channels);
            _waveProvider   = new BufferedWaveProvider(RecordingFormat)
            {
                DiscardOnBufferOverflow = true, BufferDuration = TimeSpan.FromMilliseconds(500)
            };

            _sampleChannel = new SampleChannel(_waveProvider);
            _sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;

            LastFrame = DateTime.UtcNow;

            try
            {
                while (!_stopEvent.WaitOne(10, false) && !MainForm.ShuttingDown)
                {
                    byte[] data = _afr.ReadAudioFrame();
                    if (data != null && data.Length > 0)
                    {
                        LastFrame = DateTime.UtcNow;
                        var da = DataAvailable;
                        if (da != null)
                        {
                            //forces processing of volume level without piping it out
                            _waveProvider.AddSamples(data, 0, data.Length);

                            var sampleBuffer = new float[data.Length];
                            int read         = _sampleChannel.Read(sampleBuffer, 0, data.Length);

                            da(this, new DataAvailableEventArgs((byte[])data.Clone(), read));

                            if (Listening)
                            {
                                WaveOutProvider?.AddSamples(data, 0, read);
                            }
                        }

                        if (_stopEvent.WaitOne(30, false))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if ((DateTime.UtcNow - LastFrame).TotalMilliseconds > Timeout)
                        {
                            throw new Exception("Audio source timeout");
                        }
                        if (_stopEvent.WaitOne(30, false))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MainForm.LogExceptionToFile(e, "FFMPEG");
                errmsg = e.Message;
            }

            if (_sampleChannel != null)
            {
                _sampleChannel.PreVolumeMeter -= SampleChannelPreVolumeMeter;
                _sampleChannel = null;
            }

            if (_waveProvider?.BufferedBytes > 0)
            {
                _waveProvider.ClearBuffer();
            }

            if (WaveOutProvider?.BufferedBytes > 0)
            {
                WaveOutProvider?.ClearBuffer();
            }

            ShutDown(errmsg);
        }
Пример #6
0
        private void FfmpegListener()
        {
            _reasonToStop = ReasonToFinishPlaying.StoppedByUser;
            _afr = null;
            bool open = false;
            string errmsg = "";

            try
            {
                Program.FfmpegMutex.WaitOne();
                _afr = new AudioFileReader();
                int i = _source.IndexOf("://", StringComparison.Ordinal);
                if (i>-1)
                {
                    _source = _source.Substring(0, i).ToLower() + _source.Substring(i);
                }
                _afr.Timeout = Timeout;
                _afr.AnalyzeDuration = AnalyseDuration;
                _afr.Headers = Headers;
                _afr.Cookies = Cookies;
                _afr.UserAgent = UserAgent;
                _afr.Open(_source);
                open = true;
            }
            catch (Exception ex)
            {
                Logger.LogExceptionToFile(ex,"FFMPEG");
            }
            finally
            {
                try
                {
                    Program.FfmpegMutex.ReleaseMutex();
                }
                catch (ObjectDisposedException)
                {
                    //can happen on shutdown
                }
            }

            if (_afr == null || !_afr.IsOpen || !open)
            {
                ShutDown("Could not open audio stream" + ": " + _source);
                return;
            }

            RecordingFormat = new WaveFormat(_afr.SampleRate, 16, _afr.Channels);
            _waveProvider = new BufferedWaveProvider(RecordingFormat) { DiscardOnBufferOverflow = true, BufferDuration = TimeSpan.FromMilliseconds(500) };

            _sampleChannel = new SampleChannel(_waveProvider);
            _sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;

            LastFrame = DateTime.UtcNow;

            try
            {
                while (!_stopEvent.WaitOne(10, false) && !MainForm.ShuttingDown)
                {
                    byte[] data = _afr.ReadAudioFrame();
                    if (data!=null && data.Length > 0)
                    {
                        LastFrame = DateTime.UtcNow;
                        var da = DataAvailable;
                        if (da != null)
                        {
                            //forces processing of volume level without piping it out
                            _waveProvider.AddSamples(data, 0, data.Length);

                            var sampleBuffer = new float[data.Length];
                            int read = _sampleChannel.Read(sampleBuffer, 0, data.Length);

                            da(this, new DataAvailableEventArgs((byte[])data.Clone(),read));

                            if (Listening)
                            {
                                WaveOutProvider?.AddSamples(data, 0, read);
                            }

                        }

                        if (_stopEvent.WaitOne(30, false))
                            break;

                    }
                    else
                    {
                        if ((DateTime.UtcNow - LastFrame).TotalMilliseconds > Timeout)
                        {
                            throw new Exception("Audio source timeout");
                        }
                        if (_stopEvent.WaitOne(30, false))
                            break;
                    }

                }

            }
            catch (Exception e)
            {
                Logger.LogExceptionToFile(e,"FFMPEG");
                errmsg = e.Message;
            }

            if (_sampleChannel != null)
            {
                _sampleChannel.PreVolumeMeter -= SampleChannelPreVolumeMeter;
                _sampleChannel = null;
            }

            if (_waveProvider?.BufferedBytes > 0)
                _waveProvider.ClearBuffer();

            if (WaveOutProvider?.BufferedBytes > 0) WaveOutProvider?.ClearBuffer();

            ShutDown(errmsg);
        }