<commit_before>import js.html.XMLHttpRequest;
import haxe.Json;

@:expose @:keep class WaudSound implements IWaudSound {

	/**
	* Indicates if the sound is sprite sound or normal sound.
	*
	* @property isSpriteSound
	* @type {Bool}
	* @readOnly
	* @example
 	*     snd.isSpriteSound;
	*/
	public var isSpriteSound:Bool;

	/**
	* Sound url.
	*
	* @property url
	* @type {String}
	* @readOnly
	* @example
 	*     snd.url;
	*/
	public var url:String;

	var _snd:IWaudSound;
	var _options:WaudSoundOptions;
	var _spriteData:AudioSprite;

	/**
	* Class to automatically use web audio api with HTML5 audio fallback.
	*
	* @class WaudSound
	* @constructor
	* @param {String} url - Can be audio file path or JSON file for audio sprite.
	* @param {WaudSoundOptions} [options] - Sound options.
	* @example
	* 		// MP3 Sound
	* 		var snd = new WaudSound("assets/loop.mp3", { autoplay: false, loop: true, volume: 0.5, onload: _playBgSound });
	*
	* 		// Force HTML5 Audio
	* 		var snd = new WaudSound("assets/loop.mp3", { webaudio: false });
	*
	* 		// Audio Sprite
	* 		var audSprite = new WaudSound("assets/sprite.json");
	* 		audSprite.play("glass");
	*/
	public function new(url:String, ?options:WaudSoundOptions = null) {
		if (Waud.audioManager == null) {
			trace("initialise Waud using Waud.init() before loading sounds");
			return;
		}

		_options = options;

		if (url.indexOf(".json") > 0) {
			isSpriteSound = true;
			_loadSpriteJson(url);
		}
		else {
			isSpriteSound = false;
			_init(url);
		}

		Waud.sounds.set(url, this);
	}

	/**
	* Function to load audio sprite JSON.
	*
	* @private
	* @method _loadSpriteJson
	* @param {String} url - Audio Sprite JSON path.
	*/
	function _loadSpriteJson(jsonUrl:String) {
		var xobj = new XMLHttpRequest();
		xobj.overrideMimeType("application/json");
		xobj.open("GET", jsonUrl, true);
		xobj.onreadystatechange = function() {
			if (xobj.readyState == 4 && xobj.status == 200) {
				_spriteData = Json.parse(xobj.response);
				_init(_spriteData.src);
			}
		};
		xobj.send(null);
	}

	/**
	* Function to initialize sound.
	*
	* @private
	* @method _init
	* @param {String} url - Audio file path.
	*/
	function _init(soundUrl:String) {
		url = soundUrl;
		if (Waud.isWebAudioSupported && Waud.useWebAudio && (_options == null || _options.webaudio == null || _options.webaudio)) {
			_snd = new WebAudioAPISound(url, _options);
		}
		else if (Waud.isHTML5AudioSupported) {
			_snd = new HTML5Sound(url, _options);
		}
		else {
			trace("no audio support in this browser");
			return;
		}

		_snd.isSpriteSound = isSpriteSound;
	}

	/**
	* Function to set sound volume.
	*
	* @method setVolume
	* @param {Float} val - Should be between 0 and 1.
	* @example
	*     snd.setVolume(0.5);
	*/
	public function setVolume(val:Float) {
		if (_snd == null) return;
		_snd.setVolume(val);
	}

	/**
	* Function to get sound volume.
	*
	* @method getVolume
	* @return {Float} between 0 and 1
	* @example
	*     snd.getVolume();
	*/
	public function getVolume():Float {
		if (_snd == null) return 0;
		return _snd.getVolume();
	}

	/**
	* Function to mute sound.
	*
	* @method mute
	* @param {Bool} val
	* @example
	*     snd.mute(true);
	*/
	public function mute(val:Bool) {
		if (_snd == null) return;
		_snd.mute(val);
	}

	/**
	* Function to automatically mute or unmute the sound.
	*
	* @method toggleMute
	* @example
	*     snd.toggleMute();
	*/
	public function toggleMute() {
		if (_snd == null) return;
		_snd.toggleMute();
	}

	/**
	* Function to manually load the sound if `preload` was set to `false` with optional onload callback.
	*
	* @method load
	* @param {Function} [callback] - onload callback function.
	* @return {IWaudSound} sound instance
	* @example
	*     snd.load();
	*     snd.load(callback);
	*/
	public function load(?callback:IWaudSound -> Void):IWaudSound {
		if (_snd == null) return null;
		_snd.load(callback);
		return this;
	}

	/**
	* Function to play the sound with optional sprite name when using audio sprite.
	*
	* @method play
	* @param {String} [spriteName] - Sprite name to play.
	* @return {Int} sound id
	* @example
	*     snd.play();
	*     snd.play("bell");
	*/
	public function play(?spriteName:String, ?soundProps:AudioSpriteSoundProperties = null):Int {
		if (_snd == null) return null;
		if (spriteName != null) {
			for (snd in _spriteData.sprite) {
				if (snd.name == spriteName) {
					soundProps = snd;
					break;
				}
			}
		}
		return _snd.play(spriteName, soundProps);
	}

	/**
	* Function to automatically pause or play the sound.
	*
	* @method togglePlay
	* @example
	*     snd.togglePlay();
	*/
	public function togglePlay() {
		if (_snd == null) return;
		_snd.togglePlay();
	}

	/**
	* Function to check if the sound is playing or not.
	*
	* @method isPlaying
	* @return {Bool} true or false
	* @example
	*     snd.isPlaying();
	*/
	public function isPlaying():Bool {
		if (_snd == null) return false;
		return _snd.isPlaying();
	}

	/**
	* Function to loop or unloop sound.
	*
	* @method loop
	* @param {Bool} val
	* @example
	*     snd.loop(true);
	*/
	public function loop(val:Bool) {
		if (_snd == null) return;
		_snd.loop(val);
	}

	/**
	* Function to stop sound.
	*
	* @method stop
	* @example
	*     snd.stop();
	*/
	public function stop() {
		if (_snd == null) return;
		_snd.stop();
	}

	/**
	* Function to pause sound.
	*
	* @method pause
	* @example
	*     snd.pause();
	*/
	public function pause() {
		if (_snd == null) return;
		_snd.pause();
	}

	/**
	* Function to add callback that triggers when the sound finishes playing.
	*
	* @method onEnd
	* @param {Function} callback - Callback function.
	* @return {IWaudSound} sound instance
	* @example
	*     snd.onEnd(callback);
	*/
	public function onEnd(callback:IWaudSound -> Void):IWaudSound {
		if (_snd == null) return null;
		_snd.onEnd(callback);
		return this;
	}

	/**
	* Function to add callback that triggers when the sound is loaded.
	*
	* @method onLoad
	* @param {Function} callback - Callback function.
	* @return {IWaudSound} sound instance
	* @example
	*     snd.onLoad(callback);
	*/
	public function onLoad(callback:IWaudSound -> Void):IWaudSound {
		if (_snd == null) return null;
		_snd.onLoad(callback);
		return this;<commit_msg>added new methods and duration property<commit_after>import js.html.XMLHttpRequest;
import haxe.Json;

@:expose @:keep class WaudSound implements IWaudSound {

	/**
	* Indicates if the sound is sprite sound or normal sound.
	*
	* @property isSpriteSound
	* @type {Bool}
	* @readOnly
	* @example
 	*     snd.isSpriteSound;
	*/
	public var isSpriteSound:Bool;

	/**
	* Duration of the sound.
	*
	* @property duration
	* @type {Float}
	* @readOnly
	* @example
 	*     snd.duration;
	*/
	public var duration(get, null):Float;

	/**
	* Sound url.
	*
	* @property url
	* @type {String}
	* @readOnly
	* @example
 	*     snd.url;
	*/
	public var url:String;

	var _snd:IWaudSound;
	var _options:WaudSoundOptions;
	var _spriteData:AudioSprite;

	/**
	* Class to automatically use web audio api with HTML5 audio fallback.
	*
	* @class WaudSound
	* @constructor
	* @param {String} url - Can be audio file path or JSON file for audio sprite.
	* @param {WaudSoundOptions} [options] - Sound options.
	* @example
	* 		// MP3 Sound
	* 		var snd = new WaudSound("assets/loop.mp3", { autoplay: false, loop: true, volume: 0.5, onload: _playBgSound });
	*
	* 		// Force HTML5 Audio
	* 		var snd = new WaudSound("assets/loop.mp3", { webaudio: false });
	*
	* 		// Audio Sprite
	* 		var audSprite = new WaudSound("assets/sprite.json");
	* 		audSprite.play("glass");
	*/
	public function new(url:String, ?options:WaudSoundOptions = null) {
		if (Waud.audioManager == null) {
			trace("initialise Waud using Waud.init() before loading sounds");
			return;
		}

		_options = options;

		if (url.indexOf(".json") > 0) {
			isSpriteSound = true;
			_loadSpriteJson(url);
		}
		else {
			isSpriteSound = false;
			_init(url);
		}

		Waud.sounds.set(url, this);
	}

	/**
	* Function to load audio sprite JSON.
	*
	* @private
	* @method _loadSpriteJson
	* @param {String} url - Audio Sprite JSON path.
	*/
	function _loadSpriteJson(jsonUrl:String) {
		var xobj = new XMLHttpRequest();
		xobj.overrideMimeType("application/json");
		xobj.open("GET", jsonUrl, true);
		xobj.onreadystatechange = function() {
			if (xobj.readyState == 4 && xobj.status == 200) {
				_spriteData = Json.parse(xobj.response);
				_init(_spriteData.src);
			}
		};
		xobj.send(null);
	}

	/**
	* Function to initialize sound.
	*
	* @private
	* @method _init
	* @param {String} url - Audio file path.
	*/
	function _init(soundUrl:String) {
		url = soundUrl;
		if (Waud.isWebAudioSupported && Waud.useWebAudio && (_options == null || _options.webaudio == null || _options.webaudio)) {
			_snd = new WebAudioAPISound(url, _options);
		}
		else if (Waud.isHTML5AudioSupported) {
			_snd = new HTML5Sound(url, _options);
		}
		else {
			trace("no audio support in this browser");
			return;
		}

		_snd.isSpriteSound = isSpriteSound;
	}

	function get_duration():Float {
		if (_snd == null) return 0;
		return _snd.duration;
	}

	/**
	* Function to set sound volume.
	*
	* @method setVolume
	* @param {Float} val - Should be between 0 and 1.
	* @example
	*     snd.setVolume(0.5);
	*/
	public function setVolume(val:Float) {
		if (_snd == null) return;
		_snd.setVolume(val);
	}

	/**
	* Function to get sound volume.
	*
	* @method getVolume
	* @return {Float} between 0 and 1
	* @example
	*     snd.getVolume();
	*/
	public function getVolume():Float {
		if (_snd == null) return 0;
		return _snd.getVolume();
	}

	/**
	* Function to mute sound.
	*
	* @method mute
	* @param {Bool} val
	* @example
	*     snd.mute(true);
	*/
	public function mute(val:Bool) {
		if (_snd == null) return;
		_snd.mute(val);
	}

	/**
	* Function to automatically mute or unmute the sound.
	*
	* @method toggleMute
	* @example
	*     snd.toggleMute();
	*/
	public function toggleMute() {
		if (_snd == null) return;
		_snd.toggleMute();
	}

	/**
	* Function to manually load the sound if `preload` was set to `false` with optional onload callback.
	*
	* @method load
	* @param {Function} [callback] - onload callback function.
	* @return {IWaudSound} sound instance
	* @example
	*     snd.load();
	*     snd.load(callback);
	*/
	public function load(?callback:IWaudSound -> Void):IWaudSound {
		if (_snd == null) return null;
		_snd.load(callback);
		return this;
	}

	/**
	* Function to play the sound with optional sprite name when using audio sprite.
	*
	* @method play
	* @param {String} [spriteName] - Sprite name to play.
	* @return {Int} sound id
	* @example
	*     snd.play();
	*     snd.play("bell");
	*/
	public function play(?spriteName:String, ?soundProps:AudioSpriteSoundProperties = null):Int {
		if (_snd == null) return null;
		if (spriteName != null) {
			for (snd in _spriteData.sprite) {
				if (snd.name == spriteName) {
					soundProps = snd;
					break;
				}
			}
		}
		return _snd.play(spriteName, soundProps);
	}

	/**
	* Function to automatically pause or play the sound.
	*
	* @method togglePlay
	* @example
	*     snd.togglePlay();
	*/
	public function togglePlay() {
		if (_snd == null) return;
		_snd.togglePlay();
	}

	/**
	* Function to check if the sound is playing or not.
	*
	* @method isPlaying
	* @return {Bool} true or false
	* @example
	*     snd.isPlaying();
	*/
	public function isPlaying():Bool {
		if (_snd == null) return false;
		return _snd.isPlaying();
	}

	/**
	* Function to loop or unloop sound.
	*
	* @method loop
	* @param {Bool} val
	* @example
	*     snd.loop(true);
	*/
	public function loop(val:Bool) {
		if (_snd == null) return;
		_snd.loop(val);
	}

	/**
	* Function to stop sound.
	*
	* @method stop
	* @example
	*     snd.stop();
	*/
	public function stop() {
		if (_snd == null) return;
		_snd.stop();
	}

	/**
	* Function to pause sound.
	*
	* @method pause
	* @example
	*     snd.pause();
	*/
	public function pause() {
		if (_snd == null) return;
		_snd.pause();
	}

	/**
	* Function to set playback position in seconds.
	*
	* **Not supported for audio sprites yet.**
	*
	* @method setTime
	* @param {Float} time - playback position in seconds.
	* @example
	*     snd.setTime(30);
	*/
	public function setTime(time:Float) {
		if (_snd == null) return;
		_snd.setTime(time);
	}

	/**
	* Function to get the current playback position in seconds.
	*
	* **Not supported for audio sprites yet.**
	*
	* @method getTime
	* @example
	*     snd.getTime();
	*/
	public function getTime():Float {
		if (_snd == null) return 0;
		return _snd.getTime();
	}

	/**
	* Function to add callback that triggers when the sound finishes playing.
	*
	* @method onEnd
	* @param {Function} callback - Callback function.
	* @return {IWaudSound} sound instance
	* @example
	*     snd.onEnd(callback);
	*/
	public function onEnd(callback:IWaudSound -> Void):IWaudSound {
		if (_snd == null) return null;
		_snd.onEnd(callback);
		return this;
	}

	/**
	* Function to add callback that triggers when the sound is loaded.
	*
	* @method onLoad
	* @param {Function} callback - Callback function.
	* @return {IWaudSound} sound instance
	* @example
	*     snd.onLoad(callback);
	*/
	public function onLoad(callback:IWaudSound -> Void):IWaudSound {
		if (_snd == null) return null;
		_snd.onLoad(callback);
		return this;
{'fraction_non_alphanumeric': 0.14131988862969372, 'fraction_numerical': 0.0034615095191511775, 'mean_word_length': 3.195075757575758, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6296338', 'n_tokens_mistral': 5107, 'n_tokens_neox': 4769, 'n_words': 1263}