调用api声卡声音,调用API让声卡发出声音

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 一
        /// </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>
        ///弦乐合奏音色贰
        /// </summary>
        StringEnsemble2 = 49,
        /// <summary>
        ///合成弦乐合奏音色一
        /// </summary>
        SynthStrings1 = 50,
        /// <summary>
        ///合成弦乐合奏音色贰
        /// </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>
        ///合成铜管音色一
        /// </summary>
        SynthBrass1 = 62,
        /// <summary>
        ///合成铜管音色贰
        /// </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>
        ///合成主音一(方波)
        /// </summary>           
        Lead1Square = 80,
        /// <summary>
        ///合成主音2(锯齿波)
        /// </summary>
        Lead2Sawtooth = 81,
        /// <summary>
        ///合成主音三
        /// </summary>
        Lead3CaliopeLead = 82,
        /// <summary>
        ///合成主音四
        /// </summary>
        Lead4ChiffLead = 83,
        /// <summary>
        ///合成主音5
        /// </summary>
        Lead5Charang = 84,
        /// <summary>
        ///合成主音6(人声)
        /// </summary>
        Lead6Voice = 85,
        /// <summary>
        ///合成主音7(平行伍度)
        /// </summary>
        Lead7Fifths = 86,
        /// <summary>
        ///合成主音捌(贝司加主音)
        /// </summary>
        Lead8BassLead = 87,
        /// <summary>
        ///合成音色一(新世纪)
        /// </summary>
        Pad1NewAge = 88,
        /// <summary>
        ///合成音色二 (温暖)
        /// </summary>
        Pad2Warm = 89,
        /// <summary>
        ///合成音色三
        /// </summary>
        Pad3Polysynth = 90,
        /// <summary>
        ///合成音色4 (合唱)
        /// </summary>
        Pad4Choir = 91,
        /// <summary>
        ///合成音色伍
        /// </summary>
        Pad5Bowed = 92,
        /// <summary>
        ///合成音色陆 (金属声)
        /// </summary>
        Pad6Metallic = 93,
        /// <summary>
        ///合成音色柒 (光环)
        /// </summary>
        Pad7Halo = 94,
        /// <summary>
        ///合成音色捌
        /// </summary>
        Pad8Wweep = 5,
        /// <summary>
        ///合功效果 1 雨声
        /// </summary>

调用API让声卡发出声音,调用api声卡声音

壹.定义叁个枚举,乐器项目
    #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 一
        /// </summary>
        SynthBass1 = 38,
        /// <summary>
        /// 电子合成Bass 二
        /// </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>
        ///弦乐合奏音色一
        /// </summary>   
        StringEnsemble1 = 48,
        /// <summary>
        ///弦乐合奏音色二
        /// </summary>
        StringEnsemble2 = 49,
        /// <summary>
        ///合成弦乐合奏音色壹
        /// </summary>
        SynthStrings1 = 50,
        /// <summary>
        ///合成弦乐合奏音色贰
        /// </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>
        ///合成铜管音色一
        /// </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>
        ///合成主音一(方波)
        /// </summary>           
        Lead1Square = 80,
        /// <summary>
        ///合成主音2(锯齿波)
        /// </summary>
        Lead2Sawtooth = 81,
        /// <summary>
        ///合成主音三
        /// </summary>
        Lead3CaliopeLead = 82,
        /// <summary>
        ///合成主音4
        /// </summary>
        Lead4ChiffLead = 83,
        /// <summary>
        ///合成主音5
        /// </summary>
        Lead5Charang = 84,
        /// <summary>
        ///合成主音陆(人声)
        /// </summary>
        Lead6Voice = 85,
        /// <summary>
        ///合成主音7(平行5度)
        /// </summary>
        Lead7Fifths = 86,
        /// <summary>
        ///合成主音八(贝司加主音)
        /// </summary>
        Lead8BassLead = 87,
        /// <summary>
        ///合成音色一(新世纪)
        /// </summary>
        Pad1NewAge = 88,
        /// <summary>
        ///合成音色二 (温暖)
        /// </summary>
        Pad2Warm = 89,
        /// <summary>
        ///合成音色3
        /// </summary>
        Pad3Polysynth = 90,
        /// <summary>
        ///合成音色肆 (合唱)
        /// </summary>
        Pad4Choir = 91,
        /// <summary>
        ///合成音色5
        /// </summary>
        Pad5Bowed = 92,
        /// <summary>
        ///合成音色陆 (金属声)
        /// </summary>
        Pad6Metallic = 93,
        /// <summary>
        ///合成音色7 (光环)
        /// </summary>
        Pad7Halo = 94,
        /// <summary>
        ///合成音色八
        /// </summary>
        Pad8Wweep = 5,
        /// <summary>
        ///合效能果 1 雨声
        /// </summary>

        FX1Rain = 96,
        /// <summary>
        ///合功能果 2 音轨
        /// </summary>
        FX2Soundtrack = 97,
        /// <summary>
        ///合作用果 三 水晶
        /// </summary>
        FX3Crystal = 98,
        /// <summary>
        ///合功效果 4 大气
        /// </summary>
        FX4Atmosphere = 99,
        /// <summary>
        ///合功用果 伍 明亮
        /// </summary>
        FX5Brightness = 100,
        /// <summary>
        ///合效率果 6 妖魔鬼怪
        /// </summary>
        FX6Goblins = 101,
        /// <summary>
        ///合功用果 七 回声
        /// </summary>
        FX7Echoes = 102,
        /// <summary>
        ///合效率果 八 科学幻想
        /// </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
是函数指针时钦点}
    }

叁.开拓设备再次回到值
    public enum OpenMsg:uint
    {
        MMSYSERR_BADDEVICEID = 2,//{设备ID超界}
        MMSYSERR_ALLOCATED = 4, //{钦赐的能源已被分配}
        MMSYSERR_NOMEM = 7, //{无法分红或锁定内部存款和储蓄器}
        MIDIERR_NOMAP = 66,//{当前未有 MIDI 映射,
唯有开发映射程序才恐怕产生}
        MIDIERR_NODEVICE = 6八,//{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

 伍.定义变量
        #region 自定义变量
        bool isOpen = false;
        IntPtr DeviceHandle = IntPtr.Zero;
        public bool Opend => isOpen;

        #endregion
        #region 方法

陆.开拓设备。
        public void Open()
        {
            IntPtr device;//此处后续要用
            uint hand = midiOutOpen(out device, 0, IntPtr.Zero,
IntPtr.Zero, 0);
            isOpen = hand == 0;
            DeviceHandle = device;
        }
        /// <summary>
        /// 播放音响重返0表示成功!6贰一表示设备未展开
        /// </summary>
        /// <param name=”msg”></param>
        /// <returns></returns>
     
        public uint Play(uint msg,int
time)//播放,尝试了大多写法,发现msg * 25陆 +
0x0040三B90;基本符合规律,百度完善的办法声音沙哑(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方法中的数据1二,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();

http://www.bkjia.com/C\_jc/1261038.htmlwww.bkjia.comtruehttp://www.bkjia.com/C\_jc/1261038.htmlTechArticle调用API让声卡发出声音,调用api声卡声音
一.定义一个枚举,乐器项目 #region 乐器定义 public enum MusicNo:uint {
/// summary /// //大钢琴 /// /summary…

        FX1Rain = 96,
        /// <summary>
        ///协效率果 2 音轨
        /// </summary>
        FX2Soundtrack = 97,
        /// <summary>
        ///合效用果 三 水晶
        /// </summary>
        FX3Crystal = 98,
        /// <summary>
        ///合成效果 四 大气
        /// </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>
        ///10三弦筝(东瀛)
        /// </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 = 0x000一千0,//{当 dwCallback
是窗口句柄时钦赐}
        CALLBACK_FUNCTION = 0x0002000,// {当 dwCallback
是函数指针时钦点}
    }

叁.开拓设备重返值
    public enum OpenMsg:uint
    {
        MMSYSERR_BADDEVICEID = 2,//{设备ID超界}
        MMSYSERR_ALLOCATED = 4, //{钦命的能源已被分配}
        MMSYSERR_NOMEM = 7, //{不能够分红或锁定内部存款和储蓄器}
        MIDIERR_NOMAP = 6六,//{当前从不 MIDI 映射,
唯有开发映射程序才也许发生}
        MIDIERR_NODEVICE = 6八,//{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

 伍.定义变量
        #region 自定义变量
        bool isOpen = false;
        IntPtr DeviceHandle = IntPtr.Zero;
        public bool Opend => isOpen;

        #endregion
        #region 方法

陆.打开设备。
        public void Open()
        {
            IntPtr device;//此处后续要用
            uint hand = midiOutOpen(out device, 0, IntPtr.Zero,
IntPtr.Zero, 0);
            isOpen = hand == 0;
            DeviceHandle = device;
        }
        /// <summary>
        /// 播放音响重返0表示成功!6二壹意味着设备未打开
        /// </summary>
        /// <param name=”msg”></param>
        /// <returns></returns>
     
        public uint Play(uint msg,int
time)//播放,尝试了重重写法,发现msg * 25陆 +
0x0040③B90;基本平常,百度健全的不贰秘技声音沙哑(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方法中的数据1二,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();