<commit_before>/*******************************************************************************
 * Copyright (c) 2013 See AUTHORS file.
 * 
 * This file is part of SleepFighter.
 * 
 * SleepFighter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SleepFighter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SleepFighter. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package se.chalmers.dat255.sleepfighter.speech;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import se.chalmers.dat255.sleepfighter.R;
import se.chalmers.dat255.sleepfighter.utils.debug.Debug;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;

public class TextToSpeechUtil {
	
	private static final String UTTERANCE_ID = "alarm_speech";
	private static final int CHECK_TTS_DATA_REQUEST_CODE = 2;

	private TextToSpeechUtil() {	
	}

	// set some miscellaneous settings for tts.
	public static void config(TextToSpeech tts) {
/*		tts.setSpeechRate(1.8f);
		tts.setPitch(1.2f);*/
	}

	/**
	 * Do text-to-speech through the alarm stream.
	 * 
	 * @param tts
	 *            the TextToSpeech to speak through
	 * @param string
	 *            the String that is to be spoken
	 */
	public static void speakAlarm(TextToSpeech tts, String string) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
				String.valueOf(AudioManager.STREAM_ALARM));
		params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTERANCE_ID);
		tts.speak(string, TextToSpeech.QUEUE_FLUSH, params);
	}

	// check whether we have TTS data. 
	public static void checkTextToSpeech(Activity activity) {
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		activity.startActivityForResult(checkIntent, CHECK_TTS_DATA_REQUEST_CODE);
	}
	
	/*
	 * Returns whether there exists an installed voice for the locale on the current device. <commit_msg>Speech now uses the music stream.<commit_after>/*******************************************************************************
 * Copyright (c) 2013 See AUTHORS file.
 * 
 * This file is part of SleepFighter.
 * 
 * SleepFighter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SleepFighter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SleepFighter. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package se.chalmers.dat255.sleepfighter.speech;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import se.chalmers.dat255.sleepfighter.R;
import se.chalmers.dat255.sleepfighter.utils.debug.Debug;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;

public class TextToSpeechUtil {
	
	private static final String UTTERANCE_ID = "alarm_speech";
	private static final int CHECK_TTS_DATA_REQUEST_CODE = 2;

	private TextToSpeechUtil() {	
	}

	// set some miscellaneous settings for tts.
	public static void config(TextToSpeech tts) {
		// do nothing. The user can control the speechrate and pitch in the android settings,
		// so we should not change these. 
/*		tts.setSpeechRate(1.8f);
		tts.setPitch(1.2f);*/
	}

	/**
	 * Do text-to-speech through the alarm stream.
	 * 
	 * @param tts
	 *            the TextToSpeech to speak through
	 * @param string
	 *            the String that is to be spoken
	 */
	public static void speakAlarm(TextToSpeech tts, String string) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
				String.valueOf(AudioManager.STREAM_MUSIC));
		params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTERANCE_ID);
		tts.speak(string, TextToSpeech.QUEUE_FLUSH, params);
	}

	// check whether we have TTS data. 
	public static void checkTextToSpeech(Activity activity) {
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		activity.startActivityForResult(checkIntent, CHECK_TTS_DATA_REQUEST_CODE);
	}
	
	/*
	 * Returns whether there exists an installed voice for the locale on the current device. 
{'fraction_non_alphanumeric': 0.13718546132339235, 'fraction_numerical': 0.007082945013979497, 'mean_word_length': 4.475510204081632, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5713635', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1507, 'n_words': 499}