Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Midi keyboard #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion MusicComposer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<Melody12Tone> dgComposition = Delegate.CreateDelegate(typeof(Func<Melody12Tone>), miStatic) as Func<Melody12Tone>;
Expand Down
44 changes: 44 additions & 0 deletions MusicCore/MidiKeyboard.cs
Original file line number Diff line number Diff line change
@@ -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<int> toneDown;
public event Action<int> 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);
}
}
}
}
4 changes: 4 additions & 0 deletions MusicCore/MusicCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="NAudio">
<HintPath>..\NAudio\NAudio.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -55,6 +58,7 @@
<Compile Include="Melody\MelodyComposite.cs" />
<Compile Include="Melody\MelodyDiffEnd.cs" />
<Compile Include="Melody\MelodyReversed.cs" />
<Compile Include="MidiKeyboard.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rhythm\RhythmComposite.cs" />
Expand Down
10 changes: 10 additions & 0 deletions MusicCore/TwelveToneSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEnumerator<Tone>> iters = new List<IEnumerator<Tone>>();
Expand Down