forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScale.cs
More file actions
60 lines (52 loc) · 2.01 KB
/
Scale.cs
File metadata and controls
60 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
namespace Spectrogram
{
static class Scale
{
public static Bitmap Vertical(int width, Settings settings, int offsetHz = 0, int tickSize = 3, int reduction = 1)
{
double tickHz = 1;
int minSpacingPx = 50;
double[] multipliers = { 2, 2.5, 2 };
int multiplier = 0;
while (true)
{
tickHz *= multipliers[multiplier++ % multipliers.Length];
double tickCount = settings.FreqSpan / tickHz;
double pxBetweenTicks = settings.Height / tickCount;
if (pxBetweenTicks >= minSpacingPx * reduction)
break;
}
Bitmap bmp = new Bitmap(width, settings.Height / reduction, PixelFormat.Format32bppPArgb);
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> freqs = new List<double>();
for (double f = settings.FreqMin; f <= settings.FreqMax; f += tickHz)
freqs.Add(f);
// don't show first or last tick
if (freqs.Count >= 2)
{
freqs.RemoveAt(0);
freqs.RemoveAt(freqs.Count - 1);
}
foreach (var freq in freqs)
{
int y = settings.PixelY(freq) / reduction;
gfx.DrawLine(pen, 0, y, tickSize, y);
gfx.DrawString($"{freq + offsetHz:N0} Hz", font, brush, tickSize, y, sf);
}
}
return bmp;
}
}
}