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 */
008package com.sun.speech.freetts.jsapi;
009
010import com.sun.speech.engine.synthesis.BaseVoice;
011import com.sun.speech.freetts.ValidationException;
012import com.sun.speech.freetts.Validator;
013
014/**
015 * Extends the BaseVoice class to encapsulate FreeTTSSynthesizer specific data.
016 */
017public class FreeTTSVoice extends BaseVoice {
018
019    private com.sun.speech.freetts.Voice freettsVoice;
020    private Validator validator;
021
022    /**
023     * Constructs a FreeTTSVoice
024     *
025     * @param freettsVoice the freetts voice
026     * @param validatorName the classname of the validator to use
027     */
028    public FreeTTSVoice(com.sun.speech.freetts.Voice freettsVoice,
029                        String validatorName) {
030        super(freettsVoice.getName()+Math.random(), freettsVoice.getName(),
031                genderToInt(freettsVoice.getGender()),
032                ageToInt(freettsVoice.getAge()), freettsVoice.getStyle(),
033                freettsVoice.getPitch(), freettsVoice.getPitchRange(),
034                freettsVoice.getRate(), freettsVoice.getVolume());
035        this.freettsVoice = freettsVoice;
036        
037        if (validatorName != null) {
038            try {
039                Class<?> clazz = Class.forName(validatorName);
040                validator = (Validator) clazz.newInstance();
041            } catch (ClassNotFoundException cnfe) {
042                cnfe.printStackTrace();
043            } catch (IllegalAccessException iae) {
044                iae.printStackTrace();
045            } catch (InstantiationException ie) {
046                ie.printStackTrace();
047            }
048        } else {
049            validator = null;
050        }
051    }
052
053    /**
054     * Convert a freetts gender to jsapi gender
055     *
056     * @param gender the freetts gender
057     *
058     * @return the jsapi gender
059     */
060    private static int genderToInt(com.sun.speech.freetts.Gender gender) {
061        if (gender == com.sun.speech.freetts.Gender.MALE) {
062            return javax.speech.synthesis.Voice.GENDER_MALE;
063        } else if (gender == com.sun.speech.freetts.Gender.FEMALE) {
064            return javax.speech.synthesis.Voice.GENDER_FEMALE;
065        } else if (gender == com.sun.speech.freetts.Gender.NEUTRAL) {
066            return javax.speech.synthesis.Voice.GENDER_NEUTRAL;
067        } else if (gender == com.sun.speech.freetts.Gender.DONT_CARE) {
068            return javax.speech.synthesis.Voice.GENDER_DONT_CARE;
069        } else {
070            throw new Error("jaspi does not have an equivalent to gender "
071                    + gender.toString());
072        }
073    }
074
075    /**
076     * Convert a freetts age to jsapi age
077     *
078     * @param age the freetts age
079     *
080     * @return the jsapi age
081     */
082    private static int ageToInt(com.sun.speech.freetts.Age age) {
083        if (age == com.sun.speech.freetts.Age.CHILD) {
084            return javax.speech.synthesis.Voice.AGE_CHILD;
085        } else if (age == com.sun.speech.freetts.Age.TEENAGER) {
086            return javax.speech.synthesis.Voice.AGE_TEENAGER;
087        } else if (age == com.sun.speech.freetts.Age.YOUNGER_ADULT) {
088            return javax.speech.synthesis.Voice.AGE_YOUNGER_ADULT;
089        } else if (age == com.sun.speech.freetts.Age.MIDDLE_ADULT) {
090            return javax.speech.synthesis.Voice.AGE_MIDDLE_ADULT;
091        } else if (age == com.sun.speech.freetts.Age.OLDER_ADULT) {
092            return javax.speech.synthesis.Voice.AGE_OLDER_ADULT;
093        } else if (age == com.sun.speech.freetts.Age.NEUTRAL) {
094            return javax.speech.synthesis.Voice.AGE_NEUTRAL;
095        } else if (age == com.sun.speech.freetts.Age.DONT_CARE) {
096            return javax.speech.synthesis.Voice.AGE_DONT_CARE;
097        } else {
098            throw new Error("jaspi does not have an equivalent to age "
099                    + age.toString());
100        } 
101    }
102
103    /**
104     * Gets the id for this voice.
105     * Should be unique for a synthesizer.
106     *
107     * @return the voice id
108     */
109    public String getId() {
110        return voiceId;
111    }
112
113    /**
114     * Gets a string representation of the object
115     *
116     * @return the name of this voice
117     */
118    public String toString() {
119        return getName();
120    }
121
122
123    /**
124     * Gets a FreeTTS com.sun.speech.freetts.Voice from this JSAPI voice
125     *
126     * @return a FreeTTS Voice or null, if the voice cannot be found
127     */
128    public synchronized com.sun.speech.freetts.Voice getVoice() {
129        return freettsVoice;
130    }
131
132    /**
133     * Sets the id for this voice.
134     *
135     * @param id the new id
136     */
137    public void setId(String id) {
138        voiceId = id;
139    }
140
141    /**
142     * Creates a copy of this <code>BaseVoice</code>.
143     *
144     * @return the cloned object
145     */
146    public Object clone() {
147        return super.clone();
148    }
149
150    /**
151     * Validates this FreeTTSVoice.
152     *
153     * @throws ValidationException if this FreeTTSVoice is invalid
154     */
155    public void validate() throws ValidationException {
156        if (validator != null) {
157            validator.validate();
158        }
159    }
160}
161