anxiety/scripts/game/Sounds.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-04-23 19:12:41 +00:00
Loader.addSounds([
{ id:"voice_hong", src:"sounds/voices/hong.mp3" },
{ id:"voice_beebee", src:"sounds/voices/beebee.mp3" },
{ id:"voice_narrator", src:"sounds/voices/narrator.mp3" },
{ id:"voice_narrator_emphasis", src:"sounds/voices/narrator_emphasis.mp3" }
]);
2019-04-24 19:42:13 +00:00
window.sfx = function(sound, options){
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
options = options || {};
options.volume = options.volume===undefined ? 1 : options.volume;
options.pan = options.pan===undefined ? 0 : options.pan;
2019-04-23 19:12:41 +00:00
var sfx = Library.sounds[sound];
2019-04-24 19:42:13 +00:00
sfx.volume(options.volume);
sfx.stereo(options.pan);
2019-04-23 19:12:41 +00:00
sfx.play();
};
2019-04-26 17:08:31 +00:00
window.stopAllSounds = function(){
Object.keys(Library.sounds).forEach(function(name){
Library.sounds[name].stop();
});
};
2019-04-24 19:42:13 +00:00
window.voice = function(name, options){
options = options || {};
sfx("voice_"+name, options);
2019-04-23 19:12:41 +00:00
}
window.CURRENT_MUSIC = null;
2019-04-24 19:42:13 +00:00
window.music = function(song, options){
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
options = options || {};
options.volume = options.volume===undefined ? 1 : options.volume;
options.fade = options.fade===undefined ? 0 : options.fade;
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
// Fade out or cut previous?
if(options.fade==0){
if(window.CURRENT_MUSIC) window.CURRENT_MUSIC.stop();
}else{
var currentVolume = window.CURRENT_MUSIC.volume();
2019-04-25 18:07:41 +00:00
window.CURRENT_MUSIC.fade(currentVolume, 0, options.fade*1000);
2019-04-24 19:42:13 +00:00
}
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
// Play new song (if any)
if(!song){
window.CURRENT_MUSIC = null;
2019-04-23 19:12:41 +00:00
}else{
var song = Library.sounds["music_"+song];
2019-04-29 19:19:44 +00:00
song.stop(); // just in case it was playing earlier
2019-04-23 19:12:41 +00:00
song.loop(true);
2019-04-24 19:42:13 +00:00
if(options.fade==0){
song.volume(options.volume);
}else{
song.fade(0, options.volume, options.fade);
}
2019-04-23 19:12:41 +00:00
window.CURRENT_MUSIC = song;
2019-04-24 19:42:13 +00:00
song.play();
2019-04-23 19:12:41 +00:00
}
};