jslibxm - simple JavaScript API for libxm
jslibxm is a JavaScript .XM
file (FastTracker II Extended Module) player using libxm with emscripten.
- Very small: ~69,9 kB in size for the minified version
- Easy to use API (see examples)
- Fast
To use this in your project,
- Add the script to your HTML file:
<script src="https://cdn.jsdelivr.net/gh/zeozeozeo/jslibxm@master/dist/jslibxm.min.js"></script>
- In your JavaScript, add a function that will be called once the module loads:
libxm.onload = function () {
// create a new XM module
var xm = new XMModule(); // sample rate is 48000 by default
// load a module, you can use a URL string, File or an Int8Array
var url =
"https://api.modarchive.org/downloads.php?moduleid=41760#elw-sick.xm";
xm.load(url, function (err) {
if (err) {
// this condition will run
// if there was an an error
console.error(err);
return;
}
// module is loaded, start playing the song
xm.resume();
});
};
WARNING: In most browsers, the AudioContext will only start playing audio when the user interacts when the page (e.g. clicks a button).
For more examples, look inside the examples folder.
Main constructor. Returns itself.
* sampleRate - how much samples to generate and play per second
* onfillbuffer - will be called each time when filling new audio buffer
* onxmdataupdate - will be called each time when XMModule.xmdata updates
Loads an XM module into the context.
* {(File|string|Int8Array)} input - loads the module from file, URL or Int8Array.
* {Function} callback<err> - callback function after module load.
* Callback has 1 parameter (error) - false if loaded successfully, string if not.
Example:
xm.load(input, function (err) {
if (err) {
// this condition will run
// if there was an an error
console.error(err);
return;
}
// module is loaded
});
XMModule.xmdata
is an array containing all of the events that happening while filling the previous audio buffer.
...unfinished, look into the source code
To build libxm with emscripten,
- Clone the repository with submodules
git clone --recurse-submodules git@github.com:zeozeozeo/jslibxm.git
- Download and install emscripten and add it to PATH
cd
into the directory and runmake
. For more information on building libxm, see this- If everything went successfully, the
lib
folder will havelibxm.js
, which is code generated by emscripten - If you also have uglifyjs installed, you can minify the source code for distribution by running
uglifyjs lib/libxm.js src/jslibxm.js -o dist/jslibxm.min.js
. Thedist
folder will contain the minified code for libxm.js and jslibxm.js in one file,jslibxm.min.js
.