<commit_before>'use strict';

/**
 *
 * @param {Object} _options configuration parameters
 * @param {String} _options.url  resource URL
 *
 */
function SpeechSynthesis (_options) {

	var options = _options || {
		// TODO: change to production server when released
		url: 'http://localhost:3000'
	}

	this._url = options.url;
	this._audioElement = options.audioElement;
	this._voices = [];

	var xhr = new XMLHttpRequest();
	xhr.open('GET', this._url + '/voices', true);
	xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
	xhr.onreadystatechange = function() {
		if (xhr.readyState === 4) {
			this._voices = JSON.parse(xhr.responseText).voices;
			this.onvoiceschanged();
		}
	}.bind(this);

	xhr.send();

}

// SpeechSynthesis methods
SpeechSynthesis.prototype.getVoices = function() {
	var i;
	var speechSynthesisVoiceCollection = [];
	var voices = this._voices;
	for (i=0; i < voices.length; i++) {
		var voiceOptions = voices[i];
		var speechSynthesisVoice = new SpeechSynthesisVoice(voiceOptions);
		speechSynthesisVoice.localService = false;
		speechSynthesisVoiceCollection.push(speechSynthesisVoice);
	}
	return speechSynthesisVoiceCollection;
}

SpeechSynthesis.prototype.speak = function(utterance) {
	var xhr = new XMLHttpRequest();
	var audio = this._audioElement || new Audio();
	var url = this._url + '/synthesize?' + serializeQueryString(utterance);
	xhr.open('GET', url, true);
	xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
	xhr.setRequestHeader("Accept", "audio/ogg; codecs=opus");
	xhr.responseType = 'blob';
	xhr.onreadystatechange = function() {
		if (xhr.readyState === 4) {
			var blob = new Blob([this.response], {type: 'audio/ogg'});
			if (!utterance.download) {
				var objectUrl = URL.createObjectURL(blob);
				audio.src = objectUrl;
				audio.play();
				utterance.start();
			} else {<commit_msg>Add handler to revoke object URL once resource is loaded; update default URL<commit_after>'use strict';

/**
 *
 * @param {Object} _options configuration parameters
 * @param {String} _options.url  resource URL
 *
 */
function SpeechSynthesis (_options) {

	var options = _options || {
		// TODO: change to production server when released
		url: 'https://stream-d.watsonplatform.net/text-to-speech-beta/api/v1'
	}

	this._url = options.url;
	this._audioElement = options.audioElement;
	this._voices = [];

	var xhr = new XMLHttpRequest();
	xhr.open('GET', this._url + '/voices', true);
	xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
	xhr.onreadystatechange = function() {
		if (xhr.readyState === 4) {
			this._voices = JSON.parse(xhr.responseText).voices;
			this.onvoiceschanged();
		}
	}.bind(this);

	xhr.send();

}

// SpeechSynthesis methods
SpeechSynthesis.prototype.getVoices = function() {
	var i;
	var speechSynthesisVoiceCollection = [];
	var voices = this._voices;
	for (i=0; i < voices.length; i++) {
		var voiceOptions = voices[i];
		var speechSynthesisVoice = new SpeechSynthesisVoice(voiceOptions);
		speechSynthesisVoice.localService = false;
		speechSynthesisVoiceCollection.push(speechSynthesisVoice);
	}
	return speechSynthesisVoiceCollection;
}

SpeechSynthesis.prototype.speak = function(utterance) {
	var xhr = new XMLHttpRequest();
	var audio = this._audioElement || new Audio();
	var url = this._url + '/synthesize?' + serializeQueryString(utterance);
	xhr.open('GET', url, true);
	xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
	xhr.setRequestHeader("Accept", "audio/ogg; codecs=opus");
	xhr.responseType = 'blob';
	xhr.onreadystatechange = function() {
		if (xhr.readyState === 4) {
			var blob = new Blob([this.response], {type: 'audio/ogg'});
			if (!utterance.download) {
				var objectUrl = URL.createObjectURL(blob);
				audio.src = objectUrl;
				audio.onload = function() {
					URL.revokeObjectURL(objectURL);
				};
				audio.play();
				utterance.start();
			} else {
{'fraction_non_alphanumeric': 0.14707379134860052, 'fraction_numerical': 0.003816793893129771, 'mean_word_length': 4.607703281027105, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6060176', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1279, 'n_words': 269}