1.定义一个枚举,乐器类型 #region 乐器定义 public enum MusicNo:uint { /// <summary> /// //大钢琴 /// </summary> AcousticGrandPiano = 0, /// <summary> ///明亮的钢琴 /// </summary> BrightAcousticPiano = 1, /// <summary> ///电子琴 /// </summary> ElectricGrandPiano = 2, /// <summary> ///酒吧钢琴 /// </summary> HonkyTonkPiano = 3, /// <summary> ///柔和钢琴 /// </summary> RhodesPiano = 4, /// <summary> ///合唱效果钢琴 /// </summary> ChorusedPiano = 5, /// <summary> ///羽管键琴 /// </summary> Harpsichord = 6, /// <summary> ///克拉维科特琴 /// </summary> Clavichord = 7, /// <summary> ///钢片琴 /// </summary> Celesta = 8, /// <summary> ///钟琴 /// </summary> Glockenspiel = 9, /// <summary> ///八音盒 /// </summary> Musicbox = 10, /// <summary> ///颤音琴 /// </summary> Vibraphone = 11, /// <summary> ///马林巴 /// </summary> Marimba = 12, /// <summary> ///木琴 /// </summary> Xylophone = 13, /// <summary> ///管钟 /// </summary> TubularBells = 14, /// <summary> ///大扬琴 /// </summary> Dulcimer = 15, /// <summary> /// 击杆风琴 /// </summary> HammondOrgan = 16, /// <summary> /// 打击式风琴 /// </summary> PercussiveOrgan = 17, /// <summary> /// 摇滚风琴 /// </summary> RockOrgan = 18, /// <summary> /// 教堂风琴 /// </summary> ChurchOrgan = 19, /// <summary> /// 簧管风琴 /// </summary> ReedOrgan = 20, /// <summary> /// 手风琴 /// </summary> Accordian = 21, /// <summary> /// 口琴 /// </summary> Harmonica = 22, /// <summary> ///探戈手风琴 /// </summary> TangoAccordian = 23, /// <summary> /// 尼龙弦吉他 /// </summary> AcousTicGuitarNylon = 24, /// <summary> /// 钢弦吉他 /// </summary> AcousTicGuitarSteel = 25, /// <summary> /// 爵士电吉他 /// </summary> ElectricGuitarJazz = 26, /// <summary> /// 清音电吉他 /// </summary> ElectricGuitarClean = 27, /// <summary> /// 闷音电吉他 /// </summary> ElectricGuitarMuted = 28, /// <summary> /// 加驱动效果的电吉他 /// </summary> OverdrivenGuitar = 29, /// <summary> /// 加失真效果的电吉他 /// </summary> DistortionGuitar = 30, /// <summary> /// 吉他和音 /// </summary> GuitarHarmonics = 31, /// <summary> ///大贝司(声学贝司) /// </summary> AcousticBass = 32, /// <summary> /// 电贝司(指弹) /// </summary> ElectricBassfinger = 33, /// <summary> /// 电贝司(拨片) /// </summary> ElectricBassPick = 34, /// <summary> /// 无品贝司 /// </summary> FretlessBass = 35, /// <summary> /// 掌击Bass 1 /// </summary> SlapBass1 = 36, /// <summary> /// 掌击Bass 2 /// </summary> SlapBass2 = 37, /// <summary> /// 电子合成Bass 1 /// </summary> SynthBass1 = 38, /// <summary> /// 电子合成Bass 2 /// </summary> SynthBass2 = 39, /// <summary> /// 小提琴 /// </summary> Violin = 40, /// <summary> /// 中提琴 /// </summary> Viola = 41, /// <summary> /// 大提琴 /// </summary> Cello = 42, /// <summary> /// 低音大提琴 /// </summary> Contrabass = 43, /// <summary> ///弦乐群颤音音色 /// </summary> TremoloStrings = 44, /// <summary> ///弦乐群拨弦音色 /// </summary> PizzicatoStrings = 45, /// <summary> ///竖琴 /// </summary> OrchestralHarp = 46, /// <summary> /// 定音鼓 /// </summary> Timpani = 47, /// <summary> ///弦乐合奏音色1 /// </summary> StringEnsemble1 = 48, /// <summary> ///弦乐合奏音色2 /// </summary> StringEnsemble2 = 49, /// <summary> ///合成弦乐合奏音色1 /// </summary> SynthStrings1 = 50, /// <summary> ///合成弦乐合奏音色2 /// </summary> SynthStrings2 = 51, /// <summary> ///人声合唱“啊” /// </summary> ChoirAahs = 52, /// <summary> ///人声“嘟” /// </summary> VoiceOohs = 53, /// <summary> ///合成人声 /// </summary> SynthVoice = 54, /// <summary> ///管弦敲击齐奏 /// </summary> OrchestraHit = 55, /// <summary> ///小号 /// </summary> Trumpet = 56, /// <summary> ///长号 /// </summary> Trombone = 57, /// <summary> ///大号 /// </summary> Tuba = 58, /// <summary> ///加弱音器小号 /// </summary> MutedTrumpet = 59, /// <summary> ///法国号(圆号) /// </summary> FrenchHorn = 60, /// <summary> ///铜管组(铜管乐器合奏音色) /// </summary> BrassSection = 61, /// <summary> ///合成铜管音色1 /// </summary> SynthBrass1 = 62, /// <summary> ///合成铜管音色2 /// </summary> SynthBrass2 = 63, /// <summary> ///高音萨克斯风 /// </summary> SopranoSax = 64, /// <summary> ///次中音萨克斯风 /// </summary> AltoSax = 65, /// <summary> ///中音萨克斯风 /// </summary> TenorSax = 66, /// <summary> ///低音萨克斯风 /// </summary> BaritoneSax = 67, /// <summary> ///双簧管 /// </summary> Oboe = 68, /// <summary> ///英国管 /// </summary> EnglishHorn = 69, /// <summary> ///巴松(大管) /// </summary> Bassoon = 70, /// <summary> ///单簧管(黑管) /// </summary> Clarinet = 71, /// <summary> ///短笛 /// </summary> Piccolo = 72, /// <summary> ///长笛 /// </summary> Flute = 73, /// <summary> ///竖笛 /// </summary> Recorder = 74, /// <summary> ///排箫 /// </summary> PanFlute = 75, /// <summary> ///[中文名称暂缺] /// </summary> BottleBlow = 76, /// <summary> ///日本尺八 /// </summary> Shakuhachi = 77, /// <summary> ///口哨声 /// </summary> Whistle = 78, /// <summary> ///奥卡雷那 /// </summary> Ocarina = 79, /// <summary> ///合成主音1(方波) /// </summary> Lead1Square = 80, /// <summary> ///合成主音2(锯齿波) /// </summary> Lead2Sawtooth = 81, /// <summary> ///合成主音3 /// </summary> Lead3CaliopeLead = 82, /// <summary> ///合成主音4 /// </summary> Lead4ChiffLead = 83, /// <summary> ///合成主音5 /// </summary> Lead5Charang = 84, /// <summary> ///合成主音6(人声) /// </summary> Lead6Voice = 85, /// <summary> ///合成主音7(平行五度) /// </summary> Lead7Fifths = 86, /// <summary> ///合成主音8(贝司加主音) /// </summary> Lead8BassLead = 87, /// <summary> ///合成音色1(新世纪) /// </summary> Pad1NewAge = 88, /// <summary> ///合成音色2 (温暖) /// </summary> Pad2Warm = 89, /// <summary> ///合成音色3 /// </summary> Pad3Polysynth = 90, /// <summary> ///合成音色4 (合唱) /// </summary> Pad4Choir = 91, /// <summary> ///合成音色5 /// </summary> Pad5Bowed = 92, /// <summary> ///合成音色6 (金属声) /// </summary> Pad6Metallic = 93, /// <summary> ///合成音色7 (光环) /// </summary> Pad7Halo = 94, /// <summary> ///合成音色8 /// </summary> Pad8Wweep = 5, /// <summary> ///合成效果 1 雨声 /// </summary>
FX1Rain = 96, /// <summary> ///合成效果 2 音轨 /// </summary> FX2Soundtrack = 97, /// <summary> ///合成效果 3 水晶 /// </summary> FX3Crystal = 98, /// <summary> ///合成效果 4 大气 /// </summary> FX4Atmosphere = 99, /// <summary> ///合成效果 5 明亮 /// </summary> FX5Brightness = 100, /// <summary> ///合成效果 6 鬼怪 /// </summary> FX6Goblins = 101, /// <summary> ///合成效果 7 回声 /// </summary> FX7Echoes = 102, /// <summary> ///合成效果 8 科幻 /// </summary> FX8Scifi = 103, /// <summary> ///西塔尔(印度) /// </summary> Sitar = 104, /// <summary> ///班卓琴(美洲) /// </summary> Banjo = 105, /// <summary> ///三昧线(日本) /// </summary> Shamisen = 106, /// <summary> ///十三弦筝(日本) /// </summary> Koto = 107, /// <summary> ///卡林巴 /// </summary> Kalimba = 108, /// <summary> ///风笛 /// </summary> Bagpipe = 109, /// <summary> ///民族提琴 /// </summary> Fiddle = 110, /// <summary> ///山奈 /// </summary> Shanai = 111, /// <summary> /// 叮当铃 /// </summary> TinkleBell = 112, /// <summary> ///[中文名称暂缺] /// </summary> Agogo = 113, /// <summary> ///钢鼓 /// </summary> SteelDrums = 114, /// <summary> ///木鱼 /// </summary> Woodblock = 115, /// <summary> ///太鼓 /// </summary> TaikoDrum = 116, /// <summary> ///通通鼓 /// </summary> MelodicTom = 117, /// <summary> ///合成鼓 /// </summary> SynthDrum = 118, /// <summary> ///铜钹 /// </summary> ReverseCymbal = 119, /// <summary> ///吉他换把杂音 /// </summary> GuitarFretNoise = 120, /// <summary> ///呼吸声 /// </summary> BreathNoise = 121, /// <summary> ///海浪声 /// </summary> Seashore = 122, /// <summary> ///鸟鸣 /// </summary> BirdTweet = 123, /// <summary> ///电话铃 /// </summary> TelephoneRing = 124, /// <summary> ///直升机 /// </summary> Helicopter = 125, /// <summary> ///鼓掌 /// </summary> Applause = 126, /// <summary> /// Gunshot /// </summary> Gunshot = 127, }
2.callback public enum OpenFlag:uint { CALLBACK_NULL = 0x00000000,//{当 dwCallback 是 nil 时指定} CALLBACK_WINDOW = 0x00010000,//{当 dwCallback 是窗口句柄时指定} CALLBACK_FUNCTION = 0x0003000,// {当 dwCallback 是函数指针时指定} }
3.打开设备返回值 public enum OpenMsg:uint { MMSYSERR_BADDEVICEID = 2,//{设备ID超界} MMSYSERR_ALLOCATED = 4, //{指定的资源已被分配} MMSYSERR_NOMEM = 7, //{不能分配或锁定内存} MIDIERR_NOMAP = 66,//{当前没有 MIDI 映射, 只有打开映射程序才可能发生} MIDIERR_NODEVICE = 68,//{MIDI 映射中的端口不存在, 只有打开映射程序才可能发生} } #endregion
4.midi帮助类 public class MIDIHelper { #region API [DllImport("winmm.dll")] static extern uint midiOutOpen(out IntPtr lphMidiOut, uint uDeviceID, IntPtr dwCallback, IntPtr dwInstance, uint dwFlags);//打开数字乐器接口输出 [DllImport("winmm.dll")] static extern uint midiOutClose(IntPtr hMidiOut);//关闭数字乐器接口输出 [DllImport("winmm.dll")] static extern uint midiOutShortMsg(IntPtr hMidiOut, uint dwMsg);//发送数字乐器接口消息
#endregion
5.定义变量 #region 自定义变量 bool isOpen = false; IntPtr DeviceHandle = IntPtr.Zero; public bool Opend => isOpen;
#endregion #region 方法
6.打开设备。 public void Open() { IntPtr device;//此处后续要用 uint hand = midiOutOpen(out device, 0, IntPtr.Zero, IntPtr.Zero, 0); isOpen = hand == 0; DeviceHandle = device; } /// <summary> /// 播放声音返回0表示成功!621表示设备未打开 /// </summary> /// <param name="msg"></param> /// <returns></returns> public uint Play(uint msg,int time)//播放,尝试了很多写法,发现msg * 256 + 0x00403B90;基本正常,百度百科的方法声音沙哑(msg*256+144+volume*655536+channel) { if (isOpen && DeviceHandle != IntPtr.Zero) { uint data = msg * 256 + 0x00403B90; Console.WriteLine(data); uint ms = midiOutShortMsg(DeviceHandle,data); System.Threading.Thread.Sleep(time); return msg; } else return 621; } public uint ChangeInstruments(MusicNo musicNo)//改变乐器 { return midiOutShortMsg(DeviceHandle, Convert.ToUInt32(192 + (uint)musicNo * 256)); } public void Close()//关闭设备 { uint msg = midiOutClose(DeviceHandle); } #endregion }
接下来看看调用例子:
其中play方法中的数据12,50,...............摘自网上
MIDIHelper helper = new Gaofajin.Shell.MIDIHelper(); helper.Open(); if (helper.Opend) { helper.ChangeInstruments(Gaofajin.Shell.MusicNo.Piccolo);
helper.Play(12, 250);
helper.Play(12, 250);
helper.Play(15, 250);
helper.Play(17, 250);
helper.Play(19, 1000);
helper.Play(24, 700);
helper.Play(19, 250);
helper.Play(22, 200);
helper.Play(00, 250);
helper.Play(22, 900);
helper.Play(19, 400);
helper.Play(17, 400);
helper.Play(19, 400);
helper.Play(22, 400);
helper.Play(24, 250);
helper.Play(22, 250);
helper.Play(19, 1500);
helper.Play(00, 300);
helper.Play(19, 1000);
helper.Play(24, 700);
helper.Play(19, 250);
helper.Play(22, 200);
helper.Play(00, 250);
helper.Play(22, 900);
helper.Play(19, 400);
helper.Play(17, 400);
helper.Play(19, 400);
helper.Play(22, 400);
helper.Play(24, 250);
helper.Play(22, 250);
helper.Play(19, 1000);
helper.Play(00, 300);
helper.Play(19, 400);
helper.Play(17, 250);
helper.Play(15, 600);
helper.Play(00, 100);
helper.Play(15, 250);
helper.Play(17, 400);
helper.Play(19, 400);
helper.Play(17, 100);
helper.Play(00, 250);
helper.Play(17, 500);
helper.Play(00, 100);
helper.Play(17, 400);
helper.Play(15, 250);
helper.Play(12, 250);
helper.Play(00, 100);
helper.Play(12, 250);
helper.Play(15, 250);
helper.Play(17, 400);
helper.Play(19, 400);
helper.Play(22, 600);
helper.Play(24, 250);
helper.Play(22, 400);
helper.Play(19, 250);
helper.Play(17, 250);
helper.Play(15, 600);
helper.Play(17, 600);
helper.Play(19, 400);
helper.Play(17, 150);
helper.Play(00, 250);
helper.Play(17, 600);
helper.Play(15, 600);
helper.Play(12, 300);
helper.Play(00, 100);
helper.Play(12, 250);
helper.Play(15, 250);
helper.Play(17, 250);
helper.Play(19, 400);
helper.Play(22, 250);
helper.Play(24, 700);
helper.Play(00, 100);
helper.Play(24, 500);
} helper.Close();
|