<commit_before>'use strict';

var sounds = [];
var current = null;
var playingEnabled = true;

var playSound = function(index) {
    if (current) {
        current.stop(0);
        current = null;
    }
    var context = sounds[index].context;
    var source = context.createBufferSource();
    source.buffer = sounds[index].buffer;
    source.connect(context.destination);
    current = source;
    current.onended = clearCurrent;
    source.start(0);
    return source;
};

var clearCurrent = function() {
    current = null;
};

var iterateSounds = function(soundIndexes, index) {
    if (index >= soundIndexes.length) {
        return;
    }
    if (playingEnabled) {
        var source = playSound(soundIndexes[index]);
        source.addEventListener('ended', function() {
            iterateSounds(soundIndexes, index + 1);
        });

    } else {
        playingEnabled = true;
    }
};

var AudioPlayer = module.exports = {

    loadSounds: function(array) {
        sounds = array;
    },

    playSound: playSound,

    /**
     * @param {number[]} soundIndexes
     */
    playSounds: function(soundIndexes) {
        iterateSounds(soundIndexes, 0);
    },

    stop: function() {
        playingEnabled = false;
    }
};<commit_msg>added back option to play sound from path instead of blob<commit_after>'use strict';

var Data = require('./Data');
var sounds = [];
var current = null;
var playingEnabled = true;
var audioFile = null;

var playSound = function(index) {
	console.log('playing from blob');
	if (current) {
		current.stop(0);
		current = null;
	}
	var context = sounds[index].context;
	var source = context.createBufferSource();
	source.buffer = sounds[index].buffer;
	source.connect(context.destination);
	current = source;
	current.onended = clearCurrent;
	source.start(0);
	return source;
};

var playSoundFromPath = function(path) {
	console.log('playing from path');
	if (audioFile !== null) {
		audioFile.pause();
		audioFile.startTime = 0;
	}
	audioFile = new Audio(path);
	audioFile.play().catch(function() {
		//whatever
	});
};

var clearCurrent = function() {
	current = null;
};

var iterateSounds = function(soundIndexes, index) {
	if (index >= soundIndexes.length) {
		return;
	}
	if (playingEnabled) {
		var source = playSound(soundIndexes[index]);
		source.addEventListener('ended', function() {
			iterateSounds(soundIndexes, index + 1);
		});

	} else {
		playingEnabled = true;
	}
};

var AudioPlayer = module.exports = {

	loadSounds: function(array) {
		sounds = array;
	},

	playSound: function(index) {
		if (sounds) {
			playSound(index);
		} else {
			playSoundFromPath(Data.getUrl(index));
		}
	},

    /**
     * @param {number[]} soundIndexes
     */
	playSounds: function(soundIndexes) {
		iterateSounds(soundIndexes, 0);
	},

	stop: function() {
		playingEnabled = false;
	}
};
{'fraction_non_alphanumeric': 0.13897707231040565, 'fraction_numerical': 0.0031746031746031746, 'mean_word_length': 3.296969696969697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7104931', 'n_tokens_mistral': 941, 'n_tokens_neox': 881, 'n_words': 201}