private void RenderPeakMeter(Sample Sample)
        {
            Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    int barSpacing        = 25;
                    int barWidth          = (barSpacing - 8);
                    int lineWidth         = (barWidth + 1);
                    int leftOffset        = 60;
                    byte[] frequencyPeaks = PeakMeter.CalculateFrequencies(Sample.Spectrogram, 44100);

                    if (!(_clearEffectIsEnabled))
                    {
                        ClearOutputBitmap();
                    }

                    int baseY   = 255;
                    int offsetX = 0;
                    int offsetY = 0;
                    for (int x = 0; x < frequencyPeaks.Length; x++)
                    {
                        offsetX = (x * barSpacing) + leftOffset;
                        offsetY = (baseY - frequencyPeaks[x]);

                        _lastPeaks[x]    = CalculatePeakDelay(_lastPeaks[x], offsetY, 2, baseY);
                        _maximumPeaks[x] = CalculatePeakDelay(_maximumPeaks[x], offsetY, 1, baseY);

                        _outputWriteableBitmap.FillRectangle(offsetX, _lastPeaks[x], (offsetX + barWidth), baseY, _primaryColour);
                        // Only draw the maximum peak if it is above the empty bar level (cutoff)
                        if (_lastPeaks[x] < baseY)
                        {
                            _outputWriteableBitmap.DrawLine(offsetX, _maximumPeaks[x], (offsetX + lineWidth), _maximumPeaks[x], _secondaryColour);
                        }
                    }

                    if (_clearEffectIsEnabled)
                    {
                        RunClearEffect();
                    }

                    FlushOutputBitmap();
                }
                catch
                { }
            });
        }
        protected override void GetSampleAsync(MediaStreamType mediaStreamType)
        {
            try
            {
                _logger.LogSampleRequested();

                var resetEvents = new AutoResetEvent[AudioControllers.Count];

                // Dequeue the sample from the media controllers, so that each controller's entire audio stack gets exercised.
                for (int i = 0; i < AudioControllers.Count; i++)
                {
                    var audioControllerEntry = AudioControllers[i];
                    resetEvents[i] = audioControllerEntry.ResetEvent;

                    // Throw the decoding work into a thread pool.
                    ThreadPool.QueueUserWorkItem(obj =>
                    {
                        var entry = (AudioControllerEntry)obj;
                        entry.MediaController.GetNextAudioFrame(ms =>
                        {
                            try
                            {
                                entry.LastMemoryStream = ms;

                                // Check to see if the frame has the expected frequency, and if not, update a counter.
                                if (!UseGeneratedTone)
                                {
                                    return;
                                }
                                if (entry.WriteToBuffer(entry.LastMemoryStream))
                                {
                                    entry.Clear();
                                    byte[] frequencies     = PeakMeter.CalculateFrequencies(entry.SampleBuffer, AudioFormat.Default.SamplesPerSecond);
                                    bool frequencyMismatch = false;
                                    if (frequencies[_frequencyIndex] == 0)
                                    {
                                        frequencyMismatch = true;
                                    }
                                    else
                                    {
                                        if (frequencies.Where((t, j) => j != _frequencyIndex && t > frequencies[_frequencyIndex]).Any())
                                        {
                                            frequencyMismatch = true;
                                        }
                                    }
                                    entry.LogFrequencyCounterMismatch(frequencyMismatch);
                                }
                            }
                            catch (Exception ex)
                            {
                                ClientLogger.Debug(ex.ToString);
                            }
                            finally
                            {
                                entry.ResetEvent.Set();
                            }
                        });
                    }, audioControllerEntry);
                }

                // Wait for all the decoding jobs to finish
                WaitHandle.WaitAll(resetEvents);

                // Submit only the first sample retrieved to be played.
                var rawSample = AudioControllers[0].LastMemoryStream;

                var sample = new MediaStreamSample(
                    _mediaStreamDescription,
                    rawSample,
                    0,
                    rawSample.Length,
                    (DateTime.Now - _startTime).Ticks,
                    _emptySampleDict);
                ReportGetSampleCompleted(sample);
            }
            catch (Exception ex)
            {
                ClientLogger.Debug(ex.ToString());
            }
        }
        private void RenderFFTScan(Sample Sample)
        {
            Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    int height            = (int)_outputWriteableBitmap.PixelHeight;
                    int width             = (int)_outputWriteableBitmap.PixelWidth;
                    byte[] frequencyPeaks = PeakMeter.CalculateFrequencies(Sample.Spectrogram, 44100);

                    if (_fftScanIsCleared)
                    {
                        ClearOutputBitmap();
                        _xRunningScanCounter = _marginLeft;
                        _fftScanIsCleared    = false;
                    }

                    int offsetY            = 0;
                    int baseOffsetY        = 0;
                    int maximumPeak        = 0;
                    int bandTotal          = frequencyPeaks.Length;
                    int drawingColour      = _primaryColour;
                    bool isAlternateColour = false;

                    // Clear previous scan line
                    _outputWriteableBitmap.DrawLine(_xRunningScanCounter, 0, _xRunningScanCounter, height, _clearColour);

                    for (int x = 0; x < bandTotal; x++)
                    {
                        maximumPeak      = (frequencyPeaks[x] >> 1);
                        baseOffsetY      = ((bandTotal - x) * 39);
                        _maximumPeaks[x] = ((_maximumPeaks[x] + maximumPeak) >> 1);
                        offsetY          = (baseOffsetY - _maximumPeaks[x]);

                        if (isAlternateColour)
                        {
                            drawingColour = _alternatePrimaryColour;
                        }
                        else
                        {
                            drawingColour = _primaryColour;
                        }
                        isAlternateColour = !(isAlternateColour);

                        if (offsetY == baseOffsetY)
                        {
                            _outputWriteableBitmap.SetPixel(_xRunningScanCounter, baseOffsetY, drawingColour);
                        }
                        else
                        {
                            _outputWriteableBitmap.DrawLine(_xRunningScanCounter, offsetY, _xRunningScanCounter, baseOffsetY, drawingColour);
                        }
                    }

                    // Current Scanline
                    _outputWriteableBitmap.DrawLine((_xRunningScanCounter + 1), 0, (_xRunningScanCounter + 1), height, _secondaryColour);

                    _xRunningScanCounter++;
                    if (_xRunningScanCounter >= _widthWithoutMarginRight)
                    {
                        _outputWriteableBitmap.DrawLine(_xRunningScanCounter, 0, _xRunningScanCounter, height, _clearColour);
                        _xRunningScanCounter = _marginLeft;
                    }

                    FlushOutputBitmap();
                }
                catch
                {
                }
            });
        }