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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
<!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 (with options)</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 (with options)
</h1>
<h2>
Load Options <small>(can only be changed when (re)loading a module)</small>
</h2>
<p>
<label>
<input type="checkbox" id="inputHqMixer" checked />
Use high-quality (slower) software mixer
</label>
</p>
<p>
<label>
<input type="checkbox" id="inputWrap" />
Wrap module
</label>
</p>
<p>
<label>
<input type="checkbox" id="inputLoop" />
Allow module to loop
</label>
</p>
<p>
<label>
<input type="checkbox" id="inputFadeout" checked />
Volume fade out during last pattern
</label>
</p>
<h2>
General Options
</h2>
<p>
<label for="inputReverb">Reverb (0 to 15)</label>
<br />
<input id="inputReverb" type="number" min="0" max="15" value="0" />
</p>
<p>
<label>
<input type="checkbox" id="inputInterpolation" checked />
Interpolation
</label>
</p>
<p>
<label>
<input type="checkbox" id="inputNoiseReduction" checked />
Noise reduction
</label>
</p>
<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>
<button type="button" onclick="changeGeneralOptions()" id="buttonChangeGeneralOptions" disabled>Change general options</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 inputReverb = document.getElementById("inputReverb"),
inputInterpolation = document.getElementById("inputInterpolation"),
inputNoiseReduction = document.getElementById("inputNoiseReduction"),
inputHqMixer = document.getElementById("inputHqMixer"),
inputWrap = document.getElementById("inputWrap"),
inputLoop = document.getElementById("inputLoop"),
inputFadeout = document.getElementById("inputFadeout"),
inputModuleFile = document.getElementById("inputModuleFile"),
buttonChangeGeneralOptions = document.getElementById("buttonChangeGeneralOptions"),
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 = "";
buttonChangeGeneralOptions.setAttribute("disabled", "disabled");
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,
hqMixer: inputHqMixer.checked,
wrap: inputWrap.checked,
loop: inputLoop.checked,
fadeout: inputFadeout.checked,
reverb: parseInt(inputReverb.value),
interpolation: inputInterpolation.checked,
noiseReduction: inputNoiseReduction.checked
});
}
function changeGeneralOptions() {
LibMikMod.changeGeneralOptions({
reverb: parseInt(inputReverb.value),
interpolation: inputInterpolation.checked,
noiseReduction: inputNoiseReduction.checked
});
}
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;
buttonChangeGeneralOptions.removeAttribute("disabled");
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>
|