forked from filoe/cscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaveform.xaml.cs
217 lines (183 loc) · 7.28 KB
/
Waveform.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSCore;
using CSCoreWaveform.Annotations;
namespace CSCoreWaveform
{
/// <summary>
/// Interaction logic for Waveform.xaml
/// </summary>
public partial class Waveform : UserControl, INotifyPropertyChanged
{
private LineGeometry _positionGeometry;
public Waveform()
{
InitializeComponent();
}
public IList<float> ChannelData
{
get { return (IList<float>)GetValue(ChannelDataProperty); }
set
{
SetValue(ChannelDataProperty, value);
}
}
// Using a DependencyProperty as the backing store for ChannelDataFloats. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ChannelDataProperty =
DependencyProperty.Register("ChannelData", typeof(IList<float>), typeof(Waveform), new PropertyMetadata(null, async (d, e) =>
{
await ((Waveform) d).UpdateWaveformAsync();
}));
public double PositionInPerc
{
get { return (double)GetValue(PositionInPercProperty); }
set
{
SetValue(PositionInPercProperty, value);
}
}
// Using a DependencyProperty as the backing store for PositionInPerc. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PositionInPercProperty =
DependencyProperty.Register("PositionInPerc", typeof(double), typeof(Waveform), new PropertyMetadata(0.0, (d, e) =>
{
((Waveform)d).UpdatePosition((double)e.NewValue);
}));
private async Task UpdateWaveformAsync()
{
if (!Dispatcher.CheckAccess())
{
await Dispatcher.InvokeAsync(async () => await UpdateWaveformAsync());
return;
}
List<Point> points = new List<Point>();
double centerHeight = PART_Canvas.RenderSize.Height / 2d;
double x = 0;
var channelData = ChannelData;
double minValue = 0;
double maxValue = 1.5;
double dbScale = maxValue - minValue;
points.Add(new Point(0, centerHeight));
points.Add(new Point(0, centerHeight));
int iOffset = 0;
for (int i = 0; i < channelData.Count; i++)
{
if (i == channelData.Count / 2)
{
iOffset = channelData.Count / 2;
points.Add(new Point(x, centerHeight));
points.Add(new Point(0, centerHeight));
}
x = (PART_Canvas.RenderSize.Width / (channelData.Count / 2)) * (i - iOffset);
Debug.Assert(i - iOffset >= 0);
double height = ((channelData[i] - minValue) / dbScale) * centerHeight;
height += centerHeight;
points.Add(new Point(x, height));
}
points.Add(new Point(x, centerHeight));
points.Add(new Point(0, centerHeight));
PolyLineSegment lineSegment = new PolyLineSegment(points, false);
PathFigure figure = new PathFigure();
figure.Segments.Add(lineSegment);
PathGeometry geometry = new PathGeometry();
geometry.Figures.Add(figure);
//var path = new Path
//{
// Data = geometry,
// Fill = Brushes.Green,
// Stroke = Brushes.Transparent,
// StrokeThickness = PART_Canvas.RenderSize.Width / (channelData.Count / 2)
//};
PART_Path.Data = geometry;
PART_Path.StrokeThickness = PART_Canvas.RenderSize.Width / (channelData.Count / 2);
var centerLinePath = new Path()
{
Data = new LineGeometry(new Point(points.First().X, centerHeight), new Point(x, centerHeight)),
Fill = Brushes.Transparent,
Stroke = Brushes.Black,
StrokeThickness = 1
};
_positionGeometry = new LineGeometry();
var positionPath = new Path()
{
Data = _positionGeometry,
Fill = Brushes.Transparent,
Stroke = Brushes.Red,
StrokeThickness = 1
};
geometry.FillRule = FillRule.Nonzero;
ClearWaveform();
PART_Canvas.CacheMode = new BitmapCache(1.0);
PART_Canvas.Children.Add(PART_Path);
PART_Canvas.Children.Add(centerLinePath);
PART_Canvas.Children.Add(positionPath);
}
public void UpdatePosition(double perc)
{
if (Dispatcher.CheckAccess() && _positionGeometry != null)
{
double x = perc * PART_Canvas.RenderSize.Width;
var centerLineGeometry = (LineGeometry) ((Path) PART_Canvas.Children[1]).Data;
x = centerLineGeometry.StartPoint.X + (perc * (centerLineGeometry.EndPoint - centerLineGeometry.StartPoint).X);
_positionGeometry.StartPoint = new Point(x, 0);
_positionGeometry.EndPoint = new Point(x, PART_Canvas.RenderSize.Height);
}
else
{
Dispatcher.InvokeAsync(() => UpdatePosition(perc));
}
}
public void ClearWaveform()
{
PART_Canvas.Children.Clear();
}
protected override async void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
var cache = PART_Canvas.CacheMode as BitmapCache;
if (cache != null)
{
cache.RenderAtScale = 1.0;
await UpdateWaveformAsync();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
private void PART_Canvas_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var x = e.GetPosition(PART_Canvas).X;
var perc = x / PART_Canvas.RenderSize.Width;
if(PositionChanged != null)
PositionChanged(this, new PositionChangedEventArgs(perc));
}
public event EventHandler<PositionChangedEventArgs> PositionChanged;
}
public class PositionChangedEventArgs : EventArgs
{
public PositionChangedEventArgs(double percentage)
{
Percentage = percentage;
}
public double Percentage { get; private set; }
}
}