forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScale.cs
More file actions
58 lines (48 loc) · 1.7 KB
/
Scale.cs
File metadata and controls
58 lines (48 loc) · 1.7 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
using SkiaSharp;
using System.Collections.Generic;
namespace Spectrogram;
static class Scale
{
public static SKBitmap 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;
}
var imageInfo = new SKImageInfo(width, settings.Height / reduction, SKColorType.Rgba8888);
var bitmap = new SKBitmap(imageInfo);
using var canvas = new SKCanvas(bitmap);
canvas.Clear(SKColors.White);
var paint = new SKPaint
{
Color = SKColors.Black,
TextSize = 10,
IsAntialias = true,
Typeface = SKTypeface.FromFamilyName("Monospace")
};
List<double> freqs = new List<double>();
for (double f = settings.FreqMin; f <= settings.FreqMax; f += tickHz)
freqs.Add(f);
if (freqs.Count >= 2)
{
freqs.RemoveAt(0);
freqs.RemoveAt(freqs.Count - 1);
}
foreach (var freq in freqs)
{
int y = settings.PixelY(freq) / reduction;
canvas.DrawLine(0, y, tickSize, y, paint);
var text = $"{freq + offsetHz:N0} Hz";
canvas.DrawText(text, tickSize + 2, y + 5, paint);
}
return bitmap;
}
}