forked from naudio/NAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaveFormControl.xaml.cs
102 lines (91 loc) · 3 KB
/
WaveFormControl.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
namespace NAudioWpfDemo
{
/// <summary>
/// Interaction logic for WaveFormControl.xaml
/// </summary>
public partial class WaveFormControl : UserControl, IWaveFormRenderer
{
int renderPosition;
double yTranslate = 40;
double yScale = 40;
int blankZone = 10;
List<Line> lines = new List<Line>();
public WaveFormControl()
{
InitializeComponent();
SizeChanged += WaveFormControl_SizeChanged;
}
void WaveFormControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
// To just remove what is on the right of the now cursor:
/*int remove = mainCanvas.Children.Count - x;
mainCanvas.Children.RemoveRange(0, remove);*/
// We will remove everything as we are going to rescale vertically
renderPosition = 0;
ClearAllLines();
yTranslate = ActualHeight / 2;
yScale = ActualHeight / 2;
}
private void ClearAllLines()
{
//mainCanvas.Children.Clear();
for (int n = 0; n < lines.Count; n++)
{
lines[n].Visibility = Visibility.Collapsed;
}
}
public void AddValue(float maxValue, float minValue)
{
int pixelWidth = (int)ActualWidth;
if (pixelWidth > 0)
{
Line line = CreateLine(maxValue, minValue);
if (renderPosition > ActualWidth)
{
renderPosition = 0;
}
int erasePosition = (renderPosition + blankZone) % pixelWidth;
if (erasePosition < lines.Count)
{
lines[erasePosition].Visibility = Visibility.Collapsed;
}
}
}
private Line CreateLine(float maxValue, float minValue)
{
Line line;
if (renderPosition >= lines.Count)
{
line = new Line();
lines.Add(line);
mainCanvas.Children.Add(line);
}
else
{
line = lines[renderPosition];
}
line.Stroke = Foreground;
line.X1 = renderPosition;
line.X2 = renderPosition;
line.Y1 = yTranslate + minValue * yScale;
line.Y2 = yTranslate + maxValue * yScale;
renderPosition++;
line.Visibility = Visibility.Visible;
return line;
}
/// <summary>
/// Clears the waveform and repositions on the left
/// </summary>
public void Reset()
{
renderPosition = 0;
ClearAllLines();
}
}
}