|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Drawing; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Forms; |
| 11 | + |
| 12 | +namespace Spectrogram.MicrophoneDemo |
| 13 | +{ |
| 14 | + public partial class FormQrssTest : Form |
| 15 | + { |
| 16 | + public FormQrssTest() |
| 17 | + { |
| 18 | + InitializeComponent(); |
| 19 | + |
| 20 | + if (NAudio.Wave.WaveIn.DeviceCount == 0) |
| 21 | + { |
| 22 | + MessageBox.Show("No audio input devices found.\n\nThis program will now exit.", |
| 23 | + "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 24 | + Close(); |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + cbDevice.Items.Clear(); |
| 29 | + for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++) |
| 30 | + cbDevice.Items.Add(NAudio.Wave.WaveIn.GetCapabilities(i).ProductName); |
| 31 | + cbDevice.SelectedIndex = 0; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private void FormQrssTest_Load(object sender, EventArgs e) { } |
| 36 | + |
| 37 | + private void cbDevice_SelectedIndexChanged(object sender, EventArgs e) => StartListening(); |
| 38 | + |
| 39 | + private Spectrogram spec; |
| 40 | + private Listener listener; |
| 41 | + private void StartListening() |
| 42 | + { |
| 43 | + int sampleRate = 6000; |
| 44 | + int fftSize = 1 << 15; |
| 45 | + int stepSize = fftSize / 20; |
| 46 | + |
| 47 | + pbSpectrogram.Image?.Dispose(); |
| 48 | + pbSpectrogram.Image = null; |
| 49 | + listener?.Dispose(); |
| 50 | + listener = new Listener(cbDevice.SelectedIndex, sampleRate); |
| 51 | + spec = new Spectrogram(sampleRate, fftSize, stepSize); |
| 52 | + pbSpectrogram.Height = spec.Height; |
| 53 | + |
| 54 | + pbScaleVert.Image?.Dispose(); |
| 55 | + pbScaleVert.Image = spec.GetVerticalScale(pbScaleVert.Width); |
| 56 | + pbScaleVert.Height = spec.Height; |
| 57 | + |
| 58 | + timer1.Enabled = true; |
| 59 | + } |
| 60 | + |
| 61 | + private void timer1_Tick(object sender, EventArgs e) |
| 62 | + { |
| 63 | + double[] newAudio = listener.GetNewAudio(); |
| 64 | + spec.Add(newAudio); |
| 65 | + |
| 66 | + if (spec.FftsToProcess > 0) |
| 67 | + { |
| 68 | + Stopwatch sw = Stopwatch.StartNew(); |
| 69 | + spec.Process(); |
| 70 | + spec.MakeWidth(pbSpectrogram.Width); |
| 71 | + |
| 72 | + Bitmap bmpSpec = new Bitmap(spec.Width, spec.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); |
| 73 | + using (var bmpSpecIndexed = spec.GetBitmap((double)nudBrightness.Value, false, true)) |
| 74 | + using (var gfx = Graphics.FromImage(bmpSpec)) |
| 75 | + using (var pen = new Pen(Color.White)) |
| 76 | + { |
| 77 | + gfx.DrawImage(bmpSpecIndexed, 0, 0); |
| 78 | + int x = spec.FftsProcessed % pbSpectrogram.Width - 1; |
| 79 | + gfx.DrawLine(pen, x, 0, x, pbSpectrogram.Height); |
| 80 | + } |
| 81 | + |
| 82 | + sw.Stop(); |
| 83 | + pbSpectrogram.Image?.Dispose(); |
| 84 | + pbSpectrogram.Image = bmpSpec; |
| 85 | + Debug.WriteLine($"Render time: {sw.ElapsedMilliseconds:D2} ms"); |
| 86 | + } |
| 87 | + |
| 88 | + pnlAmpInner.Width = (int)(listener.AmplitudeFrac * pnlAmpOuter.Width); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments