Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.dfki.lt.freetts.en.us;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Locale;
- import com.sun.speech.freetts.Age;
- import com.sun.speech.freetts.Gender;
- import com.sun.speech.freetts.ValidationException;
- import com.sun.speech.freetts.Voice;
- import com.sun.speech.freetts.VoiceDirectory;
- import com.sun.speech.freetts.en.us.CMULexicon;
- import com.sun.speech.freetts.util.Utilities;
- public class MbrolaVoiceDirectory extends VoiceDirectory {
- public MbrolaVoiceDirectory() {
- }
- public Voice[] getVoices() {
- String base = Utilities.getProperty("mbrola.base", null);
- if (base == null || base.trim().length() == 0) {
- System.out.println("System property \"mbrola.base\" is undefined. Will not use MBROLA voices.");
- return new Voice[0];
- }
- File mbrolaFolder = new File(base);
- CMULexicon lexicon = new CMULexicon("cmulex");
- // Voice mbrola1 = new MbrolaVoice("us1", "us1", 150F, 180F, 22F,
- // "mbrola_us1", Gender.FEMALE, Age.YOUNGER_ADULT, "MBROLA Voice us1",
- // Locale.US, "general", "mbrola", lexicon);
- // Voice mbrola2 = new MbrolaVoice("us2", "us2", 150F, 115F, 12F,
- // "mbrola_us2", Gender.MALE, Age.YOUNGER_ADULT, "MBROLA Voice us2",
- // Locale.US, "general", "mbrola", lexicon);
- // Voice mbrola3 = new MbrolaVoice("us3", "us3", 150F, 125F, 12F,
- // "mbrola_us3", Gender.MALE, Age.YOUNGER_ADULT, "MBROLA Voice us3",
- // Locale.US, "general", "mbrola", lexicon);
- ArrayList<Voice> allVoices = new ArrayList<Voice>();
- for (File file : mbrolaFolder.listFiles()) {
- if (file.isDirectory()) {
- String directoryName = file.getName();
- if (isValidVoiceDirectoryName(directoryName)) {
- Gender gender = Gender.DONT_CARE;
- if (new File(file.getAbsolutePath() + "/male.txt").exists()) {
- gender = Gender.MALE;
- } else if (new File(file.getAbsolutePath() + "/female.txt").exists()) {
- gender = Gender.FEMALE;
- }
- allVoices.add(new MbrolaVoice(directoryName, directoryName, 150F, 125F, 12F, "mbrola_" + directoryName, gender, Age.YOUNGER_ADULT, "MBROLA Voice " + directoryName, Locale.US, "general", "mbrola", lexicon));
- }
- }
- }
- ArrayList<Voice> validVoices = new ArrayList<Voice>();
- int count = 0;
- for (Voice voice : allVoices) {
- MbrolaVoiceValidator validator = new MbrolaVoiceValidator((MbrolaVoice) voice);
- try {
- validator.validate();
- validVoices.add(voice);
- count++;
- System.out.println("Added voice: " + voice.getName() + "(" + voice.getGender().toString() + ")");
- } catch (ValidationException ve) {
- System.out.println("Invalid voice: " + voice.getName());
- }
- }
- if (count == 0) {
- System.err.println("\nCould not validate any MBROLA voices at\n\n " + base + "\n");
- if (base.indexOf('~') != -1) System.err.println("DO NOT USE ~ as part of the path name\nto specify the mbrola.base property.");
- System.err.println("Make sure you FULLY specify the path to\nthe MBROLA directory using the mbrola.base\nsystem property.\n");
- return new Voice[0];
- } else {
- return validVoices.toArray(new Voice[count]);
- }
- }
- private String[] validVoiceDirectoryNames = new String[] { "us", "af", "ar", "br", "bz", "en", "ca", "cn", "cr", "cz", "nl", "nz", "ee", "pt", "fr", "de", "es", "gr", "hb", "hn", "hu", "ic", "id", "in", "ir", "it", "jp", "la", "lt", "ma", "pl", "mx", "ro", "sw", "tl", "tr", "vz" };
- public boolean isValidVoiceDirectoryName(String directoryName) {
- if (directoryName == null) return false;
- if (directoryName.isEmpty()) return false;
- for (String validVoiceDirectoryName : validVoiceDirectoryNames) {
- if (directoryName.startsWith(validVoiceDirectoryName)) {
- return true;
- }
- }
- return false;
- }
- public static void main(String[] args) {
- System.out.println((new MbrolaVoiceDirectory()).toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement