Skip to content

Commit ed99af8

Browse files
committed
add dB support
1 parent 66ec39e commit ed99af8

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

src/Spectrogram.MicrophoneDemo/Form1.Designer.cs

Lines changed: 40 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ private void timer1_Tick(object sender, EventArgs e)
6565
double[] newAudio = listener.GetNewAudio();
6666
spec.Add(newAudio);
6767

68+
double multiplier = tbBrightness.Value / 20.0;
69+
6870
if (spec.FftsToProcess > 0)
6971
{
7072
Stopwatch sw = Stopwatch.StartNew();
7173
spec.Process();
7274
spec.TrimWidth(pictureBox1.Width);
73-
Bitmap bmp = spec.GetBitmap();
75+
Bitmap bmp = spec.GetBitmap(multiplier, cbDecibels.Checked);
7476
sw.Stop();
7577
pictureBox1.Image?.Dispose();
7678
pictureBox1.Image = bmp;

src/Spectrogram.MicrophoneDemo/Listener.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,24 @@ private void OnNewAudioData(object sender, NAudio.Wave.WaveInEventArgs args)
4646
buffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample);
4747
peak = Math.Max(peak, buffer[i]);
4848
}
49-
audio.AddRange(buffer);
49+
lock (audio)
50+
{
51+
audio.AddRange(buffer);
52+
}
5053
AmplitudeFrac = peak / (1 << 15);
5154
TotalSamples += newSampleCount;
5255
}
5356

5457
public double[] GetNewAudio()
5558
{
56-
double[] values = new double[audio.Count];
57-
for (int i = 0; i < values.Length; i++)
58-
values[i] = audio[i];
59-
audio.RemoveRange(0, values.Length);
60-
return values;
59+
lock (audio)
60+
{
61+
double[] values = new double[audio.Count];
62+
for (int i = 0; i < values.Length; i++)
63+
values[i] = audio[i];
64+
audio.RemoveRange(0, values.Length);
65+
return values;
66+
}
6167
}
6268
}
6369
}

src/Spectrogram/Spectrogram.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void Process()
8888
newAudio.RemoveRange(0, newFftCount * settings.StepSize);
8989
}
9090

91-
public Bitmap GetBitmap(double multiplier = 1)
91+
public Bitmap GetBitmap(double multiplier = 1, bool dB = false)
9292
{
9393
if (Width == 0)
9494
return null;
@@ -105,7 +105,10 @@ public Bitmap GetBitmap(double multiplier = 1)
105105
{
106106
for (int row = 0; row < Height; row++)
107107
{
108-
double value = ffts[col][row] * multiplier;
108+
double value = ffts[col][row];
109+
if (dB)
110+
value = 20 * Math.Log10(value + 1);
111+
value *= multiplier;
109112
value = Math.Min(value, 255);
110113
int bytePosition = (Height - 1 - row) * stride + col;
111114
bytes[bytePosition] = (byte)value;

0 commit comments

Comments
 (0)