Advertisement
_-Katsu-_

Mbrola load all voices fix

Apr 27th, 2015
1,704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package de.dfki.lt.freetts.en.us;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Locale;
  6.  
  7. import com.sun.speech.freetts.Age;
  8. import com.sun.speech.freetts.Gender;
  9. import com.sun.speech.freetts.ValidationException;
  10. import com.sun.speech.freetts.Voice;
  11. import com.sun.speech.freetts.VoiceDirectory;
  12. import com.sun.speech.freetts.en.us.CMULexicon;
  13. import com.sun.speech.freetts.util.Utilities;
  14.  
  15. public class MbrolaVoiceDirectory extends VoiceDirectory {
  16.  
  17.     public MbrolaVoiceDirectory() {
  18.     }
  19.  
  20.     public Voice[] getVoices() {
  21.         String base = Utilities.getProperty("mbrola.base", null);
  22.  
  23.         if (base == null || base.trim().length() == 0) {
  24.             System.out.println("System property \"mbrola.base\" is undefined.  Will not use MBROLA voices.");
  25.             return new Voice[0];
  26.         }
  27.  
  28.         File mbrolaFolder = new File(base);
  29.  
  30.         CMULexicon lexicon = new CMULexicon("cmulex");
  31.  
  32.         // Voice mbrola1 = new MbrolaVoice("us1", "us1", 150F, 180F, 22F,
  33.         // "mbrola_us1", Gender.FEMALE, Age.YOUNGER_ADULT, "MBROLA Voice us1",
  34.         // Locale.US, "general", "mbrola", lexicon);
  35.         // Voice mbrola2 = new MbrolaVoice("us2", "us2", 150F, 115F, 12F,
  36.         // "mbrola_us2", Gender.MALE, Age.YOUNGER_ADULT, "MBROLA Voice us2",
  37.         // Locale.US, "general", "mbrola", lexicon);
  38.         // Voice mbrola3 = new MbrolaVoice("us3", "us3", 150F, 125F, 12F,
  39.         // "mbrola_us3", Gender.MALE, Age.YOUNGER_ADULT, "MBROLA Voice us3",
  40.         // Locale.US, "general", "mbrola", lexicon);
  41.  
  42.         ArrayList<Voice> allVoices = new ArrayList<Voice>();
  43.  
  44.         for (File file : mbrolaFolder.listFiles()) {
  45.             if (file.isDirectory()) {
  46.                 String directoryName = file.getName();
  47.                 if (isValidVoiceDirectoryName(directoryName)) {
  48.                     Gender gender = Gender.DONT_CARE;
  49.  
  50.                     if (new File(file.getAbsolutePath() + "/male.txt").exists()) {
  51.                         gender = Gender.MALE;
  52.                     } else if (new File(file.getAbsolutePath() + "/female.txt").exists()) {
  53.                         gender = Gender.FEMALE;
  54.                     }
  55.  
  56.                     allVoices.add(new MbrolaVoice(directoryName, directoryName, 150F, 125F, 12F, "mbrola_" + directoryName, gender, Age.YOUNGER_ADULT, "MBROLA Voice " + directoryName, Locale.US, "general", "mbrola", lexicon));
  57.                 }
  58.             }
  59.         }
  60.  
  61.         ArrayList<Voice> validVoices = new ArrayList<Voice>();
  62.  
  63.         int count = 0;
  64.  
  65.         for (Voice voice : allVoices) {
  66.             MbrolaVoiceValidator validator = new MbrolaVoiceValidator((MbrolaVoice) voice);
  67.  
  68.             try {
  69.                 validator.validate();
  70.                 validVoices.add(voice);
  71.                 count++;
  72.  
  73.                 System.out.println("Added voice: " + voice.getName() + "(" + voice.getGender().toString() + ")");
  74.             } catch (ValidationException ve) {
  75.                 System.out.println("Invalid voice: " + voice.getName());
  76.             }
  77.         }
  78.  
  79.         if (count == 0) {
  80.             System.err.println("\nCould not validate any MBROLA voices at\n\n  " + base + "\n");
  81.             if (base.indexOf('~') != -1) System.err.println("DO NOT USE ~ as part of the path name\nto specify the mbrola.base property.");
  82.             System.err.println("Make sure you FULLY specify the path to\nthe MBROLA directory using the mbrola.base\nsystem property.\n");
  83.             return new Voice[0];
  84.         } else {
  85.             return validVoices.toArray(new Voice[count]);
  86.         }
  87.     }
  88.  
  89.     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" };
  90.  
  91.     public boolean isValidVoiceDirectoryName(String directoryName) {
  92.         if (directoryName == null) return false;
  93.         if (directoryName.isEmpty()) return false;
  94.  
  95.         for (String validVoiceDirectoryName : validVoiceDirectoryNames) {
  96.             if (directoryName.startsWith(validVoiceDirectoryName)) {
  97.                 return true;
  98.             }
  99.         }
  100.  
  101.         return false;
  102.     }
  103.  
  104.     public static void main(String[] args) {
  105.         System.out.println((new MbrolaVoiceDirectory()).toString());
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement