/
PianoController.cs
162 lines (145 loc) · 5.76 KB
/
PianoController.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
using System;
using System.Collections.Generic;
using NAudio.Midi;
namespace PianoEnhancer
{
internal class PianoController : IDisposable
{
private MidiIn _inDevice;
private MidiOut _outDevice;
private bool _initialized;
private readonly Dictionary<int,HashSet<KeyValuePair<int,int>>> _playingNotes;
private readonly ChordFormer _chordFormer;
public TrackRecorder Recorder { get; }
public PlayBack PlayBack { get; }
public PianoController()
{
Recorder = new TrackRecorder();
PlayBack = new PlayBack();
PlayBack.SendControl += SendControl;
_playingNotes = new Dictionary<int, HashSet<KeyValuePair<int, int>>>();
_chordFormer = new ChordFormer();
_chordFormer.ChordFormed += ChordReceived;
}
public void Play()
{
PlayBack.Play(Recorder.Recording);
}
public void Stop()
{
PlayBack.Stop();
}
public void SetVoice(VoiceComposition newVoice)
{
PlayBack.CurrentVoice = newVoice;
}
public static IEnumerable<MidiInCapabilities> GetConnectedInputDevices()
{
for(var i = 0; i < MidiIn.NumberOfDevices; ++i)
{
yield return MidiIn.DeviceInfo(i);
}
}
public static IEnumerable<MidiOutCapabilities> GetConnectedOutDevices()
{
for(var i = 0; i < MidiOut.NumberOfDevices; ++i)
{
yield return MidiOut.DeviceInfo(i);
}
}
public void Init(int inDeviceNo, int outDeviceNo)
{
if (_initialized)
{
_inDevice.Stop();
_inDevice.Close();
_inDevice.Dispose();
_outDevice.Close();
_outDevice.Dispose();
}
_inDevice = new MidiIn(inDeviceNo);
_outDevice = new MidiOut(outDeviceNo);
_inDevice.MessageReceived += MessageReceived;
_inDevice.ErrorReceived += ErrorReceived;
_inDevice.Start();
_initialized = true;
}
private void RemovePlayingNote(int note)
{
if (!_playingNotes.ContainsKey(note)) return;
foreach (var (channel, noteNr) in _playingNotes[note])
{
var midi = new NoteOnEvent(0,channel,noteNr,0,1);
_outDevice.Send(midi.GetAsShortMessage());
}
_playingNotes.Remove(note);
}
private void MessageReceived(object sender, MidiInMessageEventArgs e)
{
if (e.MidiEvent.CommandCode == MidiCommandCode.TimingClock || e.MidiEvent.CommandCode == MidiCommandCode.AutoSensing) return;
if (e.MidiEvent is NoteEvent note && note.Channel == Configuration.PlayChannel)
{
if (PlayBack.CurrentVoice.ChannelAllocation == null)
{
_outDevice.Send(note.GetAsShortMessage());
}
else
{
if (note.Velocity == 0)
{
RemovePlayingNote(note.NoteNumber);
}
else
{
if (!_playingNotes.ContainsKey(note.NoteNumber))
_playingNotes[note.NoteNumber] = new HashSet<KeyValuePair<int, int>>();
for (var i = 0; i < 16; ++i)
{
var newNote = new NoteOnEvent(0, i + 1, note.NoteNumber, note.Velocity, 1);
if (i >= PlayBack.CurrentVoice.ChannelAllocation.Length) continue;
var channelSide = PlayBack.CurrentVoice.ChannelAllocation[i];
var splitPoint = PlayBack.CurrentVoice.SplitPoint;
newNote.NoteNumber = PlayBack.CurrentVoice.ChannelTransposition[i] + note.NoteNumber;
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
switch (channelSide)
{
case VoiceComposition.ChannelSide.Left when note.NoteNumber > splitPoint:
case VoiceComposition.ChannelSide.Right when note.NoteNumber <= splitPoint:
case VoiceComposition.ChannelSide.None:
continue;
default:
_outDevice.Send(newNote.GetAsShortMessage());
_playingNotes[note.NoteNumber]
.Add(new KeyValuePair<int, int>(i + 1, newNote.NoteNumber));
break;
}
}
}
}
if (note.Velocity != 0) PlayBack.NoteReceived(note.NoteNumber);
}
else if (!(e.MidiEvent is NoteEvent) && !(e.MidiEvent is PatchChangeEvent))
{
_outDevice.Send(e.MidiEvent.GetAsShortMessage());
}
_chordFormer.NoteReceived(e.MidiEvent);
}
private void ChordReceived(Chord chord)
{
Recorder.RecordChord(chord);
}
private static void ErrorReceived(object sender, MidiInMessageEventArgs e)
{
Console.Error.WriteLine("Error: {0}",e.MidiEvent);
}
private void SendControl(MidiEvent e)
{
_outDevice.Send(e.GetAsShortMessage());
}
public void Dispose()
{
_inDevice?.Dispose();
_outDevice?.Dispose();
}
}
}