-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayBack.cs
More file actions
169 lines (152 loc) · 4.9 KB
/
PlayBack.cs
File metadata and controls
169 lines (152 loc) · 4.9 KB
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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NAudio.Midi;
namespace PianoEnhancer
{
internal class PlayBack
{
public delegate void MidiEventReceiver(MidiEvent ev);
public delegate void StopEvent();
public event MidiEventReceiver SendControl;
public event StopEvent PlaybackStopped;
public VoiceComposition CurrentVoice
{
set
{
foreach (var voice in value.ChannelVoice)
{
SendControl?.Invoke(voice);
}
_currentVoice = value;
}
get => _currentVoice;
}
private VoiceComposition _currentVoice;
public bool IsPlaying { get; private set; }
private IList<Chord> _pendingChords;
private IList<NoteOnEvent> _pendingNotes;
private IList<KeyValuePair<int, VoiceComposition>> _voiceSwitchPoints;
private IList<int> _bufferedNotes;
private long _newestMatched;
public int PendingPosition { get; private set; }
private int _matchedNotes;
private IEnumerator<KeyValuePair<int, VoiceComposition>> _voicePointer;
public PlayBack()
{
_bufferedNotes = new List<int>();
}
public void Play(Recording rec)
{
if (IsPlaying) return;
_pendingChords = rec.Track;
_pendingNotes = _pendingChords.SelectMany(
chord => chord.Notes.Select(note => new NoteOnEvent(chord.AbsoluteTime,1,note,1,1))
).ToList();
_voiceSwitchPoints = rec.GetSwitchPoints().ToList();
lock (_bufferedNotes)
{
_bufferedNotes = new List<int>();
}
_newestMatched = 0;
PendingPosition = 0;
_matchedNotes = 0;
_voicePointer = _voiceSwitchPoints.GetEnumerator();
_voicePointer.MoveNext();
IsPlaying = true;
var worker = new Task(WorkerLoop);
worker.Start();
}
public void Stop()
{
IsPlaying = false;
PlaybackStopped?.Invoke();
}
public void NoteReceived(int note)
{
if (!IsPlaying) return;
lock (_bufferedNotes)
{
_bufferedNotes.Add(note);
}
}
private void WorkerLoop()
{
while (IsPlaying)
{
ConsumeWork();
}
}
private void ConsumeWork()
{
RemoveSurpassedNotes();
MatchNotes();
SetVoice();
}
private void SetVoice()
{
if (PendingPosition < _voicePointer.Current.Key) return;
_currentVoice = _voicePointer.Current.Value;
if (!_voicePointer.MoveNext())
{
Stop();
}
if (_currentVoice.ChannelVoice == null) return;
foreach (var voice in _currentVoice.ChannelVoice)
{
SendControl?.Invoke(voice);
}
}
private void IncrementMatchedNotes()
{
_matchedNotes++;
if (_matchedNotes != _pendingChords[PendingPosition].Notes.Length) return;
_matchedNotes = 0;
PendingPosition++;
}
private void RemoveSurpassedNotes()
{
var oldestPending = _pendingNotes[0];
if (_newestMatched - Configuration.PendingNoteDiscardThreshold <= oldestPending.AbsoluteTime) return;
if (_pendingNotes.Count <= 1)
{
Stop();
return;
}
_pendingNotes.RemoveAt(0);
IncrementMatchedNotes();
}
private void MatchNotes()
{
if (_pendingNotes.Count == 0)
{
Stop();
return;
}
lock (_bufferedNotes)
{
for (var i = 0; i < _bufferedNotes.Count; ++i)
{
var note = _bufferedNotes[i];
for(var j = 0; j < _pendingNotes.Count; ++j)
{
var candidate = _pendingNotes[j];
if (j > Configuration.ConsumeWindowSteps)
{
_bufferedNotes.RemoveAt(i);
--i;
break;
}
if (candidate.NoteNumber != note) continue;
_bufferedNotes.RemoveAt(i);
_pendingNotes.RemoveAt(j);
_newestMatched = candidate.AbsoluteTime;
--i;
IncrementMatchedNotes();
break;
}
}
}
}
}
}