-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChordLivePlayer.cs
189 lines (162 loc) · 5 KB
/
ChordLivePlayer.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sanford.Multimedia.Timers;
using Sanford.Multimedia.Midi;
using System.Windows.Forms;
using Sanford.Multimedia;
using static MidiChord.ChordPlayer;
namespace MidiChord
{
public class ChordLivePlayer : ChordPlayer
{
public bool PlayCountDown = false;
private System.Windows.Forms.Timer _playbackTimer = new System.Windows.Forms.Timer();
private OutputDevice _midiOutputDevice;
private int _countDown = 0;
public ChordLivePlayer(ChordList chordNotes, DrumList drumNotes)
: base(chordNotes, drumNotes)
{
_playbackTimer.Tick += new EventHandler(_playbackTimer_Tick);
}
public delegate void delegateBeatTick(int beat, int maxBeat, int position, string currentChord, string nextChord, string currentPart);
public event delegateBeatTick BeatTick;
public event Action SongEnded;
public int MidiOutputDeviceID
{
get
{
return _midiOutputDevice.DeviceID;
}
set
{
if (_midiOutputDevice != null)
{
_midiOutputDevice.Close();
_midiOutputDevice.Dispose();
}
_midiOutputDevice = new OutputDevice(value);
}
}
public void Mute()
{
_midiOutputDevice.Reset();
}
public void Delete()
{
if (_playbackTimer != null)
{
_playbackTimer.Stop();
_playbackTimer.Dispose();
_playbackTimer.Tick -= new EventHandler(_playbackTimer_Tick);
_playbackTimer = null;
}
if (_midiOutputDevice != null)
{
_midiOutputDevice.Close();
_midiOutputDevice.Dispose();
_midiOutputDevice = null;
}
}
void _playbackTimer_Tick(object sender, EventArgs e)
{
if (_countDown > 0)
{
playCountDown();
}
else
{
PlaySong();
}
}
protected void playCountDown()
{
if (EnableMetronome == true)
{
_midiOutputDevice.Send(_metronomeNoteOff);
if (_countDown % 4 == 0)
{
_midiOutputDevice.Send(_metronomeFirstBeatInstrument);
}
else
{
_midiOutputDevice.Send(_metronomeBeatInstument);
}
_midiOutputDevice.Send(_metronomeNoteOn);
}
// Notify client
if (BeatTick != null)
{
BeatTick( -_countDown, LastBeatIndex, 0, "", "", "Countdown");
}
_countDown--;
}
void PlaySong()
{
var midi = GetMidiCommandsAtBeat(CurrentBeatIndex);
playMidiCommands(midi);
NextBeatIndex();
if (midi.EndOfSong)
{
Stop();
if (SongEnded != null) SongEnded();
}
// Notify client
if (BeatTick != null)
{
// TODO: next chord
BeatTick(CurrentBeatIndex, LastBeatIndex, midi.ParserLine, midi.ChordName, "", midi.Part);
}
}
private void playMidiCommands(MidiCommands midi)
{
var order = new List<IMidiMessage>[] { midi.MetaData, midi.Metronome, midi.Chords, midi.Drums };
foreach(var list in order)
{
foreach (var cmd in list)
{
_midiOutputDevice.Send(cmd as ChannelMessage);
}
}
}
public void Start()
{
Reset();
_countDown = PlayCountDown ? 4 : 0;
SetDefaultMidiInstrument();
_playbackTimer.Interval = base.GetBeatTimeInMs(BeatsPerMinute);
_playbackTimer.Start();
}
private void SetDefaultMidiInstrument()
{
if (_midiOutputDevice != null)
{
var factory = new MidiCommandFactory();
_midiOutputDevice.Send(factory.Instrument(SongInstrument));
}
}
public void Stop()
{
_playbackTimer.Stop();
Reset();
}
public void Pauze()
{
_playbackTimer.Stop();
}
public void Continue()
{
_playbackTimer.Start();
}
internal void PlayNote(GeneralMidiInstrument instrument)
{
var factory = new MidiCommandFactory();
if (_midiOutputDevice != null)
{
_midiOutputDevice.Send(factory.Instrument(instrument));
_midiOutputDevice.Send(factory.NoteCOn());
}
}
}
}