Skip to content

Commit 692ea3b

Browse files
committed
support roll display
1 parent ed99af8 commit 692ea3b

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

src/Spectrogram.MicrophoneDemo/Form1.Designer.cs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Spectrogram.MicrophoneDemo/Form1.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,26 @@ private void timer1_Tick(object sender, EventArgs e)
7272
Stopwatch sw = Stopwatch.StartNew();
7373
spec.Process();
7474
spec.TrimWidth(pictureBox1.Width);
75-
Bitmap bmp = spec.GetBitmap(multiplier, cbDecibels.Checked);
75+
Bitmap bmp = spec.GetBitmap(multiplier, cbDecibels.Checked, cbRoll.Checked);
76+
Bitmap bmp2 = new Bitmap(spec.Width, spec.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
77+
using (var gfx = Graphics.FromImage(bmp2))
78+
using (var pen = new Pen(Color.White))
79+
{
80+
gfx.DrawImage(bmp, 0, 0);
81+
if (cbRoll.Checked)
82+
{
83+
int x = spec.FftsProcessed % pictureBox1.Width - 1;
84+
gfx.DrawLine(pen, x, 0, x, pictureBox1.Height);
85+
}
86+
}
87+
bmp.Dispose();
88+
7689
sw.Stop();
7790
pictureBox1.Image?.Dispose();
78-
pictureBox1.Image = bmp;
91+
pictureBox1.Image = bmp2;
7992
lblStatus3.Text = $"Render time: {sw.ElapsedMilliseconds:D2} ms";
8093
}
81-
94+
8295
lblStatus1.Text = $"Time: {listener.TotalTimeSec:N3} sec";
8396
lblStatus2.Text = $"FFTs processed: {spec.FftsProcessed:N0}";
8497
pbAmplitude.Value = (int)(listener.AmplitudeFrac * pbAmplitude.Maximum);

src/Spectrogram/Spectrogram.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,16 @@ public void Process()
7878
newFfts[newFftIndex] = new double[settings.Height];
7979
for (int i = 0; i < settings.Height; i++)
8080
newFfts[newFftIndex][i] = buffer[settings.FftIndex1 + i].Magnitude / settings.FftSize;
81-
82-
FftsProcessed += 1;
8381
});
8482

8583
foreach (var newFft in newFfts)
8684
ffts.Add(newFft);
85+
FftsProcessed += newFfts.Length;
8786

8887
newAudio.RemoveRange(0, newFftCount * settings.StepSize);
8988
}
9089

91-
public Bitmap GetBitmap(double multiplier = 1, bool dB = false)
90+
public Bitmap GetBitmap(double multiplier = 1, bool dB = false, bool roll = false)
9291
{
9392
if (Width == 0)
9493
return null;
@@ -103,9 +102,17 @@ public Bitmap GetBitmap(double multiplier = 1, bool dB = false)
103102
byte[] bytes = new byte[bitmapData.Stride * bmp.Height];
104103
for (int col = 0; col < Width; col++)
105104
{
105+
int sourceCol = col;
106+
if (roll)
107+
{
108+
sourceCol += Width - FftsProcessed % Width;
109+
if (sourceCol >= Width)
110+
sourceCol -= Width;
111+
}
112+
106113
for (int row = 0; row < Height; row++)
107114
{
108-
double value = ffts[col][row];
115+
double value = ffts[sourceCol][row];
109116
if (dB)
110117
value = 20 * Math.Log10(value + 1);
111118
value *= multiplier;

0 commit comments

Comments
 (0)