-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
288 lines (232 loc) · 10.6 KB
/
Plugin.cs
File metadata and controls
288 lines (232 loc) · 10.6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using NAudio.Wave;
using NWaves.Transforms;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using ScottPlot;
using ScottPlot.Interactivity.UserActionResponses;
using ScottPlot.TickGenerators;
using ScottPlot.WPF;
namespace QuickLook.Plugin.WavLabelViewer
{
public class Plugin : IViewer
{
public int Priority => 0;
private string DllLocation;
public bool IsSettingsUpdated = false;
public void Init()
{
this.DllLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += AssemblyResolve;
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
}
public bool CanHandle(string path)
{
if (!Directory.Exists(path))
{
var ext = Path.GetExtension(path).ToLower();
return ext == ".lab";
}
return false;
}
public void Prepare(string path, ContextObject context)
{
var max = WindowHelper.GetCurrentDesktopSize();
context.PreferredSize = new Size(max.Width * 0.75, max.Height * 0.75);
}
public void View(string path, ContextObject context)
{
if (!this.IsSettingsUpdated)
{
Settings.Update();
this.IsSettingsUpdated = true;
}
var settings = Settings.Load();
var wavpath = Path.ChangeExtension(path, ".wav");
var labpath = Path.ChangeExtension(path, ".lab");
bool isWavExists = File.Exists(wavpath);
var labels = LabelLine.LoadFrom(labpath);
var viewer = new WpfPlot();
var plot = viewer.Plot;
double duration = labels[labels.Length - 1].End;
viewer.Menu.AddSeparator();
viewer.UserInputProcessor.RemoveAll<DoubleClickBenchmark>();
//viewer.UserInputProcessor.RemoveAll<MouseDragPan>();
//viewer.UserInputProcessor.RemoveAll<MouseDragZoom>();
//viewer.UserInputProcessor.UserActionResponses.Add(new MouseDragPan(StandardMouseButtons.Right));
viewer.UserInputProcessor.Enable();
static string SampleCountToTimeTickFormatter(double position)
{
if (position == 0)
return "0s";
else
return $"{position:f2}s";
}
plot.Axes.Left.RemoveTickGenerator();
plot.Axes.Bottom.TickGenerator = new NumericAutomatic() { LabelFormatter = SampleCountToTimeTickFormatter };
//plot.Add.Palette = new ScottPlot.Palettes.Penumbra();
if (OSThemeHelper.AppsUseDarkTheme())
{
// change figure colors
plot.FigureBackground.Color = Color.FromHex("#181818");
plot.DataBackground.Color = Color.FromHex("#1f1f1f");
// change axis and grid colors
plot.Axes.Color(Color.FromHex("#d7d7d7"));
plot.Grid.MajorLineColor = Color.FromHex("#404040");
// change legend colors
plot.Legend.BackgroundColor = Color.FromHex("#404040");
plot.Legend.FontColor = Color.FromHex("#d7d7d7");
plot.Legend.OutlineColor = Color.FromHex("#d7d7d7");
plot.Grid.YAxisStyle.MajorLineStyle.IsVisible = false;
plot.Grid.XAxisStyle.MajorLineStyle.Color = Color.FromHex("#545a6a");
}
if (isWavExists)
{
using var audio = new WaveFileReader(wavpath);
var reader = audio.ToSampleProvider().ToMono();
var sampleCount = audio.Length / audio.WaveFormat.BlockAlign;
float[] samples = new float[sampleCount];
var wf = audio.WaveFormat;
var wavDuration = sampleCount / (double)wf.SampleRate;
if (duration < wavDuration)
duration = wavDuration;
float[] buffer = new float[4096];
int samplesIndex = 0;
int samplesRead = 0;
do
{
samplesRead = reader.Read(buffer, 0, buffer.Length);
unsafe
{
fixed (float* floatPtr = buffer)
fixed (float* shortPtr = samples)
{
for (int i = 0; i < samplesRead; i++)
{
*(shortPtr + samplesIndex + i) = *(floatPtr + i);
}
}
}
samplesIndex += samplesRead;
} while (samplesRead > 0);
var fftSize = settings.FFTSize;
var hopSize = settings.HopSize;
var windowType = settings.WindowType;
int frameSize = fftSize;
int fftBins = fftSize / 2 + 1;
int frameCount = (samples.Length - frameSize) / hopSize + 1;
var spectrogram = new double[fftBins, frameCount];
var window = NWaves.Windows.Window.OfType(windowType, frameSize);
Parallel.For(0, frameCount, frameIndex => {
var fft = new Fft(fftSize);
var re = new float[fftSize];
var im = new float[fftSize];
int pos = frameIndex * hopSize;
for (int n = 0; n < frameSize; n++)
re[n] = samples[pos + n] * window[n];
fft.Direct(re, im);
for (int k = 0; k < fftBins; k++)
{
var mag = Math.Sqrt(re[k] * re[k] + im[k] * im[k]);
spectrogram[(fftBins - 1) - k, frameIndex] = 20 * Math.Log10(mag + 1e-10f);
}
});
double freq = (fftBins - 1) * ((wf.SampleRate / 2) / (fftBins - 1));
var logYMax = Math.Log10(freq + 1);
var heatmapAxis = plot.Axes.AddLeftAxis();
heatmapAxis.RemoveTickGenerator();
LogMinorTickGenerator minorTickGen = new();
NumericAutomatic tickGen = new() {
MinorTickGenerator = minorTickGen
};
heatmapAxis.TickGenerator = tickGen;
heatmapAxis.IsVisible = false;
var heatmapPlot = plot.Add.Heatmap(spectrogram);
heatmapPlot.IsVisible = settings.ShowSpectrogram;
heatmapPlot.Axes.YAxis = heatmapAxis;
heatmapPlot.Extent = new CoordinateRect(0, duration, 0, logYMax);
heatmapPlot.Colormap = new ScottPlot.Colormaps.Inferno();
heatmapPlot.Update();
if (settings.UseAxesLock)
{
plot.Axes.Rules.Add(new PlotAxisLimitRule(new AxisLimits(0.0, duration, 0, logYMax), heatmapAxis));
plot.Axes.Rules.Add(new PlotAxisFixedLimitRule(new CoordinateRange(0, logYMax), heatmapAxis));
}
var signalPlot = plot.Add.SignalConst(samples, 1.0 / wf.SampleRate);
signalPlot.IsVisible = settings.ShowWaveform;
signalPlot.Color = Colors.Blue.WithAlpha(0.5);
viewer.Menu.Add("Toggle Waveform", (p) => {
settings.ShowWaveform = !settings.ShowWaveform;
signalPlot.IsVisible = settings.ShowWaveform;
viewer.Refresh();
settings.Save();
});
viewer.Menu.Add("Toggle Spectrogram", (p) => {
settings.ShowSpectrogram = !settings.ShowSpectrogram;
heatmapPlot.IsVisible = settings.ShowSpectrogram;
viewer.Refresh();
settings.Save();
});
}
if (settings.UseAxesLock)
{
plot.Axes.Rules.Add(new PlotAxisLimitRule(new AxisLimits(0.0, duration, -1f, 1f), plot.Axes.Left));
plot.Axes.Rules.Add(new PlotAxisFixedLimitRule(new CoordinateRange(-1f, 1f), plot.Axes.Left));
}
var labelSpans = new List<LabelSpan>();
var labelSpanLineStyleColor = Colors.Red;
var labelSpanFillStyleColor = Colors.Transparent;
//var labelSpanFillStyleColor = labelSpanLineStyleColor.WithAlpha(0.15);
foreach (var label in labels)
{
LabelSpan labelSpan = new(label) {
IsVisible = settings.ShowLabels
};
labelSpan.LineStyle.Color = labelSpanLineStyleColor;
labelSpan.FillStyle.Color = labelSpanFillStyleColor;
plot.PlottableList.Add(labelSpan);
labelSpans.Add(labelSpan);
}
viewer.Menu.Add("Toggle Label", (p) => {
settings.ShowLabels = !settings.ShowLabels;
foreach (var labelSpan in labelSpans)
labelSpan.IsVisible = settings.ShowLabels;
viewer.Refresh();
settings.Save();
});
viewer.Refresh();
context.ViewerContent = viewer;
context.Title = Path.GetFileName(path);
context.IsBusy = false;
}
public void Cleanup()
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= AssemblyResolve;
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolve;
}
private Assembly AssemblyResolve(object sender, ResolveEventArgs e)
{
var assemblyPath = Path.Combine(this.DllLocation, new AssemblyName(e.Name).Name + ".dll");
if (File.Exists(assemblyPath))
return Assembly.LoadFrom(assemblyPath);
else if (Environment.Is64BitProcess)
{
assemblyPath = Path.Combine(this.DllLocation, "x64", new AssemblyName(e.Name).Name + ".dll");
if (File.Exists(assemblyPath))
return Assembly.LoadFrom(assemblyPath);
}
else
{
assemblyPath = Path.Combine(this.DllLocation, "x86", new AssemblyName(e.Name).Name + ".dll");
if (File.Exists(assemblyPath))
return Assembly.LoadFrom(assemblyPath);
}
return null;
}
}
}