<commit_before>/**
 * New conception of audio player for web.
 *
 * @author <NAME>
 * @copyright 2017 MIT License
 * @version 0.1.0
 */
(function (_w, _d) {
	'use strict';

	// consts

	var MUZUS_SELECTOR = '.muzus',
		MUZUS_PLAY = 'muzus-play',
		MUZUS_PAUSE = 'muzus-pause',
		MUZUS_TRACK = 'muzus-track',
		MUZUS_PROGRESS = 'muzus-progress',
		TRACK_STOPED = 0,
		TRACK_PLAYING = 1,
		TRACK_PAUSED = 2;

	// helpers

	function toFormatedString (value) {
		var min = parseInt((value / 60) % 60),
			sec = parseInt(value % 60);
		return min + ':' + (sec < 10 ? '0' + sec : sec);
	}

	function createElement (tagName, className, parentElement) {
		var e = _d.createElement(tagName);
		e.className = className;
		if (typeof parentElement !== 'undefined') {
			parentElement.appendChild(e);
		}
		return e;
	}

	function createRangeElement (className, parentElement) {
		var e = createElement('input', className, parentElement);
		e.type = 'range';
		e.max = 100;
		e.min = 0;
		e.step = 0.01;
		e.value = 0;
		return e;
	}

	function createAudioElement () {
		var e = createElement('audio');
		e.preload = 'metadata';
		e.type = 'audio/mpeg';
		e.volume = 1;
		return e;
	}

	// classes

	function Player (element, _repeatTrackListEnabled) {

		// private

		var _currentTrack = null,
			_trackList = [],
			_player = createAudioElement();

		// setup player

		_player.onplaying = function () {
			_currentTrack.setEndTime(_player.duration);
		};
		_player.ontimeupdate = function () {
			if (_player.duration > 0) {
				_currentTrack.setProgress((_player.currentTime / _player.duration) * 100);
				_currentTrack.setCurrentTime(_player.currentTime);
			}
		};
		_player.onprogress = function () {
			if (_player.duration > 0) {
				var value = (_player.buffered.end(_player.buffered.length - 1) / _player.duration) * 100;
				_currentTrack.setBuffer(value);
			}
		};
		_player.onended = nextTrack;
		_player.onerror = function (e) {
			_w.console.error(e);
			stopPlayer();
			_currentTrack = null;
		};

		// TODO:
		var trackElements = element.querySelectorAll('a');
		for (var i = 0; i < trackElements.length; i++) {
			var trackElement = trackElements[i];
			var track = new Track(i, trackElement);
			track.setClickHandler(trackClickHandler);
			track.setChangeProgressHandler(trackProgressChangedHandler);

			_trackList.push(track);

			preloadTrack(track);
		}

		// private methods

		function trackClickHandler (track) {
			switch (track.getState()) {
				case TRACK_PLAYING:
					_currentTrack.pause();
					_player.pause();
				break;
				case TRACK_PAUSED:
					_currentTrack.play();
					_player.play();
				break;
				default:
					startPlayTrack(track);
				break;
			}
		}

		function trackProgressChangedHandler (value) {
			if (_player.duration) {
				_player.currentTime = (value * _player.duration) / 100;
			} else {
				value = 0;
			}
		}

		function nextTrack () {
			var currentTrackIndex = _currentTrack.id,
				nextTrackIndex = currentTrackIndex < _trackList.length - 1 ? currentTrackIndex + 1 : 0;

			if (_trackList.length == 1) {
				stopPlayer();
				return;
			}
			if (nextTrackIndex < currentTrackIndex && !_repeatTrackListEnabled) {
				stopPlayer();
				return;
			}

			startPlayTrack(_trackList[nextTrackIndex]);
		}

		function startPlayTrack (track) {
			if (!!_currentTrack) {
				_currentTrack.stop();
			}
			_currentTrack = track;
			_player.src = _currentTrack.src;

			_currentTrack.play();
			_player.play();
		}

		function stopPlayer () {
			_player.pause();
			_player.currentTime = 0;

			_currentTrack.stop();
		}

		function preloadTrack (track) {
			var preloader = createAudioElement();
			preloader.volume = 0;
			preloader.src = track.src;
			preloader.onloadedmetadata = function () {
				preloader.pause();
				track.setCurrentTime(0);
				track.setEndTime(preloader.duration);
				preloader = null;
			}
			preloader.play();
		}
	}

	function Track (id, element) {

		var _title = element.innerText,
			_src = element.href;

		// replace element

		var newElement = createElement('div', MUZUS_TRACK);
		element.parentNode.replaceChild(newElement, element);

		// private<commit_msg>Fix async play/pause for preload<commit_after>/**
 * New conception of audio player for web.
 *
 * @author <NAME>
 * @copyright 2017 MIT License
 * @version 0.1.0
 */
(function (_w, _d) {
	'use strict';

	// consts

	var MUZUS_SELECTOR = '.muzus',
		MUZUS_PLAY = 'muzus-play',
		MUZUS_PAUSE = 'muzus-pause',
		MUZUS_TRACK = 'muzus-track',
		MUZUS_PROGRESS = 'muzus-progress',
		TRACK_STOPED = 0,
		TRACK_PLAYING = 1,
		TRACK_PAUSED = 2;

	// helpers

	function toFormatedString (value) {
		var min = parseInt((value / 60) % 60),
			sec = parseInt(value % 60);
		return min + ':' + (sec < 10 ? '0' + sec : sec);
	}

	function createElement (tagName, className, parentElement) {
		var e = _d.createElement(tagName);
		e.className = className;
		if (typeof parentElement !== 'undefined') {
			parentElement.appendChild(e);
		}
		return e;
	}

	function createRangeElement (className, parentElement) {
		var e = createElement('input', className, parentElement);
		e.type = 'range';
		e.max = 100;
		e.min = 0;
		e.step = 0.01;
		e.value = 0;
		return e;
	}

	function createAudioElement () {
		var e = createElement('audio');
		e.preload = 'metadata';
		e.type = 'audio/mpeg';
		e.volume = 1;
		return e;
	}

	// classes

	function Player (element, _repeatTrackListEnabled) {

		// private

		var _currentTrack = null,
			_trackList = [],
			_player = createAudioElement();

		// setup player

		_player.onplaying = function () {
			_currentTrack.setEndTime(_player.duration);
		};
		_player.ontimeupdate = function () {
			if (_player.duration > 0) {
				_currentTrack.setProgress((_player.currentTime / _player.duration) * 100);
				_currentTrack.setCurrentTime(_player.currentTime);
			}
		};
		_player.onprogress = function () {
			if (_player.duration > 0) {
				var value = (_player.buffered.end(_player.buffered.length - 1) / _player.duration) * 100;
				_currentTrack.setBuffer(value);
			}
		};
		_player.onended = nextTrack;
		_player.onerror = function (e) {
			_w.console.error(e);
			stopPlayer();
			_currentTrack = null;
		};

		// TODO:
		var trackElements = element.querySelectorAll('a');
		for (var i = 0; i < trackElements.length; i++) {
			var trackElement = trackElements[i];
			var track = new Track(i, trackElement);
			track.setClickHandler(trackClickHandler);
			track.setChangeProgressHandler(trackProgressChangedHandler);

			_trackList.push(track);

			preloadTrack(track);
		}

		// private methods

		function trackClickHandler (track) {
			switch (track.getState()) {
				case TRACK_PLAYING:
					_currentTrack.pause();
					_player.pause();
				break;
				case TRACK_PAUSED:
					_currentTrack.play();
					_player.play();
				break;
				default:
					startPlayTrack(track);
				break;
			}
		}

		function trackProgressChangedHandler (value) {
			if (_player.duration) {
				_player.currentTime = (value * _player.duration) / 100;
			} else {
				value = 0;
			}
		}

		function nextTrack () {
			var currentTrackIndex = _currentTrack.id,
				nextTrackIndex = currentTrackIndex < _trackList.length - 1 ? currentTrackIndex + 1 : 0;

			if (_trackList.length == 1) {
				stopPlayer();
				return;
			}
			if (nextTrackIndex < currentTrackIndex && !_repeatTrackListEnabled) {
				stopPlayer();
				return;
			}

			startPlayTrack(_trackList[nextTrackIndex]);
		}

		function startPlayTrack (track) {
			if (!!_currentTrack) {
				_currentTrack.stop();
			}
			_currentTrack = track;
			_player.src = _currentTrack.src;

			_currentTrack.play();
			_player.play();
		}

		function stopPlayer () {
			_player.pause();
			_player.currentTime = 0;

			_currentTrack.stop();
		}

		function preloadTrack (track) {
			var preloader = createAudioElement();
			preloader.volume = 0;
			preloader.src = track.src;
			preloader.onplaying = function () {
				preloader.pause();
				track.setCurrentTime(0);
				track.setEndTime(preloader.duration);
				preloader = null;
			}
			_w.setTimeout(function () {
				preloader.play();
			}, 100);
		}
	}

	function Track (id, element) {

		var _title = element.innerText,
			_src = element.href;

		// replace element

		var newElement = createElement('div', MUZUS_TRACK);
		element.parentNode.replaceChild(newElement, element);

		// private
{'fraction_non_alphanumeric': 0.13275820752456266, 'fraction_numerical': 0.012101605559549485, 'mean_word_length': 3.2478371501272263, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5164106', 'n_tokens_mistral': 3426, 'n_tokens_neox': 2794, 'n_words': 583}