1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>LibMikMod Web Audio Example</title>
<style type="text/css">
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<noscript>Sorry! You need to enable JavaScript in order to run the example :(</noscript>
<h1>
LibMikMod Web Audio Example
</h1>
<h2>
Module File
</h2>
<p>
<label for="inputModuleFile">Module file (669, amf, asy, dsm, far, gdm, gt2, imf, it, m15, med, mod, mtm, okt, s3m, stm, stx, ult, umx, uni, xm)</label>
<br />
<input type="file" id="inputModuleFile" accept=".669,.amf,.asy,.dsm,.far,.gdm,.gt2,.imf,.it,.m15,.med,.mod,.mtm,.okt,.s3m,.stm,.stx,.ult,.umx,.uni,.xm" />
</p>
<h2>
Commands
</h2>
<p>
<button type="button" onclick="load()" id="buttonLoad" disabled>(Re)Load module file</button>
</p>
<p>
<button type="button" onclick="play()" id="buttonPlay" disabled>Play</button>
<button type="button" onclick="pause()" id="buttonPause" disabled>Pause</button>
</p>
<h2>
Song information
</h2>
<pre id="songInfo"></pre>
<h2>
More information
</h2>
<pre id="versionInfo"></pre>
<ul>
<li>
<a target="_blank" href="https://github.com/sezero/mikmod">https://github.com/sezero/mikmod</a>
</li>
<li>
<a target="_blank" href="https://github.com/carlosrafaelgn/mikmod/tree/master/libmikmod/webaudio">https://github.com/carlosrafaelgn/mikmod/tree/master/libmikmod/webaudio</a>
</li>
</ul>
<script type="text/javascript" src="dist/libmikmod.min.js"></script>
<script type="text/javascript">
//<![CDATA[
"use strict";
// According to Can I Use, we can assume that if a browser supports AudioWorkletNode it
// also supports WebAssembly (but not the other way around).
// Promises, ES6 and all other dependencies are also present in a browser which
// supports these two features:
// https://caniuse.com/wasm
// https://caniuse.com/mdn-api_audioworkletnode
if (!LibMikMod.isSupported())
alert("Sorry, but your browser does not meet the minimum requirements :(");
var inputModuleFile = document.getElementById("inputModuleFile"),
buttonPlay = document.getElementById("buttonPlay"),
buttonPause = document.getElementById("buttonPause"),
songInfo = document.getElementById("songInfo"),
fileLoaded = false,
audioNode = null,
audioContext = new AudioContext();
audioContext.suspend();
if (LibMikMod.isSupported())
LibMikMod.init(audioContext, "dist/").then(function () {
document.getElementById("versionInfo").textContent = "WEB_VERSION: " + LibMikMod.WEB_VERSION + "\nLIB_VERSION: " + LibMikMod.LIB_VERSION;
document.getElementById("buttonLoad").removeAttribute("disabled");
}, function (reason) {
alert("Error loading LibMikMod: " + reason);
});
function cleanUp() {
songInfo.textContent = "";
buttonPlay.setAttribute("disabled", "disabled");
buttonPause.setAttribute("disabled", "disabled");
fileLoaded = false;
audioContext.suspend();
if (audioNode)
audioNode.disconnect();
}
function load() {
if (!LibMikMod.initialized)
return;
if (!inputModuleFile.files || !inputModuleFile.files.length) {
alert("Please, select a file first");
return;
}
cleanUp();
LibMikMod.loadModule({
audioContext: audioContext,
source: inputModuleFile.files[0],
onload: moduleOnload,
onerror: moduleOnerror,
onended: moduleOnended
});
}
function play() {
if (fileLoaded)
audioContext.resume();
}
function pause() {
if (fileLoaded)
audioContext.suspend();
}
function moduleOnload(newAudioNode) {
songInfo.textContent =
"Song name: " + LibMikMod.infoSongName + "\n" +
"Module type: " + LibMikMod.infoModType + "\n" +
"Comment: " + LibMikMod.infoComment
;
newAudioNode.connect(audioContext.destination);
audioNode = newAudioNode;
buttonPlay.removeAttribute("disabled");
buttonPause.removeAttribute("disabled");
fileLoaded = true;
audioContext.resume();
}
function moduleOnerror(errorCode, reason) {
songInfo.textContent = "";
switch (errorCode) {
case LibMikMod.ERROR_FILE_READ:
alert("Error reading the file: " + reason);
break;
case LibMikMod.ERROR_MODULE_LOAD:
alert("Error loading the module: " + reason);
break;
//case LibMikMod.ERROR_PLAYBACK:
default:
alert("Error during module playback: " + reason);
break;
}
}
function moduleOnended() {
cleanUp();
alert("Playback ended!");
}
//]]>
</script>
</body>
</html>
|