Last active
November 25, 2015 16:40
-
-
Save ohiofi/073a4d9e3a74f8a1fa6c to your computer and use it in GitHub Desktop.
playSound Macro for Twine v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Audio macro for Twine version 1 | |
Not sure where I found this code, sorry. Will give proper credit when I figure it out. | |
<<playsound "sfx1.mp3">> | |
Use playsound to simply play a sound once. | |
<<loopsound "track1.mp3">><<loopsound "track2.mp3">><<loopsound "track3.mp3">> | |
Use loopsound to loop background music. | |
You can split it into tracks, like bass, guitar, drums. If the tracks are the same length, they will remain in sync. | |
<<mutesound "track2.mp3">><<mutesound "track3.mp3">> | |
You can mute a looping track. For example, I have muted the guitar and drums, but I'm letting the bass play. | |
<<unmutesound "track2.mp3">> | |
Unmute a looping track. If the tracks are the same length, they will remain in sync. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
version.extensions['soundMacros'] = { | |
major: 1, | |
minor: 1, | |
revision: 2 | |
}; | |
var p = macros['playsound'] = { | |
soundtracks: {}, | |
handler: function (a, b, c, d) { | |
var loop = function (m) { | |
if (m.loop == undefined) { | |
m.loopfn = function () { | |
this.play(); | |
}; | |
m.addEventListener('ended', m.loopfn, 0); | |
} else m.loop = true; | |
m.play(); | |
}; | |
var s = eval(d.fullArgs()); | |
if (s) { | |
s = s.toString(); | |
var m = this.soundtracks[s.slice(0, s.lastIndexOf("."))]; | |
if (m) { | |
if (b == "playsound") { | |
m.play(); | |
} else if (b == "loopsound") { | |
loop(m); | |
} else if (b == "pausesound") { | |
m.pause(); | |
} else if (b == "unloopsound") { | |
if (m.loop != undefined) { | |
m.loop = false; | |
} else if (m.loopfn) { | |
m.removeEventListener('ended', m.loopfn); | |
delete m.loopfn; | |
} | |
} else if (b == "stopsound") { | |
m.pause(); | |
m.currentTime = 0; | |
} else if (b == "mutesound") { | |
m.volume = 0; | |
} else if (b == "unmutesound") { | |
m.volume = 1; | |
} else if (b == "fadeoutsound" || b == "fadeinsound") { | |
if (m.interval) clearInterval(m.interval); | |
if (b == "fadeinsound") { | |
if (m.currentTime>0) return; | |
m.volume = 0; | |
loop(m); | |
} else { | |
if (!m.currentTime) return; | |
m.play(); | |
} | |
var v = m.volume; | |
m.interval = setInterval(function () { | |
v = Math.min(1, Math.max(0, v + 0.005 * (b == "fadeinsound" ? 1 : -1))); | |
m.volume = Math.easeInOut(v); | |
if (v == 0 || v == 1) clearInterval(m.interval); | |
if (v == 0) { | |
m.pause(); | |
m.currentTime = 0; | |
m.volume = 1; | |
} | |
}, 10); | |
} | |
} | |
} | |
} | |
} | |
macros['fadeinsound'] = p; | |
macros['fadeoutsound'] = p; | |
macros['unloopsound'] = p; | |
macros['loopsound'] = p; | |
macros['pausesound'] = p; | |
macros['stopsound'] = p; | |
macros['mutesound'] = p; | |
macros['unmutesound'] = p; | |
macros['stopallsound'] = { | |
handler: function () { | |
var s = macros.playsound.soundtracks; | |
for (var j in s) { | |
if (s.hasOwnProperty(j)) { | |
s[j].pause(); | |
if (s[j].currentTime) { | |
s[j].currentTime = 0; | |
} | |
} | |
} | |
} | |
} | |
var div = document.getElementById("storeArea").firstChild; | |
var fe = ["ogg", "mp3", "m4a", "wav", "webm"]; | |
while (div) { | |
var b = String.fromCharCode(92); | |
var q = '"'; | |
var re = "['" + q + "]([^" + q + "']*?)" + b + ".(ogg|mp3|m4a|wav|webm)['" + q + "]"; | |
k(new RegExp(re, "gi")); | |
div = div.nextSibling; | |
} | |
function k(c, e) { | |
do { | |
var d = c.exec(div.innerHTML); | |
if (d) { | |
var a = new Audio(); | |
if (a.canPlayType) { | |
for (var i = -1; i < fe.length; i += 1) { | |
if (i >= 0) d[2] = fe[i]; | |
if (a.canPlayType("audio/" + d[2])) break; | |
} | |
if (i < fe.length) { | |
a.setAttribute("src", d[1] + "." + d[2]); | |
a.interval = null; | |
macros.playsound.soundtracks[d[1]] = a; | |
} else console.log("Browser can't play '" + d[1] + "'"); | |
} | |
} | |
} while (d); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment