{"id":938,"date":"2021-06-30T21:45:06","date_gmt":"2021-06-30T12:45:06","guid":{"rendered":"https:\/\/machiaworx.net\/?p=938"},"modified":"2021-07-01T09:00:40","modified_gmt":"2021-07-01T00:00:40","slug":"unity%e3%81%a7midi%e5%87%ba%e5%8a%9b%e3%82%92%e5%8f%96%e3%82%8a%e6%89%b1%e3%81%86%e3%81%9f%e3%82%81%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e3%80%82","status":"publish","type":"post","link":"https:\/\/machiaworx.net\/?p=938","title":{"rendered":"Unity\u3067MIDI\u51fa\u529b\u3092\u53d6\u308a\u6271\u3046\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3002"},"content":{"rendered":"<h3>\u6982\u8981<\/h3>\n<p>Unity\u3067MIDI\u51fa\u529b\u3092\u53d6\u308a\u6271\u3046\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<p>Unity\u3067MIDI\u5165\u529b\u3084OSC\u3092\u6271\u3046\u305f\u3081\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u5b58\u5728\u3059\u308b\u304c\u3001<br \/>\nMIDI\u51fa\u529b\u306b\u95a2\u3059\u308b\u30b3\u30fc\u30c9\u306f\u6b86\u3069\u4e8b\u4f8b\u304c\u306a\u3044\u3002<\/p>\n<p>\u3053\u308c\u306fMIDI\u51fa\u529b\u3092\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u3069\u304a\u308a\u306b\u884c\u3046\u30b1\u30fc\u30b9\u304c\u5c11\u306a\u3044\u304b\u3089\u3060\u3068\u8003\u3048\u308b\u3002<\/p>\n<p>\u305f\u3060\u73fe\u5728\u4f5c\u6210\u4e2d\u306e\u30e9\u30a4\u30d6\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u30c4\u30fc\u30eb\u306b\u304a\u3044\u3066\u3001\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u3069\u304a\u308a\u306bMIDI\u30c7\u30fc\u30bf\u3092\u51fa\u529b\u3059\u308b\u8981\u4ef6\u304c\u767a\u751f\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u81ea\u5206\u3067\u66f8\u304f\u7fbd\u76ee\u306b\u306a\u3063\u305f\u3002<\/p>\n<p>OSC\u3060\u3051\u51fa\u529b\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u5834\u5408\u3001\u5bfe\u5fdc\u3067\u304d\u308bDAW\u304cAbletonLive\u304f\u3089\u3044\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3002<br \/>\n\u3082\u3057\u304f\u306fMax\u3068\u306e\u9023\u643a\u304c\u5fc5\u9808\u306b\u306a\u308b\u3068\u304b\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u4ed6DAW\u3068\u306e\u9023\u643a\u3082\u53ef\u80fd\u306b\u306a\u308bMIDI\u51fa\u529b\u304c\u5fc5\u8981\u306b\u306a\u3063\u305f\u3001\u3068\u3002<\/p>\n<h3>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n<p>\u3059\u3054\u304f\u9069\u5f53\u306b\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u52d5\u4f5c\u306f\u78ba\u8a8d\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing System.Runtime.InteropServices;\r\nusing System.Text;\r\nusing System;\r\n\r\npublic class MidiOutput : MonoBehaviour\r\n{\r\n    static string res;\r\n    public const int MAXPNAMELEN = 32;\r\n    public struct MidiOutCaps\r\n    {\r\n        public short wMid;\r\n        public short wPid;\r\n        public int vDriverVersion;\r\n        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXPNAMELEN)]\r\n        public string szPname;\r\n        public short wTechnology;\r\n        public short wVoices;\r\n        public short wNotes;\r\n        public short wChannelMask;\r\n        public int dwSupport;\r\n    }\r\n\r\n    IntPtr handle;\r\n    public bool KeyOn = false;\r\n    public bool device_init = false;\r\n\r\n\r\n    \/\/ MCI INterface\r\n    [DllImport(&quot;winmm.dll&quot;)]\r\n    private static extern long mciSendString(string command, StringBuilder returnValue, int returnLength, System.IntPtr winHandle);\r\n\r\n    \/\/ Midi API\r\n    [DllImport(&quot;winmm.dll&quot;)]\r\n    private static extern int midiOutGetNumDevs();\r\n\r\n\r\n    [DllImport(&quot;winmm.dll&quot;)]\r\n    private static extern int midiOutGetDevCaps(System.Int32 uDeviceID, ref MidiOutCaps lpMidiOutCaps, System.UInt32 cbMidiOutCaps);\r\n\r\n    [DllImport(&quot;winmm.dll&quot;)]\r\n    private static extern int midiOutOpen(out IntPtr handle, int deviceID, MidiCallBack proc, int instance, int flags);\r\n\r\n    [DllImport(&quot;winmm.dll&quot;)]\r\n    private static extern int midiOutShortMsg(IntPtr handle, int message);\r\n\r\n    \/\/[DllImport(&quot;winmm.dll&quot;)]\r\n    public int midiOutMsgFixed(IntPtr hmo, byte status, byte channel, byte data1, byte data2)\r\n    {\r\n        return midiOutShortMsg(hmo, (status &lt;&lt; 4) | channel | (data1 &lt;&lt; 8) | (data2 &lt;&lt; 16));\r\n    }\r\n    \/\/public static uint midiOutShortMsg(int hmo, byte status, byte channel, GMProgram data1, byte data2) { return midiOutShortMsg(hmo, (status &amp;lt;&amp;lt; 4) | channel | ((byte)data1 &amp;lt;&amp;lt; 8) | (data2 &amp;lt;&amp;lt; 16)); }\r\n\r\n    [DllImport(&quot;winmm.dll&quot;)]\r\n    private static extern int  midiOutClose(IntPtr handle);\r\n\r\n    private delegate void MidiCallBack(int handle, int msg, int instance, int param1, int param2);\r\n\r\n    static string Mci(string command)\r\n    {\r\n        StringBuilder reply = new StringBuilder(256);\r\n        mciSendString(command, reply, 256, System.IntPtr.Zero);\r\n        return reply.ToString();\r\n    }\r\n\r\n    void Start()\r\n    {\r\n        var numDevs = midiOutGetNumDevs();\r\n        MidiOutCaps myCaps = new MidiOutCaps();\r\n\r\n        \/\/0\u756a\u30dd\u30fc\u30c8\u306e\u8abf\u67fb\u3092\u884c\u3046\u3002\r\n        var res = midiOutGetDevCaps(1, ref myCaps, (System.UInt32)Marshal.SizeOf(myCaps));\r\n\r\n        \/\/\u5f15\u6570\uff11\u306f\u30dd\u30a4\u30f3\u30bf\u6271\u3044\u306e\u6a21\u69d8\u3002\r\n#if UNITY_EDITOR\r\n        Debug.Log(myCaps.szPname);\r\n#endif\r\n        DeviceInitialize();\r\n    }\r\n\r\n    void Update(){\r\n        if( KeyOn == true){\r\n            \/\/midiOutMsgFixed(handle, 0x9, 0, 0x45, 40);\r\n\r\n        }\r\n        else if(KeyOn==false){\r\n            \/\/midiOutMsgFixed(handle, 0x9, 0, 0x45, 0);\r\n        }\r\n    }\r\n\r\n    public void Test1(){\r\n            midiOutMsgFixed(handle, 0x9, 0, 0x45, 40);\r\n    }\r\n\r\n    public void Test2(){\r\n            midiOutMsgFixed(handle, 0x9, 0, 0x45, 0);\r\n    }\r\n\r\n    public void DeviceInitialize(){\r\n        int midi_no=1;\r\n        if( device_init == false){\r\n            var res = midiOutOpen( out handle, midi_no , null , 0 , 0);\r\n            device_init = true;\r\n#if UNITY_EDITOR\r\n\r\n            Debug.Log(handle);\r\n            Debug.Log(res);\r\n#endif\r\n        }\r\n    }\r\n\r\n    public void DeviceClose(){\r\n        if( device_init == true){\r\n            var res=midiOutClose(handle);\r\n#if UNITY_EDITOR\r\n            Debug.Log(handle);\r\n            Debug.Log(res);\r\n#endif\r\n            device_init = false;\r\n        }\r\n    }\r\n\r\n    static void PlayMidi()\r\n    {\r\n        res = System.String.Empty;\r\n\r\n\r\n        res = Mci(&quot;open \\&quot;&quot; + filename + &quot;\\&quot; alias music&quot;);\r\n        res = Mci(&quot;play music&quot;);\r\n    }\r\n\r\n    void OnDestroy()\r\n    {\r\n        res = Mci(&quot;close music&quot;);\r\n        DeviceClose();\r\n    }\r\n\r\n    void OnDisable()\r\n    {\r\n        res = Mci(&quot;close music&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p>### \u53c2\u8003\u8cc7\u6599<\/p>\n<p><a href=\"https:\/\/aont.hateblo.jp\/entry\/20080413\/1208060099\">MIDI on C# &#8211; aont\u2019s diary (hateblo.jp)<\/a><\/p>\n<p><a href=\"http:\/\/eternalwindows.jp\/winmm\/midi\/midi01.html\">MIDI\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 (eternalwindows.jp)<\/a><\/p>\n<h3>\u88dc\u8db3\uff1aP\/Invoke\u306e\u5b9a\u7fa9\u306b\u304a\u3044\u3066\u3001\uff08\u304a\u305d\u3089\u304fx64\u3068\uff09x86\u5074\u306e\u5b9a\u7fa9\u65b9\u6cd5\u304c\u9055\u3046<\/h3>\n<p>.NET Framework\u3067\u306f\u3001\u52d5\u7684\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\uff08dll\u3068\u304b\uff09\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3001\u57fa\u672c\u7684\u306bP\/Invoke\u306e\u5b9a\u7fa9\u306b\u6e96\u62e0\u3059\u308b\u5f62\u3067\u518d\u5ea6C#\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u5185\u306b\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u969b\u3001\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u6a5f\u80fd\u306b\u3088\u3063\u3066\u623b\u308a\u5024\u3068\u304b\u306f\u81ea\u7531\u306b\u6307\u5b9a\u3067\u304d\u3066\u3057\u307e\u3046\u3093\u3067\u3059\u304c\u3001<br \/>\n\u53c2\u7167\u5143\u3092\u9593\u9055\u3048\u308b\u3068\u6b63\u5e38\u306b\u52d5\u304b\u306a\u304f\u3066\u624b\u8a70\u307e\u308a\u306b\u306a\u308b\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u53e4\u3044\u8cc7\u6599\uff08\u3069\u3046\u3082x86\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u3053\u306e\u53ef\u80fd\u6027\u9ad8\u3044\uff09\u3067\u306f\u3001<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nprivate static extern int midiOutOpen(ref int handle, int deviceID, MidiCallBack proc, int instance, int flags);\r\n\r\n<\/pre>\n<p>\u3068\u8a18\u8f09\u3057\u3066\u308b\u307f\u305f\u3044\u306a\u3093\u3067\u3059\u304c\u3001P\/Invoke\u306b\u95a2\u3059\u308b\u8cc7\u6599\u3067\u306f\u3001\u4e0b\u8a18\u306e\u901a\u308a\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u308b\u307f\u305f\u3044\u306a\u3093\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate static extern int midiOutOpen(out IntPtr handle, int deviceID, MidiCallBack proc, int instance, int flags);\r\n<\/pre>\n<p>\u4e3b\u306a\u9055\u3044\u306f\u7b2c\uff11\u5f15\u6570\u3067\u3059\u306d\u3002<br \/>\n\uff08\u307b\u304b\u306f\u81ea\u524d\u5b9a\u7fa9\u3082\u542b\u307e\u308c\u3066\u308b\u306e\u3067\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u6a5f\u80fd\u3082\u3042\u3063\u3066\u52d5\u3044\u3066\u308b\u307f\u305f\u3044\u3067\u3059\uff09<\/p>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3068\u3057\u3066\u306f\u3069\u3061\u3089\u3082\u52d5\u304f\u3093\u3067\u3059\u304c\u3001\u521d\u671f\u5316\u2192\u7d42\u4e86\u51e6\u7406\u3001\u3068\u884c\u3063\u305f\u5834\u5408\u3001<br \/>\n\u524d\u8005\u306e\u307b\u3046\u3060\u3068\u3001<strong>\u6b63\u5e38\u5b8c\u4e86\u306e\u623b\u308a\u5024\u3092\u8fd4\u3057\u3066\u304f\u308c\u306a\u3044<\/strong>\u3093\u3067\u3059\u306d\u3002<\/p>\n<p>\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9\u3092\u898b\u308b\u3068\u30cf\u30f3\u30c9\u30eb\u304c\u6b63\u5e38\u306b\u89e3\u653e\u3055\u308c\u3066\u306a\u3044\u6271\u3044\u306b\u306a\u308b\u3053\u3068\u304b\u3089\u3001\u521d\u671f\u5316\u5b9a\u7fa9\u3068\u7d42\u4e86\u5b9a\u7fa9\u304c\u7d10\u4ed8\u3044\u3066\u306a\u3044\u3093\u3058\u3083\u306a\u3044\u304b\u3068\u8003\u3048\u307e\u3059\u3002\u9055\u3044\u304c\u3042\u308b\u3068\u3059\u308c\u3070\u5b9a\u7fa9\u306b\u95a2\u3057\u3066\u53c2\u7167\u3059\u308b\u52d5\u7684\u30e2\u30b8\u30e5\u30fc\u30eb\u304f\u3089\u3044\u304b\u306a\u3001\u3068\u3002<br \/>\n\uff08\u524d\u8005\u306e\u307b\u3046\u306f\u3069\u3046\u3082\u3059\u3052\u30fc\u524d\u306e\u8cc7\u6599\u304b\u3089\u53c2\u7167\u3057\u3066\u3044\u305f\u307f\u305f\u3044\u3067\u3001OS\u3067\u5229\u7528\u3057\u3066\u3044\u308b\u60c5\u5831\u3068\u6574\u5408\u6027\u304c\u53d6\u308c\u306a\u304f\u306a\u3063\u3066\u308b\u3093\u3058\u3083\u306a\u3044\u304b\u3068\u8003\u3048\u307e\u3059\uff09<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001Pinvoke.net\u3092\u53c2\u8003\u306b\u5f8c\u8005\u306e\u307b\u3046\u306b\u5b9a\u7fa9\u3057\u3066\u3001<br \/>\n\u307b\u304b\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3082\u30dd\u30a4\u30f3\u30bf\u4f7f\u3046\u3088\u3046\u306b\u3057\u3066\u52d5\u304b\u3057\u305f\u3089\u6b63\u5e38\u306b\u623b\u308a\u5024\u5410\u3044\u3066MIDI\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u3082\u53ef\u80fd\u306b\u306a\u3063\u305f\u3001\u3068\u3002<\/p>\n<p>\u4e0a\u8a18\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u5f8c\u8005\u306e\u5b9f\u88c5\u3057\u3066\u307e\u3059\u3002<\/p>\n<p>\u8cc7\u6599\uff1a<\/p>\n<p><a href=\"http:\/\/www.pinvoke.net\/default.aspx\/winmm.midioutopen\">pinvoke.net: midioutopen (winmm)<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/49022834\/midi-output-in-unity\">c# &#8211; Unity \u3067\u306e MIDI \u51fa\u529b &#8211; \u30b9\u30bf\u30c3\u30af \u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc (stackoverflow.com)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 Unity\u3067MIDI\u51fa\u529b\u3092\u53d6\u308a\u6271\u3046\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3059\u308b\u3002 Unity\u3067MIDI\u5165\u529b\u3084OSC\u3092\u6271\u3046\u305f\u3081\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u5b58\u5728\u3059\u308b\u304c\u3001 MIDI\u51fa\u529b\u306b\u95a2\u3059\u308b\u30b3\u30fc\u30c9\u306f\u6b86\u3069\u4e8b\u4f8b\u304c\u306a\u3044\u3002 \u3053\u308c\u306fMIDI\u51fa\u529b\u3092\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u3069\u304a\u308a\u306b&hellip; <span class=\"clear\"><\/span><a href=\"https:\/\/machiaworx.net\/?p=938\" class=\"more-link read-more\" rel=\"bookmark\"><span class=\"screen-reader-text\">Unity\u3067MIDI\u51fa\u529b\u3092\u53d6\u308a\u6271\u3046\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3002<\/span><i class=\"fa fa-arrow-right\"><\/i> \u306e\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,11,4],"tags":[],"class_list":["post-938","post","type-post","status-publish","format-standard","hentry","category-unity","category-synth-dev","category-pgm"],"_links":{"self":[{"href":"https:\/\/machiaworx.net\/index.php?rest_route=\/wp\/v2\/posts\/938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machiaworx.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machiaworx.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machiaworx.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machiaworx.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=938"}],"version-history":[{"count":8,"href":"https:\/\/machiaworx.net\/index.php?rest_route=\/wp\/v2\/posts\/938\/revisions"}],"predecessor-version":[{"id":946,"href":"https:\/\/machiaworx.net\/index.php?rest_route=\/wp\/v2\/posts\/938\/revisions\/946"}],"wp:attachment":[{"href":"https:\/\/machiaworx.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machiaworx.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machiaworx.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}