private void timer1_Tick(object sender, EventArgs e) { double[] newAudio = listener.GetNewAudio(); spec.Add(newAudio, process: false); double multiplier = tbBrightness.Value / 20.0; if (spec.FftsToProcess > 0) { Stopwatch sw = Stopwatch.StartNew(); spec.Process(); spec.SetFixedWidth(pbSpectrogram.Width); Bitmap bmpSpec = new Bitmap(spec.Width, spec.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); using (var bmpSpecIndexed = spec.GetBitmap(multiplier, cbDecibels.Checked, cbRoll.Checked)) using (var gfx = Graphics.FromImage(bmpSpec)) using (var pen = new Pen(Color.White)) { gfx.DrawImage(bmpSpecIndexed, 0, 0); if (cbRoll.Checked) { gfx.DrawLine(pen, spec.NextColumnIndex, 0, spec.NextColumnIndex, pbSpectrogram.Height); } } sw.Stop(); pbSpectrogram.Image?.Dispose(); pbSpectrogram.Image = bmpSpec; lblStatus3.Text = $"Render time: {sw.ElapsedMilliseconds:D2} ms"; lblStatus4.Text = $"Peak (Hz): {spec.GetPeak().freqHz:N0}"; } lblStatus1.Text = $"Time: {listener.TotalTimeSec:N3} sec"; lblStatus2.Text = $"FFTs processed: {spec.FftsProcessed:N0}"; pbAmplitude.Value = (int)(listener.AmplitudeFrac * pbAmplitude.Maximum); }
public void Test_SFF_Linear2() { // test creating SFF file from 16-bit 48kHz mono WAV file // read the wav file (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/03-02-03-01-02-01-19.wav"); Assert.AreEqual(48000, sampleRate); // save the SFF int fftSize = 1 << 12; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 300, maxFreq: 2000); spec.Add(audio); spec.SaveData("testDoor.sff"); // load the SFF and verify all the values are the same var spec2 = new SFF("testDoor.sff"); Assert.AreEqual(spec.SampleRate, spec2.SampleRate); Assert.AreEqual(spec.StepSize, spec2.StepSize); Assert.AreEqual(spec.Width, spec2.Width); Assert.AreEqual(spec.FftSize, spec2.FftSize); Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex); Assert.AreEqual(spec.Height, spec2.Height); Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz); Assert.AreEqual("SFF 701x170", spec2.ToString()); }
public void Test_Quickstart_Hal() { (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav"); int fftSize = 4096; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 500, maxFreq: 3000); spec.Add(audio); spec.SaveImage("../../../../../dev/graphics/hal.png", intensity: .2); Console.WriteLine(spec); }
public void Test_AGC_off() { string wavFilePath = "../../../../../data/qrss-10min.wav"; (int sampleRate, double[] L) = WavFile.ReadMono(wavFilePath); int fftSize = 8192; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 2000, maxFreq: 3000); spec.Add(L); spec.SaveImage("qrss-agc-off.png", intensity: 3); }
public void Test_Mel_Spectrogram() { (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav"); int fftSize = 4096; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 500); spec.Add(audio); spec.SaveImage("halNotMel.png", 4, true); Bitmap bmp = spec.GetBitmapMel(250, 4, true); bmp.Save("../../../../../dev/graphics/halMel.png", ImageFormat.Png); }
public void Test_SFF_LinearBigMaxFreq() { // test creating SFF file from 16-bit 48kHz mono WAV file (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/03-02-03-01-02-01-19.wav"); Assert.AreEqual(48000, sampleRate); int fftSize = 1 << 12; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 300, maxFreq: 7999); spec.Add(audio); spec.SaveData("testDoorBig.sff"); }
public void Test_SFF_Linear() { (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav"); int fftSize = 1 << 12; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 700, maxFreq: 2000); spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical spec.Add(audio); spec.SaveData("../../../../../dev/sff/hal.sff"); var spec2 = new SFF("../../../../../dev/sff/hal.sff"); Assert.AreEqual(spec.SampleRate, spec2.SampleRate); Assert.AreEqual(spec.StepSize, spec2.StepSize); Assert.AreEqual(spec.Width, spec2.Width); Assert.AreEqual(spec.FftSize, spec2.FftSize); Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex); Assert.AreEqual(spec.Height, spec2.Height); Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz); }
public void Test_AGC_normWindow() { // strategy here is to create a weighted moving window mean and normalize to that string wavFilePath = "../../../../../data/qrss-10min.wav"; (int sampleRate, double[] L) = WavFile.ReadMono(wavFilePath); int fftSize = 8192; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 2000, maxFreq: 3000); spec.Add(L); var ffts = spec.GetFFTs(); for (int i = 0; i < ffts.Count; i++) { ffts[i] = SubtractMovingWindowFloor(ffts[i]); } spec.SaveImage("qrss-agc-norm-window.png", intensity: 3); }
public void Test_Make_CommonColormaps() { (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav"); int fftSize = 1 << 12; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 700, maxFreq: 2000); spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical spec.Add(audio); // delete old colormap files foreach (var filePath in System.IO.Directory.GetFiles("../../../../../dev/graphics/", "hal-*.png")) { System.IO.File.Delete(filePath); } foreach (var cmap in Colormap.GetColormaps()) { spec.SetColormap(cmap); spec.SaveImage($"../../../../../dev/graphics/hal-{cmap.Name}.png", intensity: .5); Debug.WriteLine($"![](dev/graphics/hal-{cmap.Name}.png)"); } }
public void Test_Quickstart_Handel() { double[] audio = Mp3.Read("../../../../../data/Handel - Air and Variations.mp3"); int sampleRate = 44100; int fftSize = 16384; int targetWidthPx = 3000; int stepSize = audio.Length / targetWidthPx; var spec = new Spectrogram(sampleRate, fftSize, stepSize, maxFreq: 2200); spec.Add(audio); spec.SaveImage("../../../../../dev/spectrogram-song.jpg", intensity: 5, dB: true); Console.WriteLine(spec); /* * Spectrogram (2993, 817) * Vertical (817 px): 0 - 2,199 Hz, FFT size: 16,384 samples, 2.69 Hz/px * Horizontal (2993 px): 2.96 min, window: 0.37 sec, step: 0.06 sec, overlap: 84% */ }
public void Test_AGC_normToNoiseFloor() { // strategy here is to normalize to the magnitude of the quietest 20% of frequencies string wavFilePath = "../../../../../data/qrss-10min.wav"; (int sampleRate, double[] L) = WavFile.ReadMono(wavFilePath); int fftSize = 8192; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 2000, maxFreq: 3000); spec.Add(L); var ffts = spec.GetFFTs(); double normalIntensity = 2; for (int i = 0; i < ffts.Count; i++) { double[] sorted = new double[ffts[i].Length]; ffts[i].CopyTo(sorted, 0); Array.Sort(sorted); double percentile = 0.25; int percentileIndex = (int)(percentile * ffts[0].Length); double floorValue = sorted[percentileIndex]; for (int y = 0; y < ffts[i].Length; y++) { ffts[i][y] = ffts[i][y] / floorValue * normalIntensity; } Console.WriteLine(floorValue); } spec.SaveImage("qrss-agc-norm-floor.png", intensity: 3); }
public void Test_SFF_Mel() { (int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav"); int fftSize = 1 << 12; var spec = new Spectrogram(sampleRate, fftSize, stepSize: 700); spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical spec.Add(audio); Bitmap bmp = spec.GetBitmapMel(250, 3, true); bmp.Save("../../../../../dev/sff/halMel.png", System.Drawing.Imaging.ImageFormat.Png); spec.SaveData("../../../../../dev/sff/halMel.sff", melBinCount: 250); var spec2 = new SFF("../../../../../dev/sff/halMel.sff"); Assert.AreEqual(spec.SampleRate, spec2.SampleRate); Assert.AreEqual(spec.StepSize, spec2.StepSize); Assert.AreEqual(spec.Width, spec2.Width); Assert.AreEqual(spec.FftSize, spec2.FftSize); Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex); Assert.AreEqual(spec.Height, spec2.Height); Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz); }