47 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/*
 | 
						|
 * This Source Code Form is subject to the terms of the Mozilla Public
 | 
						|
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 | 
						|
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
						|
 */
 | 
						|
 | 
						|
var sfx = {};
 | 
						|
 | 
						|
function initSFX() {
 | 
						|
    if (localStorage.getItem("alertsound") == null) {
 | 
						|
        localStorage.setItem("alertsound", "sonar");
 | 
						|
    }
 | 
						|
    if (localStorage.getItem("alertvolume") == null) {
 | 
						|
        localStorage.setItem("alertvolume", 100);
 | 
						|
    }
 | 
						|
 | 
						|
    var alertNoiseName = localStorage.getItem("alertsound");
 | 
						|
    var alertVolume = localStorage.getItem("alertvolume");
 | 
						|
 | 
						|
    sfx = {
 | 
						|
        "alert": new Audio("assets/audio/alert." + alertNoiseName + ".mp3"),
 | 
						|
        "ok": new Audio("assets/audio/ok.mp3"),
 | 
						|
        "error": new Audio("assets/audio/error.mp3")
 | 
						|
    };
 | 
						|
 | 
						|
    setVolume("alert", alertVolume);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Play a sound.
 | 
						|
 * @param string sound Name of the sound to play (alert, ok, error)
 | 
						|
 * @returns {undefined}
 | 
						|
 */
 | 
						|
function playSound(sound) {
 | 
						|
    sfx[sound].play();
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Set sound volume
 | 
						|
 * @param string sound The name of the sound to set volume of
 | 
						|
 * @param number volume Number in range 0 to 100
 | 
						|
 */
 | 
						|
function setVolume(sound, volume) {
 | 
						|
    sfx[sound].volume = volume / 100.0;
 | 
						|
}
 | 
						|
 | 
						|
initSFX(); |