/
Form1.cs
560 lines (472 loc) · 18.6 KB
/
Form1.cs
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using WaveFile;
using Wave;
namespace waveEditerVersion1
{
public partial class Form1 : Form
{
double[] samples, selectedSamples, selectedFourier, fourierAmp;
int selectedRange, selectStart, selectEnd;
int fourierRange, startFourier, endFourier;
byte[] signalData;
byte[] bData;
int sampleStart = 0, sampleEnd = 0, sampleRange = 0;
bool selected = false;
bool selectFourier = false;
Wave.Fourier fourier = new Wave.Fourier();
double volumeVal = 1.0;
Wav wav;
public Form1()
{
InitializeComponent();
}
public void readWaveFile()
{
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
byte[] wavData = File.ReadAllBytes(fileDialog.FileName);
byte[] wavHeader = new List<byte>(wavData).GetRange(0, 44).ToArray();
wav = new WaveFile.Wav(wavHeader);
wavData = new List<byte>(wavData).GetRange(44, wavData.Length - 44).ToArray();
Debug.WriteLine("\nOpening File:"
+ "\nChunk ID: " + wav.RIFFChunkID
+ "\nRIFF Chuck Size: " + wav.RIFFChuckSize
+ "\nFormat: " + wav.format
+ "\nfmt Chuck ID: " + wav.fmtChuckID
+ "\nfmt Chunk Size: " + wav.fmtChunkSize
+ "\naudio format: " + wav.audioFormat
+ "\nNum Channels: " + wav.numChannels
+ "\nSample Rate: " + wav.sampleRate
+ "\nByte Rate: " + wav.byteRate
+ "\nBlock Align: " + wav.blockAlign
+ "\nBits Per Sample: " + wav.bitsPerSample
+ "\nData Chunk ID: " + wav.dataChunkID
+ "\nData Chunk Size: " + wav.dataChunkSize);
signalData = new byte[wavData.Length];
signalData = wavData;
generateWavSample(wavData);
}
}
public void saveAsWav()
{
if (wav == null)
return;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "WAV|*.wav";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
WaveGenerator wave = new WaveGenerator(samples);
wave.Save(saveDialog.FileName);
}
}
public void generateWavSample(byte[] data)
{
samples = new double[(int)wav.dataChunkSize / wav.blockAlign];
short[] temp = new short[samples.Length];
for (int i = 0, j = 0; i < wav.dataChunkSize - 4; i += (int)wav.blockAlign, j++)
{
temp[j] = BitConverter.ToInt16(data, i);
}
samples = temp.Select(x => (double)(x)).ToArray();
plotSample(samples.Length);
}
public void copy()
{
selectedSamples = new double[selectedRange];
selectedSamples = new List<double>(samples).GetRange(selectStart, selectedRange).ToArray();
short[] toShort = samples.Select(element => (short)(element)).ToArray();
signalData = toShort.Select(element => Convert.ToInt16(element))
.SelectMany(element => BitConverter.GetBytes(element)).ToArray();
plotSample(samples.Length);
}
public void cut()
{
if (!selected)
{
return;
}
selectedSamples = new double[selectedRange];
double[] left = new List<double>(samples).GetRange(0, selectStart).ToArray();
double[] right = new List<double>(samples).GetRange(selectEnd, samples.Length - selectEnd).ToArray();
selectedSamples = new List<double>(samples).GetRange(selectStart, selectedRange).ToArray();
samples = new double[left.Length + right.Length];
left.CopyTo(samples, 0);
right.CopyTo(samples, left.Length);
short[] toShort = samples.Select(element => (short)(element)).ToArray();
signalData = toShort.Select(element => Convert.ToInt16(element))
.SelectMany(element => BitConverter.GetBytes(element)).ToArray();
plotSample(samples.Length);
}
public void paste()
{
double[] left = new List<double>(samples).GetRange(0, selectEnd).ToArray();
double[] right = new List<double>(samples).GetRange(selectEnd, samples.Length - selectEnd).ToArray();
samples = new double[left.Length + selectedSamples.Length + right.Length];
left.CopyTo(samples, 0);
selectedSamples.CopyTo(samples, left.Length);
right.CopyTo(samples, selectedSamples.Length + left.Length);
short[] toShort = samples.Select(element => (short)(element)).ToArray();
signalData = toShort.Select(element => Convert.ToInt16(element))
.SelectMany(element => BitConverter.GetBytes(element)).ToArray();
plotSample(samples.Length);
}
public void plotSample(int length)
{
waveChart.Series["waveSeries"].Points.Clear();
Series series = waveChart.Series["waveSeries"];
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.Double;
double miny = 5;
double maxy = -5;
if (samples != null)
{
for (int i = 0; i < length; i++)
{
if (samples[i] < miny)
{
miny = samples[i];
}
else if (samples[i] > maxy)
{
maxy = samples[i];
}
waveChart.Series["waveSeries"].Points.AddXY(i, samples[i]);
}
ChartArea chartArea = waveChart.ChartAreas[waveChart.Series["waveSeries"].ChartArea];
chartArea.AxisX.Minimum = 0;
chartArea.AxisX.Maximum = length;
chartArea.AxisY.Minimum = miny;
chartArea.AxisY.Maximum = maxy;
chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chartArea.AxisX.ScrollBar.ButtonColor = Color.Black;
}
waveChart.MouseWheel += chart1_MouseWheel;
}
private void ApplyDFT()
{
Series frequencySeries = frequencyChart.Series["frequencySeries"];
frequencySeries.Points.Clear();
if (!selected)
{
return;
}
fourierAmp = fourier.DFT(selectedSamples);
int N = fourierAmp.Length;
for (int f = 0; f < N; f++)
{
frequencySeries.Points.AddXY(f, fourierAmp[f]);
}
}
private void ApplyInverse()
{
if (fourierAmp == null)
{
return;
}
samples = fourier.inverseDFT(fourierAmp);
plotSample(samples.Length);
short[] toShort = samples.Select(element => (short)(element)).ToArray();
signalData = toShort.Select(element => Convert.ToInt16(element))
.SelectMany(element => BitConverter.GetBytes(element)).ToArray();
}
private void ApplyFilter()
{
Series frequencySeries = frequencyChart.Series["frequencySeries"];
frequencySeries.Points.Clear();
if (fourierAmp == null)
{
return;
}
fourierAmp = fourier.applyFilter(startFourier, endFourier, fourierAmp);
for (int f = 0; f < fourierAmp.Length; f++)
{
frequencySeries.Points.AddXY(f, fourierAmp[f]);
}
ApplyInverse();
}
public void StartRecord()
{
Debug.WriteLine(OpenDialog() ? "open rec succeeded" : "open rec failed");
Debug.WriteLine(StartRec(16, 11025) ? "start rec succeeded" : "start rec failed");
}
public void StopRecord()
{
RecordData recordData = StopRec();
bData = new byte[recordData.len];
Debug.WriteLine(recordData.len);
Marshal.Copy(recordData.ip, bData, 0, (int)recordData.len);
WaveHeader header = (WaveHeader)Marshal.PtrToStructure(GetWaveform(), typeof(WaveHeader));
wav = new Wav("RIFF", recordData.len + 36, "WAVE",
"fmt", 16, 1, header.nChannels, header.nSamplesPerSec,
header.nAvgBytesPerSec, header.nBlockAlign, header.wBitsPerSample,
"data", recordData.len);
short[] temp = new short[recordData.len / (int)wav.blockAlign];
for (int i = 0; i < temp.Length - 1; i++)
temp[i] = BitConverter.ToInt16(bData, i * (int)wav.blockAlign);
samples = temp.Select(x => (double)(x)).ToArray();
plotSample(samples.Length);
}
public void PlayRecord()
{
if (volumeVal != 1)
{
int c = bData.Length;
Debug.WriteLine(bData[5000]);
for (int i = 0; i < c; i++)
{
double temp = bData[i];
temp -= 128;
temp *= volumeVal;
temp += 128;
if (temp > 255)
{
temp = 255;
}
if (temp < 0)
{
temp = 0;
}
bData[i] = (byte)temp;
}
Debug.WriteLine(bData[5000]);
}
IntPtr iptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * bData.Length);
Marshal.Copy(bData, 0, iptr, bData.Length);
Debug.WriteLine(PlayStart(iptr, bData.Length, 16, 11025) ?
"play start succeeded" : "play start failed");
Marshal.FreeHGlobal(iptr);
}
void playWavSignal()
{
OpenDialog();
IntPtr iptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * signalData.Length);
Marshal.Copy(signalData, 0, iptr, signalData.Length);
Debug.WriteLine(PlayStart(iptr, signalData.Length, 16, 11025) ?
"play start succeeded" : "play start failed");
Marshal.FreeHGlobal(iptr);
}
private void selectionSignal(object sender, CursorEventArgs e)
{
if (samples == null)
{
return;
}
selected = true;
selectStart = (int)e.NewSelectionStart;
selectEnd = (int)e.NewSelectionEnd;
if (selectStart > selectEnd)
{
int temp = selectEnd;
selectEnd = selectStart;
selectStart = temp;
}
selectedRange = Math.Abs(selectEnd - selectStart);
return;
}
private void applyWindowing(int w, int N, int start)
{
double[] window = new double[N];
switch (w)
{
case 1:
for (int i = 0; i < N; i++)
{
window[i] = 1.0 - Math.Abs((i - N / 2.0) / N / 2.0);
}
break;
case 2:
for (int i = 0; i < N; i++)
{
window[i] = 1.0 - Math.Pow(Math.Abs((i - N / 2.0) / N / 2.0), 2);
}
break;
}
for (int i = 0; i < N; i++)
{
samples[start + i] = samples[start + i] * window[i];
}
}
private void selectionFreq(object sender, CursorEventArgs a)
{
if (fourierAmp == null)
{
return;
}
selectFourier = true;
startFourier = (int)a.NewSelectionStart;
endFourier = (int)a.NewSelectionEnd;
if (startFourier > endFourier)
{
int temp = endFourier;
endFourier = startFourier;
startFourier = temp;
}
fourierRange = Math.Abs(endFourier - startFourier);
selectedFourier = new double[fourierRange];
selectedFourier = new List<double>(fourierAmp).GetRange((int)a.NewSelectionStart, fourierRange).ToArray();
return;
}
private void chart1_MouseWheel(object sender, MouseEventArgs e)
{
var chart = (Chart)sender;
var xAxis = chart.ChartAreas[0].AxisX;
try
{
if (e.Delta < 0) // Scrolled down.
{
var xMin = xAxis.ScaleView.ViewMinimum;
var xMax = xAxis.ScaleView.ViewMaximum;
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) * 2;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) * 2;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
}
else if (e.Delta > 0) // Scrolled up.
{
var xMin = xAxis.ScaleView.ViewMinimum;
var xMax = xAxis.ScaleView.ViewMaximum;
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
}
}
catch { }
}
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern bool OpenDialog();
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern bool CloseDialog();
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern RecordData StopRec();
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern bool StartRec(int d, int r);
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetWaveform();
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern bool PlayPause();
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern bool PlayStart(IntPtr p, int size, int d, int r);
[DllImport("Record.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern bool PlayStop();
public struct RecordData
{
public uint len;
public IntPtr ip;
}
public struct WaveHeader
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
private void Button3_Click(object sender, EventArgs e)
{
ApplyDFT();
}
private void OPENToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void WavToolStripMenuItem_Click(object sender, EventArgs e)
{
readWaveFile();
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
saveAsWav();
}
private void GroupBox1_Enter(object sender, EventArgs e)
{
}
private void Button4_Click(object sender, EventArgs e)
{
StartRecord();
}
private void Button4_Click_1(object sender, EventArgs e)
{
StopRecord();
}
private void ToolStripMenuItem2_Click(object sender, EventArgs e)
{
cut();
}
private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
{
paste();
}
private void filterButton_Click(object sender, EventArgs e)
{
ApplyFilter();
}
private void InverseDFT_Click(object sender, EventArgs e)
{
ApplyInverse();
}
private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
{
copy();
}
private void FilterToolStripMenuItem_Click(object sender, EventArgs e)
{
ApplyFilter();
}
private void TriangularToolStripMenuItem_Click(object sender, EventArgs e)
{
if (selectedRange == 0)
{
return;
}
applyWindowing(1, selectedRange, selectStart);
plotSample(samples.Length);
}
private void RectangularToolStripMenuItem_Click(object sender, EventArgs e)
{
if (selectedRange == 0)
{
return;
}
applyWindowing(2, selectedRange, selectStart);
plotSample(samples.Length);
}
private void Button4_Click_2(object sender, EventArgs e)
{
PlayRecord();
}
private void Button4_Click_3(object sender, EventArgs e)
{
volumeVal = double.Parse(volumeValue.Text) / 100.0;
}
private void Button4_Click_4(object sender, EventArgs e)
{
playWavSignal();
}
private void Button2_Click(object sender, EventArgs e)
{
sampleStart = int.Parse(sampleStartTextbox.Text);
sampleEnd = int.Parse(sampleEndTextbox.Text);
selected = true;
plotSample(300);
}
private void Button1_Click(object sender, EventArgs e)
{
}
}
}