HamBook – Diff between revs 53 and 54

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 53 Rev 54
Line 1... Line -...
1 using Configuration; -  
2 using FftSharp; -  
3 using HamBook.Utilities; -  
4 using NAudio.Utils; -  
5 using NAudio.Wave; -  
6 using Org.BouncyCastle.Math.EC.Multiplier; -  
7 using Spectrogram; -  
8 using System; 1 using System;
9 using System.Collections.Generic; -  
10 using System.ComponentModel; -  
11 using System.Data; -  
12 using System.Drawing; 2 using System.Drawing;
13 using System.IO; 3 using System.Drawing.Imaging;
14 using System.Linq; 4 using System.Linq;
15 using System.Runtime.InteropServices; 5 using System.Runtime.InteropServices;
16 using System.Text; -  
17 using System.Threading; 6 using System.Threading;
18 using System.Threading.Tasks; -  
19 using System.Windows.Forms; 7 using System.Windows.Forms;
20 using static System.Windows.Forms.VisualStyles.VisualStyleElement; 8 using HamBook.Utilities;
-   9 using NAudio.Wave;
-   10 using Spectrogram;
Line 21... Line 11...
21   11  
22 namespace HamBook 12 namespace HamBook
23 { 13 {
24 public partial class SpectrogramForm : Form 14 public partial class SpectrogramForm : Form
25 { -  
26 #region Natives -  
27 [DllImport("user32.dll")] -  
28 private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); -  
29   15 {
30 private static readonly IntPtr HWND_BOTTOM = new IntPtr(1); 16 private readonly CancellationToken _renderCancellationToken;
31 private static readonly IntPtr HWND_TOP = new IntPtr(0); -  
32 private const UInt32 SWP_NOSIZE = 0x0001; -  
33 private const UInt32 SWP_NOMOVE = 0x0002; 17 private readonly CancellationTokenSource _renderCancellationTokenSource;
34 private const UInt32 SWP_NOACTIVATE = 0x0010; -  
35   -  
36 #endregion -  
37   18 private readonly SpectrogramGenerator _spectrogramGenerator;
38 private Configuration.Configuration Configuration { get; set; } 19 private readonly ScheduledContinuation _windowZOrderScheduledContinuation;
39 private CancellationToken _cancellationToken; -  
40 SpectrogramGenerator _spectrogramGenerator; -  
41 private WaveIn _waveIn; -  
42 private CancellationTokenSource _renderCancellationTokenSource; -  
43 private CancellationToken _renderCancellationToken; -  
44 private ScheduledContinuation _windowZOrderScheduledContinuation; -  
45 private volatile bool _mouseDown; 20 private CancellationToken _cancellationToken;
-   21 private Point _formLocation;
-   22 private volatile bool _mouseDown;
Line 46... Line 23...
46 private Point _formLocation; 23 private WaveIn _waveIn;
47   24  
48 public SpectrogramForm() 25 public SpectrogramForm()
49 { 26 {
Line 50... Line 27...
50 InitializeComponent(); 27 InitializeComponent();
51 Utilities.WindowState.FormTracker.Track(this); 28 Utilities.WindowState.FormTracker.Track(this);
52   29  
53 _renderCancellationTokenSource = new CancellationTokenSource(); -  
54 _renderCancellationToken = _renderCancellationTokenSource.Token; 30 _renderCancellationTokenSource = new CancellationTokenSource();
Line 55... Line 31...
55 _windowZOrderScheduledContinuation = new ScheduledContinuation(); 31 _renderCancellationToken = _renderCancellationTokenSource.Token;
56 32 _windowZOrderScheduledContinuation = new ScheduledContinuation();
57 } 33 }
58   34  
Line 59... Line 35...
59 public SpectrogramForm(Configuration.Configuration configuration, CancellationToken cancellationToken) : this() 35 public SpectrogramForm(Configuration.Configuration configuration, CancellationToken cancellationToken) : this()
-   36 {
-   37 Configuration = configuration;
-   38 _cancellationToken = cancellationToken;
60 { 39  
Line 61... Line 40...
61 Configuration = configuration; 40 _spectrogramGenerator = new SpectrogramGenerator(Configuration.Visualisations.Spectrogram.SampleRate,
62 _cancellationToken = cancellationToken; 41 Configuration.Visualisations.Spectrogram.FftSamples,
Line -... Line 42...
-   42 Configuration.Visualisations.Spectrogram.FftSamples /
-   43 Configuration.Visualisations.Spectrogram.AudioBufferTimespan);
63   44 _spectrogramGenerator.Colormap = Colormap.Viridis;
64 _spectrogramGenerator = new SpectrogramGenerator(Configuration.Visualisations.Spectrogram.SampleRate, fftSize: Configuration.Visualisations.Spectrogram.FFTSamples, stepSize: Configuration.Visualisations.Spectrogram.FFTSamples / Configuration.Visualisations.Spectrogram.AudioBufferTimespan); 45  
65 _spectrogramGenerator.Colormap = Colormap.Viridis; 46 pictureBox2.Image = _spectrogramGenerator.GetVerticalScale(pictureBox2.Width);
66   47 }
67 pictureBox2.Image = _spectrogramGenerator.GetVerticalScale(pictureBox2.Width); 48  
68 } 49 private Configuration.Configuration Configuration { get; }
69   50  
70 /// <summary> 51 /// <summary>
71 /// Clean up any resources being used. 52 /// Clean up any resources being used.
72 /// </summary> 53 /// </summary>
73 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> -  
74 protected override void Dispose(bool disposing) -  
75 { 54 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
76 if (disposing && (components != null)) -  
Line 77... Line 55...
77 { 55 protected override void Dispose(bool disposing)
78 if(_waveIn != null) 56 {
79 { 57 if (disposing && components != null)
80 if (_renderCancellationTokenSource != null) 58 {
81 { 59 if (_waveIn != null)
Line 82... Line 60...
82 _renderCancellationTokenSource.Cancel(); 60 {
83 } 61 if (_renderCancellationTokenSource != null) _renderCancellationTokenSource.Cancel();
-   62  
84   63 _waveIn.DataAvailable -= _waveIn_DataAvailable;
85 _waveIn.DataAvailable -= _waveIn_DataAvailable; 64 _waveIn.StopRecording();
Line 86... Line 65...
86 _waveIn.StopRecording(); 65 _waveIn.Dispose();
87 _waveIn.Dispose(); 66 _waveIn = null;
88 _waveIn = null; 67 }
Line 89... Line 68...
89 } 68  
90   -  
91 components.Dispose(); 69 components.Dispose();
92 } 70 }
93 base.Dispose(disposing); -  
94 } 71  
-   72 base.Dispose(disposing);
-   73 }
95   74  
-   75 private void SpectrogramForm_Load(object sender, EventArgs e)
96 private void SpectrogramForm_Load(object sender, EventArgs e) 76 {
97 { 77 pinToDesktopToolStripMenuItem.Checked = Configuration.Visualisations.Spectrogram.PinToDesktop;
98 pinToDesktopToolStripMenuItem.Checked = Configuration.Visualisations.Spectrogram.PinToDesktop; -  
Line 99... Line 78...
99   78  
100 if (Configuration.Visualisations.Spectrogram.PinToDesktop) 79 if (Configuration.Visualisations.Spectrogram.PinToDesktop)
101 { 80 _windowZOrderScheduledContinuation.Schedule(TimeSpan.FromSeconds(1),
102 _windowZOrderScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () => 81 () =>
103 { -  
104 this.InvokeIfRequired(form => -  
105 { -  
Line 106... Line 82...
106 SetWindowPos(Handle, HWND_BOTTOM, 0, 0, Location.X, Location.Y, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); 82 {
107 }); 83 this.InvokeIfRequired(form =>
108 }, _renderCancellationToken); 84 {
Line 126... Line 102...
126 } 102 }
127 } 103 }
Line 128... Line 104...
128   104  
129 private void _waveIn_DataAvailable(object sender, WaveInEventArgs e) 105 private void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
130 { 106 {
131 int bytesPerSample = _waveIn.WaveFormat.BitsPerSample / 8; 107 var bytesPerSample = _waveIn.WaveFormat.BitsPerSample / 8;
132 int newSampleCount = e.BytesRecorded / bytesPerSample; 108 var newSampleCount = e.BytesRecorded / bytesPerSample;
133 double[] buffer = new double[newSampleCount]; 109 var buffer = new double[newSampleCount];
134 double peak = 0; 110 double peak = 0;
135 for (int i = 0; i < newSampleCount; i++) 111 for (var i = 0; i < newSampleCount; i++)
136 { 112 {
137 buffer[i] = BitConverter.ToInt16(e.Buffer, i * bytesPerSample); 113 buffer[i] = BitConverter.ToInt16(e.Buffer, i * bytesPerSample);
138 peak = Math.Max(peak, buffer[i]); 114 peak = Math.Max(peak, buffer[i]);
Line 139... Line 115...
139 } 115 }
Line 140... Line 116...
140   116  
141 var AmplitudeFrac = peak / (1 << 15); 117 var amplitudeFrac = peak / (1 << 15);
Line 142... Line 118...
142   118  
143 _spectrogramGenerator.Add(buffer, false); 119 _spectrogramGenerator.Add(buffer, false);
144 } 120 }
145   -  
146 private void timer1_Tick(object sender, EventArgs e) -  
147 { -  
Line 148... Line 121...
148 if(_spectrogramGenerator.FftsToProcess == 0) 121  
149 { 122 private void timer1_Tick(object sender, EventArgs e)
150 return; 123 {
-   124 if (_spectrogramGenerator.FftsToProcess == 0) return;
-   125  
151 } 126 _spectrogramGenerator.Process();
152   127 _spectrogramGenerator.SetFixedWidth(pictureBox1.Width);
153 _spectrogramGenerator.Process(); 128 var bmpSpec = new Bitmap(_spectrogramGenerator.Width, _spectrogramGenerator.Height,
154 _spectrogramGenerator.SetFixedWidth(pictureBox1.Width); 129 PixelFormat.Format32bppPArgb);
155 var bmpSpec = new Bitmap(_spectrogramGenerator.Width, _spectrogramGenerator.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 130 using (var bmpSpecIndexed =
156 using (var bmpSpecIndexed = _spectrogramGenerator.GetBitmap(Configuration.Visualisations.Spectrogram.SpectrumIntensity)) 131 _spectrogramGenerator.GetBitmap(Configuration.Visualisations.Spectrogram.SpectrumIntensity))
Line 168... Line 143...
168 pictureBox1.Image = bmpSpec; 143 pictureBox1.Image = bmpSpec;
169 } 144 }
Line 170... Line 145...
170   145  
171 private void SpectrogramForm_MouseClick(object sender, MouseEventArgs e) 146 private void SpectrogramForm_MouseClick(object sender, MouseEventArgs e)
172 { 147 {
173 switch(e.Button) 148 switch (e.Button)
174 { 149 {
175 case MouseButtons.Right: 150 case MouseButtons.Right:
176 var control = (Control)sender; 151 var control = (Control)sender;
177 var screenPoint = control.PointToScreen(e.Location); 152 var screenPoint = control.PointToScreen(e.Location);
Line 187... Line 162...
187 Close(); 162 Close();
188 } 163 }
Line 189... Line 164...
189   164  
190 private void SpectrogramForm_MouseDown(object sender, MouseEventArgs e) 165 private void SpectrogramForm_MouseDown(object sender, MouseEventArgs e)
191 { 166 {
192 if(e.Button != MouseButtons.Left) -  
193 { -  
194 return; -  
Line 195... Line 167...
195 } 167 if (e.Button != MouseButtons.Left) return;
196   -  
197 if(e.Clicks != 1) -  
198 { -  
Line 199... Line 168...
199 return; 168  
200 } 169 if (e.Clicks != 1) return;
201   170  
Line 202... Line 171...
202 _mouseDown = true; 171 _mouseDown = true;
203 _formLocation = new Point(e.X, e.Y); 172 _formLocation = new Point(e.X, e.Y);
204 } 173 }
205   -  
206 private void SpectrogramForm_MouseMove(object sender, MouseEventArgs e) -  
207 { -  
Line 208... Line 174...
208 if (e.Button != MouseButtons.Left) 174  
209 { -  
210 return; -  
211 } -  
Line 212... Line 175...
212   175 private void SpectrogramForm_MouseMove(object sender, MouseEventArgs e)
213 if (!_mouseDown) 176 {
Line 214... Line 177...
214 { 177 if (e.Button != MouseButtons.Left) return;
215 return; -  
216 } 178  
Line 217... Line 179...
217   179 if (!_mouseDown) return;
218 var control = (Control)sender; 180  
219 var screenPoint = control.PointToScreen(e.Location); 181 var control = (Control)sender;
220   -  
221 Location = new Point(screenPoint.X - _formLocation.X, screenPoint.Y - _formLocation.Y); -  
222   -  
Line 223... Line 182...
223 } 182 var screenPoint = control.PointToScreen(e.Location);
224   183  
Line 225... Line 184...
225 private void SpectrogramForm_MouseUp(object sender, MouseEventArgs e) 184 Location = new Point(screenPoint.X - _formLocation.X, screenPoint.Y - _formLocation.Y);
Line 239... Line 198...
239 } 198 }
Line 240... Line 199...
240   199  
241 private void SpectrogramForm_MouseEnter(object sender, EventArgs e) 200 private void SpectrogramForm_MouseEnter(object sender, EventArgs e)
242 { 201 {
243 if (Configuration.Visualisations.Spectrogram.PinToDesktop) -  
244 { 202 if (Configuration.Visualisations.Spectrogram.PinToDesktop)
245 _windowZOrderScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () => 203 _windowZOrderScheduledContinuation.Schedule(TimeSpan.FromSeconds(1),
246 { -  
247 this.InvokeIfRequired(form => 204 () =>
-   205 {
-   206 this.InvokeIfRequired(form =>
248 { 207 {
-   208 SetWindowPos(Handle, HwndTop, 0, 0, Location.X, Location.Y,
249 SetWindowPos(Handle, HWND_TOP, 0, 0, Location.X, Location.Y, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); 209 SwpNomove | SwpNosize | SwpNoactivate);
250 }); 210 });
251 }, _renderCancellationToken); -  
252 } 211 }, _renderCancellationToken);
Line 253... Line 212...
253 } 212 }
254   213  
255 private void SpectrogramForm_MouseLeave(object sender, EventArgs e) 214 private void SpectrogramForm_MouseLeave(object sender, EventArgs e)
256 { -  
257 if (Configuration.Visualisations.Spectrogram.PinToDesktop) 215 {
258 { 216 if (Configuration.Visualisations.Spectrogram.PinToDesktop)
259 _windowZOrderScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () => -  
260 { 217 _windowZOrderScheduledContinuation.Schedule(TimeSpan.FromSeconds(1),
-   218 () =>
-   219 {
261 this.InvokeIfRequired(form => 220 this.InvokeIfRequired(form =>
-   221 {
262 { 222 SetWindowPos(form.Handle, HwndBottom, 0, 0, Location.X, Location.Y,
263 SetWindowPos(form.Handle, HWND_BOTTOM, 0, 0, Location.X, Location.Y, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); -  
264 }); 223 SwpNomove | SwpNosize | SwpNoactivate);
265   -  
266 }, _renderCancellationToken); 224 });
Line 267... Line 225...
267 } 225 }, _renderCancellationToken);
268 } 226 }
269   227  
Line 270... Line 228...
270 private void pinToDesktopToolStripMenuItem_CheckStateChanged(object sender, EventArgs e) 228 private void pinToDesktopToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
271 { 229 {
272 var toolStripMenuItem = (ToolStripMenuItem)sender; 230 var toolStripMenuItem = (ToolStripMenuItem)sender;
273   231  
274 switch(toolStripMenuItem.CheckState) 232 switch (toolStripMenuItem.CheckState)
275 { 233 {
276 case CheckState.Checked: 234 case CheckState.Checked:
277 Configuration.Visualisations.Spectrogram.PinToDesktop = true; 235 Configuration.Visualisations.Spectrogram.PinToDesktop = true;
278 break; 236 break;
279 case CheckState.Unchecked: 237 case CheckState.Unchecked:
-   238 Configuration.Visualisations.Spectrogram.PinToDesktop = false;
-   239 break;
-   240 }
-   241 }
-   242  
-   243 #region Natives
-   244  
-   245 [DllImport("user32.dll")]
-   246 private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy,
-   247 uint uFlags);
-   248  
-   249 private static readonly IntPtr HwndBottom = new IntPtr(1);
-   250 private static readonly IntPtr HwndTop = new IntPtr(0);
-   251 private const uint SwpNosize = 0x0001;
280 Configuration.Visualisations.Spectrogram.PinToDesktop = false; 252 private const uint SwpNomove = 0x0002;
281 break; 253 private const uint SwpNoactivate = 0x0010;
282 } 254