diff --git a/MusicComposer/Program.cs b/MusicComposer/Program.cs index ace4e10..b9385ef 100644 --- a/MusicComposer/Program.cs +++ b/MusicComposer/Program.cs @@ -9,7 +9,7 @@ namespace MusicComposer { partial class Program { - static MidiOut midiOut = new MidiOut(0); + static MidiOut midiOut/* = new MidiOut(0)*/; static void Play(TwelveToneSet chord, int startFrom) { @@ -96,6 +96,31 @@ static void Main(string[] args) RandomSequencer(); return; } + else if (args[0] == "midikeyboard") + { + MidiKeyboard midiKeyboard; + try + { + midiKeyboard = new MidiKeyboard(); + } + catch (NAudio.MmException e) + { + Console.WriteLine("No midi keyboard detected"); + return; + } + + midiKeyboard.toneDown += (tone) => + { + Console.Write(midiKeyboard.toneset.ToString() + " "); + Console.Write(midiKeyboard.toneset.GetDisharmony()); + Console.WriteLine(); + }; + midiKeyboard.toneUp += (tone) => + { + }; + + while (true) { } + } MethodInfo miStatic = typeof(Compositions).GetMethod(args[0]); Func dgComposition = Delegate.CreateDelegate(typeof(Func), miStatic) as Func; diff --git a/MusicCore/MidiKeyboard.cs b/MusicCore/MidiKeyboard.cs new file mode 100644 index 0000000..313019a --- /dev/null +++ b/MusicCore/MidiKeyboard.cs @@ -0,0 +1,44 @@ +using NAudio.Midi; +using System; + +namespace MusicCore +{ + public class MidiKeyboard + { + static MidiOut midiOut = new MidiOut(0); + public readonly ToneSet toneset; + public event Action toneDown; + public event Action toneUp; + public bool PlayNotes { get; private set; } + public MidiKeyboard(bool playNotes = true) + { + PlayNotes = playNotes; + toneset = new ToneSet(); + MidiIn midiin = new MidiIn(0); + midiin.Start(); + midiin.MessageReceived += Midiin_MessageReceived; + } + + private void Midiin_MessageReceived(object sender, MidiInMessageEventArgs e) + { + if (e.MidiEvent.CommandCode == MidiCommandCode.NoteOn) + { + var noteonevent = e.MidiEvent as NoteOnEvent; + if (PlayNotes) + midiOut.Send(MidiMessage.StartNote(noteonevent.NoteNumber, 100, 1).RawData); + + toneset.Add(noteonevent.NoteNumber); + toneDown(noteonevent.NoteNumber); + } + else if (e.MidiEvent.CommandCode == MidiCommandCode.NoteOff) + { + var noteevent = e.MidiEvent as NoteEvent; + if (PlayNotes) + midiOut.Send(MidiMessage.StopNote(noteevent.NoteNumber, 100, 1).RawData); + + toneset.Remove(noteevent.NoteNumber); + toneUp(noteevent.NoteNumber); + } + } + } +} diff --git a/MusicCore/MusicCore.csproj b/MusicCore/MusicCore.csproj index c7c7874..eda1cac 100644 --- a/MusicCore/MusicCore.csproj +++ b/MusicCore/MusicCore.csproj @@ -37,6 +37,9 @@ + + ..\NAudio\NAudio.dll + @@ -55,6 +58,7 @@ + diff --git a/MusicCore/TwelveToneSet.cs b/MusicCore/TwelveToneSet.cs index 0b54346..2c424c1 100644 --- a/MusicCore/TwelveToneSet.cs +++ b/MusicCore/TwelveToneSet.cs @@ -42,6 +42,16 @@ public ToneSet(Tone tone, TwelveToneSet twelveToneSet) : this() bool[] tones; + public void Add(int tone) + { + tones[tone] = true; + } + + public void Remove(int tone) + { + tones[tone] = false; + } + public Tone GetLowestCommonHarmonic() { List> iters = new List>();