Created
August 28, 2009 02:36
-
-
Save atsushieno/176751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Threading; | |
using PortMidiSharp; | |
namespace Commons.Music.Midi | |
{ | |
public class Driver | |
{ | |
public static void Main () | |
{ | |
int inId, outId; | |
var a = new List<MidiDeviceInfo> (MidiDeviceManager.AllDevices); | |
foreach (var dev in a) | |
if (dev.IsInput) | |
Console.WriteLine ("ID {0}: {1}", dev.ID, dev.Name); | |
Console.WriteLine ("Type number to select MIDI In Device to use (type anything else to quit)"); | |
if (!int.TryParse (Console.ReadLine (), out inId)) | |
return; | |
foreach (var dev in MidiDeviceManager.AllDevices) | |
if (dev.IsOutput) | |
Console.WriteLine ("ID {0}: {1}", dev.ID, dev.Name); | |
Console.WriteLine ("Type number to select MIDI Out Device to use (type anything else to quit)"); | |
if (!int.TryParse (Console.ReadLine (), out outId)) | |
return; | |
new BulkDump ().Run (inId, outId); | |
} | |
} | |
public class BulkDump | |
{ | |
public void Run (int indev, int outdev) | |
{ | |
var sysex = new byte [] { | |
0xF0, 0x41, 0x10, 0x42, 0x11, // dev/cmd | |
// addr | |
0x0C, 0x00, 0x00, | |
// size | |
0x00, 0x00, 0x00, | |
// chksum/EOX | |
0, 0xF7 }; | |
int chksum = 0; | |
for (int i = 5; i < 11; i++) | |
chksum += sysex [i]; | |
sysex [sysex.Length - 2] = (byte) (0x80 - chksum % 0x80); | |
MidiDeviceManager.OpenOutput (outdev).WriteSysEx (0, sysex); | |
var dev = MidiDeviceManager.OpenInput (indev); | |
Console.WriteLine ("Sent dump operation. Type [CR] to stop receive."); | |
new Action (delegate { Loop (dev); }).BeginInvoke (null, null); | |
Console.ReadLine (); | |
loop = false; | |
wait_handle.WaitOne (); | |
} | |
ManualResetEvent wait_handle = new ManualResetEvent (false); | |
bool loop; | |
void Loop (MidiInput dev) | |
{ | |
while (loop) { | |
var events = new MidiEvent [0x10000]; | |
int size = dev.Read (events, events.Length); | |
//Console.WriteLine ("Read {0} events", size); | |
for (int i = 0; i < size; i++) { | |
var ev = events [i]; | |
Console.Write ("{0:X}[{1:X}] ", ev.Message.Value, ev.Timestamp); | |
} | |
} | |
wait_handle.Set (); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment