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