private MediaRenderer(bool isDefaultInstance)
        {
            streamRenderer = null;

            SuiteRegistrationSupport.Init(GetSupportedFileProvider());

            timerCheckState          = new Timer();
            timerCheckState.Enabled  = true;
            timerCheckState.Interval = 500;
            timerCheckState.Start();
            timerCheckState.Tick += new EventHandler(timerCheckState_Tick);

            if (isDefaultInstance &&
                ProTONEConfig.IsPlayer &&
                ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.WCFInterface))
            {
                InternalInitSignalAnalisysWCF();
            }
        }
        public void OnUpdateMediaScreens()
        {
            bool showVU          = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.VUMeter);
            bool showWaveform    = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Waveform);
            bool showSpectrogram = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Spectrogram);

            pnlVuMeter.Visible = showVU;

            opmTableLayoutPanel1.RowStyles[0] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, showVU ? 70F : 0F);

            pnlWaveform.Visible = showWaveform;

            pnlSpectrogram.Visible = showSpectrogram;

            if (showSpectrogram && showWaveform)
            {
                opmTableLayoutPanel1.RowStyles[1] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F);
                opmTableLayoutPanel1.RowStyles[2] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F);
            }
            else if (showWaveform)
            {
                opmTableLayoutPanel1.RowStyles[1] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F);
                opmTableLayoutPanel1.RowStyles[2] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0F);
            }
            else if (showSpectrogram)
            {
                opmTableLayoutPanel1.RowStyles[1] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0F);
                opmTableLayoutPanel1.RowStyles[2] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F);
            }
            else
            {
                opmTableLayoutPanel1.RowStyles[1] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0F);
                opmTableLayoutPanel1.RowStyles[2] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0F);
            }

            UpdateLabels();
        }
        public MediaScreensOptionsPage()
        {
            InitializeComponent();

            chkShowPlaylist.Checked       = ProTONEConfig.MediaScreenActive(MediaScreen.Playlist);
            chkShowTrackInfo.Checked      = ProTONEConfig.MediaScreenActive(MediaScreen.TrackInfo);
            chkShowSignalAnalisys.Checked = ProTONEConfig.MediaScreenActive(MediaScreen.SignalAnalisys);
            chkShowBookmarkInfo.Checked   = ProTONEConfig.MediaScreenActive(MediaScreen.BookmarkInfo);

            chkVuMeter.Checked      = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.VUMeter);
            chkWaveform.Checked     = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Waveform);
            chkSpectrogram.Checked  = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Spectrogram);
            chkWCFInterface.Checked = ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.WCFInterface);

            this.chkShowPlaylist.CheckedChanged       += new System.EventHandler(this.OnSettingsChanged);
            this.chkShowTrackInfo.CheckedChanged      += new System.EventHandler(this.OnSettingsChanged);
            this.chkShowSignalAnalisys.CheckedChanged += new System.EventHandler(this.OnSettingsChanged);
            this.chkShowBookmarkInfo.CheckedChanged   += new System.EventHandler(this.OnSettingsChanged);

            this.chkVuMeter.CheckedChanged      += new System.EventHandler(this.OnSettingsChanged);
            this.chkWaveform.CheckedChanged     += new System.EventHandler(this.OnSettingsChanged);
            this.chkSpectrogram.CheckedChanged  += new System.EventHandler(this.OnSettingsChanged);
            this.chkWCFInterface.CheckedChanged += new System.EventHandler(this.OnSettingsChanged);
        }
        void _tmrUpdate_Tick(object sender, EventArgs e)
        {
            try
            {
                _tmrUpdate.Stop();

                if (ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.VUMeter))
                {
                    AudioSampleData vuData = MediaRenderer.DefaultInstance.VuMeterData;
                    if (vuData != null)
                    {
                        vuLeft.Value  = 0.5 * (vuLeft.Value + vuLeft.Maximum * vuData.LVOL);
                        vuRight.Value = 0.5 * (vuRight.Value + vuRight.Maximum * vuData.RVOL);
                    }
                    else
                    {
                        vuLeft.Value  = 0;
                        vuRight.Value = 0;
                    }
                }

                if (ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Waveform))
                {
                    gpWaveform.Reset(false);

                    double[][] waveformData = MediaRenderer.DefaultInstance.WaveformData;
                    if (waveformData != null && waveformData[0].Length > 0)
                    {
                        if (_prevWaveform == null)
                        {
                            _prevWaveform = new double[waveformData[0].Length];
                        }

                        for (int k = 0; k < _prevWaveform.Length; k++)
                        {
                            _prevWaveform[k] = 0.5 * (_prevWaveform[k] + waveformData[0][k]);
                        }

                        gpWaveform.MinVal = -1 * MediaRenderer.DefaultInstance.MaxLevel;
                        gpWaveform.MaxVal = MediaRenderer.DefaultInstance.MaxLevel;
                        gpWaveform.AddDataRange(_prevWaveform, ThemeManager.GradientGaugeColor1);
                    }
                    else
                    {
                        gpWaveform.Reset(true);
                    }
                }

                if (ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Spectrogram))
                {
                    double maxFftLevel = SpectrogramTransferFunction(MediaRenderer.DefaultInstance.MaxFFTLevel);

                    spSpectrogram.Reset(false);
                    spSpectrogram.MinVal = maxFftLevel / 2; // Min level = -6 dBM
                    spSpectrogram.MaxVal = maxFftLevel;     // Max level = 0 dBM

                    double[] spectrogramData = MediaRenderer.DefaultInstance.SpectrogramData;
                    if (spectrogramData != null && spectrogramData.Length > 0)
                    {
                        double[] spectrogramData2 = new double[spectrogramData.Length];
                        Array.Clear(spectrogramData2, 0, spectrogramData.Length);

                        double[] bands = new double[BandCount];
                        Array.Clear(bands, 0, BandCount);

                        int jBand = 0;

                        int div = spectrogramData.Length / (BandCount);

                        try
                        {
                            int maxSize = (int)Math.Min(BandCount, spectrogramData.Length);
                            for (int i = 0; i < maxSize; i++)
                            {
                                bands[i]  = Math.Max(0, Math.Min(maxFftLevel, SpectrogramTransferFunction(spectrogramData[i])));
                                _bands[i] = 0.5 * (_bands[i] + bands[i]);
                            }

                            spSpectrogram.AddDataRange(_bands, Color.Transparent);
                        }
                        catch (Exception ex)
                        {
                            string s = ex.Message;
                            spSpectrogram.Reset(true);
                            Array.Clear(_bands, 0, _bands.Length);
                        }
                    }
                    else
                    {
                        spSpectrogram.Reset(true);
                        Array.Clear(_bands, 0, _bands.Length);
                    }
                }
            }
            finally
            {
                _tmrUpdate.Start();
            }
        }
        private void ExtractSamples(AudioSample smp)
        {
            if (smp == null || _actualAudioFormat == null || mediaPosition == null)
            {
                return;
            }

            double mediaTime = 0;

            mediaPosition.get_CurrentPosition(out mediaTime);

            double delay    = smp.SampleTime - mediaTime;
            double absDelay = Math.Abs(delay);

            // Discard samples too far in time from current media time
            if (absDelay > 1)
            {
                return;
            }

            //CalculateAverageDelay(delay * 1000);

            if (delay > 0)
            {
                Thread.Sleep(TimeSpan.FromSeconds(delay));
            }

            FilterState ms = GetFilterState();

            if (smp.RawSamples.Length <= 0 || ms != FilterState.Running || _actualAudioFormat == null)
            {
                return;
            }

            int bytesPerChannel      = _actualAudioFormat.wBitsPerSample / 8;
            int totalChannels        = _actualAudioFormat.nChannels;
            int totalChannelsInArray = Math.Min(2, totalChannels);

            int i = 0;

            while (i < smp.RawSamples.Length)
            {
                double[] channels = new double[totalChannelsInArray];
                Array.Clear(channels, 0, totalChannelsInArray);

                int j = 0;
                while (j < totalChannelsInArray)
                {
                    int k = 0;
                    while (k < bytesPerChannel)
                    {
                        if (bytesPerChannel <= 2)
                        {
                            channels[j] += (short)(smp.RawSamples[i] << (8 * k));
                        }
                        else
                        {
                            channels[j] += (int)(smp.RawSamples[i] << (8 * k));
                        }

                        i++;
                        k++;
                    }

                    j++;
                }

                if (channels.Length >= 2)
                {
                    _sampleData.Enqueue(new AudioSampleData((double)channels[0], (double)channels[1]));
                }
                else
                {
                    _sampleData.Enqueue(new AudioSampleData((double)channels[0], 0));
                }

                _gatheredSamples++;
                if (_gatheredSamples % _waveformWindowSize == 0)
                {
                    if (ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.VUMeter) ||
                        ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Waveform) ||
                        ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.WCFInterface))
                    {
                        AnalyzeWaveform(_sampleData.Skip(_sampleData.Count - _waveformWindowSize).Take(_waveformWindowSize).ToArray(),
                                        smp.SampleTime);
                    }
                }

                Thread.Yield();
            }

            AudioSampleData lostSample = null;

            while (_sampleData.Count > _fftWindowSize)
            {
                _sampleData.TryDequeue(out lostSample);
            }

            if (ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.Spectrogram) ||
                ProTONEConfig.SignalAnalisysFunctionActive(SignalAnalisysFunction.WCFInterface))
            {
                AnalyzeFFT(_sampleData.ToArray());
            }

            Thread.Yield();
        }