-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipEditor.cs
159 lines (136 loc) · 5.25 KB
/
ClipEditor.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Design;
using System.ComponentModel;
using System.Xml.Linq;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using Ephemera.NBagOfTricks;
using Ephemera.NBagOfUis;
using Ephemera.AudioLib;
namespace Wavicler
{
[ToolboxItem(false), Browsable(false)] // not useable in designer
public partial class ClipEditor : UserControl
{
#region Fields
#endregion
#region Backing fields
readonly ClipSampleProvider _prov = new(Array.Empty<float>());
#endregion
#region Properties
/// <summary>The bound input sample provider.</summary>
public ClipSampleProvider SampleProvider { get { return _prov; } }
/// <summary>Current file.</summary>
public string FileName { get; private set; } = "";
#endregion
#region Events
/// <summary>Ask the owner to do something.</summary>
public event EventHandler<ServiceRequestEventArgs>? ServiceRequest;
public enum ServiceRequestType { CopySelectionToNewClip, Close }
public class ServiceRequestEventArgs : EventArgs
{
public ServiceRequestType Request { get; set; }
}
#endregion
#region Lifecycle
/// <summary>
/// Normal constructor.
/// </summary>
/// <param name="prov">The bound input sample provider.</param>
/// <param name="fn">Associated filename.</param>
public ClipEditor(ClipSampleProvider prov, string fn)
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
FileName = fn;
// Hook up provider and UI.
_prov = prov;
wvData.Init(_prov);
wvData.WaveColor = Globals.WaveColor;
wvData.GridColor = Globals.GridColor;
wvData.MarkColor = Globals.MarkColor;
wvData.TextColor = Globals.TextColor;
_prov.Rewind();
//_prov.ClipProgress += (object? sender, ClipSampleProvider.ClipProgressEventArgs e) => progBar.Current = (int)e.Position;
timer.Tick += (_, __) => { progBar.Current = _prov.SampleIndex; };
timer.Enabled = true;
// Viewer events.
wvData.ViewerChange += ProcessViewerChange;
// Add some stuff to viewer context menu.
wvData.ExtraMenuItems.Add(new ToolStripSeparator());
wvData.ExtraMenuItems.Add(new ToolStripMenuItem(
"Copy To New Clip",
null,
(_, __) => { ServiceRequest?.Invoke(this, new() { Request = ServiceRequestType.CopySelectionToNewClip }); }
));
wvData.ExtraMenuItems.Add(new ToolStripMenuItem(
"Close",
null,
(_, __) => { ServiceRequest?.Invoke(this, new() { Request = ServiceRequestType.Close }); }
));
// Progress bar.
progBar.ProgressColor = Globals.ControlColor;
progBar.Length = _prov.SamplesPerChannel;
progBar.Current = 0;
var thumb = wvData.RenderThumbnail(progBar.Width, progBar.Height, Globals.WaveColor, SystemColors.Control, true);
progBar.Thumbnail = thumb;
progBar.CurrentChanged += (_, __) => { _prov.SampleIndex = progBar.Current; };
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
wvData.Dispose();
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region Public functions
/// <summary>
///
/// </summary>
public void Rewind()
{
SampleProvider.SampleIndex = SampleProvider.SelStart;
progBar.Current = SampleProvider.SelStart;
}
#endregion
#region Private functions
/// <summary>
/// Process viewer UI changes. Update the bound sample provider.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ProcessViewerChange(object? sender, WaveViewer.ViewerChangeEventArgs e)
{
switch (e.Change)
{
case ParamChange.SelStart when sender == wvData:
SampleProvider.SelStart = wvData.SelStart;
SampleProvider.Rewind();
break;
case ParamChange.SelLength when sender == wvData:
SampleProvider.SelLength = wvData.SelLength;
break;
case ParamChange.Gain when sender == wvData:
SampleProvider.Gain = wvData.Gain;
break;
case ParamChange.Marker when sender == wvData:
break;
};
}
#endregion
}
}