Пример #1
0
        public Bitmap CreateSpectrumLine(Size size, Brush brush, Color background, bool highQuality)
        {
            if (!UpdateFrequencyMappingIfNessesary(size))
            {
                return(null);
            }

            var fftBuffer = new float[(int)FftSize];

            //get the fft result from the spectrum provider
            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                using (var pen = new Pen(brush, (float)_barWidth))
                {
                    var bitmap = new Bitmap(size.Width, size.Height);

                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        PrepareGraphics(graphics, highQuality);
                        graphics.Clear(background);

                        CreateSpectrumLineInternal(graphics, pen, fftBuffer, size);
                    }

                    return(bitmap);
                }
            }
            return(null);
        }
Пример #2
0
        public Bitmap CreateSpectrogramLine(Size size, Color foregroundColor, Color backgroundColor, bool highQuality)
        {
            if (!UpdateFrequencyMappingIfNessesary(size))
            {
                return(null);
            }

            var fftBuffer = new float[(int)FftSize];

            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                using (var foregroundPen = new Pen(foregroundColor))
                    using (var backgroundPen = new Pen(backgroundColor))
                    {
                        var bitmap = _previousBitmap == null ? new Bitmap(size.Width, (int)FftSize) : new Bitmap(_previousBitmap, size.Width, (int)FftSize);

                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            PrepareGraphics(graphics, highQuality);

                            if (_previousBitmap == null)
                            {
                                graphics.Clear(backgroundColor);
                            }

                            DrawSpectrogramLine(graphics, foregroundPen, backgroundPen, fftBuffer, size);
                        }

                        _previousBitmap = bitmap;
                        return(new Bitmap(bitmap, size));
                    }
            }
            return(null);
        }
Пример #3
0
        public void Draw()
        {
            var fftBuffer = new float[FftSize];

            if (!SpectrumProvider.GetFftData(fftBuffer, this))
            {
                return;
            }

            var spectrumPoints = CalculateSpectrumPoints(MatrixPanel.Height, fftBuffer);

            for (var x = 0; x < spectrumPoints.Count; x++)
            {
                var height = (int)Math.Round(spectrumPoints[x].Value * Amplifier);

                using (var brush = new LinearGradientBrush(new Rectangle(x, 0, 1, MatrixPanel.Height), ColorHelper.HsvToColor((byte)(Hue + 32)), ColorHelper.HsvToColor(Hue), LinearGradientMode.Vertical))
                {
                    Frame.Graphics.FillRectangle(brush, x, MatrixPanel.Height - height, 1, height);
                }



                //                using (var brush = new LinearGradientBrush(new Rectangle(x, 0, 1, MatrixPanel.Height), Color.Red, Color.Green, LinearGradientMode.Vertical))
                //                {
                //                    Frame.Graphics.FillRectangle(brush, x, MatrixPanel.Height - height, 1, height);
                //                }
            }

            Hue++;
        }
Пример #4
0
        public float[] livelyGetSystemAudioSpectrum()
        {
            var fftBuffer = new float[(int)FftSize];

            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                //return fftBuffer;
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("livelygetspectrum error");
                return(null);
            }

            int height = CurrentSize.Height;

            SpectrumPointData[] dats = CalculateSpectrumPoints(2, fftBuffer);
            float[]             res  = new float[dats.Length];
            for (int i = 0; i < dats.Length; i++)
            {
                res[i] = (float)dats[i].Value;
            }

            return(res);
        }
Пример #5
0
        private void TimerTick(object state)
        {
            // This is a loop for the life of the source that reports things like
            // position back to a subscriber.
            if (_waveSource != null)
            {
                if (Position != cachedPosition)
                {
                    // position has changed
                    cachedPosition = Position;
                    RaiseSourcePropertyChangedEvent(ESourceProperty.Position, cachedPosition);
                }

                if (_soundOut != null)
                {
                    if (PlaybackState != cachedPlaybackState)
                    {
                        cachedPlaybackState = PlaybackState;
                        RaiseSourcePropertyChangedEvent(ESourceProperty.PlaybackState, cachedPlaybackState);
                    }
                }

                if (SpectrumProvider != null && cachedPlaybackState == PlaybackState.Playing)
                {
                    fftData = new float[(int)fftSize];
                    SpectrumProvider.GetFftData(fftData);
                    RaiseSourcePropertyChangedEvent(ESourceProperty.FftData, FftData);
                }
            }
        }
Пример #6
0
 public bool GetFftData(float[] fftDataBuffer)
 {
     if (SpectrumProvider == null)
     {
         return(false);
     }
     return(SpectrumProvider.GetFftData(fftDataBuffer));
 }
Пример #7
0
        public double[] CreateSpectrumData()
        {
            var fftBuffer = new float[(int)FftSize];

            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                return(GetSpectrumPointData(fftBuffer, 100).Select(x => x.Value).ToArray());
            }
            return(new double[50]);
        }
Пример #8
0
        public void CreateSpectrumLine(int type)
        {
            var fftBuffer = new float[(int)FftSize];

            //get the fft result from the spectrum provider
            if (!SpectrumProvider.GetFftData(fftBuffer, this))
            {
                return;
            }
            CreateSpectrumLineInternal(fftBuffer, type);
        }
Пример #9
0
        public double?GetValue()
        {
            var fftBuffer = new float[(int)FftSize];

            // get the fft result from the spectrum provider
            if (SpectrumProvider == null || !SpectrumProvider.GetFftData(fftBuffer, this))
            {
                return(null);
            }

            var spectrumPoints = CalculateSpectrumPoints(1, fftBuffer);

            return(spectrumPoints[0].Value);
        }
Пример #10
0
        public List <double> GetLineValues(double height)
        {
            var fftBuffer = new float[(int)FftSize];

            // get the fft result from the spectrum provider
            if (!SpectrumProvider.GetFftData(fftBuffer, this))
            {
                return(null);
            }

            var spectrumPoints = CalculateSpectrumPoints(height, fftBuffer);

            return(spectrumPoints?.Select(s => s.Value).ToList());
        }
Пример #11
0
        public bool CreateVoicePrint3D(Graphics graphics, RectangleF clipRectangle, float xPos, Color background,
                                       float lineThickness = 1f)
        {
            if (!_isInitialized)
            {
                UpdateFrequencyMapping();
                _isInitialized = true;
            }

            var fftBuffer = new float[(int)FftSize];

            //get the fft result from the spectrumprovider
            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                //prepare the fft result for rendering
                var spectrumPoints = CalculateSpectrumPoints(1.0, fftBuffer);
                using (var pen = new Pen(background, lineThickness))
                {
                    var currentYOffset = clipRectangle.Y + clipRectangle.Height;

                    //render the fft result
                    for (var i = 0; i < spectrumPoints.Length; i++)
                    {
                        var p = spectrumPoints[i];

                        var xCoord      = clipRectangle.X + xPos;
                        var pointHeight = clipRectangle.Height / spectrumPoints.Length;

                        //get the color based on the fft band value
                        pen.Color = _colorCalculator.GetColor((float)p.Value);

                        var p1 = new PointF(xCoord, currentYOffset);
                        var p2 = new PointF(xCoord, currentYOffset - pointHeight);

                        graphics.DrawLine(pen, p1, p2);

                        currentYOffset -= pointHeight;
                    }
                }

                return(true);
            }

            return(false);
        }
        public double[] GetPointData(double multiplier)
        {
            float[] fftBuffer = new float[(int)FftSize];

            SpectrumProvider.GetFftData(fftBuffer, this);

            SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(multiplier, fftBuffer);

            double[] values = new double[spectrumPoints.Length];

            for (int i = 0; i < spectrumPoints.Length; i++)
            {
                SpectrumPointData p = spectrumPoints[i];
                values[i] = p.Value;
            }

            return(values);
        }
Пример #13
0
        /// <summary>
        /// Calculate the colors
        /// </summary>
        protected override bool GetColors(Graphics graphics, float lineThickness)
        {
            if (!m_IsInitialized)
            {
                UpdateFrequencyMapping();
                m_IsInitialized = true;
            }

            var fftBuffer = new float[(int)Analyzer.FFTSize];

            //get the fft result from the spectrumprovider
            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                //prepare the fft result for rendering
                SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(1.0, fftBuffer);
                using (var pen = new System.Drawing.Pen(ColorPalette.PanelBG_Drawing, lineThickness))
                {
                    float currentYOffset = m_Bitmap.Size.Height;

                    //render the fft result
                    for (int i = 0; i < spectrumPoints.Length; i++)
                    {
                        SpectrumPointData p = spectrumPoints[i];

                        float xCoord      = m_Position;
                        float pointHeight = (float)m_Bitmap.Size.Height / (float)spectrumPoints.Length;

                        //get the color based on the fft band value
                        double value = p.Value * m_Multiplier;

                        pen.Color = Utils.Lerp(ColorPalette.PanelBG, ColorPalette.Accent, value);

                        var p1 = new PointF(xCoord, currentYOffset);
                        var p2 = new PointF(xCoord, currentYOffset - pointHeight);

                        graphics.DrawLine(pen, p1, p2);

                        currentYOffset -= pointHeight;
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #14
0
        public float[] GetSpectrumData(double maxValue)
        {
            // Get spectrum data internal
            var fftBuffer = new float[(int)FftSize];

            UpdateFrequencyMapping();

            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(maxValue, fftBuffer);

                // Convert to float[]
                List <float> spectrumData = new List <float>();
                spectrumPoints.ToList().ForEach(point => spectrumData.Add((float)point.Value));
                return(spectrumData.ToArray());
            }

            return(null);
        }
Пример #15
0
        public void CreateSpectrum()
        {
            var fftBuffer = new float[(int)FftSize];

            SpectrumProvider.GetFftData(fftBuffer, this);
            SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(255, fftBuffer);
            if (spectrumPoints[0].Value > 0)
            {
                Mode.Colors[0].SetR(CalculateScale(spectrumPoints[0].Value));
            }
            if (spectrumPoints[1].Value > 0)
            {
                Mode.Colors[0].SetG(CalculateScale(spectrumPoints[1].Value));
            }
            if (spectrumPoints[2].Value > 0)
            {
                Mode.Colors[0].SetB(CalculateScale(spectrumPoints[2].Value));
            }
        }
Пример #16
0
        public float[] livelyGetSystemAudioSpectrum()
        {
            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";

            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

            var fftBuffer = new float[(int)FftSize];

            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                //return fftBuffer;
            }
            else
            {
                //System.Diagnostics.Debug.WriteLine("livelygetspectrum error");
                //return fftBuffer;
                //return null;
            }

            //return fftBuffer;

            for (int i = 0; i < fftBuffer.Length; i++)
            {
                //fftBuffer[i] /= 100;
                //Debug.Write(fftBuffer[i] + " ");
            }

            int height = CurrentSize.Height;

            SpectrumPointData[] dats = CalculateSpectrumPoints(2, fftBuffer);
            float[]             res  = new float[dats.Length];
            for (int i = 0; i < dats.Length; i++)
            {
                res[i] = (float)dats[i].Value;
                //System.Diagnostics.Debug.WriteLine(res[i]);
                //res[i] = (float)dats[i].Value - 1;
            }

            return(res);
        }
Пример #17
0
        public bool CreateVoicePrint3D(Graphics graphics, RectangleF clipRectangle, float xPos, Color background,
                                       float lineThickness = 1f)
        {
            if (!_isInitialized)
            {
                UpdateFrequencyMapping();
                _isInitialized = true;
            }

            var fftBuffer = new float[(int)FftSize];

            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(1.0, fftBuffer);
                using (var pen = new Pen(background, lineThickness))
                {
                    float currentYOffset = clipRectangle.Y + clipRectangle.Height;

                    for (int i = 0; i < spectrumPoints.Length; i++)
                    {
                        SpectrumPointData p = spectrumPoints[i];

                        float xCoord      = clipRectangle.X + xPos;
                        float pointHeight = clipRectangle.Height / spectrumPoints.Length;

                        pen.Color = _colorCalculator.GetColor((float)p.Value);
                        //pen.Color = Color.FromArgb(255, pen.Color.R, pen.Color.G, pen.Color.B);

                        var p1 = new PointF(xCoord, currentYOffset);
                        var p2 = new PointF(xCoord, currentYOffset - pointHeight);

                        graphics.DrawLine(pen, p1, p2);

                        currentYOffset -= pointHeight;
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #18
0
        private void TimerTick(object state)
        {
            if (_waveSource != null)
            {
                if (recordingState == RecordingState.Recording)
                {
                    if (Position - TimeSpan.FromMilliseconds(50) > cachedPosition)
                    //if (Position != cachedPosition)
                    {
                        // position has changed
                        cachedPosition = Position;
                        RaiseSourcePropertyChangedEvent(SourceProperty.Position, cachedPosition);
                    }
                }

                if (SpectrumProvider != null && recordingState == RecordingState.Recording)
                {
                    fftData = new float[(int)fftSize];
                    SpectrumProvider.GetFftData(fftData);
                    RaiseSourcePropertyChangedEvent(SourceProperty.FftData, FftData);
                }
            }
        }
Пример #19
0
        public float[] GetSpectrumData(double maxValue, int bandIndex)
        {
            // Get spectrum data internal
            if (fftBuffer == null)
            {
                fftBuffer = new float[(int)FftSize];
            }

            UpdateFrequencyMapping(bandIndex);

            SpectrumProvider.GetFftData(fftBuffer, this);

            SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(maxValue, fftBuffer, bandIndex);

            // Convert to float[]
            List <float> spectrumData = new List <float>();

            spectrumPoints.ToList().
            ForEach
            (
                point =>
            {
                float val = (float)point.Value;
                if (_BandFrecuencies.TryGetValue(bandIndex, out var filterList))
                {
                    foreach (var filter in filterList)
                    {
                        val = filter.Process(val);
                    }
                }

                spectrumData.Add(val);
            }
            );
            return(spectrumData.ToArray());
        }
Пример #20
0
 /// <summary>
 /// Update our math.
 /// </summary>
 /// <returns></returns>
 public bool Update()
 {
     return(SpectrumProvider.GetFftData(iFftBuffer, this));
 }
Пример #21
0
        public bool CreateVoicePrint3D(Graphics graphics, RectangleF clipRectangle, float xPos, Color background, DrawPurpose dp,
                                       float lineThickness = 1f)
        {
            if (!_isInitialized)
            {
                UpdateFrequencyMapping();
                _isInitialized = true;
            }

            var fftBuffer = new float[(int)FftSize];

            //get the fft result from the spectrumprovider
            if (SpectrumProvider.GetFftData(fftBuffer, this))
            {
                SpectrumPointData[] spectrumPoints;
                //prepare the fft result for rendering
                switch (dp)
                {
                case DrawPurpose.ForAnalog:
                    spectrumPoints = CalculateSpectrumPoints(0.99, fftBuffer);
                    break;

                case DrawPurpose.ForDigitalBass:
                    spectrumPoints = CalculateSpectrumPoints(0.99, fftBuffer, bassFactor, BassFactorSqrt);
                    break;

                case DrawPurpose.ForDigitalMedio:
                    spectrumPoints = CalculateSpectrumPoints(0.99, fftBuffer, medioFactor, MedioFactorSqrt);
                    break;

                case DrawPurpose.ForDigitalTreble:
                    spectrumPoints = CalculateSpectrumPoints(0.99, fftBuffer, trebleFactor, trebleFactorSqrt);
                    break;

                case DrawPurpose.ForGeneric:
                    spectrumPoints = CalculateSpectrumPoints(0.99, fftBuffer);
                    break;

                default:
                    spectrumPoints = CalculateSpectrumPoints(0.99, fftBuffer);
                    break;
                }


                using (var pen = new Pen(background, lineThickness))
                {
                    float currentYOffset = clipRectangle.Y + clipRectangle.Height;

                    //render the fft result
                    for (int i = 0; i < spectrumPoints.Length; i++)
                    {
                        SpectrumPointData p = spectrumPoints[i];

                        float xCoord      = clipRectangle.X + xPos;
                        float pointHeight = clipRectangle.Height / spectrumPoints.Length;

                        //get the color based on the fft band value
                        pen.Color = _colorCalculator.GetColor((float)p.Value);

                        switch (dp)
                        {
                        case DrawPurpose.ForAnalog:
                            FirmataModule.SoundSpectrumColor = pen.Color;
                            break;

                        case DrawPurpose.ForDigitalBass:
                            FirmataModule.DigitalSpectrumPattern[0] = pen.Color;
                            break;

                        case DrawPurpose.ForDigitalMedio:
                            FirmataModule.DigitalSpectrumPattern[1] = pen.Color;
                            break;

                        case DrawPurpose.ForDigitalTreble:
                            FirmataModule.DigitalSpectrumPattern[2] = pen.Color;
                            break;

                        case DrawPurpose.ForGeneric:
                            break;

                        default:
                            break;
                        }


                        var p1 = new PointF(xCoord, currentYOffset);
                        var p2 = new PointF(xCoord, currentYOffset - pointHeight);

                        graphics.DrawLine(pen, p1, p2);

                        currentYOffset -= pointHeight;
                    }
                }
                return(true);
            }
            return(false);
        }