upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / jsapi / FreeTTSSynthesizerModeDesc.java
1 /**
2  * Copyright 2003 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8
9 package com.sun.speech.freetts.jsapi;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Locale;
13
14 import javax.speech.Engine;
15 import javax.speech.EngineCreate;
16 import javax.speech.EngineException;
17 import javax.speech.synthesis.SynthesizerModeDesc;
18
19 import com.sun.speech.engine.synthesis.BaseVoice;
20 import com.sun.speech.freetts.ValidationException;
21
22 /**
23  * Represents a SynthesizerModeDesc for the
24  * FreeTTSSynthesizer. A FreeTTSSynthesizerModeDesc adds 
25  * an audio player to the standard mode items.
26  */
27 public class FreeTTSSynthesizerModeDesc extends SynthesizerModeDesc 
28 implements EngineCreate {
29
30     /**
31      * Creates a fully-specified descriptor.
32      * Any of the features may be <code>null</code>.
33      *
34      * @param engineName  the name of the engine
35      * @param modeName   the name of the mode
36      * @param locale  the locale associated with this mode
37      */
38     public FreeTTSSynthesizerModeDesc( String engineName, String modeName,
39             Locale locale) {
40         super(engineName, modeName, locale, Boolean.FALSE, null);
41     }
42
43     /**
44      * Returns the valid voices in this synthesizer mode.
45      *
46      * @return an array of valid voices, if no valid voices, it will
47      *    return an array of size 0
48      */
49     public javax.speech.synthesis.Voice[] getVoices() {
50         List voiceList = new LinkedList();
51         javax.speech.synthesis.Voice[] voices = super.getVoices();
52         int count = 0;
53         for (int i = 0; i < voices.length; i++) {
54             FreeTTSVoice freettsVoice = (FreeTTSVoice) voices[i];
55             try {
56                 freettsVoice.validate();
57                 voiceList.add(freettsVoice);
58                 count++;
59             } catch (ValidationException ve) {
60                 // don't do anything here if a FreeTTSVoice is invalid
61             }
62         }
63         javax.speech.synthesis.Voice[] validVoices =
64             new javax.speech.synthesis.Voice[count];
65         voiceList.toArray(validVoices);
66         
67         return validVoices;
68     }
69     
70     /**
71      * Returns true if this is a valid FreeTTSSynthesizerModeDesc.
72      * It is valid if it contains at least one valid Voice.
73      * Returns false otherwise.
74      *
75      * @throws ValidationException if this FreeTTSSynthesizerModeDesc
76      *    is invalid
77      */
78     public void validate() throws ValidationException {
79         javax.speech.synthesis.Voice[] voices = super.getVoices();
80         int invalidCount = 0;
81         String validationMessage = "";
82
83         for (int i = 0; i < voices.length; i++) {
84             try {
85                 ((FreeTTSVoice) voices[i]).validate();
86             } catch (ValidationException ve) {
87                 invalidCount++;
88                 validationMessage += (ve.getMessage() + "\n");
89             }
90         }
91         if (invalidCount == voices.length) {
92             throw new ValidationException
93                 (validationMessage + getModeName() + " has no valid voices.");
94         }
95     }
96
97     /**
98      * Constructs a FreeTTSSynthesizer with the properties of this mode
99      * descriptor.
100      * 
101      * @return a synthesizer that mathes the mode
102      *
103      * @throws IllegalArgumentException  if the properties of this
104      *          descriptor do not match any known engine or mode
105      * @throws EngineException if the engine could not be created
106      * @throws SecurityException if the caller does not have
107      *          permission to use the speech engine
108      */
109     public Engine createEngine()
110         throws IllegalArgumentException, EngineException, SecurityException {
111         FreeTTSSynthesizer s = new FreeTTSSynthesizer(this);
112         return s;
113     }
114
115 }