Midi.js怎么玩?

时间:2014-02-10 12:09:24

标签: javascript android html5 midi

我想通过mudcube Midi.js demo-basic.html的例子听到声音,但我听不到声音。谁能帮助我?我只是个初学者,谢谢你

    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     <!-- midi.js package -->
     <script src="./js/MIDI/AudioDetect.js" type="text/javascript"></script>
     <script src="./js/MIDI/LoadPlugin.js" type="text/javascript"></script>
     <script src="./js/MIDI/Plugin.js" type="text/javascript"></script>
     <script src="./js/MIDI/Player.js" type="text/javascript"></script>
     <script src="./js/Window/DOMLoader.XMLHttp.js" type="text/javascript"></script>
     <!-- extras -->
     <script src="./inc/Base64.js" type="text/javascript"></script>
     <script src="./inc/base64binary.js" type="text/javascript"></script>
    </head>
    <body>
    <script type="text/javascript">

            window.onload = function () {
                MIDI.loadPlugin({
                    soundfontUrl: "./soundfont/",
                    instrument: "acoustic_grand_piano",
                    callback: function() {
                var delay = 0; // play one note every quarter second
                var note = 50; // the MIDI note
                var velocity = 127; // how hard the note hits
                // play the note
        MIDI.setVolume(0, 127);
        MIDI.noteOn(0, note, velocity, delay);
        MIDI.noteOff(0, note, delay + 0.75);
    }
        });
    };

    </script>
    </body>
    </html>

4 个答案:

答案 0 :(得分:1)

发生了一个Cross Origin请求,不允许Soundfont上传。您可以在Google Chrome控制台中看到它。它目前是midi.js Github回购的一个公开问题。 (https://github.com/mudcube/MIDI.js/issues/63)。有一些建议可以帮助你启动并运行,直到他们推出一个解决这个问题的版本。

答案 1 :(得分:0)

它适用于FireFox浏览器。我不能让它一次播放一个音符。我把它放在一个循环中,一次一个地放置3个不同的音符,但它们都像和弦一样。

答案 2 :(得分:0)

试试这段代码。时机不正确,因为它正在进行中大声笑但它应该是生日快乐。

onsuccess: function() { var delay = 0; // play one note every quarter second
var note = 50; // the MIDI note
var velocity = 127; // how hard the note hits
var i;  
var Note1Play = [60, 60, 62, 60, 65, 64, 60, 60];
var Length1_Note = [0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455];
MIDI.setVolume(0, 127);
delay = .455;
for (i=0; i<=24; i++){
  delay = delay + Length1_Note[i];
  MIDI.noteOn(0, Note1Play[i], velocity,delay);
  MIDI.noteOff(0,Note1Play[i],delay + 32);
}    }    });    };

答案 3 :(得分:0)

如果您具有正确的脚本源,此代码将播放“生日快乐”歌曲:

<script type="text/javascript">
        function myFunction() {
            MIDI.loadPlugin({
                onsuccess: function () {
                    var delay = 0; // play one note every quarter second
                    var note = 50; // the MIDI note
                    var velocity = 127; // how hard the note hits
                    var i;
                    var Note1Play =    [60,   60,  67,  67,  69,  69, 67,  65,  65,  64,  64,  62,  62, 60 ];
                    var Length1_Note = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,  1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5];
                    MIDI.setVolume(0, 127);
                    delay = 0;
                    for (i = 0; i <= Length1_Note.length; i++) {
                        delay = delay + Length1_Note[i];
                        MIDI.noteOn(0, Note1Play[i], velocity, delay);
                        MIDI.noteOff(0, Note1Play[i], delay + 25);
                    }
                }
            });
        };
    </script>

(有一个具有名为myFunction的onclick功能的按钮) 我在Microsoft Edge上运行它。由于CORS政策,我无法在Chrome上听到声音。