<commit_before>/*
 * MelonJS Game Engine
 * Copyright (C) 2012, <NAME>
 * http://www.melonjs.org
 *
 * Audio Mngt Objects
 *
 *
 */

(function($, undefined) {
	/*
	 * -----------------------------------------------------
	 * 
	 * a audio class singleton to manage the game fx & music
	 * -----------------------------------------------------
	 */

	/**
	 * There is no constructor function for me.audio.
	 * 
	 * @final
	 * @memberOf me
	 * @constructor Should not be called by the user.
	 */

	me.audio = (function() {
		// hold public stuff in our singletong
		var obj = {};

		// audio channel array
		var audio_channels = [];

		// supported Audio Format
		var supportedFormat = [ "mp3", "ogg", "wav" ];

		// Request format by the app/game
		var requestedFormat = null;

		// Active (supported) audio extension
		var activeAudioExt = -1;

		// loadcb function
		var load_cb = null;

		// current music
		var current_track = null;

		// enable/disable flag
		var sound_enable = true;

		// defaut reset value
		var reset_val = 0;// .01;

		// a retry counter
		var retry_counter = 0;

		/*
		 * ---------------------------------------------
		 * 
		 * PRIVATE STUFF
		 * 
		 * ---------------------------------------------
		 */

		/*
		 * ---
		 * 
		 * return the audio format extension supported by the browser ---
		 */

		function getSupportedAudioFormat() {

			var extIdx = 0;

			// check for sound support by the browser
			if (!me.sys.sound) {
				sound_enable = false;
				return;
			}

			// check for MP3
			if ((requestedFormat.search(/mp3/i) != -1) && obj.capabilities.mp3) {
				// console.log("mp3 audio supported");
				return supportedFormat[extIdx];
			}

			// check for OGG/Vorbis
			if ((requestedFormat.search(/ogg/i) != -1) && obj.capabilities.ogg) {
				// console.log("ogg audio supported");
				return supportedFormat[++extIdx];
			}

			// check for WAV
			if ((requestedFormat.search(/wav/i) != -1) && obj.capabilities.wav) {
				// console.log("wav audio supported");
				return supportedFormat[++extIdx];
			}

			// deactivate sound
			sound_enable = false;

			return -1;
		}
		;

		/*
		 * ---
		 * 
		 * return the specified sound ---
		 */

		function get(sound_id) {
			var channels = audio_channels[sound_id];
			// find which channel is available
			for ( var i = 0, soundclip; soundclip = channels[i++];) {
				if (soundclip.ended || !soundclip.currentTime)// soundclip.paused)
				{
					// console.log ("requested %s on channel %d",sound_id, i);
					soundclip.currentTime = reset_val;
					return soundclip;
				}
			}
			// else force on channel 0
			channels[0].pause();
			channels[0].currentTime = reset_val;
			return channels[0];
		}
		;

		/*
		 * ---
		 * 
		 * event listener callback on load error<commit_msg>Simplify getSupportedAudioFormat()<commit_after>/*
 * MelonJS Game Engine
 * Copyright (C) 2012, <NAME>
 * http://www.melonjs.org
 *
 * Audio Mngt Objects
 *
 *
 */

(function($, undefined) {
	/*
	 * -----------------------------------------------------
	 * 
	 * a audio class singleton to manage the game fx & music
	 * -----------------------------------------------------
	 */

	/**
	 * There is no constructor function for me.audio.
	 * 
	 * @final
	 * @memberOf me
	 * @constructor Should not be called by the user.
	 */

	me.audio = (function() {
		// hold public stuff in our singletong
		var obj = {};

		// audio channel array
		var audio_channels = [];

		// supported Audio Format
		var supportedFormat = [ "mp3", "ogg", "wav" ];

		// Request format by the app/game
		var requestedFormat = null;

		// Active (supported) audio extension
		var activeAudioExt = -1;

		// loadcb function
		var load_cb = null;

		// current music
		var current_track = null;

		// enable/disable flag
		var sound_enable = true;

		// defaut reset value
		var reset_val = 0;// .01;

		// a retry counter
		var retry_counter = 0;

		/*
		 * ---------------------------------------------
		 * 
		 * PRIVATE STUFF
		 * 
		 * ---------------------------------------------
		 */

		/*
		 * ---
		 * 
		 * return the audio format extension supported by the browser ---
		 */

		function getSupportedAudioFormat() {

			var result = "";
			var ext = "";
			var rx = null;
			var i = 0;
			var len = supportedFormat.length;

			// check for sound support by the browser
			if (me.sys.sound) {
				for (; i < len; i++) {
					ext = supportedFormat[i];
					rx = new RegExp(ext, "i");
					if ((requestedFormat.search(rx) != -1) && obj.capabilities[ext]) {
						result = ext;
						break;
					}
				}
			}

			if (result === "") {
				// deactivate sound
				sound_enable = false;
			}

			return result;
		}

		/*
		 * ---
		 * 
		 * return the specified sound ---
		 */

		function get(sound_id) {
			var channels = audio_channels[sound_id];
			// find which channel is available
			for ( var i = 0, soundclip; soundclip = channels[i++];) {
				if (soundclip.ended || !soundclip.currentTime)// soundclip.paused)
				{
					// console.log ("requested %s on channel %d",sound_id, i);
					soundclip.currentTime = reset_val;
					return soundclip;
				}
			}
			// else force on channel 0
			channels[0].pause();
			channels[0].currentTime = reset_val;
			return channels[0];
		}
		;

		/*
		 * ---
		 * 
		 * event listener callback on load error
{'fraction_non_alphanumeric': 0.20200113271663206, 'fraction_numerical': 0.007740230319048518, 'mean_word_length': 1.7796432318992654, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6843189', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1565, 'n_words': 450}