Пример #1
0
        static void DemoMozart()
        {
            var spec = new Spectrogram.Spectrogram(sampleRate: 8000, fftSize: 2048, step: 700);

            float[] values = Spectrogram.Tools.ReadWav("mozart.wav");
            spec.AddExtend(values);
            Bitmap bmp = spec.GetBitmap(intensity: 2, freqHigh: 2500);

            spec.SaveBitmap(bmp, "mozart.jpg");
        }
Пример #2
0
        public static Bitmap GetVerticalScale(int width, Spectrogram.Spectrogram spec, int offsetFromZeroHz = 0, int offsetHz = 0, int reduction = 1)
        {
            int majorTickHz     = 50;
            int majorTickLength = 4;
            int minorTickHz     = 10;
            int minorTickLength = 1;

            Bitmap bmp = new Bitmap(width, spec.Height / reduction);

            using (var gfx = Graphics.FromImage(bmp))
                using (var pen = new Pen(Color.Black))
                    using (var brush = new SolidBrush(Color.Black))
                        using (var font = new Font(FontFamily.GenericMonospace, 10))
                            using (var sf = new StringFormat()
                            {
                                LineAlignment = StringAlignment.Center
                            })
                            {
                                gfx.Clear(Color.White);

                                List <double> freqsMajor = new List <double>();
                                for (double f = spec.FreqMin; f <= spec.FreqMax; f += majorTickHz)
                                {
                                    freqsMajor.Add(f);
                                }

                                List <double> freqsMinor = new List <double>();
                                for (double f = spec.FreqMin; f <= spec.FreqMax; f += minorTickHz)
                                {
                                    freqsMinor.Add(f);
                                }

                                // don't show first or last major tick
                                if (freqsMajor.Count >= 2)
                                {
                                    freqsMajor.RemoveAt(0);
                                    freqsMajor.RemoveAt(freqsMajor.Count - 1);
                                }

                                foreach (var freq in freqsMajor)
                                {
                                    int y = spec.PixelY(freq + offsetHz) / reduction;
                                    gfx.DrawLine(pen, 0, y, majorTickLength, y);
                                    gfx.DrawString($"{freq + offsetFromZeroHz:N0} Hz", font, brush, majorTickLength, y, sf);
                                }

                                foreach (var freq in freqsMinor)
                                {
                                    int y = spec.PixelY(freq + offsetHz) / reduction;
                                    gfx.DrawLine(pen, 0, y, minorTickLength, y);
                                }
                            }

            return(bmp);
        }
Пример #3
0
        static void DemoQRSS()
        {
            var spec = new Spectrogram.Spectrogram(sampleRate: 8000, fftSize: 16384, step: 8000);

            float[] values = Spectrogram.Tools.ReadMp3("qrss-w4hbk.mp3");
            spec.AddExtend(values);
            Bitmap bmp = spec.GetBitmap(intensity: 1.5, freqLow: 1100, freqHigh: 1500,
                                        showTicks: true, tickSpacingHz: 50, tickSpacingSec: 60);

            spec.SaveBitmap(bmp, "qrss.png");
        }
Пример #4
0
        public void StartListening(int deviceIndex = 0, int sampleRate = 8000, int fftSize = 1024)
        {
            spec = new Spectrogram.Spectrogram(sampleRate, fftSize, 250);

            int bitRate            = 16;
            int channels           = 1;
            int bufferMilliseconds = 10;

            wvin = new NAudio.Wave.WaveInEvent();
            wvin.DeviceNumber       = deviceIndex;
            wvin.WaveFormat         = new NAudio.Wave.WaveFormat(sampleRate, bitRate, channels);
            wvin.DataAvailable     += OnDataAvailable;
            wvin.BufferMilliseconds = bufferMilliseconds;
            wvin.StartRecording();
        }
Пример #5
0
        private static void CreateSpectrogram(string fileName)
        {
            var spectrogramName = fileName.Substring(0, fileName.Length - 4) + "-spectro.jpg";

            if (File.Exists(spectrogramName))
            {
                return;
            }

            var spec = new Spectrogram.Spectrogram(sampleRate: 8000, fftSize: 2048, step: 700);

            float[] values = Spectrogram.Tools.ReadWav(fileName);
            spec.AddExtend(values);

            var bitmap = spec.GetBitmap(intensity: 2, freqHigh: 2500);

            spec.SaveBitmap(bitmap, spectrogramName);
        }
Пример #6
0
        private void AudioMonitorInitialize(
            int DeviceIndex        = 0,
            int sampleRate         = 8000,
            int bitRate            = 16,
            int channels           = 1,
            int bufferMilliseconds = 10,
            int fftSize            = 1024,
            int step = 250
            )
        {
            spec = new Spectrogram.Spectrogram(sampleRate, fftSize, step);

            wvin = new NAudio.Wave.WaveInEvent();
            wvin.DeviceNumber       = DeviceIndex;
            wvin.WaveFormat         = new NAudio.Wave.WaveFormat(sampleRate, bitRate, channels);
            wvin.DataAvailable     += OnDataAvailable;
            wvin.BufferMilliseconds = bufferMilliseconds;
            wvin.StartRecording();
        }
Пример #7
0
        static void DemoHal()
        {
            var spec = new Spectrogram.Spectrogram(sampleRate: 15000, fftSize: 4096, step: 400);

            float[] values = Spectrogram.Tools.ReadMp3("cant-do-that.mp3");
            spec.AddExtend(values);
            Bitmap bmp;

            bmp = spec.GetBitmap(intensity: .2, freqHigh: 1000);
            spec.SaveBitmap(bmp, "cant-do-that.jpg");

            bmp = spec.GetBitmap(intensity: .2, freqHigh: 1000,
                                 colormap: Spectrogram.Colormap.grayscale);
            spec.SaveBitmap(bmp, "cant-do-that-grayscale.jpg");

            bmp = spec.GetBitmap(intensity: .2, freqHigh: 1000,
                                 colormap: Spectrogram.Colormap.grayscaleInverted);
            spec.SaveBitmap(bmp, "cant-do-that-grayscale-inverted.jpg");

            bmp = spec.GetBitmap(intensity: .2, freqHigh: 1000,
                                 colormap: Spectrogram.Colormap.vdGreen);
            spec.SaveBitmap(bmp, "cant-do-that-green.jpg");
        }
Пример #8
0
        /*static void DemoMozart()
         * {
         *  var spec = new Spectrogram.Spectrogram(sampleRate: 8000, fftSize: 2048, step: 700);
         *  float[] values = Spectrogram.Tools.ReadWav("mozart.wav");
         *  spec.AddExtend(values);
         *  int [] detectedFrec = Spectrogram.Tools.LoadDetectedFrec("data.bin", spec.displaySettings.fftResolution);
         *  spec.AddDetectedFrec(detectedFrec);
         *  Bitmap bmp = spec.GetBitmap(intensity: 2, freqHigh: 2500, showTicks: true);
         *  spec.SaveBitmap(bmp, "mozart.jpg");
         * }*/


        static void DemoKike()
        {
            string name = "440";
            int    fs = 48000, fft_size = 2048, frec_high = 2000;
            int    height = frec_high * fft_size / fs;
            double scaler = (double)frec_high / (double)height;



            float[] values_in  = Spectrogram.Tools.ReadWav(name + ".wav");
            float[] values_out = Spectrogram.Tools.ReadWav(name + "_det.wav");

            int[] detectedFrec_v2  = Spectrogram.Tools.LoadDetectedFrec(name + "_freq_det.bin", scaler);
            int[] objectiveFrec_v2 = Spectrogram.Tools.LoadDetectedFrec(name + "_freq_obj.bin", scaler);

            var spec_in_v2  = new Spectrogram.Spectrogram(sampleRate: fs, fftSize: fft_size, step: 2048);
            var spec_out_v2 = new Spectrogram.Spectrogram(sampleRate: fs, fftSize: fft_size, step: 2048);

            spec_in_v2.AddExtend(values_in);
            spec_out_v2.AddExtend(values_out);


            spec_in_v2.AddDetectedFrec(detectedFrec_v2);
            spec_out_v2.AddDetectedFrec(detectedFrec_v2);

            spec_in_v2.AddObjectiveFrec(objectiveFrec_v2);
            spec_out_v2.AddObjectiveFrec(objectiveFrec_v2);



            Bitmap bmp_in_v2  = spec_in_v2.GetBitmap(intensity: 2, freqHigh: frec_high, showTicks: true, colormap: Spectrogram.Colormap.grayscale);
            Bitmap bmp_out_v2 = spec_out_v2.GetBitmap(intensity: 2, freqHigh: frec_high, showTicks: true, colormap: Spectrogram.Colormap.grayscale);


            spec_in_v2.SaveBitmap(bmp_in_v2, name + "_v2.jpg");
            spec_out_v2.SaveBitmap(bmp_out_v2, name + "_out_v2.jpg");
        }
Пример #9
0
 public FormBrightness(Spectrogram.Spectrogram spec)
 {
     InitializeComponent();
     this.spec = spec;
 }
Пример #10
0
 public FormFreqRange(Spectrogram.Spectrogram spec)
 {
     InitializeComponent();
     this.spec = spec;
     LoadSettings();
 }
Пример #11
0
        public static void Spectrogram(
            Spectrogram.Spectrogram spec, WsprBand band, List <WsprSpot> spots,
            Bitmap bmpSpectrogram, Bitmap bmpVericalScale,
            bool drawBandLines, bool drawVerticalLine, ProgramSettings settings)
        {
            using (Graphics gfx = Graphics.FromImage(bmpSpectrogram))
                using (Bitmap bmpIndexed = spec.GetBitmapMax(settings.brightness, reduction: settings.verticalReduction, roll: settings.roll))
                    using (Pen bandEdgePen = new Pen(Color.White)
                    {
                        DashStyle = System.Drawing.Drawing2D.DashStyle.Dash
                    })
                        using (Pen grabEdgePen = new Pen(Color.Yellow)
                        {
                            DashStyle = System.Drawing.Drawing2D.DashStyle.Dash
                        })
                            using (Pen rollPen = new Pen(Color.White))
                                using (var font = new Font("consolas", 10, FontStyle.Bold))
                                    using (var sfMiddleCenter = new StringFormat {
                                        LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center
                                    })
                                        using (var sfUpperLeft = new StringFormat {
                                            LineAlignment = StringAlignment.Near, Alignment = StringAlignment.Near
                                        })
                                        {
                                            // copy source bitmaps onto this display bitmap
                                            gfx.DrawImage(bmpIndexed, 0, 0);
                                            gfx.DrawImage(bmpVericalScale, spec.Width, 0);

                                            int wsprBandTopPx    = spec.PixelY(band.upperFreq - band.dialFreq, settings.verticalReduction) + 1;
                                            int wsprBandBottomPx = spec.PixelY(band.lowerFreq - band.dialFreq, settings.verticalReduction) + 1;
                                            int qrssBandBottomPx = spec.PixelY(band.lowerFreq - band.dialFreq - 200, settings.verticalReduction) + 1;
                                            int grabTopPx        = wsprBandTopPx - settings.grabSavePxAbove;
                                            int grabBotPx        = qrssBandBottomPx + settings.grabSavePxBelow;
                                            if (drawBandLines)
                                            {
                                                gfx.DrawLine(bandEdgePen, 0, wsprBandTopPx, spec.Width, wsprBandTopPx);
                                                gfx.DrawLine(bandEdgePen, 0, wsprBandBottomPx, spec.Width, wsprBandBottomPx);
                                                gfx.DrawLine(bandEdgePen, 0, qrssBandBottomPx, spec.Width, qrssBandBottomPx);
                                                gfx.DrawLine(grabEdgePen, 0, grabTopPx - 1, spec.Width, grabTopPx - 1);
                                                gfx.DrawLine(grabEdgePen, 0, grabBotPx, spec.Width, grabBotPx);
                                            }

                                            if (settings.roll && drawVerticalLine)
                                            {
                                                gfx.DrawLine(rollPen, spec.NextColumnIndex, 0, spec.NextColumnIndex, spec.Height);
                                            }

                                            if (settings.isWsprEnabled == false)
                                            {
                                                return;
                                            }

                                            // plot spots in a single segment (the 2m block within a 10m time frame)
                                            for (int segment = 0; segment < 5; segment++)
                                            {
                                                WsprSpot[] segmentSpots = spots
                                                                          .Where(x => x.segment == segment)                                        // only this segment
                                                                          .Where(x => Math.Abs(x.frequencyHz - band.dialFreq) < 1e5)               // only this band
                                                                          .OrderBy(x => x.strength).GroupBy(x => x.callsign).Select(x => x.Last()) // only strongest
                                                                          .OrderBy(x => x.frequencyHz).Reverse().ToArray();                        // top to bottom

                                                int segmentX = spec.Width * segment / 5;
                                                if (settings.roll == false && segmentSpots.Length > 0)
                                                {
                                                    segmentX = (int)(spec.Width - segmentSpots[0].ageSec / spec.SecPerPx) + 10;
                                                }

                                                for (int spotIndex = 0; spotIndex < segmentSpots.Length; spotIndex++)
                                                {
                                                    WsprSpot spot = segmentSpots[spotIndex];

                                                    int r = 7;
                                                    int y = spec.PixelY(spot.frequencyHz - band.dialFreq, settings.verticalReduction);

                                                    // draw the marker
                                                    if (spot.isWspr)
                                                    {
                                                        int xSpot = segmentX + r * 2 * (spotIndex % 8 + 1);

                                                        // determine where to place the marker
                                                        gfx.FillEllipse(Brushes.Black, xSpot - r, y - r, r * 2, r * 2);
                                                        gfx.DrawString($"{spotIndex + 1}", font, Brushes.White, xSpot + 0.5f, y + 1, sfMiddleCenter);

                                                        // draw the key label at the top of the band edge
                                                        DrawStringWithShadow(gfx, $"{spotIndex + 1}: {spot.callsign} ({spot.strength} dB) ",
                                                                             segmentX, wsprBandTopPx - settings.grabSavePxAbove + 13 * spotIndex,
                                                                             font, sfUpperLeft, Brushes.White, Brushes.Black);
                                                    }
                                                    else
                                                    {
                                                        // draw the callsign exactly where the spot is on the spectrogram
                                                        int xSpot = (int)(segmentX + (spot.dt.Minute % 2 * 60 + spot.dt.Second) / spec.SecPerPx);
                                                        DrawStringWithShadow(gfx, spot.callsign, xSpot, y, font, sfUpperLeft, Brushes.White, Brushes.Black);
                                                    }
                                                }
                                            }
                                        }
        }
Пример #12
0
 public FormFFT(Spectrogram.Spectrogram spec)
 {
     InitializeComponent();
     this.spec = spec;
 }