調用API讓音效卡發出聲音

来源:https://www.cnblogs.com/gfjin/archive/2018/01/02/8179078.html
-Advertisement-
Play Games

1.定義一個枚舉,樂器類型 #region 樂器定義 public enum MusicNo:uint { /// <summary> /// //大鋼琴 /// </summary> AcousticGrandPiano = 0, /// <summary> ///明亮的鋼琴 /// </summ ...


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();


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...