001/**
002 * Copyright 2003 Sun Microsystems, Inc.
003 * 
004 * See the file "license.terms" for information on usage and
005 * redistribution of this file, and for a DISCLAIMER OF ALL 
006 * WARRANTIES.
007 */
008
009package com.sun.speech.freetts.jsapi;
010import java.util.LinkedList;
011import java.util.List;
012import java.util.Locale;
013
014import javax.speech.Engine;
015import javax.speech.EngineCreate;
016import javax.speech.EngineException;
017import javax.speech.synthesis.SynthesizerModeDesc;
018
019import com.sun.speech.freetts.ValidationException;
020
021/**
022 * Represents a SynthesizerModeDesc for the
023 * FreeTTSSynthesizer. A FreeTTSSynthesizerModeDesc adds 
024 * an audio player to the standard mode items.
025 */
026public class FreeTTSSynthesizerModeDesc extends SynthesizerModeDesc 
027implements EngineCreate {
028
029    /**
030     * Creates a fully-specified descriptor.
031     * Any of the features may be <code>null</code>.
032     *
033     * @param engineName  the name of the engine
034     * @param modeName   the name of the mode
035     * @param locale  the locale associated with this mode
036     */
037    public FreeTTSSynthesizerModeDesc( String engineName, String modeName,
038            Locale locale) {
039        super(engineName, modeName, locale, Boolean.FALSE, null);
040    }
041
042    /**
043     * Returns the valid voices in this synthesizer mode.
044     *
045     * @return an array of valid voices, if no valid voices, it will
046     *    return an array of size 0
047     */
048    public javax.speech.synthesis.Voice[] getVoices() {
049        List voiceList = new LinkedList();
050        javax.speech.synthesis.Voice[] voices = super.getVoices();
051        int count = 0;
052        for (int i = 0; i < voices.length; i++) {
053            FreeTTSVoice freettsVoice = (FreeTTSVoice) voices[i];
054            try {
055                freettsVoice.validate();
056                voiceList.add(freettsVoice);
057                count++;
058            } catch (ValidationException ve) {
059                // don't do anything here if a FreeTTSVoice is invalid
060            }
061        }
062        javax.speech.synthesis.Voice[] validVoices =
063            new javax.speech.synthesis.Voice[count];
064        voiceList.toArray(validVoices);
065        
066        return validVoices;
067    }
068    
069    /**
070     * Returns true if this is a valid FreeTTSSynthesizerModeDesc.
071     * It is valid if it contains at least one valid Voice.
072     * Returns false otherwise.
073     *
074     * @throws ValidationException if this FreeTTSSynthesizerModeDesc
075     *    is invalid
076     */
077    public void validate() throws ValidationException {
078        javax.speech.synthesis.Voice[] voices = super.getVoices();
079        int invalidCount = 0;
080        StringBuilder validationMessage = new StringBuilder();
081
082        for (int i = 0; i < voices.length; i++) {
083            try {
084                ((FreeTTSVoice) voices[i]).validate();
085            } catch (ValidationException ve) {
086                invalidCount++;
087                validationMessage.append(ve.getMessage());
088                validationMessage.append(System.getProperty("line.separator"));
089            }
090        }
091        if (invalidCount == voices.length) {
092            throw new ValidationException
093                (validationMessage + getModeName() + " has no valid voices.");
094        }
095    }
096
097    /**
098     * Constructs a FreeTTSSynthesizer with the properties of this mode
099     * 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}