deskjet / chiptune2.js

much like chiptune.js - but newer and neater
375 stars 52 forks source link

Autoplay music #21

Closed matty45 closed 7 years ago

matty45 commented 7 years ago

Hello, i am new to javascript and i want to make it so that when the webpage loads, it plays the mod file and loops it. However i have no idea how this works and i dont seem to find tutorials or guides anywhere. Could anyone help me with this please?

Sorry if i posted this in the wrong place.

deskjet commented 7 years ago

You can basically copy the code from the demo page.

Just put something like that in your page header:

<!-- include libopenmpt and chiptune.js -->
<script type="text/javascript" src="js/libopenmpt.js"></script>
<script type="text/javascript" src="js/chiptune2.js"></script>

<!-- create player, load file -->
<script type="text/javascript">
var player = new ChiptuneJsPlayer(new ChiptuneJsConfig(-1));
player.load('tunes/rfchip001.xm', function(buffer) {
   player.play(buffer);
 });
</script>

Make sure the paths to the scripts and module are correct. These are only examples.

Some browsers (like Chrome) block XHR when the page is served via the file system (file://). You may need to run a local http server to make it work. If there are problems check your browsers Dev-Tools.

gs11 commented 6 years ago

I'm getting an "libopenmpt is not defined" error when trying the above example @deskjet.

deskjet commented 6 years ago

Yeah, it doesn't work that way anymore. You have to define a function libopenmpt.onRuntimeInitialized for Emscripten to call to signal when it's loaded and ready. Look at the demo page source for an example. You can just strip away the bits that you don't need.

gs11 commented 6 years ago

Thanks! Got it to run with these few lines (for others who want to do the same thing):

var libopenmpt = {};
libopenmpt.onRuntimeInitialized = function() {
    var player = new ChiptuneJsPlayer(new ChiptuneJsConfig(-1));
    player.load("illa2.mod", function(buffer) {
        player.play(buffer);
    });
};