| 1093 |
(function (jg) { |
(function (jg) { |
| 1094 |
var SimpleSound = (function () { |
var SimpleSound = (function () { |
| 1095 |
function SimpleSound() { } |
function SimpleSound() { } |
| 1096 |
SimpleSound.getAudioContext = function getAudioContext() { |
SimpleSound.play = function play(sound, loop) { |
| 1097 |
if(SimpleSound.context) { |
var _sound = new Audio(null); |
| 1098 |
return SimpleSound.context; |
_sound.src = sound.src; |
| 1099 |
} |
_sound.loop = loop ? loop : false; |
| 1100 |
SimpleSound.context = SimpleSound._getAudioContext(); |
; |
| 1101 |
return SimpleSound.context; |
_sound.play(); |
| 1102 |
}; |
return _sound; |
|
SimpleSound._getAudioContext = function _getAudioContext() { |
|
|
if(window["AudioContext"]) { |
|
|
return new window["AudioContext"](); |
|
|
} else if(window["webkitAudioContext"]) { |
|
|
return new window["webkitAudioContext"](); |
|
|
} else { |
|
|
return null; |
|
|
} |
|
|
}; |
|
|
SimpleSound.play = function play(sound, loop, when, gain) { |
|
|
var context = SimpleSound.getAudioContext(); |
|
|
var bufferSource = context.createBufferSource(); |
|
|
if(loop) { |
|
|
bufferSource.loop = true; |
|
|
} |
|
|
bufferSource.buffer = sound; |
|
|
if(gain) { |
|
|
bufferSource.connect(gain); |
|
|
} else { |
|
|
if(!SimpleSound.soundGain) { |
|
|
var soundGain = context.createGain(); |
|
|
soundGain.connect(context.destination); |
|
|
SimpleSound.soundGain = soundGain; |
|
|
} |
|
|
bufferSource.connect(SimpleSound.soundGain); |
|
|
} |
|
|
bufferSource.start(when === undefined ? 0 : when); |
|
|
return bufferSource; |
|
| 1103 |
}; |
}; |
| 1104 |
SimpleSound.hasBgm = function hasBgm() { |
SimpleSound.hasBgm = function hasBgm() { |
| 1105 |
return SimpleSound.bgmSource !== undefined; |
return SimpleSound.bgm !== undefined; |
| 1106 |
}; |
}; |
| 1107 |
SimpleSound.playBgm = function playBgm(sound, loop, when) { |
SimpleSound.playBgm = function playBgm(sound, loop) { |
| 1108 |
if(SimpleSound.bgmSource) { |
if(SimpleSound.bgm) { |
| 1109 |
SimpleSound.stopBgm(); |
SimpleSound.stopBgm(); |
| 1110 |
} |
} |
| 1111 |
if(!SimpleSound.bgmGain) { |
sound.load(); |
| 1112 |
var context = SimpleSound.getAudioContext(); |
sound.loop = loop ? loop : false; |
| 1113 |
var bgmGain = context.createGain(); |
sound.play(); |
| 1114 |
bgmGain.connect(context.destination); |
SimpleSound.bgm = sound; |
| 1115 |
SimpleSound.bgmGain = bgmGain; |
return SimpleSound.bgm; |
|
} |
|
|
SimpleSound.bgmSource = SimpleSound.play(sound, loop, when, SimpleSound.bgmGain); |
|
|
return SimpleSound.bgmSource; |
|
| 1116 |
}; |
}; |
| 1117 |
SimpleSound.stop = function stop(source, when) { |
SimpleSound.stop = function stop(source) { |
| 1118 |
if(!source) { |
if(!source) { |
| 1119 |
return; |
return; |
| 1120 |
} |
} |
| 1121 |
source.stop(when === undefined ? 0 : when); |
source.pause(); |
| 1122 |
}; |
}; |
| 1123 |
SimpleSound.stopBgm = function stopBgm(when) { |
SimpleSound.stopBgm = function stopBgm() { |
| 1124 |
SimpleSound.stop(SimpleSound.bgmSource, when); |
SimpleSound.stop(SimpleSound.bgm); |
| 1125 |
delete SimpleSound.bgmSource; |
delete SimpleSound.bgm; |
|
}; |
|
|
SimpleSound.tone = function tone(hertz, seconds) { |
|
|
hertz = hertz !== undefined ? hertz : 200; |
|
|
seconds = seconds !== undefined ? seconds : 1; |
|
|
var nChannels = 1; |
|
|
var sampleRate = 44100; |
|
|
var amplitude = 2; |
|
|
var context = SimpleSound.getAudioContext(); |
|
|
var buffer = context.createBuffer(nChannels, seconds * sampleRate, sampleRate); |
|
|
var fArray = buffer.getChannelData(0); |
|
|
for(var i = 0; i < fArray.length; i++) { |
|
|
var time = i / buffer.sampleRate; |
|
|
var angle = hertz * time * Math.PI; |
|
|
fArray[i] = Math.sin(angle) * amplitude; |
|
|
} |
|
|
return buffer; |
|
| 1126 |
}; |
}; |
| 1127 |
return SimpleSound; |
return SimpleSound; |
| 1128 |
})(); |
})(); |
| 1244 |
} |
} |
| 1245 |
SoundResourceLoader.prototype.load = function (url, identifier) { |
SoundResourceLoader.prototype.load = function (url, identifier) { |
| 1246 |
var _this = this; |
var _this = this; |
| 1247 |
var request = new XMLHttpRequest(); |
var audio = new Audio(null); |
| 1248 |
request.open("GET", this.resource.structure.soundUrl(url), true); |
audio.autoplay = false; |
| 1249 |
request.responseType = "arraybuffer"; |
audio.preload = "auto"; |
| 1250 |
var callback = this.completed; |
audio.src = this.resource.structure.soundUrl(url); |
| 1251 |
request.onload = function () { |
audio.load(); |
| 1252 |
var context = jg.SimpleSound.getAudioContext(); |
audio.addEventListener("canplaythrough", function () { |
| 1253 |
if(context) { |
_this.completed(identifier, audio, true); |
| 1254 |
context.decodeAudioData(request.response, function (decodedAudio) { |
}, true); |
| 1255 |
callback.call(_this, identifier, decodedAudio, true); |
audio.addEventListener("error", function () { |
| 1256 |
}, function () { |
_this.completed(identifier, audio, false); |
| 1257 |
callback.call(_this, identifier, null, false); |
}, true); |
|
}); |
|
|
} else { |
|
|
callback.call(_this, identifier, null, false); |
|
|
} |
|
|
}; |
|
|
request.onerror = function () { |
|
|
callback.call(_this, identifier, null, false); |
|
|
}; |
|
|
request.send(); |
|
| 1258 |
}; |
}; |
| 1259 |
SoundResourceLoader.prototype.completed = function (name, audio, is_success) { |
SoundResourceLoader.prototype.completed = function (name, audio, is_success) { |
| 1260 |
if(!is_success) { |
if(!is_success) { |