Controller.cs
//**********************************************************
//* MidiOut exaple *
//* Author: D. Zouchinski *
//* http://zouchinski.co.uk *
//* Copyright @ 2011-2012 *
//**********************************************************
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MidiOut
{
public class Controller : IDisposable
{
private IntPtr hMidi = IntPtr.Zero;
public Controller(uint id)
{
NativeApi.midiOutOpen(out hMidi, new IntPtr(id), IntPtr.Zero, IntPtr.Zero, 0);
}
protected void Dispose(bool disposing)
{
if (hMidi != IntPtr.Zero)
{
if (NativeApi.midiOutReset(hMidi) == 0 &&
NativeApi.midiOutClose(hMidi) == 0)
hMidi = IntPtr.Zero;
}
}
~Controller()
{
Dispose(false);
}
public bool SetInstrument(uint channel, uint instrument)
{
return NativeApi.midiOutShortMsg(hMidi, 0xC0 | channel | (instrument << 8)) == 0;
}
public bool PlayNote(uint channel, uint note)
{
uint velosity = 0x7F;
return NativeApi.midiOutShortMsg(hMidi, 0x90 | (channel & 0xF) | ((note & 0x7F) << 8) | (velosity & 0x7F) << 16) == 0;
}
public bool StopNote(uint channel, uint note)
{
return NativeApi.midiOutShortMsg(hMidi, 0x80 | (channel & 0xF) | ((note & 0x7F) << 8)) == 0;
}
public static string[] GetMidiOutDevices()
{
uint count = NativeApi.midiOutGetNumDevs();
string[] devices = new string[count];
for (uint id = 0; id < count; id++)
{
NativeApi.MIDIOUTCAPS caps = new NativeApi.MIDIOUTCAPS();
NativeApi.midiOutGetDevCaps(id, ref caps, (uint)Marshal.SizeOf(caps));
devices[id] = caps.szPname;
}
return devices;
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
}
#endregion
public class InstrumentCategory
{
private static InstrumentCategory[] categories = new InstrumentCategory[]
{
new InstrumentCategory()
{
Name = "Piano Timbres",
Instruments = new string[]
{
"Acoustic Grand Piano",
"Bright Acoustic Piano",
"Electric Grand Piano",
"Honky-tonk Piano",
"Rhodes Piano",
"Chorused Piano",
"Harpsichord",
"Clavinet"
}
},
new InstrumentCategory()
{
Name = "Chromatic Percussion",
Instruments = new string[]
{
"Celesta",
"Glockenspiel",
"Music Box",
"Vibraphone",
"Marimba",
"Xylophone",
"Tubular Bells",
"Dulcimer"
}
},
new InstrumentCategory()
{
Name = "Organ Timbres",
Instruments = new string[]
{
"Hammond Organ",
"Percussive Organ",
"Rock Organ",
"Church Organ",
"Reed Organ",
"Accordion",
"Harmonica",
"Tango Accordion"
}
},
new InstrumentCategory()
{
Name = "Guitar Timbres",
Instruments = new string[]
{
"Acoustic Nylon Guitar",
"Acoustic Steel Guitar",
"Electric Jazz Guitar",
"Electric Clean Guitar",
"Electric Muted Guitar",
"Overdriven Guitar",
"Distortion Guitar",
"Guitar Harmonics"
}
},
new InstrumentCategory()
{
Name = "Bass Timbres",
Instruments = new string[]
{
"Acoustic Bass",
"Fingered Electric Bass",
"Plucked Electric Bass",
"Fretless Bass",
"Slap Bass 1",
"Slap Bass 2",
"Synth Bass 1",
"Synth Bass 2"
}
},
new InstrumentCategory()
{
Name = "String Timbres",
Instruments = new string[]
{
"Violin",
"Viola",
"Cello",
"Contrabass",
"Tremolo Strings",
"Pizzicato Strings",
"Orchestral Harp",
"Timpani"
}
},
new InstrumentCategory()
{
Name = "Ensemble Timbres",
Instruments = new string[]
{
"String Ensemble 1",
"String Ensemble 2",
"Synth Strings 1",
"Synth Strings 2",
"Choir \"Aah\"",
"Choir \"Ooh\"",
"Synth Voice",
"Orchestral Hit"
}
},
new InstrumentCategory()
{
Name = "Brass Timbres",
Instruments = new string[]
{
"Trumpet",
"Trombone",
"Tuba",
"Muted Trumpet",
"French Horn",
"Brass Section",
"Synth Brass 1",
"Synth Brass 2"
}
},
new InstrumentCategory()
{
Name = "Reed Timbres",
Instruments = new string[]
{
"Soprano Sax",
"Alto Sax",
"Tenor Sax",
"Baritone Sax",
"Oboe",
"English Horn",
"Bassoon",
"Clarinet"
}
},
new InstrumentCategory()
{
Name = "Pipe Timbres",
Instruments = new string[]
{
"Piccolo",
"Flute",
"Recorder",
"Pan Flute",
"Bottle Blow",
"Shakuhachi",
"Whistle",
"Ocarina"
}
},
new InstrumentCategory()
{
Name = "Synth Lead",
Instruments = new string[]
{
"Square Wave Lead",
"Sawtooth Wave Lead",
"Calliope Lead",
"Chiff Lead",
"Charang Lead",
"Voice Lead",
"Fifths Lead",
"Bass Lead"
}
},
new InstrumentCategory()
{
Name = "Synth Pad",
Instruments = new string[]
{
"New Age Pad",
"Warm Pad",
"Polysynth Pad",
"Choir Pad",
"Bowed Pad",
"Metallic Pad",
"Halo Pad",
"Sweep Pad"
}
},
new InstrumentCategory()
{
Name = "Synth Effects",
Instruments = new string[]
{
"Rain Effect",
"Soundtrack Effect",
"Crystal Effect",
"Atmosphere Effect",
"Brightness Effect",
"Goblins Effect",
"Echoes Effect",
"Sci-Fi Effect"
}
},
new InstrumentCategory()
{
Name = "Ethnic Timbres",
Instruments = new string[]
{
"Sitar",
"Banjo",
"Shamisen",
"Koto",
"Kalimba",
"Bagpipe",
"Fiddle",
"Shanai"
}
},
new InstrumentCategory()
{
Name = "Sound Effects",
Instruments = new string[]
{
"Tinkle Bell",
"Agogo",
"Steel Drums",
"Woodblock",
"Taiko Drum",
"Melodic Tom",
"Synth Drum",
"Reverse Cymbal"
}
}
};
private InstrumentCategory()
{
}
public string Name { get; private set; }
public string[] Instruments { get; private set; }
public static InstrumentCategory[] GetInstrumentCategories()
{
return categories;
}
}
public static InstrumentCategory[] GetInstrumentCategories()
{
return InstrumentCategory.GetInstrumentCategories();
}
}
}