Skip to content

Instantly share code, notes, and snippets.

@xyx0no646
Last active August 25, 2021 18:43
Show Gist options
  • Save xyx0no646/e9c4ddad70f0db64a87cfefd87dd3eb9 to your computer and use it in GitHub Desktop.
Save xyx0no646/e9c4ddad70f0db64a87cfefd87dd3eb9 to your computer and use it in GitHub Desktop.
;  注意:もっと良いバージョン(JavaScriptで実装されたWaveSoundBuffer)ができました。
; 以下を参照してください。
; https://gist.github.com/xyx0no646/71363ab2b47dcfdcc920afe0570abd8d
;
;[playse2]/[stopse2]/[playbgm2]/[stopbgm2]
;
;Web版吉里吉里SDL2で動作するplaybgm / playseタグもどき
;oggの再生と停止以外は対応しない
;Licence CC0 / author:@xyx0no646
;ご自由にお使いください
;
;Web版以外の環境ではplayse/playbgmにファールバックする
;大域音量はplayse/playbgmのものを取ってくる
;
;実際に使う場合は設定画面にも
;
;BMGの音量を設定
;global.jsplayBGM.setVolume(kag.bgm.buf1.volume * kag.bgm.buf1.volume2/10000000000);
;
;SEの音量を設定
;global.jsplaySE.setVolume(kag.se[0].volume * kag.se[0].volume2/10000000);
;
;記述が必要かも
;https://twitter.com/pnfqno646/status/1426541484770164742
;
;このマクロは2021/8/14版の吉里吉里SDL2から対応しています
[macro name="playse2"]
[if exp="System.platformName === 'Emscripten'"]
;emscriptenの場合はやっつけplayse
[eval exp="global.jsplayBGM.loop=false"]
[eval exp="global.jsplaySE.setVolume(kag.se[0].volume * kag.se[0].volume2/10000000)"]
[eval exp="global.jsplaySE.play(mp.storage + '.ogg')"]
[else]
;Web版以外は普通のplayse
[playse storage="&mp.storage" loop=false]
[endif]
[endmacro]
[macro name="stopse2"]
[if exp="System.platformName === 'Emscripten'"]
;emscriptenの場合はやっつけstopse
[eval exp="global.jsplaySE.stop()"]
[else]
;Web版以外は普通のstopse
[playse]
[endif]
[endmacro]
[macro name="playbgm2"]
[if exp="System.platformName === 'Emscripten'"]
;emscriptenの場合はやっつけplaybgm
[eval exp="global.jsplayBGM.loop=true"]
[eval exp="global.jsplayBGM.setVolume(kag.bgm.buf1.volume * kag.bgm.buf1.volume2/10000000000)"]
[eval exp="global.jsplayBGM.play(mp.storage + '.ogg')"]
[else]
;Web版以外は普通のplaybgm
[playbgm storage="&mp.storage"]
[endif]
[endmacro]
[macro name="stopbgm2"]
[if exp="System.platformName === 'Emscripten'"]
;emscriptenの場合はやっつけstopbgm
[eval exp="global.jsplayBGM.stop()"]
[else]
;Web版以外は普通のstopse
[stopbgm]
[endif]
[endmacro]
[iscript]
class JSPlayer {
var audioTag;
var loop;
var volume;
function play(file) {
if(typeof(global.KirikiriEmscriptenInterface) == "Object"){
var blobURL=KirikiriEmscriptenInterface.evalJS('URL.createObjectURL(new Blob([Module.getStorageUInt8Array("'+file+'")], {type: "audio/ogg"}))');
this.audioTag=KirikiriEmscriptenInterface.evalJS('new Audio()');
this.audioTag.src=blobURL;
this.audioTag.loop=this.loop;
this.audioTag.play();
}
}
function stop(){
if(typeof(global.KirikiriEmscriptenInterface) == "Object" && typeof(this.audioTag) == "Object"){
this.audioTag.pause();
}
}
function setVolume(volume){
if(typeof(global.KirikiriEmscriptenInterface) == "Object"){
this.volume = volume;
if(typeof(this.audioTag) == "Object"){
this.audioTag.volume = this.volume;
}
}
}
}
global.jsplaySE = new JSPlayer();
global.jsplayBGM = new JSPlayer();
[endscript]
[return]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment