forked from filoe/cscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
178 lines (160 loc) · 5.96 KB
/
MainWindow.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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using CSCore;
using CSCore.Codecs;
using CSCore.MediaFoundation;
using CSCore.SoundOut;
using CSCore.Streams;
using CSCoreWaveform.Annotations;
using Microsoft.Win32;
namespace CSCoreWaveform
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : INotifyPropertyChanged
{
private readonly ISoundOut _soundOut;
private ObservableCollection<WaveformDataModel> _channels = new ObservableCollection<WaveformDataModel>();
private NotificationSource _notificationSource;
private ISampleSource _sampleSource;
public MainWindow()
{
InitializeComponent();
DataContext = this;
_soundOut = new WasapiOut();
}
public ObservableCollection<WaveformDataModel> Channels
{
get { return _channels; }
private set
{
if (Equals(value, _channels))
return;
_channels = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private async void Open_Click(object sender, RoutedEventArgs e)
{
var ofn = new OpenFileDialog {Filter = CodecFactory.SupportedFilesFilterEn};
if (ofn.ShowDialog() == true)
{
_soundOut.Stop();
if (_notificationSource != null)
_notificationSource.Dispose();
var source = CodecFactory.Instance.GetCodec(ofn.FileName);
//since the mediafoundationdecoder isn't really accurate what position and length concerns
//read the whole file into a cache
if (source is MediaFoundationDecoder)
{
if (source.Length < 10485760) //10MB
{
source = new CachedSoundSource(source);
}
else
{
Stopwatch stopwatch = Stopwatch.StartNew();
source = new FileCachedSoundSource(source);
stopwatch.Stop();
Debug.WriteLine(stopwatch.Elapsed.ToString());
}
}
source.Position = 0;
//load the waveform
await LoadWaveformsAsync(source);
source.Position = 0;
_sampleSource = source.ToSampleSource();
_notificationSource = new NotificationSource(_sampleSource) {Interval = 100};
_notificationSource.BlockRead += (o, args) => { UpdatePosition(); };
_soundOut.Initialize(_notificationSource.ToWaveSource());
_soundOut.Play();
}
}
private async Task LoadWaveformsAsync(IWaveSource waveSource)
{
Channels = null;
//read the specified waveSource into n arrays of samples where n is the number of channels of the waveSource
var channelData = await WaveformData.GetData(waveSource);
//by setting the Channels property, the Waveform Control automatically renders the waveform
Channels =
new ObservableCollection<WaveformDataModel>(channelData.Select(x => new WaveformDataModel {Data = x}));
}
private void UpdatePosition()
{
Dispatcher.InvokeAsync(() =>
{
var x = (float) _sampleSource.Position / WaveformData.Length;
foreach (var waveformData in Channels)
{
waveformData.PositionInPerc = x;
}
});
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
private void MainWindow_OnClosing(object sender, CancelEventArgs e)
{
if (_soundOut != null)
_soundOut.Dispose();
if (_notificationSource != null)
_notificationSource.Dispose();
}
private void Waveform_OnPositionChanged(object sender, PositionChangedEventArgs e)
{
if (_notificationSource != null)
{
var position = (long) (e.Percentage * WaveformData.Length);
position -= position % _notificationSource.WaveFormat.BlockAlign;
_notificationSource.Position = position;
}
}
}
public class WaveformDataModel : INotifyPropertyChanged
{
private float[] _data;
private double _positionInPerc;
public IList<float> Data
{
get { return _data; }
set
{
if (Equals(value, _data))
return;
_data = value.ToArray();
OnPropertyChanged();
}
}
public double PositionInPerc
{
get { return _positionInPerc; }
set
{
if (value.Equals(_positionInPerc))
return;
_positionInPerc = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}